bundle_compiler.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "bundle_compiler.h"
  7. #include "vector.h"
  8. #include "dynamic_string.h"
  9. #include "allocator.h"
  10. #include "os.h"
  11. #include "log.h"
  12. #include "path.h"
  13. #include "disk_filesystem.h"
  14. #include "compile_options.h"
  15. #include "resource_registry.h"
  16. #include "resource_id.h"
  17. #include <inttypes.h>
  18. namespace crown
  19. {
  20. BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
  21. : _source_fs(source_dir)
  22. , _bundle_fs(bundle_dir)
  23. {
  24. DiskFilesystem temp;
  25. temp.create_directory(bundle_dir);
  26. }
  27. bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum platform)
  28. {
  29. const ResourceId id(type, name);
  30. char out_name[512];
  31. snprintf(out_name, 512, "%.16"PRIx64"-%.16"PRIx64, id.type, id.name);
  32. char path[512];
  33. snprintf(path, 512, "%s.%s", name, type);
  34. CE_LOGI("%s <= %s.%s", out_name, name, type);
  35. File* outf = _bundle_fs.open(out_name, FOM_WRITE);
  36. CompileOptions opts(_source_fs, outf, platform);
  37. resource_on_compile(id.type, path, opts);
  38. _bundle_fs.close(outf);
  39. }
  40. bool BundleCompiler::compile_all(Platform::Enum platform)
  41. {
  42. Vector<DynamicString> files(default_allocator());
  43. BundleCompiler::scan("", files);
  44. if (!_source_fs.is_file("crown.config"))
  45. {
  46. CE_LOGD("'crown.config' does not exist.");
  47. return false;
  48. }
  49. File* src = _source_fs.open("crown.config", FOM_READ);
  50. File* dst = _bundle_fs.open("crown.config", FOM_WRITE);
  51. src->copy_to(*dst, src->size());
  52. _source_fs.close(src);
  53. _bundle_fs.close(dst);
  54. // Compile all resources
  55. for (uint32_t i = 0; i < vector::size(files); i++)
  56. {
  57. if (files[i].ends_with(".tga")
  58. || files[i].ends_with(".dds")
  59. || files[i].ends_with(".sh")
  60. || files[i].ends_with(".sc")
  61. || files[i].starts_with(".")
  62. || files[i].ends_with(".config")
  63. || files[i].ends_with(".tmp")
  64. || files[i].ends_with(".wav"))
  65. continue;
  66. const char* filename = files[i].c_str();
  67. char type[256];
  68. char name[256];
  69. path::extension(filename, type, 256);
  70. path::filename_without_extension(filename, name, 256);
  71. compile(type, name, platform);
  72. }
  73. return true;
  74. }
  75. void BundleCompiler::scan(const char* cur_dir, Vector<DynamicString>& files)
  76. {
  77. Vector<DynamicString> my_files(default_allocator());
  78. _source_fs.list_files(cur_dir, my_files);
  79. for (uint32_t i = 0; i < vector::size(my_files); i++)
  80. {
  81. DynamicString file_i(default_allocator());
  82. if (strcmp(cur_dir, "") != 0)
  83. {
  84. file_i += cur_dir;
  85. file_i += '/';
  86. }
  87. file_i += my_files[i];
  88. if (_source_fs.is_directory(file_i.c_str()))
  89. {
  90. BundleCompiler::scan(file_i.c_str(), files);
  91. }
  92. else // Assume a regular file
  93. {
  94. vector::push_back(files, file_i);
  95. }
  96. }
  97. }
  98. namespace bundle_compiler
  99. {
  100. bool main(bool do_compile, bool do_continue, Platform::Enum platform)
  101. {
  102. if (do_compile)
  103. {
  104. bool ok = bundle_compiler_globals::compiler()->compile_all(platform);
  105. if (!ok || !do_continue)
  106. {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. } // namespace bundle_compiler
  113. namespace bundle_compiler_globals
  114. {
  115. char _buffer[sizeof(BundleCompiler)];
  116. BundleCompiler* _compiler = NULL;
  117. void init(const char* source_dir, const char* bundle_dir)
  118. {
  119. #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
  120. _compiler = new (_buffer) BundleCompiler(source_dir, bundle_dir);
  121. #endif
  122. }
  123. void shutdown()
  124. {
  125. _compiler->~BundleCompiler();
  126. _compiler = NULL;
  127. }
  128. BundleCompiler* compiler()
  129. {
  130. return _compiler;
  131. }
  132. } // namespace bundle_compiler_globals
  133. } // namespace crown