Compiler.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. VS_C = "vs-compiler"
  32. PS_C = "ps-compiler"
  33. RES_H = "resource-hash"
  34. ROOT_P = "--root-path"
  35. DEST_P = "--dest-path"
  36. RES_IN = "--resource-in"
  37. SEED = "--seed"
  38. # Compiler is an helper for compiling resources in a repository
  39. # either one by one or in a whole
  40. class Compiler:
  41. def __init__(self, repository, dest_path):
  42. self.m_repository = repository
  43. self.m_perfect_seed = -1
  44. self.m_dest_path = dest_path
  45. # Returns the destination path where the compiled
  46. # resources will be put
  47. def dest_path(self):
  48. return self.m_dest_path
  49. # Returns the perfect seed
  50. def perfect_seed(self):
  51. return self.m_perfect_seed
  52. # Computes a perfect seed for the resources contained
  53. # in the repository
  54. def compute_perfect_seed(self):
  55. # Obtain resources from repository
  56. resources = self.m_repository.all_resources()
  57. resource_hashes = []
  58. seed = 0
  59. # Calculate the hash resource by resource
  60. # Note: we can speed-up this process by modifying resource-hash to
  61. # calculate more than a hash at time...
  62. while True:
  63. for res in resources:
  64. p = subprocess.check_output([RES_H, RES_IN, res, SEED, str(seed)])
  65. resource_hashes.append(str(p))
  66. if len(resource_hashes) == len(set(resource_hashes)):
  67. self.m_perfect_seed = seed
  68. return
  69. resource_hashes.clear()
  70. seed = seed + 1;
  71. # Compiles all the texture resources in the repository
  72. def compile_textures(self):
  73. textures = self.m_repository.texture_resources();
  74. for res in textures:
  75. self.compile(res)
  76. # Compiles all the mesh resources in the repository
  77. def compile_meshes(self):
  78. meshes = self.m_repository.mesh_resources();
  79. for res in meshes:
  80. self.compile(res)
  81. # Compiles all the text resources in the repository
  82. def compile_texts(self):
  83. texts = self.m_repository.text_resources();
  84. for res in texts:
  85. self.compile(res)
  86. # Compiles all the script resources in the repository
  87. def compile_scripts(self):
  88. scripts = self.m_repository.script_resources();
  89. for res in scripts:
  90. self.compile(res)
  91. # Compiles all the vertex shader resources in the repository
  92. def compile_vertex_shaders(self):
  93. vss = self.m_repository.vertex_shader_resources();
  94. for res in vss:
  95. self.compile(res)
  96. # Compiles all the vertex shader resources in the repository
  97. def compile_pixel_shaders(self):
  98. pss = self.m_repository.pixel_shader_resources();
  99. for res in pss:
  100. self.compile(res)
  101. # Compiles all the resources in the repository
  102. def compile_all(self):
  103. print("Compiling textures...")
  104. self.compile_textures()
  105. print("Compiling meshes...")
  106. self.compile_meshes()
  107. print("Compiling texts...")
  108. self.compile_texts()
  109. print("Compiling scripts...")
  110. self.compile_scripts()
  111. print("Compiling vertex shaders...")
  112. self.compile_vertex_shaders()
  113. print("Compiling pixel shaders...")
  114. self.compile_pixel_shaders()
  115. # Compile a single resource from the repository
  116. def compile(self, resource):
  117. # Compute perfect seed if necessary
  118. if (self.m_perfect_seed == -1):
  119. self.compute_perfect_seed()
  120. root_path = self.m_repository.root_path()
  121. print(resource + " => ???")
  122. # Call appropriate compiler based on resource extension
  123. if resource.endswith('.txt'):
  124. p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
  125. if resource.endswith('.tga'):
  126. p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
  127. if resource.endswith('.lua'):
  128. path = os.path.normpath(root_path + "/" + resource)
  129. f = subprocess.call([LUAJIT, BC_G, path]);
  130. p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
  131. if resource.endswith('.vs'):
  132. p = subprocess.call([VS_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
  133. if resource.endswith('.ps'):
  134. p = subprocess.call([PS_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);