2
0

Compiler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, uint32_t type_expected) :
  33. m_root_fs(root_path),
  34. m_dest_fs(dest_path),
  35. m_type_expected(type_expected)
  36. {
  37. memset(m_resource_name, 0, MAX_RESOURCE_NAME_LENGTH);
  38. }
  39. //-----------------------------------------------------------------------------
  40. Compiler::~Compiler()
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. size_t Compiler::compile(const char* resource, uint32_t name, uint32_t type)
  45. {
  46. string::strncpy(m_resource_name, resource, MAX_RESOURCE_NAME_LENGTH);
  47. char resource_name[MAX_RESOURCE_NAME_LENGTH];
  48. char resource_type[MAX_RESOURCE_TYPE_LENGTH];
  49. path::filename_without_extension(resource, resource_name, MAX_RESOURCE_NAME_LENGTH);
  50. path::extension(resource, resource_type, MAX_RESOURCE_TYPE_LENGTH);
  51. char output_name[17];
  52. snprintf(output_name, 17, "%.8X%.8X", name, type);
  53. Log::i("%s <= %s", output_name, resource);
  54. if (type != m_type_expected)
  55. {
  56. Log::e("'%s': resource type does not match expected type.", resource);
  57. return 0;
  58. }
  59. if (!m_root_fs.exists(resource))
  60. {
  61. Log::e("'%s': resource does not exist.");
  62. return 0;
  63. }
  64. // Read source file
  65. FileStream* input_file = m_root_fs.open(resource, SOM_READ);
  66. size_t header_size = read_header(input_file);
  67. size_t resource_size = read_resource(input_file);
  68. // Write compiled file
  69. FileStream* output_file;
  70. if (m_dest_fs.exists(output_name))
  71. {
  72. m_dest_fs.delete_file(output_name);
  73. }
  74. m_dest_fs.create_file(output_name);
  75. output_file = m_dest_fs.open(output_name, SOM_WRITE);
  76. write_header(output_file, name, type, resource_size);
  77. write_resource(output_file);
  78. m_root_fs.close(input_file);
  79. m_dest_fs.close(output_file);
  80. // Cleanup
  81. cleanup();
  82. }
  83. //-----------------------------------------------------------------------------
  84. size_t Compiler::read_header(FileStream* in_file)
  85. {
  86. return read_header_impl(in_file);
  87. }
  88. //-----------------------------------------------------------------------------
  89. size_t Compiler::read_resource(FileStream* in_file)
  90. {
  91. return read_resource_impl(in_file);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Compiler::write_header(FileStream* out_file, uint32_t name, uint32_t type, uint32_t resource_size)
  95. {
  96. CompiledHeader header;
  97. header.magic = COMPILED_HEADER_MAGIC_NUMBER;
  98. header.version = COMPILER_VERSION;
  99. header.name = name;
  100. header.type = type;
  101. header.size = resource_size;
  102. out_file->write(&header, sizeof(CompiledHeader));
  103. write_header_impl(out_file);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void Compiler::write_resource(FileStream* out_file)
  107. {
  108. write_resource_impl(out_file);
  109. }
  110. //-----------------------------------------------------------------------------
  111. void Compiler::cleanup()
  112. {
  113. cleanup_impl();
  114. string::strncpy(m_resource_name, "", MAX_RESOURCE_NAME_LENGTH);
  115. }
  116. //-----------------------------------------------------------------------------
  117. const char* Compiler::root_path() const
  118. {
  119. return m_root_fs.root_path();
  120. }
  121. //-----------------------------------------------------------------------------
  122. const char* Compiler::dest_path() const
  123. {
  124. return m_dest_fs.root_path();
  125. }
  126. //-----------------------------------------------------------------------------
  127. const char* Compiler::resource_name() const
  128. {
  129. return m_resource_name;
  130. }
  131. } // namespace crown