resource-compiler.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/python
  2. import os
  3. import subprocess
  4. resource_extensions = ('.txt', '.tga', '.dae', '.lua')
  5. resources = []
  6. resource_hashes = []
  7. root_path = "/home/dani/test/resources"
  8. #------------------------------------------------------------------------------
  9. def read_resources(root_path):
  10. for dirname, dirnames, filenames in os.walk(root_path):
  11. # print path to all filenames.
  12. for filename in filenames:
  13. # get the resource name
  14. abs_path = os.path.join(dirname, filename)
  15. resource = os.path.relpath(abs_path, root_path)
  16. # filter resource names by type
  17. if resource.endswith(resource_extensions):
  18. resources.append(resource)
  19. #------------------------------------------------------------------------------
  20. def perfect_seed(resources):
  21. seed = 0
  22. while True:
  23. for res in resources:
  24. p = subprocess.check_output(["resource-hash", "--resource-in", res, "--seed", str(seed)])
  25. resource_hashes.append(str(p))
  26. if len(resource_hashes) == len(set(resource_hashes)):
  27. return seed
  28. resource_hashes.clear()
  29. seed = seed + 1;
  30. #------------------------------------------------------------------------------
  31. def compile(dest_path, resource, perfect_seed):
  32. if resource.endswith('.txt'):
  33. p = subprocess.call(["txt-compiler", "--root-path", root_path, "--dest-path", dest_path, "--resource-in", resource, "--seed", str(perfect_seed)]);
  34. if resource.endswith('.tga'):
  35. p = subprocess.call(["tga-compiler", "--root-path", root_path, "--dest-path", dest_path, "--resource-in", resource, "--seed", str(perfect_seed)]);
  36. #------------------------------------------------------------------------------
  37. def compile_all(dest_path, resources, perfect_seed):
  38. for res in resources:
  39. compile(dest_path, res, perfect_seed)
  40. #------------------------------------------------------------------------------
  41. def create_output_folder(root_path):
  42. output_path = os.path.dirname(root_path) + "/" + os.path.basename(root_path) + "_compiled"
  43. output_path = os.path.normpath(output_path)
  44. if not os.path.exists(output_path):
  45. os.makedirs(output_path)
  46. return output_path
  47. #------------------------------------------------------------------------------
  48. def write_perfect_seed(dest_path, perfect_seed):
  49. output_path = dest_path + "/" + "seed.ini"
  50. output_path = os.path.normpath(output_path)
  51. file = open(output_path, "w");
  52. file.write(str(perfect_seed))
  53. file.close()
  54. #------------------------------------------------------------------------------
  55. print("Reading resources...\n")
  56. read_resources(root_path)
  57. perfect_seed = perfect_seed(resources)
  58. print("Perfect seed is: ", perfect_seed)
  59. print("Creating output folder...\n")
  60. dest_path = create_output_folder(root_path)
  61. print("Writing perfect seed...\n")
  62. write_perfect_seed(dest_path, perfect_seed)
  63. print("Compiling resources...\n")
  64. compile_all(dest_path, resources, perfect_seed)
  65. print("\n")
  66. print("Done.")