package_resource.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. //-----------------------------------------------------------------------------
  34. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  35. {
  36. File* file = fs.open(resource_path, FOM_READ);
  37. JSONParser json(*file);
  38. fs.close(file);
  39. JSONElement root = json.root();
  40. BinaryWriter bw(*out_file);
  41. JSONElement texture = root.key_or_nil("texture");
  42. JSONElement script = root.key_or_nil("lua");
  43. JSONElement sound = root.key_or_nil("sound");
  44. JSONElement mesh = root.key_or_nil("mesh");
  45. JSONElement unit = root.key_or_nil("unit");
  46. JSONElement sprite = root.key_or_nil("sprite");
  47. JSONElement physics = root.key_or_nil("physics");
  48. JSONElement material = root.key_or_nil("material");
  49. JSONElement font = root.key_or_nil("font");
  50. JSONElement level = root.key_or_nil("level");
  51. JSONElement phyconf = root.key_or_nil("physics_config");
  52. JSONElement shader = root.key_or_nil("shader");
  53. JSONElement sprite_animation = root.key_or_nil("sprite_animation");
  54. const uint32_t num_textures = texture.is_nil() ? 0 : texture.size();
  55. const uint32_t num_scripts = script.is_nil() ? 0 : script.size();
  56. const uint32_t num_sounds = sound.is_nil() ? 0 : sound.size();
  57. const uint32_t num_meshes = mesh.is_nil() ? 0 : mesh.size();
  58. const uint32_t num_units = unit.is_nil() ? 0 : unit.size();
  59. const uint32_t num_sprites = sprite.is_nil() ? 0 : sprite.size();
  60. const uint32_t num_physics = physics.is_nil() ? 0 : physics.size();
  61. const uint32_t num_materials = material.is_nil() ? 0 : material.size();
  62. const uint32_t num_fonts = font.is_nil() ? 0 : font.size();
  63. const uint32_t num_levels = level.is_nil() ? 0 : level.size();
  64. const uint32_t num_phyconfs = phyconf.is_nil() ? 0 : phyconf.size();
  65. const uint32_t num_shaders = shader.is_nil() ? 0 : shader.size();
  66. const uint32_t num_sprite_animations = sprite_animation.is_nil() ? 0 : sprite_animation.size();
  67. // Write header
  68. bw.write(num_textures);
  69. uint32_t offt = sizeof(PackageHeader);
  70. bw.write(offt);
  71. bw.write(num_scripts);
  72. offt += sizeof(ResourceId) * num_textures;
  73. bw.write(offt);
  74. bw.write(num_sounds);
  75. offt += sizeof(ResourceId) * num_scripts;
  76. bw.write(offt);
  77. bw.write(num_meshes);
  78. offt += sizeof(ResourceId) * num_sounds;
  79. bw.write(offt);
  80. bw.write(num_units);
  81. offt += sizeof(ResourceId) * num_meshes;
  82. bw.write(offt);
  83. bw.write(num_sprites);
  84. offt += sizeof(ResourceId) * num_units;
  85. bw.write(offt);
  86. bw.write(num_physics);
  87. offt += sizeof(ResourceId) * num_sprites;
  88. bw.write(offt);
  89. bw.write(num_materials);
  90. offt += sizeof(ResourceId) * num_physics;
  91. bw.write(offt);
  92. bw.write(num_fonts);
  93. offt += sizeof(ResourceId) * num_materials;
  94. bw.write(offt);
  95. bw.write(num_levels);
  96. offt += sizeof(ResourceId) * num_fonts;
  97. bw.write(offt);
  98. bw.write(num_phyconfs);
  99. offt += sizeof(ResourceId) * num_levels;
  100. bw.write(offt);
  101. bw.write(num_shaders);
  102. offt += sizeof(ResourceId) * num_phyconfs;
  103. bw.write(offt);
  104. bw.write(num_sprite_animations);
  105. offt += sizeof(ResourceId) * num_shaders;
  106. bw.write(offt);
  107. // Write resource ids
  108. for (uint32_t i = 0; i < num_textures; i++)
  109. bw.write(texture[i].to_resource_id("texture"));
  110. for (uint32_t i = 0; i < num_scripts; i++)
  111. bw.write(script[i].to_resource_id("lua"));
  112. for (uint32_t i = 0; i < num_sounds; i++)
  113. bw.write(sound[i].to_resource_id("sound"));
  114. for (uint32_t i = 0; i < num_meshes; i++)
  115. bw.write(mesh[i].to_resource_id("mesh"));
  116. for (uint32_t i = 0; i < num_units; i++)
  117. bw.write(unit[i].to_resource_id("unit"));
  118. for (uint32_t i = 0; i < num_sprites; i++)
  119. bw.write(sprite[i].to_resource_id("sprite"));
  120. for (uint32_t i = 0; i < num_physics; i++)
  121. bw.write(physics[i].to_resource_id("physics"));
  122. for (uint32_t i = 0; i < num_materials; i++)
  123. bw.write(material[i].to_resource_id("material"));
  124. for (uint32_t i = 0; i < num_fonts; i++)
  125. bw.write(font[i].to_resource_id("font"));
  126. for (uint32_t i = 0; i < num_levels; i++)
  127. bw.write(level[i].to_resource_id("level"));
  128. for (uint32_t i = 0; i < num_phyconfs; i++)
  129. bw.write(phyconf[i].to_resource_id("physics_config"));
  130. for (uint32_t i = 0; i < num_shaders; i++)
  131. bw.write(shader[i].to_resource_id("shader"));
  132. for (uint32_t i = 0; i < num_sprite_animations; i++)
  133. bw.write(sprite_animation[i].to_resource_id("sprite_animation"));
  134. }
  135. //-----------------------------------------------------------------------------
  136. void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  137. {
  138. File* file = bundle.open(id);
  139. const size_t file_size = file->size();
  140. void* res = allocator.allocate(file_size);
  141. file->read(res, file_size);
  142. bundle.close(file);
  143. return res;
  144. }
  145. //-----------------------------------------------------------------------------
  146. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  147. {
  148. }
  149. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  150. {
  151. }
  152. //-----------------------------------------------------------------------------
  153. void unload(Allocator& allocator, void* resource)
  154. {
  155. allocator.deallocate(resource);
  156. }
  157. } // namespace package_resource
  158. } // namespace crown