Compiler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  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. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <cstring>
  24. #include <cstdlib>
  25. #include "Compiler.h"
  26. #include "Hash.h"
  27. #include "Path.h"
  28. #include "DiskFile.h"
  29. #include "Log.h"
  30. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. Compiler::Compiler(const char* root_path, const char* dest_path, uint32_t type_expected) :
  34. m_root_fs(root_path),
  35. m_dest_fs(dest_path),
  36. m_type_expected(type_expected)
  37. {
  38. memset(m_resource_name, 0, MAX_RESOURCE_NAME_LENGTH);
  39. }
  40. //-----------------------------------------------------------------------------
  41. Compiler::~Compiler()
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. size_t Compiler::compile(const char* resource, uint32_t name, uint32_t type)
  46. {
  47. string::strncpy(m_resource_name, resource, MAX_RESOURCE_NAME_LENGTH);
  48. char resource_name[MAX_RESOURCE_NAME_LENGTH];
  49. char resource_type[MAX_RESOURCE_TYPE_LENGTH];
  50. path::filename_without_extension(resource, resource_name, MAX_RESOURCE_NAME_LENGTH);
  51. path::extension(resource, resource_type, MAX_RESOURCE_TYPE_LENGTH);
  52. char output_name[17];
  53. snprintf(output_name, 17, "%.8X%.8X", name, type);
  54. Log::i("%s <= %s", output_name, resource);
  55. if (type != m_type_expected)
  56. {
  57. Log::e("'%s': resource type does not match expected type.", resource);
  58. return 0;
  59. }
  60. if (!m_root_fs.exists(resource))
  61. {
  62. Log::e("'%s': resource does not exist.");
  63. return 0;
  64. }
  65. // Read source file
  66. DiskFile* input_file = m_root_fs.open(resource, FOM_READ);
  67. size_t header_size = read_header(input_file);
  68. size_t resource_size = read_resource(input_file);
  69. // Write compiled file
  70. DiskFile* output_file;
  71. if (m_dest_fs.exists(output_name))
  72. {
  73. m_dest_fs.delete_file(output_name);
  74. }
  75. m_dest_fs.create_file(output_name);
  76. output_file = m_dest_fs.open(output_name, FOM_WRITE);
  77. write_header(output_file, name, type, resource_size);
  78. write_resource(output_file);
  79. m_root_fs.close(input_file);
  80. m_dest_fs.close(output_file);
  81. // Cleanup
  82. cleanup();
  83. }
  84. //-----------------------------------------------------------------------------
  85. size_t Compiler::read_header(DiskFile* in_file)
  86. {
  87. return read_header_impl(in_file);
  88. }
  89. //-----------------------------------------------------------------------------
  90. size_t Compiler::read_resource(DiskFile* in_file)
  91. {
  92. return read_resource_impl(in_file);
  93. }
  94. //-----------------------------------------------------------------------------
  95. void Compiler::write_header(DiskFile* out_file, uint32_t name, uint32_t type, uint32_t resource_size)
  96. {
  97. CompiledHeader header;
  98. header.magic = COMPILED_HEADER_MAGIC_NUMBER;
  99. header.version = COMPILER_VERSION;
  100. header.name = name;
  101. header.type = type;
  102. header.size = resource_size;
  103. out_file->write(&header, sizeof(CompiledHeader));
  104. write_header_impl(out_file);
  105. }
  106. //-----------------------------------------------------------------------------
  107. void Compiler::write_resource(DiskFile* out_file)
  108. {
  109. write_resource_impl(out_file);
  110. }
  111. //-----------------------------------------------------------------------------
  112. void Compiler::cleanup()
  113. {
  114. cleanup_impl();
  115. string::strncpy(m_resource_name, "", MAX_RESOURCE_NAME_LENGTH);
  116. }
  117. //-----------------------------------------------------------------------------
  118. const char* Compiler::root_path() const
  119. {
  120. return m_root_fs.root_path();
  121. }
  122. //-----------------------------------------------------------------------------
  123. const char* Compiler::dest_path() const
  124. {
  125. return m_dest_fs.root_path();
  126. }
  127. //-----------------------------------------------------------------------------
  128. const char* Compiler::resource_name() const
  129. {
  130. return m_resource_name;
  131. }
  132. } // namespace crown