PackageResource.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_guis;
  51. uint32_t guis_offset;
  52. };
  53. struct PackageResource
  54. {
  55. //-----------------------------------------------------------------------------
  56. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  57. {
  58. File* file = bundle.open(id);
  59. const size_t file_size = file->size();
  60. void* res = allocator.allocate(file_size);
  61. file->read(res, file_size);
  62. bundle.close(file);
  63. return res;
  64. }
  65. //-----------------------------------------------------------------------------
  66. static void online(void* /*resource*/)
  67. {
  68. }
  69. //-----------------------------------------------------------------------------
  70. static void unload(Allocator& allocator, void* resource)
  71. {
  72. CE_ASSERT_NOT_NULL(resource);
  73. allocator.deallocate(resource);
  74. }
  75. //-----------------------------------------------------------------------------
  76. static void offline(void* /*resource*/)
  77. {
  78. }
  79. //-----------------------------------------------------------------------------
  80. uint32_t num_textures() const
  81. {
  82. return ((PackageHeader*) this)->num_textures;
  83. }
  84. //-----------------------------------------------------------------------------
  85. uint32_t num_scripts() const
  86. {
  87. return ((PackageHeader*) this)->num_scripts;
  88. }
  89. //-----------------------------------------------------------------------------
  90. uint32_t num_sounds() const
  91. {
  92. return ((PackageHeader*) this)->num_sounds;
  93. }
  94. //-----------------------------------------------------------------------------
  95. uint32_t num_meshes() const
  96. {
  97. return ((PackageHeader*) this)->num_meshes;
  98. }
  99. //-----------------------------------------------------------------------------
  100. uint32_t num_units() const
  101. {
  102. return ((PackageHeader*) this)->num_units;
  103. }
  104. //-----------------------------------------------------------------------------
  105. uint32_t num_sprites() const
  106. {
  107. return ((PackageHeader*) this)->num_sprites;
  108. }
  109. //-----------------------------------------------------------------------------
  110. uint32_t num_physics() const
  111. {
  112. return ((PackageHeader*) this)->num_physics;
  113. }
  114. //-----------------------------------------------------------------------------
  115. uint32_t num_materials() const
  116. {
  117. return ((PackageHeader*) this)->num_materials;
  118. }
  119. //-----------------------------------------------------------------------------
  120. uint32_t num_guis() const
  121. {
  122. return ((PackageHeader*) this)->num_guis;
  123. }
  124. //-----------------------------------------------------------------------------
  125. ResourceId get_texture_id(uint32_t i) const
  126. {
  127. CE_ASSERT(i < num_textures(), "Index out of bounds");
  128. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->textures_offset);
  129. return begin[i];
  130. }
  131. //-----------------------------------------------------------------------------
  132. ResourceId get_script_id(uint32_t i) const
  133. {
  134. CE_ASSERT(i < num_scripts(), "Index out of bounds");
  135. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->scripts_offset);
  136. return begin[i];
  137. }
  138. //-----------------------------------------------------------------------------
  139. ResourceId get_sound_id(uint32_t i) const
  140. {
  141. CE_ASSERT(i < num_sounds(), "Index out of bounds");
  142. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->sounds_offset);
  143. return begin[i];
  144. }
  145. //-----------------------------------------------------------------------------
  146. ResourceId get_mesh_id(uint32_t i) const
  147. {
  148. CE_ASSERT(i < num_meshes(), "Index out of bounds");
  149. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->meshes_offset);
  150. return begin[i];
  151. }
  152. //-----------------------------------------------------------------------------
  153. ResourceId get_unit_id(uint32_t i) const
  154. {
  155. CE_ASSERT(i < num_units(), "Index out of bounds");
  156. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->units_offset);
  157. return begin[i];
  158. }
  159. //-----------------------------------------------------------------------------
  160. ResourceId get_sprite_id(uint32_t i) const
  161. {
  162. CE_ASSERT(i < num_sprites(), "Index out of bounds");
  163. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->sprites_offset);
  164. return begin[i];
  165. }
  166. //-----------------------------------------------------------------------------
  167. ResourceId get_physics_id(uint32_t i) const
  168. {
  169. CE_ASSERT(i < num_physics(), "Index out of bounds");
  170. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->physics_offset);
  171. return begin[i];
  172. }
  173. //-----------------------------------------------------------------------------
  174. ResourceId get_material_id(uint32_t i) const
  175. {
  176. CE_ASSERT(i < num_materials(), "Index out of bounds");
  177. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->materials_offset);
  178. return begin[i];
  179. }
  180. //-----------------------------------------------------------------------------
  181. ResourceId get_gui_id(uint32_t i) const
  182. {
  183. CE_ASSERT(i < num_guis(), "Index out of bounds");
  184. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->guis_offset);
  185. return begin[i];
  186. }
  187. private:
  188. // Disable construction
  189. PackageResource();
  190. };
  191. } // namespace crown