Compiler.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. TXT_C = "txt-compiler"
  26. TGA_C = "tga-compiler"
  27. RES_H = "resource-hash"
  28. ROOT_P = "--root-path"
  29. DEST_P = "--dest-path"
  30. RES_IN = "--resource-in"
  31. SEED = "--seed"
  32. # Compiler is an helper for compiling resources in a repository
  33. # either one by one or in a whole
  34. class Compiler:
  35. def __init__(self, repository, dest_path):
  36. self.m_repository = repository
  37. self.m_perfect_seed = -1
  38. self.m_dest_path = dest_path
  39. # Returns the destination path where the compiled
  40. # resources will be put
  41. def dest_path(self):
  42. return self.m_dest_path
  43. # Returns the perfect seed
  44. def perfect_seed(self):
  45. return self.m_perfect_seed
  46. # Computes a perfect seed for the resources contained
  47. # in the repository
  48. def compute_perfect_seed(self):
  49. # Obtain resources from repository
  50. resources = self.m_repository.all_resources()
  51. resource_hashes = []
  52. seed = 0
  53. # Calculate the hash resource by resource
  54. # Note: we can speed-up this process by modifying resource-hash to
  55. # calculate more than a hash at time...
  56. while True:
  57. for res in resources:
  58. p = subprocess.check_output([RES_H, RES_IN, res, SEED, str(seed)])
  59. resource_hashes.append(str(p))
  60. if len(resource_hashes) == len(set(resource_hashes)):
  61. self.m_perfect_seed = seed
  62. return
  63. resource_hashes.clear()
  64. seed = seed + 1;
  65. # Compiles all the resources in the repository
  66. def compile_all(self):
  67. # Obtain resources from repository
  68. resources = self.m_repository.all_resources()
  69. for res in resources:
  70. self.compile(res)
  71. # Compile a single resource from the repository
  72. def compile(self, resource):
  73. # Compute perfect seed if necessary
  74. if (self.m_perfect_seed == -1):
  75. self.compute_perfect_seed()
  76. root_path = self.m_repository.root_path()
  77. # Call appropriate compiler based on resource extension
  78. if resource.endswith('.txt'):
  79. p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
  80. if resource.endswith('.tga'):
  81. p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);