Compiler.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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"
  27. OPTION = "-b"
  28. COMPILER_NAME = "./resource-compiler"
  29. RES_H = "./resource-hash"
  30. ROOT_P = "--root-path"
  31. DEST_P = "--dest-path"
  32. RES_IN = "--resource-in"
  33. SEED = "--seed"
  34. # Compiler is an helper for compiling resources in a repository
  35. # either one by one or in a whole
  36. class Compiler:
  37. def __init__(self, repository, dest_path):
  38. self.m_repository = repository
  39. self.m_perfect_seed = -1
  40. self.m_dest_path = dest_path
  41. # Returns the destination path where the compiled
  42. # resources will be put
  43. def dest_path(self):
  44. return self.m_dest_path
  45. # Returns the perfect seed
  46. def perfect_seed(self):
  47. return self.m_perfect_seed
  48. # Computes a perfect seed for the resources contained
  49. # in the repository
  50. def compute_perfect_seed(self):
  51. # Obtain resources from repository
  52. resources = self.m_repository.all_resources()
  53. resource_hashes = []
  54. seed = 0
  55. # Calculate the hash resource by resource
  56. # Note: we can speed-up this process by modifying resource-hash to
  57. # calculate more than a hash at time...
  58. while True:
  59. for res in resources:
  60. p = subprocess.check_output([RES_H, RES_IN, res, SEED, str(seed)])
  61. resource_hashes.append(str(p))
  62. if len(resource_hashes) == len(set(resource_hashes)):
  63. self.m_perfect_seed = seed
  64. return
  65. resource_hashes.clear()
  66. seed = seed + 1;
  67. # Compiles all the texture resources in the repository
  68. def compile_textures(self):
  69. self.compile(self.m_repository.texture_resources());
  70. # Compiles all the mesh resources in the repository
  71. def compile_meshes(self):
  72. self.compile(self.m_repository.mesh_resources());
  73. # Compiles all the text resources in the repository
  74. def compile_texts(self):
  75. self.compile(self.m_repository.text_resources());
  76. # Compiles all the script resources in the repository
  77. def compile_scripts(self):
  78. lua_resources = self.m_repository.script_resources()
  79. root_path = self.m_repository.root_path()
  80. for res in lua_resources:
  81. path_in = os.path.normpath(root_path + "/" + res)
  82. path_out = os.path.normpath(os.path.dirname(self.m_dest_path + "/" + res))
  83. if not os.path.exists(path_out):
  84. os.makedirs(path_out)
  85. head, out_filename = os.path.split(res)
  86. out_filename, ext = os.path.splitext(out_filename)
  87. res_in = res
  88. res_out = out_filename + ".raw"
  89. print(res_out, "<=", res_in)
  90. f = subprocess.call([LUAJIT, OPTION, path_in, path_out + "/" + res_out]);
  91. # Compiles all the vertex shader resources in the repository
  92. def compile_vertex_shaders(self):
  93. self.compile(self.m_repository.vertex_shader_resources());
  94. # Compiles all the vertex shader resources in the repository
  95. def compile_pixel_shaders(self):
  96. self.compile(self.m_repository.pixel_shader_resources());
  97. # Compiles all the resources in the repository
  98. def compile_all(self):
  99. self.compile_textures()
  100. self.compile_meshes()
  101. self.compile_texts()
  102. self.compile_scripts()
  103. self.compile_vertex_shaders()
  104. self.compile_pixel_shaders()
  105. # Compile a single resource from the repository
  106. def compile(self, resources):
  107. if (len(resources) == 0):
  108. return
  109. # Compute perfect seed if necessary
  110. if (self.m_perfect_seed == -1):
  111. self.compute_perfect_seed()
  112. root_path = self.m_repository.root_path()
  113. resource_params = (' '.join(resources)).split();
  114. compiler_params = [COMPILER_NAME, "--root-path", root_path, "--dest-path", self.m_dest_path, "--seed", str(self.m_perfect_seed)]
  115. p = subprocess.call(compiler_params + resource_params)