Get Image Paths.ahk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * -- Get image paths from glife.txt --
  3. *
  4. * Requires images subfolder to exist in current directory
  5. *
  6. * OutAllFile - List of All unprocessed file names from glife.txt that are not marked missing in the code
  7. * OutUsedFile - List of Found file names from glife.txt and Auto List
  8. * OutMissFile - List of Missing file names from glife.txt that are marked missing in the code.
  9. * OutAutoFile - List of file paths from glife.txt that were added to Output file using code in this script
  10. * OutManFile - List of file paths from glife.txt that will have to be added manualy checked either in glife code or added to OutUsedFile
  11. *
  12. *
  13. * WD: Jul 2015
  14. *
  15. *
  16. */
  17. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  18. ; #Warn ; Enable warnings to assist with detecting common errors.
  19. SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
  20. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
  21. SourceFile = glife.txt
  22. OutAllFile = Image List - All.txt
  23. OutUsedFile = Image List - Used.txt
  24. OutMissFile = Image List - Missing.txt
  25. OutAutoFile = Image Paths - Auto List.txt
  26. OutManFile = Image Paths - Manual List.txt
  27. SearchRegEx = i)<img\s+src\s*=\s*"(.*?)"
  28. CommentRegEx = ^\s*!
  29. MarkMissRegEx = i)!\s*WD:\s+IMAGE NEEDED\s*~\s*.*?<img\s+src\s*=\s*"(.*?)"
  30. FileEncoding, UTF-16
  31. ;-- backup files and delete --
  32. IfNotExist %SourceFile%
  33. {
  34. MsgBox, 16, Get Image Paths, Unable to locate "%SourceFile%" in "%A_WorkingDir%", 10
  35. Exit
  36. }
  37. ifExist %OutAllFile% ;; Backup File
  38. {
  39. FileMove, %OutAllFile%, %OutAllFile%.bak, 1
  40. FileDelete, %OutAllFile%
  41. }
  42. ifExist %OutUsedFile% ;; Backup File
  43. {
  44. FileMove, %OutUsedFile%, %OutUsedFile%.bak, 1
  45. FileDelete, %OutUsedFile%
  46. }
  47. ifExist %OutMissFile% ;; Backup File
  48. {
  49. FileMove, %OutMissFile%, %OutMissFile%.bak, 1
  50. FileDelete, %OutMissFile%
  51. }
  52. ifExist %OutAutoFile% ;; Backup File
  53. {
  54. FileMove, %OutAutoFile%, %OutAutoFile%.bak, 1
  55. FileDelete, %OutAutoFile%
  56. }
  57. ifExist %OutManFile% ;; Backup File
  58. {
  59. FileMove, %OutManFile%, %OutManFile%.bak, 1
  60. FileDelete, %OutManFile%
  61. }
  62. ; -- load data from file --
  63. FileRead, Source, %SourceFile%
  64. ; -- vars need plenty of space to work with --
  65. VarSetCapacity(OutAll, 4194304) ;; 4mb
  66. VarSetCapacity(OutUsed, 4194304) ;; 4mb
  67. VarSetCapacity(OutMiss, 4194304) ;; 4mb
  68. VarSetCapacity(OutAuto, 1048576) ;; 1mb
  69. VarSetCapacity(OutMan, 1048576) ;; 1mb
  70. ; -- Parse data one line at a time --
  71. Loop, Parse, Source, `n, `r ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
  72. {
  73. ; -- Check is Comment line --
  74. FoundCmntPos := RegExMatch(A_LoopField, CommentRegEx)
  75. if (ErrorLevel)
  76. {
  77. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %A_LoopField%`n`nusing search: %CommentRegEx%
  78. break
  79. }
  80. if (FoundCmntPos) ;; comment found
  81. {
  82. FoundPos := RegExMatch(A_LoopField, MarkMissRegEx, Match) ;; Check for known missing file
  83. if (ErrorLevel)
  84. {
  85. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %A_LoopField%`n`nusing search: %MarkMissRegEx%
  86. break
  87. }
  88. if (FoundPos) ;; Found File
  89. {
  90. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  91. IfExist %Match1%
  92. {
  93. OutMiss .= Spacer(Match1) . "- Missing file exists`n" ;; Missing File found
  94. }
  95. else
  96. {
  97. OutMiss .= Match1 . "`n" ;; Missing file Not found
  98. }
  99. }
  100. Continue
  101. }
  102. ; -- Find Image File Path --
  103. FoundPos := RegExMatch(A_LoopField, SearchRegEx, Match) ;; Search for image path
  104. if (ErrorLevel)
  105. {
  106. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %A_LoopField%`n`nusing search: %SearchRegEx%
  107. break
  108. }
  109. if (FoundPos) ;; Found File
  110. {
  111. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  112. OutAll .= Match1 . "`n"
  113. ; -- Image Path Contains Code --
  114. if inStr(Match1, "<<") ;; String contains expression
  115. {
  116. if inStr(Match1, "FUNC('$face_image'") ;; Hairstyle images Function
  117. {
  118. if GetImageFiles("images\body\hairstyles", "hcol*.jpg", OutUsed)
  119. {
  120. OutAuto .= Spacer(Match1) . "- Found 'images\body\hairstyles\*\hcol*.jpg'`n"
  121. }
  122. else
  123. {
  124. OutMan .= Spacer(Match1) . "- no files found in 'images\body\hairstyles\*\hcol*.jpg'`n"
  125. }
  126. }
  127. else if inStr(Match1, "FUNC('$clothing_image'") ;; Clothing images Function
  128. {
  129. if GetImageFiles("images\clothes", "vatnik.jpg", OutUsed)
  130. {
  131. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\vatnik.jpg'`n"
  132. }
  133. else
  134. {
  135. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\vatnik.jpg'`n"
  136. }
  137. ; GetImageFiles("images\clothes\newclo", "131.jpg", OutUsed) ;; Dupe See below
  138. if GetImageFiles("images\clothes", "jeans*.jpg", OutUsed)
  139. {
  140. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\jeans*.jpg'`n"
  141. }
  142. else
  143. {
  144. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\jeans*.jpg'`n"
  145. }
  146. if GetImageFiles("images\clothes", "yoga*.jpg", OutUsed)
  147. {
  148. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\yoga*.jpg'`n"
  149. }
  150. else
  151. {
  152. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\yoga*.jpg'`n"
  153. }
  154. if GetImageFiles("images\clothes", "sarafan*.jpg", OutUsed)
  155. {
  156. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\sarafan*.jpg'`n"
  157. }
  158. else
  159. {
  160. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\sarafan*.jpg'`n"
  161. }
  162. if GetImageFiles("images\clothes", "short*.jpg", OutUsed)
  163. {
  164. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\short*.jpg'`n"
  165. }
  166. else
  167. {
  168. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\short*.jpg'`n"
  169. }
  170. if GetImageFiles("images\clothes", "skirt*.jpg", OutUsed)
  171. {
  172. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\skirt*.jpg'`n"
  173. }
  174. else
  175. {
  176. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\skirt*.jpg'`n"
  177. }
  178. if GetImageFiles("images\clothes", "dress*.jpg", OutUsed)
  179. {
  180. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\dress*.jpg'`n"
  181. }
  182. else
  183. {
  184. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\dress*.jpg'`n"
  185. }
  186. if GetImageFiles("images\clothes", "profi*.jpg", OutUsed)
  187. {
  188. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\profi*.jpg'`n"
  189. }
  190. else
  191. {
  192. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\profi*.jpg'`n"
  193. }
  194. if GetImageFiles("images\clothes", "pants*.jpg", OutUsed)
  195. {
  196. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\pants*.jpg'`n"
  197. }
  198. else
  199. {
  200. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\pants*.jpg'`n"
  201. }
  202. if GetImageFiles("images\clothes", "latex*.jpg", OutUsed)
  203. {
  204. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\latex*.jpg'`n"
  205. }
  206. else
  207. {
  208. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\latex*.jpg'`n"
  209. }
  210. if GetImageFiles("images\clothes", "hooker*.jpg", OutUsed)
  211. {
  212. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\hooker*.jpg'`n"
  213. }
  214. else
  215. {
  216. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\hooker*.jpg'`n"
  217. }
  218. if GetImageFiles("images\clothes", "k*.jpg", OutUsed)
  219. {
  220. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\k*.jpg'`n"
  221. }
  222. else
  223. {
  224. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\k*.jpg'`n"
  225. }
  226. if GetImageFiles("images\clothes\newclo", "*.jpg", OutUsed)
  227. {
  228. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\newclo\*.jpg'`n"
  229. }
  230. else
  231. {
  232. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\newclo\*.jpg'`n"
  233. }
  234. if GetImageFiles("images\img\dress", "ero*.jpg", OutUsed)
  235. {
  236. OutAuto .= Spacer(Match1) . "- Found 'images\img\dress\ero*.jpg'`n"
  237. }
  238. else
  239. {
  240. OutMan .= Spacer(Match1) . "- no files found in 'images\img\dress\ero*.jpg'`n"
  241. }
  242. }
  243. else if inStr(Match1, "images\qwest\card") ;; Playingcard Images
  244. {
  245. if GetImageFiles("images\qwest\card", "*.jpg", Output)
  246. {
  247. OutAuto .= Spacer(Match1) . "- Found 'images\qwest\card\*\*.jpg'`n"
  248. }
  249. else
  250. {
  251. OutMan .= Spacer(Match1) . "- No files found in 'images\qwest\card\*\*.jpg'`n"
  252. }
  253. }
  254. else if inStr(Match1, "FUNC") ;; Unknown Function - Manual
  255. {
  256. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  257. }
  258. else if inStr(Match1, "$") ;; Uses String variable - Manual
  259. {
  260. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  261. }
  262. else if not GetImagefromPath(Match1, OutUsed) ;; Try to find images
  263. {
  264. OutMan .= Spacer(Match1) . "- Files not found`n"
  265. }
  266. }
  267. ; -- Image path is just a file name --
  268. else
  269. {
  270. IfExist %Match1%
  271. {
  272. OutUsed .= Match1 . "`n" ;; Normal file found
  273. }
  274. else
  275. {
  276. OutMan .= Spacer(Match1) . "- File not found`n" ;; Normal file Not found
  277. }
  278. }
  279. }
  280. }
  281. ; -- Set path to use / seperators --
  282. OutAll := StrReplace(OutAll, "\", "/")
  283. OutUsed := StrReplace(OutUsed, "\", "/")
  284. OutMiss := StrReplace(OutMiss, "\", "/")
  285. OutAuto := StrReplace(OutAuto, "\", "/")
  286. OutMan := StrReplace(OutMan, "\", "/")
  287. ; -- sort filenames and remove dupes --
  288. Sort, OutAll, U
  289. Sort, OutUsed, U
  290. Sort, OutMiss, U
  291. Sort, OutAuto, U
  292. Sort, OutMan, U
  293. ;-- save files --
  294. FileAppend, %OutAll%, %OutAllFile%
  295. FileAppend, %OutUsed%, %OutUsedFile%
  296. FileAppend, %OutMiss%, %OutMissFile%
  297. FileAppend, %OutAuto%, %OutAutoFile%
  298. FileAppend, %OutMan%, %OutManFile%
  299. Exit
  300. ; ########## Functions ##########
  301. Spacer(TxtStr = "")
  302. {
  303. Static SpaceFill := " "
  304. ;~ ps := strlen(TxtStr) + 1 ;; allow empty string = 1
  305. ;~ ss := TxtStr . SubStr(SpaceFill, ps) . "`t"
  306. return TxtStr . SubStr(SpaceFill, strlen(TxtStr)+1) . "`t"
  307. }
  308. GetImageFiles(FilePath, FileName, ByRef Output)
  309. { ;; Get image paths in filepath using filepattern FileName
  310. IfNotExist, %FilePath%
  311. {
  312. MsgBox, 48, Get Image Paths, Error in GetImageFiles()`n`nUnable to locate "%FilePath%" folder, Filename '%FileName%'.`n`nCurrent working dir is "%A_WorkingDir%"
  313. return false
  314. }
  315. SaveWorkDir := A_WorkingDir
  316. Setworkingdir, %FilePath%
  317. found := false
  318. Loop, Files, %FileName%, FR ;; Find files in FilePath using Filepattern FileName
  319. {
  320. fp := FilePath . "\" . A_LoopFileFullPath
  321. Output .= fp . "`n"
  322. found := true
  323. }
  324. Setworkingdir, %SaveWorkDir%
  325. return found
  326. }
  327. GetImagefromPath(FilePath, ByRef Output)
  328. { ;; Try to get Images from path
  329. FindRegEx := "<<.*?>>"
  330. ReplaceStr := "*"
  331. fp := StrReplace(FilePath, "/", "\")
  332. fp := RegExReplace(fp, FindRegEx, ReplaceStr)
  333. splitpath, fp, FileName, Dir, Ext
  334. ;; -- Manual if wildcard in path --
  335. if inStr(Dir, "*")
  336. {
  337. ; MsgBox, 64, Get Image Path, GetImageFromPath() error: Path contains wildcard`n `nPath : '%fp%'`nDir : '%Dir%'
  338. return false
  339. }
  340. return GetImageFiles(Dir, FileName, Output)
  341. }