Compiler.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Types.h"
  24. #include "Filesystem.h"
  25. namespace crown
  26. {
  27. const size_t MAX_RESOURCE_NAME_LENGTH = 1024;
  28. const size_t MAX_RESOURCE_TYPE_LENGTH = 64;
  29. const size_t MAX_RESOURCE_PATH_LENGTH = 1024;
  30. const uint32_t COMPILED_HEADER_MAGIC_NUMBER = 0xCE010101;
  31. const uint32_t COMPILER_VERSION = 1;
  32. /// Contains the header data common to all
  33. /// types of resources passing through the
  34. /// standard Compiler mechanics.
  35. struct CompiledHeader
  36. {
  37. uint32_t magic; // Magic number used to identify the file
  38. uint32_t version; // Version of the compiler used to compile the resource
  39. uint32_t name; // Name of the resource (murmur2_32 hash)
  40. uint32_t type; // Type of the resource (murmur2_32 hash)
  41. uint32_t size; // Size of the resource data _not_ including header (in bytes)
  42. };
  43. class FileStream;
  44. /// Resource compiler interface.
  45. /// Every specific resource compiler must inherith from this
  46. /// interface and implement its methods accordingly.
  47. class Compiler
  48. {
  49. public:
  50. Compiler(const char* root_path, const char* dest_path, uint32_t type_expected);
  51. virtual ~Compiler();
  52. size_t compile(const char* resource, uint32_t name, uint32_t type);
  53. size_t read_header(FileStream* in_file);
  54. size_t read_resource(FileStream* in_file);
  55. void write_header(FileStream* out_file, uint32_t name, uint32_t type, uint32_t resource_size);
  56. void write_resource(FileStream* out_file);
  57. void cleanup();
  58. const char* root_path() const;
  59. const char* dest_path() const;
  60. const char* resource_name() const;
  61. protected:
  62. virtual size_t read_header_impl(FileStream* in_file) = 0;
  63. virtual size_t read_resource_impl(FileStream* in_file) = 0;
  64. virtual void write_header_impl(FileStream* out_file) = 0;
  65. virtual void write_resource_impl(FileStream* out_file) = 0;
  66. virtual void cleanup_impl() = 0;
  67. private:
  68. // Filesystems
  69. Filesystem m_root_fs;
  70. Filesystem m_dest_fs;
  71. uint32_t m_type_expected;
  72. char m_resource_name[MAX_RESOURCE_NAME_LENGTH];
  73. };
  74. } // namespace crown