Get Image Paths.ahk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. FoundPos := 1
  103. Haystack := A_LoopField
  104. Loop, 200
  105. {
  106. ; -- Find Image File Path --
  107. FoundPos := RegExMatch(Haystack, SearchRegEx, Match, FoundPos) ;; Search for image path
  108. if (ErrorLevel)
  109. {
  110. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %Haystack%`n`nusing search: %SearchRegEx%
  111. break
  112. }
  113. if (FoundPos = 0) ;; Not Found exit loop
  114. {
  115. break
  116. }
  117. else
  118. ; if (FoundPos) ;; Found File
  119. {
  120. FoundPos += 8 + StrLen(Match1) ;; Increment Search Position
  121. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  122. OutAll .= Match1 . "`n"
  123. ; -- Image Path Contains Code --
  124. if inStr(Match1, "<<") ;; String contains expression
  125. {
  126. if inStr(Match1, "FUNC(''$face_image''") ;; Hairstyle images Function
  127. {
  128. if GetImageFiles("images\body\hairstyles", "hcol*.jpg", OutUsed)
  129. {
  130. OutAuto .= Spacer(Match1) . "- Found 'images\body\hairstyles\*\hcol*.jpg'`n"
  131. }
  132. else
  133. {
  134. OutMan .= Spacer(Match1) . "- no files found in 'images\body\hairstyles\*\hcol*.jpg'`n"
  135. }
  136. }
  137. else if inStr(Match1, "FUNC(''$clothing_image''") ;; Clothing images Function
  138. {
  139. if GetImageFiles("images\clothes", "vatnik.jpg", OutUsed)
  140. {
  141. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\vatnik.jpg'`n"
  142. }
  143. else
  144. {
  145. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\vatnik.jpg'`n"
  146. }
  147. ; GetImageFiles("images\clothes\newclo", "131.jpg", OutUsed) ;; Dupe See below
  148. if GetImageFiles("images\clothes", "jeans*.jpg", OutUsed)
  149. {
  150. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\jeans*.jpg'`n"
  151. }
  152. else
  153. {
  154. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\jeans*.jpg'`n"
  155. }
  156. if GetImageFiles("images\clothes", "yoga*.jpg", OutUsed)
  157. {
  158. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\yoga*.jpg'`n"
  159. }
  160. else
  161. {
  162. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\yoga*.jpg'`n"
  163. }
  164. if GetImageFiles("images\clothes", "sarafan*.jpg", OutUsed)
  165. {
  166. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\sarafan*.jpg'`n"
  167. }
  168. else
  169. {
  170. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\sarafan*.jpg'`n"
  171. }
  172. if GetImageFiles("images\clothes", "short*.jpg", OutUsed)
  173. {
  174. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\short*.jpg'`n"
  175. }
  176. else
  177. {
  178. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\short*.jpg'`n"
  179. }
  180. if GetImageFiles("images\clothes", "skirt*.jpg", OutUsed)
  181. {
  182. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\skirt*.jpg'`n"
  183. }
  184. else
  185. {
  186. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\skirt*.jpg'`n"
  187. }
  188. if GetImageFiles("images\clothes", "dress*.jpg", OutUsed)
  189. {
  190. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\dress*.jpg'`n"
  191. }
  192. else
  193. {
  194. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\dress*.jpg'`n"
  195. }
  196. if GetImageFiles("images\clothes", "profi*.jpg", OutUsed)
  197. {
  198. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\profi*.jpg'`n"
  199. }
  200. else
  201. {
  202. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\profi*.jpg'`n"
  203. }
  204. if GetImageFiles("images\clothes", "pants*.jpg", OutUsed)
  205. {
  206. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\pants*.jpg'`n"
  207. }
  208. else
  209. {
  210. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\pants*.jpg'`n"
  211. }
  212. if GetImageFiles("images\clothes", "latex*.jpg", OutUsed)
  213. {
  214. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\latex*.jpg'`n"
  215. }
  216. else
  217. {
  218. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\latex*.jpg'`n"
  219. }
  220. if GetImageFiles("images\clothes", "hooker*.jpg", OutUsed)
  221. {
  222. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\hooker*.jpg'`n"
  223. }
  224. else
  225. {
  226. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\hooker*.jpg'`n"
  227. }
  228. if GetImageFiles("images\clothes", "k*.jpg", OutUsed)
  229. {
  230. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\k*.jpg'`n"
  231. }
  232. else
  233. {
  234. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\k*.jpg'`n"
  235. }
  236. if GetImageFiles("images\clothes\newclo", "*.jpg", OutUsed)
  237. {
  238. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\newclo\*.jpg'`n"
  239. }
  240. else
  241. {
  242. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\newclo\*.jpg'`n"
  243. }
  244. if GetImageFiles("images\img\dress", "ero*.jpg", OutUsed)
  245. {
  246. OutAuto .= Spacer(Match1) . "- Found 'images\img\dress\ero*.jpg'`n"
  247. }
  248. else
  249. {
  250. OutMan .= Spacer(Match1) . "- no files found in 'images\img\dress\ero*.jpg'`n"
  251. }
  252. }
  253. else if inStr(Match1, "images\qwest\card") ;; Playingcard Images
  254. {
  255. if GetImageFiles("images\qwest\card", "*.jpg", Output)
  256. {
  257. OutAuto .= Spacer(Match1) . "- Found 'images\qwest\card\*\*.jpg'`n"
  258. }
  259. else
  260. {
  261. OutMan .= Spacer(Match1) . "- No files found in 'images\qwest\card\*\*.jpg'`n"
  262. }
  263. }
  264. else if inStr(Match1, "FUNC") ;; Unknown Function - Manual
  265. {
  266. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  267. }
  268. else if inStr(Match1, "$") ;; Uses String variable - Manual
  269. {
  270. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  271. }
  272. else if not GetImagefromPath(Match1, OutUsed) ;; Try to find images
  273. {
  274. OutMan .= Spacer(Match1) . "- Files not found`n"
  275. }
  276. }
  277. ; -- Image path is just a file name --
  278. else
  279. {
  280. IfExist %Match1%
  281. {
  282. OutUsed .= Match1 . "`n" ;; Normal file found
  283. }
  284. else
  285. {
  286. OutMan .= Spacer(Match1) . "- File not found`n" ;; Normal file Not found
  287. }
  288. }
  289. }
  290. }
  291. }
  292. ; -- Set path to use / seperators --
  293. OutAll := StrReplace(OutAll, "\", "/")
  294. OutUsed := StrReplace(OutUsed, "\", "/")
  295. OutMiss := StrReplace(OutMiss, "\", "/")
  296. OutAuto := StrReplace(OutAuto, "\", "/")
  297. OutMan := StrReplace(OutMan, "\", "/")
  298. ; -- sort filenames and remove dupes --
  299. Sort, OutAll, U
  300. Sort, OutUsed, U
  301. Sort, OutMiss, U
  302. Sort, OutAuto, U
  303. Sort, OutMan, U
  304. ;-- save files --
  305. FileAppend, %OutAll%, %OutAllFile%
  306. FileAppend, %OutUsed%, %OutUsedFile%
  307. FileAppend, %OutMiss%, %OutMissFile%
  308. FileAppend, %OutAuto%, %OutAutoFile%
  309. FileAppend, %OutMan%, %OutManFile%
  310. Exit
  311. ; ########## Functions ##########
  312. Spacer(TxtStr = "")
  313. {
  314. Static SpaceFill := " "
  315. ;~ ps := strlen(TxtStr) + 1 ;; allow empty string = 1
  316. ;~ ss := TxtStr . SubStr(SpaceFill, ps) . "`t"
  317. return TxtStr . SubStr(SpaceFill, strlen(TxtStr)+1) . "`t"
  318. }
  319. GetImageFiles(FilePath, FileName, ByRef Output)
  320. { ;; Get image paths in filepath using filepattern FileName
  321. IfNotExist, %FilePath%
  322. {
  323. MsgBox, 48, Get Image Paths, Error in GetImageFiles()`n`nUnable to locate "%FilePath%" folder, Filename '%FileName%'.`n`nCurrent working dir is "%A_WorkingDir%"
  324. return false
  325. }
  326. SaveWorkDir := A_WorkingDir
  327. Setworkingdir, %FilePath%
  328. found := false
  329. Loop, Files, %FileName%, FR ;; Find files in FilePath using Filepattern FileName
  330. {
  331. fp := FilePath . "\" . A_LoopFileFullPath
  332. Output .= fp . "`n"
  333. found := true
  334. }
  335. Setworkingdir, %SaveWorkDir%
  336. return found
  337. }
  338. GetImagefromPath(FilePath, ByRef Output)
  339. { ;; Try to get Images from path
  340. FindRegEx := "<<.*?>>"
  341. ReplaceStr := "*"
  342. fp := StrReplace(FilePath, "/", "\")
  343. fp := RegExReplace(fp, FindRegEx, ReplaceStr)
  344. splitpath, fp, FileName, Dir, Ext
  345. ;; -- Manual if wildcard in path --
  346. if inStr(Dir, "*")
  347. {
  348. ; MsgBox, 64, Get Image Path, GetImageFromPath() error: Path contains wildcard`n `nPath : '%fp%'`nDir : '%Dir%'
  349. return false
  350. }
  351. return GetImageFiles(Dir, FileName, Output)
  352. }