2
0

FileBundle.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <stdio.h>
  24. #include <inttypes.h>
  25. #include "Allocator.h"
  26. #include "Bundle.h"
  27. #include "Filesystem.h"
  28. #include "Resource.h"
  29. #include "StringUtils.h"
  30. #include "Types.h"
  31. #include "OS.h"
  32. namespace crown
  33. {
  34. const uint32_t RESOURCE_MAGIC_NUMBER = 0xCE010101;
  35. const uint32_t RESOURCE_VERSION = 2;
  36. /// Contains the header data common to all
  37. /// types of resources passing through the
  38. /// standard Compiler mechanics.
  39. struct ResourceHeader
  40. {
  41. uint32_t magic; // Magic number used to identify the file
  42. uint32_t version; // Version of the compiler used to compile the resource
  43. uint32_t size; // Size of the resource data _not_ including this header in bytes
  44. };
  45. class FileBundle : public Bundle
  46. {
  47. public:
  48. //-----------------------------------------------------------------------------
  49. FileBundle(Filesystem& fs) : m_filesystem(fs) {}
  50. //-----------------------------------------------------------------------------
  51. File* open(ResourceId name)
  52. {
  53. // Convert name/type into strings
  54. char resource_name[512];
  55. snprintf(resource_name, 512, "%"PRIx64"", name.id);
  56. // Search the resource in the filesystem
  57. // bool exists = m_filesystem.exists(resource_name);
  58. // CE_ASSERT(exists == true, "Resource does not exist: %s", resource_name);
  59. // Open the resource and check magic number/version
  60. File* file = m_filesystem.open(resource_name, FOM_READ);
  61. CE_ASSERT(file != NULL, "Resource %s does not exist", resource_name);
  62. ResourceHeader header;
  63. file->read(&header, sizeof(ResourceHeader));
  64. CE_ASSERT(header.magic == RESOURCE_MAGIC_NUMBER, "Resource is not valid: %s", resource_name);
  65. CE_ASSERT(header.version == RESOURCE_VERSION, "Resource version mismatch: %s", resource_name);
  66. return file;
  67. }
  68. //-----------------------------------------------------------------------------
  69. void close(File* resource)
  70. {
  71. m_filesystem.close(resource);
  72. }
  73. private:
  74. Filesystem& m_filesystem;
  75. };
  76. //-----------------------------------------------------------------------------
  77. Bundle* Bundle::create(Allocator& a, Filesystem& fs)
  78. {
  79. return CE_NEW(a, FileBundle)(fs);
  80. }
  81. //-----------------------------------------------------------------------------
  82. void Bundle::destroy(Allocator& a, Bundle* bundle)
  83. {
  84. CE_DELETE(a, bundle);
  85. }
  86. } // namespace crown