Get Image Paths.ahk 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. Search2RegEx = i)view\s*'+(.*?)'+
  29. CommentRegEx = ^\s*!
  30. MarkMissRegEx = i)!\s*WD:\s+IMAGE NEEDED\s*~\s*.*?<img\s+src\s*=\s*"(.*?)"
  31. FileEncoding, UTF-16
  32. ;-- backup files and delete --
  33. IfNotExist %SourceFile%
  34. {
  35. MsgBox, 16, Get Image Paths, Unable to locate "%SourceFile%" in "%A_WorkingDir%", 10
  36. Exit
  37. }
  38. ifExist %OutAllFile% ;; Backup File
  39. {
  40. FileMove, %OutAllFile%, %OutAllFile%.bak, 1
  41. FileDelete, %OutAllFile%
  42. }
  43. ifExist %OutUsedFile% ;; Backup File
  44. {
  45. FileMove, %OutUsedFile%, %OutUsedFile%.bak, 1
  46. FileDelete, %OutUsedFile%
  47. }
  48. ifExist %OutMissFile% ;; Backup File
  49. {
  50. FileMove, %OutMissFile%, %OutMissFile%.bak, 1
  51. FileDelete, %OutMissFile%
  52. }
  53. ifExist %OutAutoFile% ;; Backup File
  54. {
  55. FileMove, %OutAutoFile%, %OutAutoFile%.bak, 1
  56. FileDelete, %OutAutoFile%
  57. }
  58. ifExist %OutManFile% ;; Backup File
  59. {
  60. FileMove, %OutManFile%, %OutManFile%.bak, 1
  61. FileDelete, %OutManFile%
  62. }
  63. ; -- load data from file --
  64. FileRead, Source, %SourceFile%
  65. ; -- vars need plenty of space to work with --
  66. VarSetCapacity(OutAll, 4194304) ;; 4mb
  67. VarSetCapacity(OutUsed, 4194304) ;; 4mb
  68. VarSetCapacity(OutMiss, 4194304) ;; 4mb
  69. VarSetCapacity(OutAuto, 1048576) ;; 1mb
  70. VarSetCapacity(OutMan, 1048576) ;; 1mb
  71. ; -- Parse data one line at a time --
  72. Loop, Parse, Source, `n, `r ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
  73. {
  74. ; -- Check is Comment line --
  75. FoundCmntPos := RegExMatch(A_LoopField, CommentRegEx)
  76. if (ErrorLevel)
  77. {
  78. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %A_LoopField%`n`nusing search: %CommentRegEx%
  79. break
  80. }
  81. if (FoundCmntPos) ;; comment found
  82. {
  83. FoundPos := RegExMatch(A_LoopField, MarkMissRegEx, Match) ;; Check for known missing file
  84. if (ErrorLevel)
  85. {
  86. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %A_LoopField%`n`nusing search: %MarkMissRegEx%
  87. break
  88. }
  89. if (FoundPos) ;; Found File
  90. {
  91. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  92. IfExist %Match1%
  93. {
  94. OutMiss .= Spacer(Match1) . "- Missing file exists`n" ;; Missing File found
  95. }
  96. else
  97. {
  98. OutMiss .= Match1 . "`n" ;; Missing file Not found
  99. }
  100. }
  101. Continue
  102. }
  103. ; -- HTML images --
  104. FoundPos := 1
  105. Haystack := A_LoopField
  106. Loop, 200
  107. {
  108. ; -- Find Image File Path --
  109. FoundPos := RegExMatch(Haystack, SearchRegEx, Match, FoundPos) ;; Search for image path in html
  110. if (ErrorLevel)
  111. {
  112. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %Haystack%`n`nusing search: %SearchRegEx%
  113. break
  114. }
  115. if (FoundPos = 0) ;; Not Found exit loop
  116. {
  117. break
  118. }
  119. else
  120. ; if (FoundPos) ;; Found File
  121. {
  122. FoundPos += 8 + StrLen(Match1) ;; Increment Search Position
  123. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  124. OutAll .= Match1 . "`n"
  125. ; -- Image Path Contains Code --
  126. if inStr(Match1, "<<") ;; String contains expression
  127. {
  128. if inStr(Match1, "FUNC(''$face_image''") ;; Hairstyle images Function
  129. {
  130. if GetImageFiles("images\body\hairstyles", "hcol*.jpg", OutUsed)
  131. {
  132. OutAuto .= Spacer(Match1) . "- Found 'images\body\hairstyles\*\hcol*.jpg'`n"
  133. }
  134. else
  135. {
  136. OutMan .= Spacer(Match1) . "- no files found in 'images\body\hairstyles\*\hcol*.jpg'`n"
  137. }
  138. }
  139. else if inStr(Match1, "FUNC(''$clothing_image''") ;; Clothing images Function
  140. {
  141. if GetImageFiles("images\clothes", "vatnik.jpg", OutUsed)
  142. {
  143. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\vatnik.jpg'`n"
  144. }
  145. else
  146. {
  147. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\vatnik.jpg'`n"
  148. }
  149. ; GetImageFiles("images\clothes\newclo", "131.jpg", OutUsed) ;; Dupe See below
  150. if GetImageFiles("images\clothes", "jeans*.jpg", OutUsed)
  151. {
  152. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\jeans*.jpg'`n"
  153. }
  154. else
  155. {
  156. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\jeans*.jpg'`n"
  157. }
  158. if GetImageFiles("images\clothes", "yoga*.jpg", OutUsed)
  159. {
  160. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\yoga*.jpg'`n"
  161. }
  162. else
  163. {
  164. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\yoga*.jpg'`n"
  165. }
  166. if GetImageFiles("images\clothes", "sarafan*.jpg", OutUsed)
  167. {
  168. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\sarafan*.jpg'`n"
  169. }
  170. else
  171. {
  172. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\sarafan*.jpg'`n"
  173. }
  174. if GetImageFiles("images\clothes", "short*.jpg", OutUsed)
  175. {
  176. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\short*.jpg'`n"
  177. }
  178. else
  179. {
  180. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\short*.jpg'`n"
  181. }
  182. if GetImageFiles("images\clothes", "skirt*.jpg", OutUsed)
  183. {
  184. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\skirt*.jpg'`n"
  185. }
  186. else
  187. {
  188. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\skirt*.jpg'`n"
  189. }
  190. if GetImageFiles("images\clothes", "dress*.jpg", OutUsed)
  191. {
  192. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\dress*.jpg'`n"
  193. }
  194. else
  195. {
  196. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\dress*.jpg'`n"
  197. }
  198. if GetImageFiles("images\clothes", "profi*.jpg", OutUsed)
  199. {
  200. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\profi*.jpg'`n"
  201. }
  202. else
  203. {
  204. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\profi*.jpg'`n"
  205. }
  206. if GetImageFiles("images\clothes", "pants*.jpg", OutUsed)
  207. {
  208. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\pants*.jpg'`n"
  209. }
  210. else
  211. {
  212. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\pants*.jpg'`n"
  213. }
  214. if GetImageFiles("images\clothes", "latex*.jpg", OutUsed)
  215. {
  216. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\latex*.jpg'`n"
  217. }
  218. else
  219. {
  220. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\latex*.jpg'`n"
  221. }
  222. if GetImageFiles("images\clothes", "hooker*.jpg", OutUsed)
  223. {
  224. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\hooker*.jpg'`n"
  225. }
  226. else
  227. {
  228. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\hooker*.jpg'`n"
  229. }
  230. if GetImageFiles("images\clothes", "k*.jpg", OutUsed)
  231. {
  232. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\k*.jpg'`n"
  233. }
  234. else
  235. {
  236. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\k*.jpg'`n"
  237. }
  238. if GetImageFiles("images\clothes\newclo", "*.jpg", OutUsed)
  239. {
  240. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\newclo\*.jpg'`n"
  241. }
  242. else
  243. {
  244. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\newclo\*.jpg'`n"
  245. }
  246. if GetImageFiles("images\img\dress", "ero*.jpg", OutUsed)
  247. {
  248. OutAuto .= Spacer(Match1) . "- Found 'images\img\dress\ero*.jpg'`n"
  249. }
  250. else
  251. {
  252. OutMan .= Spacer(Match1) . "- no files found in 'images\img\dress\ero*.jpg'`n"
  253. }
  254. }
  255. else if inStr(Match1, "images\qwest\card") ;; Playingcard Images
  256. {
  257. if GetImageFiles("images\qwest\card", "*.jpg", Output)
  258. {
  259. OutAuto .= Spacer(Match1) . "- Found 'images\qwest\card\*\*.jpg'`n"
  260. }
  261. else
  262. {
  263. OutMan .= Spacer(Match1) . "- No files found in 'images\qwest\card\*\*.jpg'`n"
  264. }
  265. }
  266. else if inStr(Match1, "FUNC") ;; Unknown Function - Manual
  267. {
  268. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  269. }
  270. else if inStr(Match1, "$") ;; Uses String variable - Manual
  271. {
  272. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  273. }
  274. else if not GetImagefromPath(Match1, OutUsed) ;; Try to find images
  275. {
  276. OutMan .= Spacer(Match1) . "- Files not found`n"
  277. }
  278. }
  279. ; -- Image path is just a file name --
  280. else
  281. {
  282. IfExist %Match1%
  283. {
  284. OutUsed .= Match1 . "`n" ;; Normal file found
  285. }
  286. else
  287. {
  288. OutMan .= Spacer(Match1) . "- File not found`n" ;; Normal file Not found
  289. }
  290. }
  291. }
  292. }
  293. ; -- VIEW images --
  294. FoundPos := 1
  295. Haystack := A_LoopField
  296. Loop, 200
  297. {
  298. ; -- Find Image File Path --
  299. FoundPos := RegExMatch(Haystack, Search2RegEx, Match, FoundPos) ;; Search for 'view' cmd path
  300. if (ErrorLevel)
  301. {
  302. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %Haystack%`n`nusing search: %SearchRegEx%
  303. break
  304. }
  305. if (FoundPos = 0) ;; Not Found exit loop
  306. {
  307. break
  308. }
  309. else
  310. ; if (FoundPos) ;; Found File
  311. {
  312. FoundPos += 5 + StrLen(Match1) ;; Increment Search Position
  313. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  314. OutAll .= Match1 . "`n"
  315. ; -- Image Path Contains Code --
  316. if inStr(Match1, "<<") ;; String contains expression
  317. {
  318. if inStr(Match1, "FUNC") ;; Unknown Function - Manual
  319. {
  320. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  321. }
  322. else if inStr(Match1, "$") ;; Uses String variable - Manual
  323. {
  324. OutMan .= Spacer(Match1) . "- Unknown Function`n"
  325. }
  326. else if not GetImagefromPath(Match1, OutUsed) ;; Try to find images
  327. {
  328. OutMan .= Spacer(Match1) . "- Files not found`n"
  329. }
  330. }
  331. ; -- Image path is just a file name --
  332. else
  333. {
  334. IfExist %Match1%
  335. {
  336. OutUsed .= Match1 . "`n" ;; Normal file found
  337. }
  338. else
  339. {
  340. OutMan .= Spacer(Match1) . "- File not found`n" ;; Normal file Not found
  341. }
  342. }
  343. }
  344. }
  345. }
  346. ; -- Set path to use / seperators --
  347. OutAll := StrReplace(OutAll, "\", "/")
  348. OutUsed := StrReplace(OutUsed, "\", "/")
  349. OutMiss := StrReplace(OutMiss, "\", "/")
  350. OutAuto := StrReplace(OutAuto, "\", "/")
  351. OutMan := StrReplace(OutMan, "\", "/")
  352. ; -- sort filenames and remove dupes --
  353. Sort, OutAll, U
  354. Sort, OutUsed, U
  355. Sort, OutMiss, U
  356. Sort, OutAuto, U
  357. Sort, OutMan, U
  358. ;-- save files --
  359. FileAppend, %OutAll%, %OutAllFile%
  360. FileAppend, %OutUsed%, %OutUsedFile%
  361. FileAppend, %OutMiss%, %OutMissFile%
  362. FileAppend, %OutAuto%, %OutAutoFile%
  363. FileAppend, %OutMan%, %OutManFile%
  364. Exit
  365. ; ########## Functions ##########
  366. Spacer(TxtStr = "")
  367. {
  368. Static SpaceFill := " "
  369. ;~ ps := strlen(TxtStr) + 1 ;; allow empty string = 1
  370. ;~ ss := TxtStr . SubStr(SpaceFill, ps) . "`t"
  371. return TxtStr . SubStr(SpaceFill, strlen(TxtStr)+1) . "`t"
  372. }
  373. GetImageFiles(FilePath, FileName, ByRef Output)
  374. { ;; Get image paths in filepath using filepattern FileName
  375. IfNotExist, %FilePath%
  376. {
  377. MsgBox, 48, Get Image Paths, Error in GetImageFiles()`n`nUnable to locate "%FilePath%" folder, Filename '%FileName%'.`n`nCurrent working dir is "%A_WorkingDir%"
  378. return false
  379. }
  380. SaveWorkDir := A_WorkingDir
  381. Setworkingdir, %FilePath%
  382. found := false
  383. Loop, Files, %FileName%, FR ;; Find files in FilePath using Filepattern FileName
  384. {
  385. fp := FilePath . "\" . A_LoopFileFullPath
  386. Output .= fp . "`n"
  387. found := true
  388. }
  389. Setworkingdir, %SaveWorkDir%
  390. return found
  391. }
  392. GetImagefromPath(FilePath, ByRef Output)
  393. { ;; Try to get Images from path
  394. FindRegEx := "<<.*?>>"
  395. ReplaceStr := "*"
  396. fp := StrReplace(FilePath, "/", "\")
  397. fp := RegExReplace(fp, FindRegEx, ReplaceStr)
  398. splitpath, fp, FileName, Dir, Ext
  399. ;; -- Manual if wildcard in path --
  400. if inStr(Dir, "*")
  401. {
  402. ; MsgBox, 64, Get Image Path, GetImageFromPath() error: Path contains wildcard`n `nPath : '%fp%'`nDir : '%Dir%'
  403. return false
  404. }
  405. return GetImageFiles(Dir, FileName, Output)
  406. }