PackageResource.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "StringUtils.h"
  27. #include "JSONParser.h"
  28. #include "Log.h"
  29. #include "PackageResource.h"
  30. #include "TempAllocator.h"
  31. namespace crown
  32. {
  33. namespace package_resource
  34. {
  35. //-----------------------------------------------------------------------------
  36. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  37. {
  38. File* file = fs.open(resource_path, FOM_READ);
  39. char file_buf[4096];
  40. file->read(file_buf, file->size());
  41. fs.close(file);
  42. JSONParser json(file_buf);
  43. JSONElement root = json.root();
  44. Array<ResourceId> m_texture(default_allocator());
  45. Array<ResourceId> m_script(default_allocator());
  46. Array<ResourceId> m_sound(default_allocator());
  47. Array<ResourceId> m_mesh(default_allocator());
  48. Array<ResourceId> m_unit(default_allocator());
  49. Array<ResourceId> m_sprite(default_allocator());
  50. Array<ResourceId> m_physics(default_allocator());
  51. Array<ResourceId> m_materials(default_allocator());
  52. Array<ResourceId> m_guis(default_allocator());
  53. Array<ResourceId> m_fonts(default_allocator());
  54. Array<ResourceId> m_levels(default_allocator());
  55. // Check for resource types
  56. if (root.has_key("texture"))
  57. {
  58. JSONElement texture_array = root.key("texture");
  59. uint32_t texture_array_size = texture_array.size();
  60. for (uint32_t i = 0; i < texture_array_size; i++)
  61. {
  62. DynamicString texture_name;
  63. texture_array[i].to_string(texture_name); texture_name += ".texture";
  64. if (!fs.is_file(texture_name.c_str()))
  65. {
  66. CE_LOGE("Texture '%s' does not exist.", texture_name.c_str());
  67. return;
  68. }
  69. array::push_back(m_texture, ResourceId(texture_name.c_str()));
  70. }
  71. }
  72. // Check for scripts
  73. if (root.has_key("lua"))
  74. {
  75. JSONElement lua_array = root.key("lua");
  76. //lua_array = root.key("lua");
  77. uint32_t lua_array_size = lua_array.size();
  78. for (uint32_t i = 0; i < lua_array_size; i++)
  79. {
  80. DynamicString lua_name;
  81. lua_array[i].to_string(lua_name); lua_name += ".lua";
  82. if (!fs.is_file(lua_name.c_str()))
  83. {
  84. CE_LOGE("Lua script '%s' does not exist.", lua_name.c_str());
  85. return;
  86. }
  87. array::push_back(m_script, ResourceId(lua_name.c_str()));
  88. }
  89. }
  90. // Check for sounds
  91. if (root.has_key("sound"))
  92. {
  93. JSONElement sound_array = root.key("sound");
  94. uint32_t sound_array_size = sound_array.size();
  95. for (uint32_t i = 0; i < sound_array_size; i++)
  96. {
  97. DynamicString sound_name;
  98. sound_array[i].to_string(sound_name); sound_name += ".sound";
  99. if (!fs.is_file(sound_name.c_str()))
  100. {
  101. CE_LOGE("Sound '%s' does not exist.", sound_name.c_str());
  102. return;
  103. }
  104. array::push_back(m_sound, ResourceId(sound_name.c_str()));
  105. }
  106. }
  107. // Check for meshes
  108. if (root.has_key("mesh"))
  109. {
  110. JSONElement mesh_array = root.key("mesh");
  111. uint32_t mesh_array_size = mesh_array.size();
  112. for (uint32_t i = 0; i < mesh_array_size; i++)
  113. {
  114. DynamicString mesh_name;
  115. mesh_array[i].to_string(mesh_name); mesh_name += ".mesh";
  116. if (!fs.is_file(mesh_name.c_str()))
  117. {
  118. CE_LOGE("Mesh '%s' does not exist.", mesh_name.c_str());
  119. return;
  120. }
  121. array::push_back(m_mesh, ResourceId(mesh_name.c_str()));
  122. }
  123. }
  124. // Check for units
  125. if (root.has_key("unit"))
  126. {
  127. JSONElement unit_array = root.key("unit");
  128. uint32_t unit_array_size = unit_array.size();
  129. for (uint32_t i = 0; i < unit_array_size; i++)
  130. {
  131. DynamicString unit_name;
  132. unit_array[i].to_string(unit_name); unit_name += ".unit";
  133. if (!fs.is_file(unit_name.c_str()))
  134. {
  135. CE_LOGE("Unit '%s' does not exist.", unit_name.c_str());
  136. }
  137. array::push_back(m_unit, ResourceId(unit_name.c_str()));
  138. }
  139. }
  140. // Check for meshes
  141. if (root.has_key("sprite"))
  142. {
  143. JSONElement sprite_array = root.key("sprite");
  144. uint32_t sprite_array_size = sprite_array.size();
  145. for (uint32_t i = 0; i < sprite_array_size; i++)
  146. {
  147. DynamicString sprite_name;
  148. sprite_array[i].to_string(sprite_name); sprite_name += ".sprite";
  149. if (!fs.is_file(sprite_name.c_str()))
  150. {
  151. CE_LOGE("Sprite '%s' does not exist.", sprite_name.c_str());
  152. return;
  153. }
  154. array::push_back(m_sprite, ResourceId(sprite_name.c_str()));
  155. }
  156. }
  157. // Check for physics
  158. if (root.has_key("physics"))
  159. {
  160. JSONElement physics_array = root.key("physics");
  161. uint32_t physics_array_size = physics_array.size();
  162. for (uint32_t i = 0; i < physics_array_size; i++)
  163. {
  164. DynamicString physics_name;
  165. physics_array[i].to_string(physics_name); physics_name += ".physics";
  166. if (!fs.is_file(physics_name.c_str()))
  167. {
  168. CE_LOGE("Physics '%s' does not exist.", physics_name.c_str());
  169. return;
  170. }
  171. array::push_back(m_physics, ResourceId(physics_name.c_str()));
  172. }
  173. }
  174. // Check for materials
  175. if (root.has_key("material"))
  176. {
  177. JSONElement materials_array = root.key("material");
  178. uint32_t materials_array_size = materials_array.size();
  179. for (uint32_t i = 0; i < materials_array_size; i++)
  180. {
  181. DynamicString materials_name;
  182. materials_array[i].to_string(materials_name); materials_name += ".material";
  183. if (!fs.is_file(materials_name.c_str()))
  184. {
  185. CE_LOGE("Material '%s' does not exist.", materials_name.c_str());
  186. return;
  187. }
  188. array::push_back(m_materials, ResourceId(materials_name.c_str()));
  189. }
  190. }
  191. // Check for materials
  192. if (root.has_key("gui"))
  193. {
  194. JSONElement guis_array = root.key("gui");
  195. uint32_t guis_array_size = guis_array.size();
  196. for (uint32_t i = 0; i < guis_array_size; i++)
  197. {
  198. DynamicString guis_name;
  199. guis_array[i].to_string(guis_name); guis_name += ".gui";
  200. if (!fs.is_file(guis_name.c_str()))
  201. {
  202. CE_LOGE("gui '%s' does not exist.", guis_name.c_str());
  203. return;
  204. }
  205. array::push_back(m_guis, ResourceId(guis_name.c_str()));
  206. }
  207. }
  208. // Check for fonts
  209. if (root.has_key("font"))
  210. {
  211. JSONElement fonts_array = root.key("font");
  212. uint32_t fonts_array_size = fonts_array.size();
  213. for (uint32_t i = 0; i < fonts_array_size; i++)
  214. {
  215. DynamicString font_name;
  216. fonts_array[i].to_string(font_name); font_name += ".font";
  217. if (!fs.is_file(font_name.c_str()))
  218. {
  219. CE_LOGE("font '%s' does not exist.", font_name.c_str());
  220. return;
  221. }
  222. array::push_back(m_fonts, ResourceId(font_name.c_str()));
  223. }
  224. }
  225. // Check for fonts
  226. if (root.has_key("level"))
  227. {
  228. JSONElement levels_array = root.key("level");
  229. uint32_t levels_array_size = levels_array.size();
  230. for (uint32_t i = 0; i < levels_array_size; i++)
  231. {
  232. DynamicString level_name;
  233. levels_array[i].to_string(level_name); level_name += ".level";
  234. if (!fs.is_file(level_name.c_str()))
  235. {
  236. CE_LOGE("level '%s' does not exist.", level_name.c_str());
  237. return;
  238. }
  239. array::push_back(m_levels, ResourceId(level_name.c_str()));
  240. }
  241. }
  242. PackageHeader header;
  243. header.num_textures = array::size(m_texture);
  244. header.num_scripts = array::size(m_script);
  245. header.num_sounds = array::size(m_sound);
  246. header.num_meshes = array::size(m_mesh);
  247. header.num_units = array::size(m_unit);
  248. header.num_sprites = array::size(m_sprite);
  249. header.num_physics = array::size(m_physics);
  250. header.num_materials = array::size(m_materials);
  251. header.num_guis = array::size(m_guis);
  252. header.num_fonts = array::size(m_fonts);
  253. header.num_levels = array::size(m_levels);
  254. header.textures_offset = sizeof(PackageHeader);
  255. header.scripts_offset = header.textures_offset + sizeof(ResourceId) * header.num_textures;
  256. header.sounds_offset = header.scripts_offset + sizeof(ResourceId) * header.num_scripts;
  257. header.meshes_offset = header.sounds_offset + sizeof(ResourceId) * header.num_sounds;
  258. header.units_offset = header.meshes_offset + sizeof(ResourceId) * header.num_meshes;
  259. header.sprites_offset = header.units_offset + sizeof(ResourceId) * header.num_units;
  260. header.physics_offset = header.sprites_offset + sizeof(ResourceId) * header.num_sprites;
  261. header.materials_offset = header.physics_offset + sizeof(ResourceId) * header.num_physics;
  262. header.guis_offset = header.materials_offset + sizeof(ResourceId) * header.num_materials;
  263. header.fonts_offset = header.guis_offset + sizeof(ResourceId) * header.num_guis;
  264. header.levels_offset = header.fonts_offset + sizeof(ResourceId) * header.num_fonts;
  265. out_file->write((char*) &header, sizeof(PackageHeader));
  266. if (array::size(m_texture) > 0)
  267. {
  268. out_file->write((char*) array::begin(m_texture), sizeof(ResourceId) * header.num_textures);
  269. }
  270. if (array::size(m_script) > 0)
  271. {
  272. out_file->write((char*) array::begin(m_script), sizeof(ResourceId) * header.num_scripts);
  273. }
  274. if (array::size(m_sound) > 0)
  275. {
  276. out_file->write((char*) array::begin(m_sound), sizeof(ResourceId) * header.num_sounds);
  277. }
  278. if (array::size(m_mesh) > 0)
  279. {
  280. out_file->write((char*) array::begin(m_mesh), sizeof(ResourceId) * header.num_meshes);
  281. }
  282. if (array::size(m_unit) > 0)
  283. {
  284. out_file->write((char*) array::begin(m_unit), sizeof(ResourceId) * header.num_units);
  285. }
  286. if (array::size(m_sprite) > 0)
  287. {
  288. out_file->write((char*) array::begin(m_sprite), sizeof(ResourceId) * header.num_sprites);
  289. }
  290. if (array::size(m_physics) > 0)
  291. {
  292. out_file->write((char*) array::begin(m_physics), sizeof(ResourceId) * header.num_physics);
  293. }
  294. if (array::size(m_materials) > 0)
  295. {
  296. out_file->write((char*) array::begin(m_materials), sizeof(ResourceId) * header.num_materials);
  297. }
  298. if (array::size(m_guis) > 0)
  299. {
  300. out_file->write((char*) array::begin(m_guis), sizeof(ResourceId) * header.num_guis);
  301. }
  302. if (array::size(m_fonts) > 0)
  303. {
  304. out_file->write((char*) array::begin(m_fonts), sizeof(ResourceId) * header.num_fonts);
  305. }
  306. if (array::size(m_levels) > 0)
  307. {
  308. out_file->write((char*) array::begin(m_levels), sizeof(ResourceId) * header.num_levels);
  309. }
  310. }
  311. } // namespace package_resource
  312. } // namespace crown