Compiler.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /// Looks for the @resource int the @root_path and prepares it to
  51. /// compilation using @seed to generate hashes for the resource name.
  52. /// Implementation must declare the type of resource they are expecting
  53. /// to work on by setting @type_expected appropriately.
  54. Compiler(const char* root_path, const char* dest_path, const char* resource,
  55. uint32_t type_expected, uint32_t seed);
  56. virtual ~Compiler();
  57. /// Actually compiles the resource.
  58. virtual bool compile() = 0;
  59. virtual void write() = 0;
  60. const char* root_path() const;
  61. const char* dest_path() const;
  62. const char* resource_path() const;
  63. uint32_t resource_name_hash() const;
  64. uint32_t resource_type_hash() const;
  65. uint32_t seed() const;
  66. const char* resource_name() const;
  67. const char* resource_type() const;
  68. FileStream* source_file();
  69. FileStream* destination_file();
  70. protected:
  71. void prepare_header(uint32_t size);
  72. void write_header();
  73. private:
  74. // These memebers are private to prevent
  75. // derived classes from manipulating them.
  76. // Generic informations
  77. char m_root_path[MAX_RESOURCE_PATH_LENGTH];
  78. char m_dest_path[MAX_RESOURCE_PATH_LENGTH];
  79. char m_resource[MAX_RESOURCE_PATH_LENGTH];
  80. uint32_t m_name_hash;
  81. uint32_t m_type_hash;
  82. uint32_t m_seed;
  83. char m_name[MAX_RESOURCE_NAME_LENGTH];
  84. char m_type[MAX_RESOURCE_TYPE_LENGTH];
  85. // Filesystems
  86. Filesystem m_root_fs;
  87. Filesystem m_dest_fs;
  88. FileStream* m_src_file;
  89. FileStream* m_dest_file;
  90. // Compilation stage
  91. CompiledHeader m_compiled_header;
  92. bool m_prepared;
  93. // Global compiler settings
  94. bool m_verbose;
  95. };
  96. } // namespace crown