ArchiveBundle.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "Bundle.h"
  24. #include "Filesystem.h"
  25. #include "HeapAllocator.h"
  26. #include "Log.h"
  27. #include "Memory.h"
  28. #include "Resource.h"
  29. #include "Types.h"
  30. namespace crown
  31. {
  32. /// Structure of the archive
  33. ///
  34. /// [ArchiveHeader]
  35. /// [ArchiveEntry]
  36. /// [ArchiveEntry]
  37. /// ...
  38. /// [ArchiveEntry]
  39. /// [ResourceData]
  40. /// [ResourceData]
  41. /// ...
  42. /// [ResourceData]
  43. ///
  44. /// A valid archive must always have at least the archive header,
  45. /// starting at byte 0 of the archive file.
  46. ///
  47. /// Newer archive versions must be totally backward compatible
  48. /// across minor engine releases, in order to be able to use
  49. /// recent version of the engine with older game archives.
  50. /// Source of resources
  51. class ArchiveBundle : public Bundle
  52. {
  53. public:
  54. //-----------------------------------------------------------------------------
  55. ArchiveBundle(Filesystem& fs) :
  56. m_filesystem(fs), m_archive_file(NULL), m_entries_count(0), m_entries(NULL)
  57. {
  58. // FIXME Default archive name
  59. m_archive_file = m_filesystem.open( "archive.bin", FOM_READ);
  60. ArchiveHeader header;
  61. // Read the header of the archive
  62. m_archive_file->read(&header, sizeof(ArchiveHeader));
  63. Log::d("Version: %d", header.version);
  64. Log::d("Entries: %d", header.entries_count);
  65. Log::d("Checksum: %d", header.checksum);
  66. // No need to initialize memory
  67. m_entries = (ArchiveEntry*)m_allocator.allocate(header.entries_count * sizeof(ArchiveEntry));
  68. m_entries_count = header.entries_count;
  69. // Read the entries
  70. m_archive_file->read(m_entries, m_entries_count * sizeof(ArchiveEntry));
  71. }
  72. //-----------------------------------------------------------------------------
  73. ~ArchiveBundle()
  74. {
  75. if (m_archive_file != NULL)
  76. {
  77. m_filesystem.close(m_archive_file);
  78. }
  79. if (m_entries != NULL)
  80. {
  81. m_allocator.deallocate(m_entries);
  82. }
  83. m_entries = NULL;
  84. m_entries_count = 0;
  85. }
  86. //-----------------------------------------------------------------------------
  87. File* open(ResourceId name)
  88. {
  89. // Search the resource in the archive
  90. for (uint32_t i = 0; i < m_entries_count; i++)
  91. {
  92. if (m_entries[i].name == name.name && m_entries[i].type == name.type)
  93. {
  94. // If found, seek to the first byte of the resource data
  95. m_archive_file->seek(m_entries[i].offset);
  96. return m_archive_file;
  97. }
  98. }
  99. return NULL;
  100. }
  101. //-----------------------------------------------------------------------------
  102. void close(File* resource)
  103. {
  104. // Does nothing, the stream is automatically closed at exit.
  105. (void)resource;
  106. }
  107. private:
  108. HeapAllocator m_allocator;
  109. Filesystem& m_filesystem;
  110. File* m_archive_file;
  111. uint32_t m_entries_count;
  112. ArchiveEntry* m_entries;
  113. };
  114. //-----------------------------------------------------------------------------
  115. Bundle* create(Allocator& a, Filesystem& fs)
  116. {
  117. return CE_NEW(a, ArchiveBundle)(fs);
  118. }
  119. //-----------------------------------------------------------------------------
  120. void destroy(Allocator& a, Bundle* bundle)
  121. {
  122. CE_DELETE(a, bundle);
  123. }
  124. } // namespace crown