Get Image Paths.ahk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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)\bview\s*'+(.*?)'+
  29. CommentRegEx = ^\s*!
  30. MarkMissRegEx = i)!.*?:\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. if trim(A_LoopField) = "" ;; Skip blank line
  75. continue
  76. ; -- Check is Comment line --
  77. FoundCmntPos := RegExMatch(A_LoopField, CommentRegEx)
  78. if (ErrorLevel)
  79. {
  80. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %A_LoopField%`n`nusing search: %CommentRegEx%
  81. break
  82. }
  83. if (FoundCmntPos) ;; comment found
  84. {
  85. FoundPos := RegExMatch(A_LoopField, MarkMissRegEx, Match) ;; Check for known missing file
  86. if (ErrorLevel)
  87. {
  88. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %A_LoopField%`n`nusing search: %MarkMissRegEx%
  89. break
  90. }
  91. if (FoundPos) ;; Found File
  92. {
  93. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  94. IfExist %Match1%
  95. {
  96. OutMiss .= Spacer(Match1) . "- Missing file exists`n" ;; Missing File found
  97. }
  98. else
  99. {
  100. OutMiss .= Match1 . "`n" ;; Missing file Not found
  101. }
  102. }
  103. Continue
  104. }
  105. LineNo := A_index ;; Save line no
  106. ; -- HTML images --
  107. FoundPos := 1
  108. Haystack := A_LoopField
  109. Loop, 200
  110. {
  111. ; -- Find Image File Path --
  112. FoundPos := RegExMatch(Haystack, SearchRegEx, Match, FoundPos) ;; Search for image path in html
  113. if (ErrorLevel)
  114. {
  115. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %Haystack%`n`nusing search: %SearchRegEx%
  116. break
  117. }
  118. if (FoundPos = 0) ;; Not Found exit loop
  119. {
  120. break
  121. }
  122. else
  123. ; if (FoundPos) ;; Found File
  124. {
  125. IdxTxt := " (L:" . LineNo . ", P:" . FoundPos . ")"
  126. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  127. OutAll .= Match1 . "`n"
  128. ; -- Image Path Contains Code --
  129. if inStr(Match1, "<<") ;; String contains expression
  130. {
  131. if inStr(Match1, "FUNC(''$face_image''") ;; Hairstyle images Function - only use JPG files
  132. {
  133. if GetImageFiles("images\body\hairstyles", "hcol*.jpg", OutUsed, "", "FR")
  134. {
  135. OutAuto .= Spacer(Match1) . "- Found 'images\body\hairstyles\*\hcol*.jpg'`n"
  136. }
  137. else
  138. {
  139. OutMan .= Spacer(Match1) . "- no files found in 'images\body\hairstyles\*\hcol*.jpg' " . IdxTxt . "`n"
  140. }
  141. }
  142. else if inStr(Match1, "FUNC(''$clothing_image''") ;; Clothing images Function - only use JPG files
  143. {
  144. if GetImageFiles("images\clothes", "vatnik.jpg", OutUsed)
  145. {
  146. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\vatnik.jpg'`n"
  147. }
  148. else
  149. {
  150. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\vatnik.jpg' " . IdxTxt . "`n"
  151. }
  152. ; GetImageFiles("images\clothes\newclo", "131.jpg", OutUsed) ;; Dupe See below
  153. if GetImageFiles("images\clothes", "jeans*.jpg", OutUsed, "i)jeans\d+\.jpg")
  154. {
  155. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\jeans*.jpg'`n"
  156. }
  157. else
  158. {
  159. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\jeans*.jpg' " . IdxTxt . "`n"
  160. }
  161. if GetImageFiles("images\clothes", "yoga*.jpg", OutUsed, "i)yoga\d+\.jpg")
  162. {
  163. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\yoga*.jpg'`n"
  164. }
  165. else
  166. {
  167. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\yoga*.jpg' " . IdxTxt . "`n"
  168. }
  169. if GetImageFiles("images\clothes", "sarafan*.jpg", OutUsed, "i)sarafan\d+\.jpg")
  170. {
  171. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\sarafan*.jpg'`n"
  172. }
  173. else
  174. {
  175. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\sarafan*.jpg' " . IdxTxt . "`n"
  176. }
  177. if GetImageFiles("images\clothes", "short*.jpg", OutUsed, "i)short\d+\.jpg")
  178. {
  179. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\short*.jpg'`n"
  180. }
  181. else
  182. {
  183. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\short*.jpg' " . IdxTxt . "`n"
  184. }
  185. if GetImageFiles("images\clothes", "skirt*.jpg", OutUsed, "i)skirt\d+\.jpg")
  186. {
  187. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\skirt*.jpg'`n"
  188. }
  189. else
  190. {
  191. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\skirt*.jpg' " . IdxTxt . "`n"
  192. }
  193. if GetImageFiles("images\clothes", "dress*.jpg", OutUsed, "i)dress\d+\.jpg")
  194. {
  195. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\dress*.jpg'`n"
  196. }
  197. else
  198. {
  199. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\dress*.jpg' " . IdxTxt . "`n"
  200. }
  201. if GetImageFiles("images\clothes", "profi*.jpg", OutUsed, "i)profi\d+\.jpg")
  202. {
  203. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\profi*.jpg'`n"
  204. }
  205. else
  206. {
  207. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\profi*.jpg' " . IdxTxt . "`n"
  208. }
  209. if GetImageFiles("images\clothes", "pants*.jpg", OutUsed "i)pants\d+\.jpg")
  210. {
  211. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\pants*.jpg'`n"
  212. }
  213. else
  214. {
  215. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\pants*.jpg' " . IdxTxt . "`n"
  216. }
  217. if GetImageFiles("images\clothes", "latex*.jpg", OutUsed, "i)latex\d+\.jpg")
  218. {
  219. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\latex*.jpg'`n"
  220. }
  221. else
  222. {
  223. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\latex*.jpg' " . IdxTxt . "`n"
  224. }
  225. if GetImageFiles("images\clothes", "hooker*.jpg", OutUsed, "i)hooker\d+\.jpg")
  226. {
  227. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\hooker*.jpg'`n"
  228. }
  229. else
  230. {
  231. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\hooker*.jpg' " . IdxTxt . "`n"
  232. }
  233. if GetImageFiles("images\clothes", "k*.jpg", OutUsed, "i)k\d+\.jpg")
  234. {
  235. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\k*.jpg'`n"
  236. }
  237. else
  238. {
  239. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\k*.jpg' " . IdxTxt . "`n"
  240. }
  241. if GetImageFiles("images\clothes\newclo", "*.jpg", OutUsed, "i)\d+\.jpg")
  242. {
  243. OutAuto .= Spacer(Match1) . "- Found 'images\clothes\newclo\*.jpg'`n"
  244. }
  245. else
  246. {
  247. OutMan .= Spacer(Match1) . "- no files found in 'images\clothes\newclo\*.jpg' " . IdxTxt . "`n"
  248. }
  249. if GetImageFiles("images\img\dress", "ero*.jpg", OutUsed, "i)ero\d+\.jpg")
  250. {
  251. OutAuto .= Spacer(Match1) . "- Found 'images\img\dress\ero*.jpg'`n"
  252. }
  253. else
  254. {
  255. OutMan .= Spacer(Match1) . "- no files found in 'images\img\dress\ero*.jpg' " . IdxTxt . "`n"
  256. }
  257. }
  258. else if inStr(Match1, "images\qwest\card") ;; Playingcard Images - only use JPG files
  259. {
  260. if GetImageFiles("images\qwest\card", "*.jpg", OutUsed, "", "FR")
  261. {
  262. OutAuto .= Spacer(Match1) . "- Found 'images\qwest\card\*\*.jpg'`n"
  263. }
  264. else
  265. {
  266. OutMan .= Spacer(Match1) . "- No files found in 'images\qwest\card\*\*.jpg' " . IdxTxt . "`n"
  267. }
  268. }
  269. else if inStr(Match1, "FUNC") ;; Unknown Function - Manual
  270. {
  271. OutMan .= Spacer(Match1) . "- Unknown Function " . IdxTxt . "`n"
  272. }
  273. else if inStr(Match1, "$") ;; Uses String variable - Manual
  274. {
  275. OutMan .= Spacer(Match1) . "- Used String Variable " . IdxTxt . "`n"
  276. }
  277. else if GetImagefromPath(Match1, OutUsed) ;; Try to find images
  278. {
  279. OutAuto .= Spacer(Match1) . "- Found image files`n"
  280. }
  281. else
  282. {
  283. OutMan .= Spacer(Match1) . "- Files not found " . IdxTxt . "`n"
  284. }
  285. }
  286. ; -- Image path is just a file name --
  287. else
  288. {
  289. IfExist %Match1%
  290. {
  291. OutUsed .= Match1 . "`n" ;; Normal file found
  292. }
  293. else
  294. {
  295. OutMan .= Spacer(Match1) . "- File not found " . IdxTxt . "`n"
  296. }
  297. }
  298. FoundPos += 8 + StrLen(Match1) ;; Increment Search Position
  299. }
  300. }
  301. ; -- VIEW images --
  302. FoundPos := 1
  303. Haystack := A_LoopField
  304. Loop, 200
  305. {
  306. ; -- Find Image File Path --
  307. FoundPos := RegExMatch(Haystack, Search2RegEx, Match, FoundPos) ;; Search for 'view' cmd path
  308. if (ErrorLevel)
  309. {
  310. MsgBox, 48, Get Image Paths, RegExMatch runtime error: %ErrorLevel%`n`nFound searching string: %Haystack%`n`nusing search: %SearchRegEx%
  311. break
  312. }
  313. if (FoundPos = 0) ;; Not Found exit loop
  314. {
  315. break
  316. }
  317. else
  318. ; if (FoundPos) ;; Found File
  319. {
  320. IdxTxt := " (L:" . LineNo . ", P:" . FoundPos . ")"
  321. Match1 := StrReplace(Match1, "/", "\") ;; Use correct Win path seperator
  322. OutAll .= Match1 . "`n"
  323. ; -- Image Path Contains Code --
  324. if inStr(Match1, "<<") ;; String contains expression
  325. {
  326. if inStr(Match1, "FUNC") ;; Unknown Function - Manual
  327. {
  328. OutMan .= Spacer(Match1) . "- Unknown Function " . IdxTxt . "`n"
  329. }
  330. else if inStr(Match1, "$") ;; Uses String variable - Manual
  331. {
  332. OutMan .= Spacer(Match1) . "- Used String Variable " . IdxTxt . "`n"
  333. }
  334. else if GetImagefromPath(Match1, OutUsed) ;; Try to find images
  335. {
  336. OutAuto .= Spacer(Match1) . "- Found image files`n"
  337. }
  338. else
  339. {
  340. OutMan .= Spacer(Match1) . "- Files not found " . IdxTxt . "`n"
  341. }
  342. }
  343. ; -- Image path is just a file name --
  344. else
  345. {
  346. IfExist %Match1%
  347. {
  348. OutUsed .= Match1 . "`n" ;; Normal file found
  349. }
  350. else
  351. {
  352. OutMan .= Spacer(Match1) . "- File not found " . IdxTxt . "`n"
  353. }
  354. }
  355. FoundPos += 5 + StrLen(Match1) ;; Increment Search Position
  356. }
  357. }
  358. }
  359. ; -- Set path to use / seperators --
  360. OutAll := StrReplace(OutAll, "\", "/")
  361. OutUsed := StrReplace(OutUsed, "\", "/")
  362. OutMiss := StrReplace(OutMiss, "\", "/")
  363. OutAuto := StrReplace(OutAuto, "\", "/")
  364. OutMan := StrReplace(OutMan, "\", "/")
  365. ; -- sort filenames and remove dupes --
  366. Sort, OutAll, U
  367. Sort, OutUsed, U
  368. Sort, OutMiss, U
  369. Sort, OutAuto, U
  370. Sort, OutMan, U
  371. ;-- save files --
  372. FileAppend, %OutAll%, %OutAllFile%
  373. FileAppend, %OutUsed%, %OutUsedFile%
  374. FileAppend, %OutMiss%, %OutMissFile%
  375. FileAppend, %OutAuto%, %OutAutoFile%
  376. FileAppend, %OutMan%, %OutManFile%
  377. Exit
  378. ; ########## Functions ##########
  379. Spacer(TxtStr = "")
  380. {
  381. Static SpaceFill := " "
  382. ;~ ps := strlen(TxtStr) + 1 ;; allow empty string = 1
  383. ;~ ss := TxtStr . SubStr(SpaceFill, ps) . "`t"
  384. return TxtStr . SubStr(SpaceFill, strlen(TxtStr)+1) . "`t"
  385. }
  386. GetImageFiles(FilePath, FileName, ByRef Output, FileNameRegEx := "", FileLoopMode := "F")
  387. { ;; Get image paths in filepath using filepattern FileName
  388. ;~ msgbox GetImageFiles()`nFilePath := %FilePath%`nFileName := %FileName%`nFileNameRegEx := %FileNameRegEx%`nFileLoopMode := %FileLoopMode%
  389. IfNotExist, %FilePath%
  390. {
  391. MsgBox, 48, Get Image Paths, Error in GetImageFiles()`n`nUnable to locate "%FilePath%" folder, Filename '%FileName%'.`n`nCurrent working dir is "%A_WorkingDir%"
  392. return false
  393. }
  394. SaveWorkDir := A_WorkingDir
  395. Setworkingdir, %FilePath%
  396. found := false
  397. ; -- Loop with or Without recursion --
  398. Loop, Files, %FileName%, %FileLoopMode% ;; Find files in FilePath using Filepattern FileName
  399. {
  400. if(FileNameRegEx) ;; Can we test file name
  401. {
  402. FoundPos := RegExMatch(A_LoopFileName, FileNameRegEx) ;; File Found matches
  403. if (ErrorLevel)
  404. {
  405. MsgBox, 48, Get Image Paths, RegExMatch Error in function GetImageFiles()`n`nruntime error: %ErrorLevel%`n`nFound searching string: %A_LoopFileName%`n`nusing search: %FileNameRegEx%
  406. break
  407. }
  408. if (not FoundPos) ;; Filename not matches skip
  409. {
  410. continue
  411. }
  412. }
  413. fp := FilePath . "\" . A_LoopFileFullPath
  414. Output .= fp . "`n"
  415. found := true
  416. }
  417. Setworkingdir, %SaveWorkDir%
  418. return found
  419. }
  420. GetImagefromPath(FilePath, ByRef Output)
  421. { ;; Try to get Images from path
  422. FindRegEx := "<<.*?>>"
  423. ReplaceStr := "*"
  424. fp := StrReplace(FilePath, "/", "\")
  425. fp := RegExReplace(fp, FindRegEx, ReplaceStr)
  426. splitpath, fp, FileName, Dir, Ext
  427. ;; -- Manual if wildcard in path --
  428. if inStr(Dir, "*")
  429. {
  430. ; MsgBox, 64, Get Image Path, GetImageFromPath() error: Path contains wildcard`n `nPath : '%fp%'`nDir : '%Dir%'
  431. return false
  432. }
  433. FileNameRegEx := StrReplace(FileName, "*", "\d+") ; set up test for filename wildcard to be numbers only
  434. FileNameRegEx := "i)" . StrReplace(FileNameRegEx, ".", "\.")
  435. return GetImageFiles(Dir, FileName, Output, FileNameRegEx)
  436. }