package_resource.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #pragma once
  24. #include "types.h"
  25. #include "resource.h"
  26. #include "bundle.h"
  27. #include "allocator.h"
  28. #include "file.h"
  29. namespace crown
  30. {
  31. // All offsets are absolute
  32. struct PackageHeader
  33. {
  34. uint32_t num_textures;
  35. uint32_t textures_offset;
  36. uint32_t num_scripts;
  37. uint32_t scripts_offset;
  38. uint32_t num_sounds;
  39. uint32_t sounds_offset;
  40. uint32_t num_meshes;
  41. uint32_t meshes_offset;
  42. uint32_t num_units;
  43. uint32_t units_offset;
  44. uint32_t num_sprites;
  45. uint32_t sprites_offset;
  46. uint32_t num_physics;
  47. uint32_t physics_offset;
  48. uint32_t num_materials;
  49. uint32_t materials_offset;
  50. uint32_t num_fonts;
  51. uint32_t fonts_offset;
  52. uint32_t num_levels;
  53. uint32_t levels_offset;
  54. uint32_t num_physics_configs;
  55. uint32_t physics_configs_offset;
  56. uint32_t num_shaders;
  57. uint32_t shaders_offset;
  58. uint32_t num_sprite_animations;
  59. uint32_t sprite_animations_offset;
  60. };
  61. struct PackageResource
  62. {
  63. //-----------------------------------------------------------------------------
  64. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  65. {
  66. File* file = bundle.open(id);
  67. const size_t file_size = file->size();
  68. void* res = allocator.allocate(file_size);
  69. file->read(res, file_size);
  70. bundle.close(file);
  71. return res;
  72. }
  73. //-----------------------------------------------------------------------------
  74. static void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  75. {
  76. }
  77. static void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  78. {
  79. }
  80. //-----------------------------------------------------------------------------
  81. static void unload(Allocator& allocator, void* resource)
  82. {
  83. allocator.deallocate(resource);
  84. }
  85. //-----------------------------------------------------------------------------
  86. uint32_t num_textures() const
  87. {
  88. return ((PackageHeader*) this)->num_textures;
  89. }
  90. //-----------------------------------------------------------------------------
  91. uint32_t num_scripts() const
  92. {
  93. return ((PackageHeader*) this)->num_scripts;
  94. }
  95. //-----------------------------------------------------------------------------
  96. uint32_t num_sounds() const
  97. {
  98. return ((PackageHeader*) this)->num_sounds;
  99. }
  100. //-----------------------------------------------------------------------------
  101. uint32_t num_meshes() const
  102. {
  103. return ((PackageHeader*) this)->num_meshes;
  104. }
  105. //-----------------------------------------------------------------------------
  106. uint32_t num_units() const
  107. {
  108. return ((PackageHeader*) this)->num_units;
  109. }
  110. //-----------------------------------------------------------------------------
  111. uint32_t num_sprites() const
  112. {
  113. return ((PackageHeader*) this)->num_sprites;
  114. }
  115. //-----------------------------------------------------------------------------
  116. uint32_t num_physics() const
  117. {
  118. return ((PackageHeader*) this)->num_physics;
  119. }
  120. //-----------------------------------------------------------------------------
  121. uint32_t num_materials() const
  122. {
  123. return ((PackageHeader*) this)->num_materials;
  124. }
  125. //-----------------------------------------------------------------------------
  126. uint32_t num_fonts() const
  127. {
  128. return ((PackageHeader*) this)->num_fonts;
  129. }
  130. //-----------------------------------------------------------------------------
  131. uint32_t num_levels() const
  132. {
  133. return ((PackageHeader*) this)->num_levels;
  134. }
  135. //-----------------------------------------------------------------------------
  136. uint32_t num_physics_configs() const
  137. {
  138. return ((PackageHeader*) this)->num_physics_configs;
  139. }
  140. //-----------------------------------------------------------------------------
  141. uint32_t num_shaders() const
  142. {
  143. return ((PackageHeader*) this)->num_shaders;
  144. }
  145. //-----------------------------------------------------------------------------
  146. uint32_t num_sprite_animations() const
  147. {
  148. return ((PackageHeader*) this)->num_sprite_animations;
  149. }
  150. //-----------------------------------------------------------------------------
  151. ResourceId get_texture_id(uint32_t i) const
  152. {
  153. CE_ASSERT(i < num_textures(), "Index out of bounds");
  154. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->textures_offset);
  155. return begin[i];
  156. }
  157. //-----------------------------------------------------------------------------
  158. ResourceId get_script_id(uint32_t i) const
  159. {
  160. CE_ASSERT(i < num_scripts(), "Index out of bounds");
  161. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->scripts_offset);
  162. return begin[i];
  163. }
  164. //-----------------------------------------------------------------------------
  165. ResourceId get_sound_id(uint32_t i) const
  166. {
  167. CE_ASSERT(i < num_sounds(), "Index out of bounds");
  168. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->sounds_offset);
  169. return begin[i];
  170. }
  171. //-----------------------------------------------------------------------------
  172. ResourceId get_mesh_id(uint32_t i) const
  173. {
  174. CE_ASSERT(i < num_meshes(), "Index out of bounds");
  175. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->meshes_offset);
  176. return begin[i];
  177. }
  178. //-----------------------------------------------------------------------------
  179. ResourceId get_unit_id(uint32_t i) const
  180. {
  181. CE_ASSERT(i < num_units(), "Index out of bounds");
  182. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->units_offset);
  183. return begin[i];
  184. }
  185. //-----------------------------------------------------------------------------
  186. ResourceId get_sprite_id(uint32_t i) const
  187. {
  188. CE_ASSERT(i < num_sprites(), "Index out of bounds");
  189. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->sprites_offset);
  190. return begin[i];
  191. }
  192. //-----------------------------------------------------------------------------
  193. ResourceId get_physics_id(uint32_t i) const
  194. {
  195. CE_ASSERT(i < num_physics(), "Index out of bounds");
  196. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->physics_offset);
  197. return begin[i];
  198. }
  199. //-----------------------------------------------------------------------------
  200. ResourceId get_material_id(uint32_t i) const
  201. {
  202. CE_ASSERT(i < num_materials(), "Index out of bounds");
  203. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->materials_offset);
  204. return begin[i];
  205. }
  206. //-----------------------------------------------------------------------------
  207. ResourceId get_font_id(uint32_t i) const
  208. {
  209. CE_ASSERT(i < num_fonts(), "Index out of bounds");
  210. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->fonts_offset);
  211. return begin[i];
  212. }
  213. //-----------------------------------------------------------------------------
  214. ResourceId get_level_id(uint32_t i) const
  215. {
  216. CE_ASSERT(i < num_levels(), "Index out of bounds");
  217. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->levels_offset);
  218. return begin[i];
  219. }
  220. //-----------------------------------------------------------------------------
  221. ResourceId get_physics_config_id(uint32_t i) const
  222. {
  223. CE_ASSERT(i < num_physics_configs(), "Index out of bounds");
  224. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->physics_configs_offset);
  225. return begin[i];
  226. }
  227. //-----------------------------------------------------------------------------
  228. ResourceId get_shader_id(uint32_t i) const
  229. {
  230. CE_ASSERT(i < num_shaders(), "Index out of bounds");
  231. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->shaders_offset);
  232. return begin[i];
  233. }
  234. //-----------------------------------------------------------------------------
  235. ResourceId get_sprite_animation_id(uint32_t i) const
  236. {
  237. CE_ASSERT(i < num_sprite_animations(), "Index out of bounds");
  238. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->sprite_animations_offset);
  239. return begin[i];
  240. }
  241. private:
  242. // Disable construction
  243. PackageResource();
  244. };
  245. } // namespace crown