data_compiler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2012-2021 Daniele Bartolini et al.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "core/containers/types.h"
  7. #include "core/filesystem/file_monitor.h"
  8. #include "core/filesystem/filesystem_disk.h"
  9. #include "core/guid.h"
  10. #include "device/console_server.h"
  11. #include "device/device_options.h"
  12. #include "resource/resource_id.h"
  13. #include "resource/types.h"
  14. namespace crown
  15. {
  16. /// Source data index.
  17. /// Associates a path on disk with its metadata.
  18. ///
  19. /// @ingroup Resource
  20. struct SourceIndex
  21. {
  22. HashMap<DynamicString, Stat> _paths;
  23. ///
  24. SourceIndex();
  25. ///
  26. void scan_directory(FilesystemDisk& fs, const char* prefix, const char* directory);
  27. /// Scans all directories defined by @a source_dirs.
  28. /// @a source_dirs maps a relative directory name to its absolute parent
  29. /// directory.
  30. void scan(const HashMap<DynamicString, DynamicString>& source_dirs);
  31. };
  32. /// Compiles source data into binary.
  33. ///
  34. /// @ingroup Resource
  35. struct DataCompiler
  36. {
  37. typedef s32 (*CompileFunction)(CompileOptions& opts);
  38. struct ResourceTypeData
  39. {
  40. u32 version;
  41. CompileFunction compiler;
  42. };
  43. const DeviceOptions* _options;
  44. ConsoleServer* _console_server;
  45. FilesystemDisk _source_fs;
  46. HashMap<DynamicString, DynamicString> _source_dirs;
  47. HashMap<DynamicString, ResourceTypeData> _compilers;
  48. Vector<DynamicString> _globs;
  49. HashMap<StringId64, DynamicString> _data_index;
  50. HashMap<StringId64, u64> _data_mtimes;
  51. HashMap<StringId64, HashMap<DynamicString, u32> > _data_dependencies;
  52. HashMap<StringId64, HashMap<DynamicString, u32> > _data_requirements;
  53. HashMap<DynamicString, u32> _data_versions;
  54. FileMonitor _file_monitor;
  55. SourceIndex _source_index;
  56. HashMap<StringId64, u32> _data_revisions;
  57. HashMap<Guid, u32> _client_revisions;
  58. u32 _revision;
  59. void add_file(const char* path);
  60. void remove_file(const char* path);
  61. void add_tree(const char* path);
  62. void remove_tree(const char* path);
  63. void remove_file_or_tree(const char* path);
  64. void file_monitor_callback(FileMonitorEvent::Enum fme, bool is_dir, const char* path, const char* path_renamed);
  65. static void file_monitor_callback(void* thiz, FileMonitorEvent::Enum fme, bool is_dir, const char* path_original, const char* path_modified);
  66. ///
  67. DataCompiler(const DeviceOptions& opts, ConsoleServer& cs);
  68. ///
  69. ~DataCompiler();
  70. ///
  71. void map_source_dir(const char* name, const char* source_dir);
  72. ///
  73. void source_dir(const char* resource_name, DynamicString& source_dir);
  74. /// Adds a @a glob pattern to ignore when scanning the source directory.
  75. void add_ignore_glob(const char* glob);
  76. /// Scans the source directory for resources and restores the state of the compiler from
  77. /// the previous run (if any).
  78. void scan_and_restore(const char* data_dir);
  79. /// Saves to disk the state of the compiler.
  80. void save(const char* data_dir);
  81. /// Compiles all the resources found in the source directory and puts them in @a data_dir.
  82. /// Returns true on success, false otherwise.
  83. bool compile(const char* data_dir, const char* platform);
  84. /// Registers the resource @a compiler for the given resource @a type and @a version.
  85. void register_compiler(const char* type, u32 version, CompileFunction compiler);
  86. /// Returns whether there is a compiler for the resource @a type.
  87. bool can_compile(const char* type);
  88. /// Returns the version of the compiler for @a type or COMPILER_NOT_FOUND if no compiler
  89. /// is found.
  90. u32 data_version(const char* type);
  91. ///
  92. u32 data_version_stored(const char* type);
  93. /// Returns whether any dependency of @a path, including itself, has changed
  94. /// since last call to compile().
  95. bool dependency_changed(const DynamicString& path, ResourceId id, u64 mtime);
  96. /// Returns whether the data version for @a path or any of its dependencies
  97. /// has changed since last call to compile().
  98. bool version_changed(const DynamicString& path, ResourceId id);
  99. /// Returns whether the @a path should be ignored because
  100. /// it matches a pattern from the CROWN_DATAIGNORE file or
  101. /// by other means.
  102. bool path_matches_ignore_glob(const char* path);
  103. /// Returns whether the @a path is special, hence it must
  104. /// be treated differently by some code somewhere. Sigh.
  105. bool path_is_special(const char* path);
  106. ///
  107. void error(const char* msg, va_list args);
  108. static const u32 COMPILER_NOT_FOUND = UINT32_MAX;
  109. };
  110. int main_data_compiler(const DeviceOptions& opts);
  111. } // namespace crown