Compiler.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "Filesystem.h"
  26. namespace crown
  27. {
  28. const size_t MAX_RESOURCE_NAME_LENGTH = 1024;
  29. const size_t MAX_RESOURCE_TYPE_LENGTH = 64;
  30. const size_t MAX_RESOURCE_PATH_LENGTH = 2048;
  31. const uint32_t COMPILED_HEADER_MAGIC_NUMBER = 0xCE010101;
  32. const uint32_t COMPILER_VERSION = 1;
  33. /// Contains the header data common to all
  34. /// types of resources passing through the
  35. /// standard Compiler mechanics.
  36. struct CompiledHeader
  37. {
  38. uint32_t magic; // Magic number used to identify the file
  39. uint32_t version; // Version of the compiler used to compile the resource
  40. uint32_t name; // Name of the resource (murmur2_32 hash)
  41. uint32_t type; // Type of the resource (murmur2_32 hash)
  42. uint32_t size; // Size of the resource data _not_ including header (in bytes)
  43. };
  44. class DiskFile;
  45. /// Resource compiler interface.
  46. /// Every specific resource compiler must inherith from this
  47. /// interface and implement its methods accordingly.
  48. class Compiler
  49. {
  50. public:
  51. Compiler(const char* root_path, const char* dest_path, uint32_t type_expected);
  52. virtual ~Compiler();
  53. size_t compile(const char* resource, uint32_t name, uint32_t type);
  54. size_t read_header(DiskFile* in_file);
  55. size_t read_resource(DiskFile* in_file);
  56. void write_header(DiskFile* out_file, uint32_t name, uint32_t type, uint32_t resource_size);
  57. void write_resource(DiskFile* out_file);
  58. void cleanup();
  59. const char* root_path() const;
  60. const char* dest_path() const;
  61. const char* resource_name() const;
  62. const char* resource_path() const;
  63. protected:
  64. virtual size_t read_header_impl(DiskFile* in_file) = 0;
  65. virtual size_t read_resource_impl(DiskFile* in_file) = 0;
  66. virtual void write_header_impl(DiskFile* out_file) = 0;
  67. virtual void write_resource_impl(DiskFile* out_file) = 0;
  68. virtual void cleanup_impl() = 0;
  69. private:
  70. // Filesystems
  71. Filesystem m_root_fs;
  72. Filesystem m_dest_fs;
  73. uint32_t m_type_expected;
  74. char m_resource_name[MAX_RESOURCE_NAME_LENGTH];
  75. char m_resource_path[MAX_RESOURCE_PATH_LENGTH];
  76. };
  77. } // namespace crown