Compiler.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <cstring>
  23. #include <cstdlib>
  24. #include "Compiler.h"
  25. #include "Hash.h"
  26. #include "Path.h"
  27. #include "FileStream.h"
  28. #include "Log.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. Compiler::Compiler(const char* root_path, const char* dest_path, const char* resource, uint32_t type_expected, uint32_t seed) :
  33. m_name_hash(0),
  34. m_type_hash(0),
  35. m_seed(seed),
  36. m_root_fs(root_path),
  37. m_dest_fs(dest_path),
  38. m_src_file(NULL),
  39. m_dest_file(NULL),
  40. m_prepared(false),
  41. m_verbose(false)
  42. {
  43. // Init structures
  44. memset(m_root_path, 0, MAX_RESOURCE_PATH_LENGTH);
  45. memset(m_dest_path, 0, MAX_RESOURCE_PATH_LENGTH);
  46. memset(m_resource, 0, MAX_RESOURCE_PATH_LENGTH);
  47. memset(&m_compiled_header, 0, sizeof(CompiledHeader));
  48. memset(m_name, 0, MAX_RESOURCE_NAME_LENGTH);
  49. memset(m_type, 0, MAX_RESOURCE_TYPE_LENGTH);
  50. string::strncpy(m_root_path, root_path, MAX_RESOURCE_PATH_LENGTH);
  51. string::strncpy(m_dest_path, dest_path, MAX_RESOURCE_PATH_LENGTH);
  52. string::strncpy(m_resource, resource, MAX_RESOURCE_PATH_LENGTH);
  53. // Extract resource name and type
  54. path::filename_without_extension(m_resource, m_name, MAX_RESOURCE_NAME_LENGTH);
  55. path::extension(m_resource, m_type, MAX_RESOURCE_TYPE_LENGTH);
  56. // Compute the resource hashes
  57. m_name_hash = hash::murmur2_32(m_name, string::strlen(m_name), m_seed);
  58. // NOTE: The type hash _MUST_ be generated with seed = 0
  59. m_type_hash = hash::murmur2_32(m_type, string::strlen(m_type), 0);
  60. if (m_type_hash != type_expected)
  61. {
  62. Log::e("Compiler: Trying to compile '%s' with the wrong compiler. Aborting.", resource_path());
  63. exit(-1);
  64. }
  65. char dest_name[17];
  66. memset(dest_name, 0, 17);
  67. snprintf(dest_name, 17, "%.8X%.8X", m_name_hash, m_type_hash);
  68. // Open streams
  69. m_src_file = (FileStream*)m_root_fs.open(m_resource, SOM_READ);
  70. if (!m_dest_fs.exists(dest_name))
  71. {
  72. m_dest_fs.create_file(dest_name);
  73. }
  74. m_dest_file = (FileStream*)m_dest_fs.open(dest_name, SOM_WRITE);
  75. }
  76. //-----------------------------------------------------------------------------
  77. Compiler::~Compiler()
  78. {
  79. if (m_src_file != NULL)
  80. {
  81. m_root_fs.close(m_src_file);
  82. }
  83. if (m_dest_file != NULL)
  84. {
  85. m_dest_fs.close(m_dest_file);
  86. }
  87. }
  88. //-----------------------------------------------------------------------------
  89. const char* Compiler::root_path() const
  90. {
  91. return m_root_path;
  92. }
  93. //-----------------------------------------------------------------------------
  94. const char* Compiler::dest_path() const
  95. {
  96. return m_dest_path;
  97. }
  98. //-----------------------------------------------------------------------------
  99. const char* Compiler::resource_path() const
  100. {
  101. return m_resource;
  102. }
  103. //-----------------------------------------------------------------------------
  104. uint32_t Compiler::resource_name_hash() const
  105. {
  106. return m_name_hash;
  107. }
  108. //-----------------------------------------------------------------------------
  109. uint32_t Compiler::resource_type_hash() const
  110. {
  111. return m_type_hash;
  112. }
  113. //-----------------------------------------------------------------------------
  114. uint32_t Compiler::seed() const
  115. {
  116. return m_seed;
  117. }
  118. //-----------------------------------------------------------------------------
  119. const char* Compiler::resource_name() const
  120. {
  121. return m_name;
  122. }
  123. //-----------------------------------------------------------------------------
  124. const char* Compiler::resource_type() const
  125. {
  126. return m_type;
  127. }
  128. //-----------------------------------------------------------------------------
  129. FileStream* Compiler::source_file()
  130. {
  131. return m_src_file;
  132. }
  133. //-----------------------------------------------------------------------------
  134. FileStream* Compiler::destination_file()
  135. {
  136. return m_dest_file;
  137. }
  138. //-----------------------------------------------------------------------------
  139. void Compiler::prepare_header(uint32_t size)
  140. {
  141. m_compiled_header.magic = COMPILED_HEADER_MAGIC_NUMBER;
  142. m_compiled_header.version = COMPILER_VERSION;
  143. m_compiled_header.name = m_name_hash;
  144. m_compiled_header.type = m_type_hash;
  145. m_compiled_header.size = size;
  146. }
  147. //-----------------------------------------------------------------------------
  148. void Compiler::write_header()
  149. {
  150. m_dest_file->write(&m_compiled_header, sizeof(CompiledHeader));
  151. }
  152. } // namespace crown