Compiler.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  2. # Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. #
  4. # Permission is hereby granted, free of charge, to any person
  5. # obtaining a copy of this software and associated documentation
  6. # files (the "Software"), to deal in the Software without
  7. # restriction, including without limitation the rights to use,
  8. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following
  11. # conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. # OTHER DEALINGS IN THE SOFTWARE.
  24. import subprocess
  25. import os
  26. LUAJIT = "luajit-2.0.1"
  27. BC_G = "bytecode-generator.lua"
  28. TXT_C = "txt-compiler"
  29. TGA_C = "tga-compiler"
  30. LUA_C = "lua-compiler"
  31. RES_H = "resource-hash"
  32. ROOT_P = "--root-path"
  33. DEST_P = "--dest-path"
  34. RES_IN = "--resource-in"
  35. SEED = "--seed"
  36. # Compiler is an helper for compiling resources in a repository
  37. # either one by one or in a whole
  38. class Compiler:
  39. def __init__(self, repository, dest_path):
  40. self.m_repository = repository
  41. self.m_perfect_seed = -1
  42. self.m_dest_path = dest_path
  43. # Returns the destination path where the compiled
  44. # resources will be put
  45. def dest_path(self):
  46. return self.m_dest_path
  47. # Returns the perfect seed
  48. def perfect_seed(self):
  49. return self.m_perfect_seed
  50. # Computes a perfect seed for the resources contained
  51. # in the repository
  52. def compute_perfect_seed(self):
  53. # Obtain resources from repository
  54. resources = self.m_repository.all_resources()
  55. resource_hashes = []
  56. seed = 0
  57. # Calculate the hash resource by resource
  58. # Note: we can speed-up this process by modifying resource-hash to
  59. # calculate more than a hash at time...
  60. while True:
  61. for res in resources:
  62. p = subprocess.check_output([RES_H, RES_IN, res, SEED, str(seed)])
  63. resource_hashes.append(str(p))
  64. if len(resource_hashes) == len(set(resource_hashes)):
  65. self.m_perfect_seed = seed
  66. return
  67. resource_hashes.clear()
  68. seed = seed + 1;
  69. # Compiles all the texture resources in the repository
  70. def compile_textures(self):
  71. textures = self.m_repository.texture_resources();
  72. for res in textures:
  73. self.compile(res)
  74. # Compiles all the mesh resources in the repository
  75. def compile_meshes(self):
  76. meshes = self.m_repository.mesh_resources();
  77. for res in meshes:
  78. self.compile(res)
  79. # Compiles all the text resources in the repository
  80. def compile_texts(self):
  81. texts = self.m_repository.text_resources();
  82. for res in texts:
  83. self.compile(res)
  84. # Compiles all the script resources in the repository
  85. def compile_scripts(self):
  86. scripts = self.m_repository.script_resources();
  87. for res in scripts:
  88. self.compile(res)
  89. # Compiles all the resources in the repository
  90. def compile_all(self):
  91. print("Compiling textures...")
  92. self.compile_textures()
  93. print("Compiling meshes...")
  94. self.compile_meshes()
  95. print("Compiling texts...")
  96. self.compile_texts()
  97. print("Compiling scripts...")
  98. self.compile_scripts()
  99. # Compile a single resource from the repository
  100. def compile(self, resource):
  101. # Compute perfect seed if necessary
  102. if (self.m_perfect_seed == -1):
  103. self.compute_perfect_seed()
  104. root_path = self.m_repository.root_path()
  105. print(resource + " => ???")
  106. # Call appropriate compiler based on resource extension
  107. if resource.endswith('.txt'):
  108. p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
  109. if resource.endswith('.tga'):
  110. p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
  111. if resource.endswith('.lua'):
  112. path = os.path.normpath(root_path + "/" + resource)
  113. f = subprocess.call([LUAJIT, BC_G, path]);
  114. p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);