PackageCompiler.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "Allocator.h"
  24. #include "File.h"
  25. #include "Filesystem.h"
  26. #include "Hash.h"
  27. #include "JSONParser.h"
  28. #include "PackageCompiler.h"
  29. #include "PackageResource.h"
  30. #include "TempAllocator.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. PackageCompiler::PackageCompiler()
  35. : m_has_texture(false)
  36. , m_has_lua(false)
  37. , m_has_sound(false)
  38. , m_texture(default_allocator())
  39. , m_script(default_allocator())
  40. , m_sound(default_allocator())
  41. , m_mesh(default_allocator())
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. size_t PackageCompiler::compile_impl(Filesystem& fs, const char* resource_path)
  46. {
  47. File* file = fs.open(resource_path, FOM_READ);
  48. char file_buf[4096];
  49. file->read(file_buf, file->size());
  50. fs.close(file);
  51. JSONParser json(file_buf);
  52. JSONElement root = json.root();
  53. // Check for resource types
  54. if (root.has_key("texture"))
  55. {
  56. JSONElement texture_array = root.key("texture");
  57. uint32_t texture_array_size = texture_array.size();
  58. for (uint32_t i = 0; i < texture_array_size; i++)
  59. {
  60. TempAllocator256 alloc;
  61. DynamicString texture_name(alloc);
  62. texture_name += texture_array[i].string_value();
  63. texture_name += ".texture";
  64. if (!fs.is_file(texture_name.c_str()))
  65. {
  66. Log::e("Texture '%s' does not exist.", texture_name.c_str());
  67. return 0;
  68. }
  69. ResourceId id;
  70. id.id = hash::murmur2_64(texture_name.c_str(), string::strlen(texture_name.c_str()), 0);
  71. m_texture.push_back(id);
  72. }
  73. }
  74. // Check for scripts
  75. if (root.has_key("lua"))
  76. {
  77. JSONElement lua_array = root.key("lua");
  78. //lua_array = root.key("lua");
  79. uint32_t lua_array_size = lua_array.size();
  80. for (uint32_t i = 0; i < lua_array_size; i++)
  81. {
  82. TempAllocator256 alloc;
  83. DynamicString lua_name(alloc);
  84. lua_name += lua_array[i].string_value();
  85. lua_name += ".lua";
  86. if (!fs.is_file(lua_name.c_str()))
  87. {
  88. Log::e("Lua script '%s' does not exist.", lua_name.c_str());
  89. return 0;
  90. }
  91. ResourceId id;
  92. id.id = hash::murmur2_64(lua_name.c_str(), string::strlen(lua_name.c_str()), 0);
  93. m_script.push_back(id);
  94. }
  95. }
  96. // Check for sounds
  97. if (root.has_key("sound"))
  98. {
  99. JSONElement sound_array = root.key("sound");
  100. uint32_t sound_array_size = sound_array.size();
  101. for (uint32_t i = 0; i < sound_array_size; i++)
  102. {
  103. TempAllocator256 alloc;
  104. DynamicString sound_name(alloc);
  105. sound_name += sound_array[i].string_value();
  106. sound_name += ".sound";
  107. if (!fs.is_file(sound_name.c_str()))
  108. {
  109. Log::e("Sound '%s' does not exist.", sound_name.c_str());
  110. return 0;
  111. }
  112. ResourceId id;
  113. id.id = hash::murmur2_64(sound_name.c_str(), string::strlen(sound_name.c_str()), 0);
  114. m_sound.push_back(id);
  115. }
  116. }
  117. // Check for meshes
  118. if (root.has_key("mesh"))
  119. {
  120. JSONElement mesh_array = root.key("mesh");
  121. uint32_t mesh_array_size = mesh_array.size();
  122. for (uint32_t i = 0; i < mesh_array_size; i++)
  123. {
  124. TempAllocator256 alloc;
  125. DynamicString mesh_name(alloc);
  126. mesh_name += mesh_array[i].string_value();
  127. mesh_name += ".mesh";
  128. if (!fs.is_file(mesh_name.c_str()))
  129. {
  130. Log::e("Mesh '%s' does not exist.", mesh_name.c_str());
  131. return 0;
  132. }
  133. ResourceId id;
  134. id.id = hash::murmur2_64(mesh_name.c_str(), string::strlen(mesh_name.c_str()), 0);
  135. m_mesh.push_back(id);
  136. }
  137. }
  138. return sizeof(PackageHeader) +
  139. m_texture.size() * sizeof(ResourceId) +
  140. m_script.size() * sizeof(ResourceId) +
  141. m_sound.size() * sizeof(ResourceId) +
  142. m_mesh.size() * sizeof(ResourceId);
  143. }
  144. //-----------------------------------------------------------------------------
  145. void PackageCompiler::write_impl(File* out_file)
  146. {
  147. PackageHeader header;
  148. header.num_textures = m_texture.size();
  149. header.num_scripts = m_script.size();
  150. header.num_sounds = m_sound.size();
  151. header.num_meshes = m_mesh.size();
  152. header.textures_offset = sizeof(PackageHeader);
  153. header.scripts_offset = header.textures_offset + sizeof(ResourceId) * header.num_textures;
  154. header.sounds_offset = header.scripts_offset + sizeof(ResourceId) * header.num_scripts;
  155. header.meshes_offset = header.sounds_offset + sizeof(ResourceId) * header.num_sounds;
  156. out_file->write((char*) &header, sizeof(PackageHeader));
  157. if (m_texture.size() > 0)
  158. {
  159. out_file->write((char*) m_texture.begin(), sizeof(ResourceId) * header.num_textures);
  160. }
  161. if (m_script.size() > 0)
  162. {
  163. out_file->write((char*) m_script.begin(), sizeof(ResourceId) * header.num_scripts);
  164. }
  165. if (m_sound.size() > 0)
  166. {
  167. out_file->write((char*) m_sound.begin(), sizeof(ResourceId) * header.num_sounds);
  168. }
  169. if (m_mesh.size() > 0)
  170. {
  171. out_file->write((char*) m_mesh.begin(), sizeof(ResourceId) * header.num_meshes);
  172. }
  173. // Cleanup
  174. m_texture.clear();
  175. m_script.clear();
  176. m_sound.clear();
  177. m_mesh.clear();
  178. }
  179. } // namespace crown