data_compiler.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/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 "device/console_server.h"
  10. #include "resource/types.h"
  11. #include <setjmp.h>
  12. namespace crown
  13. {
  14. /// Compiles source data into binary.
  15. ///
  16. /// @ingroup Resource
  17. struct DataCompiler
  18. {
  19. typedef void (*CompileFunction)(CompileOptions& opts);
  20. struct ResourceTypeData
  21. {
  22. u32 version;
  23. CompileFunction compiler;
  24. };
  25. ConsoleServer* _console_server;
  26. FilesystemDisk _source_fs;
  27. Map<DynamicString, DynamicString> _source_dirs;
  28. HashMap<StringId64, ResourceTypeData> _compilers;
  29. Vector<DynamicString> _files;
  30. Vector<DynamicString> _globs;
  31. Map<DynamicString, DynamicString> _data_index;
  32. FileMonitor _file_monitor;
  33. jmp_buf _jmpbuf;
  34. void add_file(const char* path);
  35. void add_tree(const char* path);
  36. void remove_file(const char* path);
  37. void remove_tree(const char* path);
  38. void scan_source_dir(const char* prefix, const char* path);
  39. void filemonitor_callback(FileMonitorEvent::Enum fme, bool is_dir, const char* path, const char* path_renamed);
  40. static void filemonitor_callback(void* thiz, FileMonitorEvent::Enum fme, bool is_dir, const char* path_original, const char* path_modified);
  41. ///
  42. DataCompiler(ConsoleServer& cs);
  43. ///
  44. ~DataCompiler();
  45. ///
  46. void map_source_dir(const char* name, const char* source_dir);
  47. ///
  48. void source_dir(const char* resource_name, DynamicString& source_dir);
  49. /// Adds a @a glob pattern to ignore when scanning the source directory.
  50. void add_ignore_glob(const char* glob);
  51. /// Scans source directory for resources.
  52. void scan();
  53. /// Compiles all the resources found in the source directory and puts them in @a data_dir.
  54. /// Returns true on success, false otherwise.
  55. bool compile(const char* data_dir, const char* platform);
  56. /// Registers the resource @a compiler for the given resource @a type and @a version.
  57. void register_compiler(StringId64 type, u32 version, CompileFunction compiler);
  58. /// Returns whether there is a compiler for the resource @a type.
  59. bool can_compile(StringId64 type);
  60. /// Returns the version of the compiler for @a type or COMPILER_NOT_FOUND if no compiler
  61. /// is found.
  62. u32 version(StringId64 type);
  63. ///
  64. void error(const char* msg, va_list args);
  65. static const u32 COMPILER_NOT_FOUND = UINT32_MAX;
  66. };
  67. int main_data_compiler(int argc, char** argv);
  68. } // namespace crown