Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import os
  2. import zipfile
  3. import time
  4.  
  5. def is_ztd(str):
  6. if any([x in str for x in zt_excludes]):
  7. return
  8. return str.lower().endswith(".ztd")
  9.  
  10. # the ZT root folder
  11. zt_dir = "C:/Program Files (x86)/Microsoft Games/Zoo Tycoon"
  12. # also try these subfolders
  13. zt_subfolders = ("Updates", "XPACK1", "XPACK2", "ZUPDATE", "ZUPDATE1", "dlupdate", "dupdate")
  14. # skip ZTD files whose names include any of the following strings
  15. zt_excludes = ("ztatb","zts")
  16.  
  17. def query_entity(name, file_to_ztd, lowercase_to_original_name):
  18. print("Getting all files for "+name)
  19. lower_name = name.lower()
  20. # get all content files in their original name formatting
  21. content_files = [lowercase_to_original_name[n] for n in lowercase_to_original_name if lower_name in n]
  22. # for each of these, get the ZTD it should be extracted from
  23. ztd_paths = [file_to_ztd[n] for n in content_files]
  24.  
  25. # now open each of the required ZTD files once
  26. unique_ztd_paths = set(ztd_paths)
  27. ztd_path_to_ztd_file = {}
  28. for ztd_path in unique_ztd_paths:
  29. ztd_path_to_ztd_file[ztd_path] = zipfile.ZipFile(ztd_path)
  30.  
  31. # just unzip each content file from its ZTD into the CWD
  32. # could be made much faster by unzipping all required files per ZTD in bulk
  33. for content, ztd in zip(content_files, ztd_paths):
  34. print("extract "+content+" from "+ztd)
  35. try:
  36. ztd_path_to_ztd_file[ztd].extract(content)
  37. except:
  38. print("ERROR: Could not unzip "+file)
  39.  
  40. def read_ztd_lib():
  41. # take the start time
  42. start = time.clock()
  43.  
  44. #this dict remembers all files and where an updated version was found so it can be fetched to construct the animal
  45. file_to_ztd = {}
  46. #lowercase for lookup
  47. lowercase_to_original_name = {}
  48.  
  49. # create a list of all folders that might contain ZTD files
  50. folders = [zt_dir,] + [os.path.join(zt_dir, sub) for sub in zt_subfolders]
  51. folders = [f for f in folders if os.path.isdir(f)]
  52.  
  53. # get all ZTD files from those folders and store their absolute file paths in a list
  54. # note: should make sure the folders are sorted in the order they are read by the game, so overwriting works as intended
  55. ztd_files = [os.path.join(zt_dir, ztd_name) for ztd_name in os.listdir(zt_dir) if is_ztd(ztd_name)]
  56.  
  57. # go over all ZTD files
  58. for ztd_path in ztd_files:
  59. # see if it is a ZTD with ZIP compression
  60. try:
  61. ztd_file = zipfile.ZipFile(ztd_path)
  62. except zipfile.BadZipFile:
  63. print(ztd_path+' is a not a ZIP file! Maybe a RAR file?')
  64. continue
  65. # just query the ZTD's entries
  66. for content_raw in ztd_file.namelist():
  67. # just read original ai files for now
  68. # if content_raw.endswith(".ai"):
  69. # normalize the path to make sure new versions overwrite previous entries
  70. content_lower = os.path.normpath(content_raw.lower())
  71. # link to where this file was found; overwrites older entry so we get the latest version
  72. file_to_ztd[content_raw] = ztd_path
  73. # store the link from this idealized file name back to the actual one
  74. lowercase_to_original_name[content_lower] = content_raw
  75. duration = time.clock()-start
  76. # print all files that
  77. print(file_to_ztd.keys())
  78. print("Done in {0:.2f} seconds".format(duration))
  79. return file_to_ztd, lowercase_to_original_name
  80.  
  81. file_to_ztd, lowercase_to_original_name = read_ztd_lib()
  82. query_entity("lodgpine", file_to_ztd, lowercase_to_original_name)
  83. query_entity("graywolf", file_to_ztd, lowercase_to_original_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement