package_resource.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "file.h"
  24. #include "filesystem.h"
  25. #include "json_parser.h"
  26. #include "package_resource.h"
  27. #include "temp_allocator.h"
  28. #include "reader_writer.h"
  29. namespace crown
  30. {
  31. namespace package_resource
  32. {
  33. void compile(const char* path, CompileOptions& opts)
  34. {
  35. static const uint32_t VERSION = 1;
  36. Buffer buf = opts.read(path);
  37. JSONParser json(array::begin(buf));
  38. JSONElement root = json.root();
  39. JSONElement texture = root.key_or_nil("texture");
  40. JSONElement script = root.key_or_nil("lua");
  41. JSONElement sound = root.key_or_nil("sound");
  42. JSONElement mesh = root.key_or_nil("mesh");
  43. JSONElement unit = root.key_or_nil("unit");
  44. JSONElement sprite = root.key_or_nil("sprite");
  45. JSONElement physics = root.key_or_nil("physics");
  46. JSONElement material = root.key_or_nil("material");
  47. JSONElement font = root.key_or_nil("font");
  48. JSONElement level = root.key_or_nil("level");
  49. JSONElement phyconf = root.key_or_nil("physics_config");
  50. JSONElement shader = root.key_or_nil("shader");
  51. JSONElement sprite_animation = root.key_or_nil("sprite_animation");
  52. const uint32_t num_textures = texture.is_nil() ? 0 : texture.size();
  53. const uint32_t num_scripts = script.is_nil() ? 0 : script.size();
  54. const uint32_t num_sounds = sound.is_nil() ? 0 : sound.size();
  55. const uint32_t num_meshes = mesh.is_nil() ? 0 : mesh.size();
  56. const uint32_t num_units = unit.is_nil() ? 0 : unit.size();
  57. const uint32_t num_sprites = sprite.is_nil() ? 0 : sprite.size();
  58. const uint32_t num_physics = physics.is_nil() ? 0 : physics.size();
  59. const uint32_t num_materials = material.is_nil() ? 0 : material.size();
  60. const uint32_t num_fonts = font.is_nil() ? 0 : font.size();
  61. const uint32_t num_levels = level.is_nil() ? 0 : level.size();
  62. const uint32_t num_phyconfs = phyconf.is_nil() ? 0 : phyconf.size();
  63. const uint32_t num_shaders = shader.is_nil() ? 0 : shader.size();
  64. const uint32_t num_sprite_animations = sprite_animation.is_nil() ? 0 : sprite_animation.size();
  65. // Write header
  66. opts.write(VERSION);
  67. opts.write(num_textures);
  68. uint32_t offt = sizeof(PackageResource);
  69. opts.write(offt);
  70. opts.write(num_scripts);
  71. offt += sizeof(StringId64) * num_textures;
  72. opts.write(offt);
  73. opts.write(num_sounds);
  74. offt += sizeof(StringId64) * num_scripts;
  75. opts.write(offt);
  76. opts.write(num_meshes);
  77. offt += sizeof(StringId64) * num_sounds;
  78. opts.write(offt);
  79. opts.write(num_units);
  80. offt += sizeof(StringId64) * num_meshes;
  81. opts.write(offt);
  82. opts.write(num_sprites);
  83. offt += sizeof(StringId64) * num_units;
  84. opts.write(offt);
  85. opts.write(num_physics);
  86. offt += sizeof(StringId64) * num_sprites;
  87. opts.write(offt);
  88. opts.write(num_materials);
  89. offt += sizeof(StringId64) * num_physics;
  90. opts.write(offt);
  91. opts.write(num_fonts);
  92. offt += sizeof(StringId64) * num_materials;
  93. opts.write(offt);
  94. opts.write(num_levels);
  95. offt += sizeof(StringId64) * num_fonts;
  96. opts.write(offt);
  97. opts.write(num_phyconfs);
  98. offt += sizeof(StringId64) * num_levels;
  99. opts.write(offt);
  100. opts.write(num_shaders);
  101. offt += sizeof(StringId64) * num_phyconfs;
  102. opts.write(offt);
  103. opts.write(num_sprite_animations);
  104. offt += sizeof(StringId64) * num_shaders;
  105. opts.write(offt);
  106. // Write resource ids
  107. for (uint32_t i = 0; i < num_textures; i++)
  108. opts.write(texture[i].to_resource_id("texture").name);
  109. for (uint32_t i = 0; i < num_scripts; i++)
  110. opts.write(script[i].to_resource_id("lua").name);
  111. for (uint32_t i = 0; i < num_sounds; i++)
  112. opts.write(sound[i].to_resource_id("sound").name);
  113. for (uint32_t i = 0; i < num_meshes; i++)
  114. opts.write(mesh[i].to_resource_id("mesh").name);
  115. for (uint32_t i = 0; i < num_units; i++)
  116. opts.write(unit[i].to_resource_id("unit").name);
  117. for (uint32_t i = 0; i < num_sprites; i++)
  118. opts.write(sprite[i].to_resource_id("sprite").name);
  119. for (uint32_t i = 0; i < num_physics; i++)
  120. opts.write(physics[i].to_resource_id("physics").name);
  121. for (uint32_t i = 0; i < num_materials; i++)
  122. opts.write(material[i].to_resource_id("material").name);
  123. for (uint32_t i = 0; i < num_fonts; i++)
  124. opts.write(font[i].to_resource_id("font").name);
  125. for (uint32_t i = 0; i < num_levels; i++)
  126. opts.write(level[i].to_resource_id("level").name);
  127. for (uint32_t i = 0; i < num_phyconfs; i++)
  128. opts.write(phyconf[i].to_resource_id("physics_config").name);
  129. for (uint32_t i = 0; i < num_shaders; i++)
  130. opts.write(shader[i].to_resource_id("shader").name);
  131. for (uint32_t i = 0; i < num_sprite_animations; i++)
  132. opts.write(sprite_animation[i].to_resource_id("sprite_animation").name);
  133. }
  134. void* load(File& file, Allocator& a)
  135. {
  136. const size_t file_size = file.size();
  137. void* res = a.allocate(file_size);
  138. file.read(res, file_size);
  139. return res;
  140. }
  141. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  142. {
  143. }
  144. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  145. {
  146. }
  147. void unload(Allocator& allocator, void* resource)
  148. {
  149. allocator.deallocate(resource);
  150. }
  151. uint32_t num_textures(const PackageResource* pr)
  152. {
  153. return pr->num_textures;
  154. }
  155. uint32_t num_scripts(const PackageResource* pr)
  156. {
  157. return pr->num_scripts;
  158. }
  159. uint32_t num_sounds(const PackageResource* pr)
  160. {
  161. return pr->num_sounds;
  162. }
  163. uint32_t num_meshes(const PackageResource* pr)
  164. {
  165. return pr->num_meshes;
  166. }
  167. uint32_t num_units(const PackageResource* pr)
  168. {
  169. return pr->num_units;
  170. }
  171. uint32_t num_sprites(const PackageResource* pr)
  172. {
  173. return pr->num_sprites;
  174. }
  175. uint32_t num_physics(const PackageResource* pr)
  176. {
  177. return pr->num_physics;
  178. }
  179. uint32_t num_materials(const PackageResource* pr)
  180. {
  181. return pr->num_materials;
  182. }
  183. uint32_t num_fonts(const PackageResource* pr)
  184. {
  185. return pr->num_fonts;
  186. }
  187. uint32_t num_levels(const PackageResource* pr)
  188. {
  189. return pr->num_levels;
  190. }
  191. uint32_t num_physics_configs(const PackageResource* pr)
  192. {
  193. return pr->num_physics_configs;
  194. }
  195. uint32_t num_shaders(const PackageResource* pr)
  196. {
  197. return pr->num_shaders;
  198. }
  199. uint32_t num_sprite_animations(const PackageResource* pr)
  200. {
  201. return pr->num_sprite_animations;
  202. }
  203. StringId64 get_texture_id(const PackageResource* pr, uint32_t i)
  204. {
  205. CE_ASSERT(i < num_textures(pr), "Index out of bounds");
  206. StringId64* begin = (StringId64*) ((char*)pr + pr->textures_offset);
  207. return begin[i];
  208. }
  209. StringId64 get_script_id(const PackageResource* pr, uint32_t i)
  210. {
  211. CE_ASSERT(i < num_scripts(pr), "Index out of bounds");
  212. StringId64* begin = (StringId64*) ((char*)pr + pr->scripts_offset);
  213. return begin[i];
  214. }
  215. StringId64 get_sound_id(const PackageResource* pr, uint32_t i)
  216. {
  217. CE_ASSERT(i < num_sounds(pr), "Index out of bounds");
  218. StringId64* begin = (StringId64*) ((char*)pr + pr->sounds_offset);
  219. return begin[i];
  220. }
  221. StringId64 get_mesh_id(const PackageResource* pr, uint32_t i)
  222. {
  223. CE_ASSERT(i < num_meshes(pr), "Index out of bounds");
  224. StringId64* begin = (StringId64*) ((char*)pr + pr->meshes_offset);
  225. return begin[i];
  226. }
  227. StringId64 get_unit_id(const PackageResource* pr, uint32_t i)
  228. {
  229. CE_ASSERT(i < num_units(pr), "Index out of bounds");
  230. StringId64* begin = (StringId64*) ((char*)pr + pr->units_offset);
  231. return begin[i];
  232. }
  233. StringId64 get_sprite_id(const PackageResource* pr, uint32_t i)
  234. {
  235. CE_ASSERT(i < num_sprites(pr), "Index out of bounds");
  236. StringId64* begin = (StringId64*) ((char*)pr + pr->sprites_offset);
  237. return begin[i];
  238. }
  239. StringId64 get_physics_id(const PackageResource* pr, uint32_t i)
  240. {
  241. CE_ASSERT(i < num_physics(pr), "Index out of bounds");
  242. StringId64* begin = (StringId64*) ((char*)pr + pr->physics_offset);
  243. return begin[i];
  244. }
  245. StringId64 get_material_id(const PackageResource* pr, uint32_t i)
  246. {
  247. CE_ASSERT(i < num_materials(pr), "Index out of bounds");
  248. StringId64* begin = (StringId64*) ((char*)pr + pr->materials_offset);
  249. return begin[i];
  250. }
  251. StringId64 get_font_id(const PackageResource* pr, uint32_t i)
  252. {
  253. CE_ASSERT(i < num_fonts(pr), "Index out of bounds");
  254. StringId64* begin = (StringId64*) ((char*)pr + pr->fonts_offset);
  255. return begin[i];
  256. }
  257. StringId64 get_level_id(const PackageResource* pr, uint32_t i)
  258. {
  259. CE_ASSERT(i < num_levels(pr), "Index out of bounds");
  260. StringId64* begin = (StringId64*) ((char*)pr + pr->levels_offset);
  261. return begin[i];
  262. }
  263. StringId64 get_physics_config_id(const PackageResource* pr, uint32_t i)
  264. {
  265. CE_ASSERT(i < num_physics_configs(pr), "Index out of bounds");
  266. StringId64* begin = (StringId64*) ((char*)pr + pr->physics_configs_offset);
  267. return begin[i];
  268. }
  269. StringId64 get_shader_id(const PackageResource* pr, uint32_t i)
  270. {
  271. CE_ASSERT(i < num_shaders(pr), "Index out of bounds");
  272. StringId64* begin = (StringId64*) ((char*)pr + pr->shaders_offset);
  273. return begin[i];
  274. }
  275. StringId64 get_sprite_animation_id(const PackageResource* pr, uint32_t i)
  276. {
  277. CE_ASSERT(i < num_sprite_animations(pr), "Index out of bounds");
  278. StringId64* begin = (StringId64*) ((char*)pr + pr->sprite_animations_offset);
  279. return begin[i];
  280. }
  281. } // namespace package_resource
  282. } // namespace crown