bundle_compiler.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "allocator.h"
  6. #include "bundle_compiler.h"
  7. #include "compile_options.h"
  8. #include "config.h"
  9. #include "config_resource.h"
  10. #include "disk_filesystem.h"
  11. #include "dynamic_string.h"
  12. #include "font_resource.h"
  13. #include "level_resource.h"
  14. #include "log.h"
  15. #include "lua_resource.h"
  16. #include "material_resource.h"
  17. #include "mesh_resource.h"
  18. #include "os.h"
  19. #include "package_resource.h"
  20. #include "path.h"
  21. #include "physics_resource.h"
  22. #include "shader_resource.h"
  23. #include "sort_map.h"
  24. #include "sound_resource.h"
  25. #include "sprite_resource.h"
  26. #include "temp_allocator.h"
  27. #include "texture_resource.h"
  28. #include "unit_resource.h"
  29. #include "vector.h"
  30. #include <setjmp.h>
  31. namespace crown
  32. {
  33. BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
  34. : _source_fs(source_dir)
  35. , _bundle_fs(bundle_dir)
  36. , _compilers(default_allocator())
  37. , _files(default_allocator())
  38. {
  39. namespace pcr = physics_config_resource;
  40. namespace phr = physics_resource;
  41. namespace pkr = package_resource;
  42. namespace sdr = sound_resource;
  43. namespace mhr = mesh_resource;
  44. namespace utr = unit_resource;
  45. namespace txr = texture_resource;
  46. namespace mtr = material_resource;
  47. namespace lur = lua_resource;
  48. namespace ftr = font_resource;
  49. namespace lvr = level_resource;
  50. namespace spr = sprite_resource;
  51. namespace shr = shader_resource;
  52. namespace sar = sprite_animation_resource;
  53. namespace cor = config_resource;
  54. register_resource_compiler(SCRIPT_TYPE, SCRIPT_VERSION, lur::compile);
  55. register_resource_compiler(TEXTURE_TYPE, TEXTURE_VERSION, txr::compile);
  56. register_resource_compiler(MESH_TYPE, MESH_VERSION, mhr::compile);
  57. register_resource_compiler(SOUND_TYPE, SOUND_VERSION, sdr::compile);
  58. register_resource_compiler(UNIT_TYPE, UNIT_VERSION, utr::compile);
  59. register_resource_compiler(SPRITE_TYPE, SPRITE_VERSION, spr::compile);
  60. register_resource_compiler(PACKAGE_TYPE, PACKAGE_VERSION, pkr::compile);
  61. register_resource_compiler(PHYSICS_TYPE, PHYSICS_VERSION, phr::compile);
  62. register_resource_compiler(MATERIAL_TYPE, MATERIAL_VERSION, mtr::compile);
  63. register_resource_compiler(PHYSICS_CONFIG_TYPE, PHYSICS_CONFIG_VERSION, pcr::compile);
  64. register_resource_compiler(FONT_TYPE, FONT_VERSION, ftr::compile);
  65. register_resource_compiler(LEVEL_TYPE, LEVEL_VERSION, lvr::compile);
  66. register_resource_compiler(SHADER_TYPE, SHADER_VERSION, shr::compile);
  67. register_resource_compiler(SPRITE_ANIMATION_TYPE, SPRITE_ANIMATION_VERSION, sar::compile);
  68. register_resource_compiler(CONFIG_TYPE, CONFIG_VERSION, cor::compile);
  69. DiskFilesystem temp;
  70. temp.create_directory(bundle_dir);
  71. scan_source_dir("");
  72. if (!_bundle_fs.exists(CROWN_DATA_DIRECTORY))
  73. _bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
  74. }
  75. bool BundleCompiler::compile(const char* type, const char* name, const char* platform)
  76. {
  77. StringId64 _type(type);
  78. StringId64 _name(name);
  79. TempAllocator512 alloc;
  80. DynamicString path(alloc);
  81. TempAllocator512 alloc2;
  82. DynamicString src_path(alloc2);
  83. src_path += name;
  84. src_path += ".";
  85. src_path += type;
  86. char res_name[1 + 2*StringId64::STRING_LENGTH];
  87. _type.to_string(res_name);
  88. res_name[16] = '-';
  89. _name.to_string(res_name + 17);
  90. path::join(CROWN_DATA_DIRECTORY, res_name, path);
  91. CE_LOGI("%s <= %s.%s", res_name, name, type);
  92. bool success = true;
  93. jmp_buf buf;
  94. Buffer output(default_allocator());
  95. array::reserve(output, 4*1024*1024);
  96. if (!setjmp(buf))
  97. {
  98. CompileOptions opts(_source_fs, output, platform, &buf);
  99. compile(_type, src_path.c_str(), opts);
  100. File* outf = _bundle_fs.open(path.c_str(), FileOpenMode::WRITE);
  101. uint32_t size = array::size(output);
  102. uint32_t written = outf->write(array::begin(output), size);
  103. _bundle_fs.close(*outf);
  104. success = size == written;
  105. }
  106. else
  107. {
  108. success = false;
  109. }
  110. return success;
  111. }
  112. bool BundleCompiler::compile_all(const char* platform)
  113. {
  114. for (uint32_t i = 0; i < vector::size(_files); ++i)
  115. {
  116. if (_files[i].ends_with(".tga")
  117. || _files[i].ends_with(".dds")
  118. || _files[i].ends_with(".sh")
  119. || _files[i].ends_with(".sc")
  120. || _files[i].starts_with(".")
  121. || _files[i].ends_with(".tmp")
  122. || _files[i].ends_with(".wav"))
  123. continue;
  124. const char* filename = _files[i].c_str();
  125. const char* type = path::extension(filename);
  126. char name[256];
  127. const uint32_t size = uint32_t(type - filename - 1);
  128. strncpy(name, filename, size);
  129. name[size] = '\0';
  130. if (!compile(type, name, platform))
  131. return false;
  132. }
  133. return true;
  134. }
  135. void BundleCompiler::register_resource_compiler(StringId64 type, uint32_t version, CompileFunction compiler)
  136. {
  137. CE_ASSERT(!sort_map::has(_compilers, type), "Type already registered");
  138. CE_ASSERT_NOT_NULL(compiler);
  139. ResourceTypeData rtd;
  140. rtd.version = version;
  141. rtd.compiler = compiler;
  142. sort_map::set(_compilers, type, rtd);
  143. sort_map::sort(_compilers);
  144. }
  145. void BundleCompiler::compile(StringId64 type, const char* path, CompileOptions& opts)
  146. {
  147. CE_ASSERT(sort_map::has(_compilers, type), "Compiler not found");
  148. sort_map::get(_compilers, type, ResourceTypeData()).compiler(path, opts);
  149. }
  150. uint32_t BundleCompiler::version(StringId64 type)
  151. {
  152. CE_ASSERT(sort_map::has(_compilers, type), "Compiler not found");
  153. return sort_map::get(_compilers, type, ResourceTypeData()).version;
  154. }
  155. void BundleCompiler::scan_source_dir(const char* cur_dir)
  156. {
  157. Vector<DynamicString> my_files(default_allocator());
  158. _source_fs.list_files(cur_dir, my_files);
  159. for (uint32_t i = 0; i < vector::size(my_files); ++i)
  160. {
  161. TempAllocator512 ta;
  162. DynamicString file_i(ta);
  163. if (strcmp(cur_dir, "") != 0)
  164. {
  165. file_i += cur_dir;
  166. file_i += '/';
  167. }
  168. file_i += my_files[i];
  169. if (_source_fs.is_directory(file_i.c_str()))
  170. {
  171. BundleCompiler::scan_source_dir(file_i.c_str());
  172. }
  173. else // Assume a regular file
  174. {
  175. vector::push_back(_files, file_i);
  176. }
  177. }
  178. }
  179. namespace bundle_compiler
  180. {
  181. bool main(bool do_compile, bool do_continue, const char* platform)
  182. {
  183. bool can_proceed = true;
  184. if (do_compile)
  185. {
  186. bool success = bundle_compiler_globals::compiler()->compile_all(platform);
  187. can_proceed = !(!success || !do_continue);
  188. }
  189. return can_proceed;
  190. }
  191. } // namespace bundle_compiler
  192. namespace bundle_compiler_globals
  193. {
  194. char _buffer[sizeof(BundleCompiler)];
  195. BundleCompiler* _compiler = NULL;
  196. void init(const char* source_dir, const char* bundle_dir)
  197. {
  198. #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
  199. _compiler = new (_buffer) BundleCompiler(source_dir, bundle_dir);
  200. #endif
  201. }
  202. void shutdown()
  203. {
  204. _compiler->~BundleCompiler();
  205. _compiler = NULL;
  206. }
  207. BundleCompiler* compiler()
  208. {
  209. return _compiler;
  210. }
  211. } // namespace bundle_compiler_globals
  212. } // namespace crown