Compiler.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "Compiler.h"
  23. #include "Hash.h"
  24. #include "Path.h"
  25. #include "FileStream.h"
  26. #include <cstring>
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. Compiler::Compiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
  31. m_name_hash(0),
  32. m_type_hash(0),
  33. m_seed(seed),
  34. m_root_fs(root_path),
  35. m_dest_fs(dest_path),
  36. m_src_file(NULL),
  37. m_dest_file(NULL),
  38. m_prepared(false),
  39. m_verbose(false)
  40. {
  41. // Init structures
  42. memset(m_root_path, 0, MAX_RESOURCE_PATH_LENGTH);
  43. memset(m_dest_path, 0, MAX_RESOURCE_PATH_LENGTH);
  44. memset(m_resource, 0, MAX_RESOURCE_PATH_LENGTH);
  45. memset(&m_compiled_header, 0, sizeof(CompiledHeader));
  46. memset(m_name, 0, MAX_RESOURCE_NAME_LENGTH);
  47. memset(m_type, 0, MAX_RESOURCE_TYPE_LENGTH);
  48. string::strncpy(m_root_path, root_path, MAX_RESOURCE_PATH_LENGTH);
  49. string::strncpy(m_dest_path, dest_path, MAX_RESOURCE_PATH_LENGTH);
  50. string::strncpy(m_resource, resource, MAX_RESOURCE_PATH_LENGTH);
  51. // Extract resource name and type
  52. path::filename_without_extension(m_resource, m_name, MAX_RESOURCE_NAME_LENGTH);
  53. path::extension(m_resource, m_type, MAX_RESOURCE_TYPE_LENGTH);
  54. // Compute hashes
  55. m_name_hash = hash::murmur2_32(m_name, string::strlen(m_name), m_seed);
  56. m_type_hash = hash::murmur2_32(m_type, string::strlen(m_type), m_seed);
  57. char dest_name[17];
  58. memset(dest_name, 0, 17);
  59. snprintf(dest_name, 17, "%X%X", m_name_hash, m_type_hash);
  60. // Open streams
  61. m_src_file = (FileStream*)m_root_fs.open(m_resource, SOM_READ);
  62. m_dest_file = (FileStream*)m_dest_fs.open(dest_name, SOM_WRITE);
  63. }
  64. //-----------------------------------------------------------------------------
  65. Compiler::~Compiler()
  66. {
  67. if (m_src_file != NULL)
  68. {
  69. m_root_fs.close(m_src_file);
  70. }
  71. if (m_dest_file != NULL)
  72. {
  73. m_dest_fs.close(m_dest_file);
  74. }
  75. }
  76. //-----------------------------------------------------------------------------
  77. const char* Compiler::root_path() const
  78. {
  79. return m_root_path;
  80. }
  81. //-----------------------------------------------------------------------------
  82. const char* Compiler::dest_path() const
  83. {
  84. return m_dest_path;
  85. }
  86. //-----------------------------------------------------------------------------
  87. const char* Compiler::resource_path() const
  88. {
  89. return m_resource;
  90. }
  91. //-----------------------------------------------------------------------------
  92. uint32_t Compiler::resource_name_hash() const
  93. {
  94. return m_name_hash;
  95. }
  96. //-----------------------------------------------------------------------------
  97. uint32_t Compiler::resource_type_hash() const
  98. {
  99. return m_type_hash;
  100. }
  101. //-----------------------------------------------------------------------------
  102. uint32_t Compiler::seed() const
  103. {
  104. return m_seed;
  105. }
  106. //-----------------------------------------------------------------------------
  107. const char* Compiler::resource_name() const
  108. {
  109. return m_name;
  110. }
  111. //-----------------------------------------------------------------------------
  112. const char* Compiler::resource_type() const
  113. {
  114. return m_type;
  115. }
  116. //-----------------------------------------------------------------------------
  117. FileStream* Compiler::source_file()
  118. {
  119. return m_src_file;
  120. }
  121. //-----------------------------------------------------------------------------
  122. FileStream* Compiler::destination_file()
  123. {
  124. return m_dest_file;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Compiler::prepare_header(uint32_t size)
  128. {
  129. m_compiled_header.name = m_name_hash;
  130. m_compiled_header.type = m_type_hash;
  131. m_compiled_header.size = size;
  132. }
  133. //-----------------------------------------------------------------------------
  134. void Compiler::write_header()
  135. {
  136. m_dest_file->write(&m_compiled_header, sizeof(CompiledHeader));
  137. }
  138. } // namespace crown