PackageResource.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. };
  47. struct PackageResource
  48. {
  49. //-----------------------------------------------------------------------------
  50. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  51. {
  52. File* file = bundle.open(id);
  53. const size_t file_size = file->size();
  54. void* res = allocator.allocate(file_size);
  55. file->read(res, file_size);
  56. bundle.close(file);
  57. return res;
  58. }
  59. //-----------------------------------------------------------------------------
  60. static void online(void* /*resource*/)
  61. {
  62. }
  63. //-----------------------------------------------------------------------------
  64. static void unload(Allocator& allocator, void* resource)
  65. {
  66. CE_ASSERT_NOT_NULL(resource);
  67. allocator.deallocate(resource);
  68. }
  69. //-----------------------------------------------------------------------------
  70. static void offline(void* /*resource*/)
  71. {
  72. }
  73. //-----------------------------------------------------------------------------
  74. uint32_t num_textures() const
  75. {
  76. return ((PackageHeader*) this)->num_textures;
  77. }
  78. //-----------------------------------------------------------------------------
  79. uint32_t num_scripts() const
  80. {
  81. return ((PackageHeader*) this)->num_scripts;
  82. }
  83. //-----------------------------------------------------------------------------
  84. uint32_t num_sounds() const
  85. {
  86. return ((PackageHeader*) this)->num_sounds;
  87. }
  88. //-----------------------------------------------------------------------------
  89. uint32_t num_meshes() const
  90. {
  91. return ((PackageHeader*) this)->num_meshes;
  92. }
  93. //-----------------------------------------------------------------------------
  94. uint32_t num_units() const
  95. {
  96. return ((PackageHeader*) this)->num_units;
  97. }
  98. //-----------------------------------------------------------------------------
  99. uint32_t num_sprites() const
  100. {
  101. return ((PackageHeader*) this)->num_sprites;
  102. }
  103. //-----------------------------------------------------------------------------
  104. ResourceId get_texture_id(uint32_t i) const
  105. {
  106. CE_ASSERT(i < num_textures(), "Index out of bounds");
  107. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->textures_offset);
  108. return begin[i];
  109. }
  110. //-----------------------------------------------------------------------------
  111. ResourceId get_script_id(uint32_t i) const
  112. {
  113. CE_ASSERT(i < num_scripts(), "Index out of bounds");
  114. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->scripts_offset);
  115. return begin[i];
  116. }
  117. //-----------------------------------------------------------------------------
  118. ResourceId get_sound_id(uint32_t i) const
  119. {
  120. CE_ASSERT(i < num_sounds(), "Index out of bounds");
  121. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->sounds_offset);
  122. return begin[i];
  123. }
  124. //-----------------------------------------------------------------------------
  125. ResourceId get_mesh_id(uint32_t i) const
  126. {
  127. CE_ASSERT(i < num_meshes(), "Index out of bounds");
  128. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->meshes_offset);
  129. return begin[i];
  130. }
  131. //-----------------------------------------------------------------------------
  132. ResourceId get_unit_id(uint32_t i) const
  133. {
  134. CE_ASSERT(i < num_units(), "Index out of bounds");
  135. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->units_offset);
  136. return begin[i];
  137. }
  138. //-----------------------------------------------------------------------------
  139. ResourceId get_sprite_id(uint32_t i) const
  140. {
  141. CE_ASSERT(i < num_sprites(), "Index out of bounds");
  142. ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->sprites_offset);
  143. return begin[i];
  144. }
  145. private:
  146. // Disable construction
  147. PackageResource();
  148. };
  149. } // namespace crown