bundle_compiler.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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(default_allocator(), source_dir)
  35. , _bundle_fs(default_allocator(), 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(RESOURCE_TYPE_SCRIPT, RESOURCE_VERSION_SCRIPT, lur::compile);
  55. register_resource_compiler(RESOURCE_TYPE_TEXTURE, RESOURCE_VERSION_TEXTURE, txr::compile);
  56. register_resource_compiler(RESOURCE_TYPE_MESH, RESOURCE_VERSION_MESH, mhr::compile);
  57. register_resource_compiler(RESOURCE_TYPE_SOUND, RESOURCE_VERSION_SOUND, sdr::compile);
  58. register_resource_compiler(RESOURCE_TYPE_UNIT, RESOURCE_VERSION_UNIT, utr::compile);
  59. register_resource_compiler(RESOURCE_TYPE_SPRITE, RESOURCE_VERSION_SPRITE, spr::compile);
  60. register_resource_compiler(RESOURCE_TYPE_PACKAGE, RESOURCE_VERSION_PACKAGE, pkr::compile);
  61. register_resource_compiler(RESOURCE_TYPE_PHYSICS, RESOURCE_VERSION_PHYSICS, phr::compile);
  62. register_resource_compiler(RESOURCE_TYPE_MATERIAL, RESOURCE_VERSION_MATERIAL, mtr::compile);
  63. register_resource_compiler(RESOURCE_TYPE_PHYSICS_CONFIG, RESOURCE_VERSION_PHYSICS_CONFIG, pcr::compile);
  64. register_resource_compiler(RESOURCE_TYPE_FONT, RESOURCE_VERSION_FONT, ftr::compile);
  65. register_resource_compiler(RESOURCE_TYPE_LEVEL, RESOURCE_VERSION_LEVEL, lvr::compile);
  66. register_resource_compiler(RESOURCE_TYPE_SHADER, RESOURCE_VERSION_SHADER, shr::compile);
  67. register_resource_compiler(RESOURCE_TYPE_SPRITE_ANIMATION, RESOURCE_VERSION_SPRITE_ANIMATION, sar::compile);
  68. register_resource_compiler(RESOURCE_TYPE_CONFIG, RESOURCE_VERSION_CONFIG, cor::compile);
  69. _bundle_fs.create_directory(bundle_dir);
  70. scan_source_dir("");
  71. if (!_bundle_fs.exists(CROWN_DATA_DIRECTORY))
  72. _bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
  73. }
  74. bool BundleCompiler::compile(const char* type, const char* name, const char* platform)
  75. {
  76. StringId64 _type(type);
  77. StringId64 _name(name);
  78. TempAllocator512 alloc;
  79. DynamicString path(alloc);
  80. TempAllocator512 alloc2;
  81. DynamicString src_path(alloc2);
  82. src_path += name;
  83. src_path += ".";
  84. src_path += type;
  85. char res_name[1 + 2*StringId64::STRING_LENGTH];
  86. _type.to_string(res_name);
  87. res_name[16] = '-';
  88. _name.to_string(res_name + 17);
  89. path::join(CROWN_DATA_DIRECTORY, res_name, path);
  90. CE_LOGI("%s <= %s.%s", res_name, name, type);
  91. bool success = true;
  92. jmp_buf buf;
  93. Buffer output(default_allocator());
  94. array::reserve(output, 4*1024*1024);
  95. if (!setjmp(buf))
  96. {
  97. CompileOptions opts(_source_fs, output, platform, &buf);
  98. compile(_type, src_path.c_str(), opts);
  99. File* outf = _bundle_fs.open(path.c_str(), FileOpenMode::WRITE);
  100. u32 size = array::size(output);
  101. u32 written = outf->write(array::begin(output), size);
  102. _bundle_fs.close(*outf);
  103. success = size == written;
  104. }
  105. else
  106. {
  107. success = false;
  108. }
  109. return success;
  110. }
  111. bool BundleCompiler::compile_all(const char* platform)
  112. {
  113. for (u32 i = 0; i < vector::size(_files); ++i)
  114. {
  115. if (_files[i].ends_with(".sh")
  116. || _files[i].ends_with(".sc")
  117. || _files[i].ends_with(".tmp")
  118. || _files[i].ends_with(".temp")
  119. || _files[i].ends_with(".wav")
  120. || _files[i].ends_with(".ogg")
  121. || _files[i].ends_with(".png")
  122. || _files[i].ends_with(".tga")
  123. || _files[i].ends_with(".dds")
  124. || _files[i].ends_with(".ktx")
  125. || _files[i].ends_with(".pvr")
  126. || _files[i].ends_with(".swp") // VIM swap file.
  127. || _files[i].ends_with("~")
  128. || _files[i].starts_with(".")
  129. )
  130. continue;
  131. const char* filename = _files[i].c_str();
  132. const char* type = path::extension(filename);
  133. // Ignore files without extension
  134. if (type == NULL)
  135. continue;
  136. char name[256];
  137. const u32 size = u32(type - filename - 1);
  138. strncpy(name, filename, size);
  139. name[size] = '\0';
  140. if (!compile(type, name, platform))
  141. return false;
  142. }
  143. return true;
  144. }
  145. void BundleCompiler::register_resource_compiler(StringId64 type, u32 version, CompileFunction compiler)
  146. {
  147. CE_ASSERT(!sort_map::has(_compilers, type), "Type already registered");
  148. CE_ASSERT_NOT_NULL(compiler);
  149. ResourceTypeData rtd;
  150. rtd.version = version;
  151. rtd.compiler = compiler;
  152. sort_map::set(_compilers, type, rtd);
  153. sort_map::sort(_compilers);
  154. }
  155. void BundleCompiler::compile(StringId64 type, const char* path, CompileOptions& opts)
  156. {
  157. CE_ASSERT(sort_map::has(_compilers, type), "Compiler not found");
  158. sort_map::get(_compilers, type, ResourceTypeData()).compiler(path, opts);
  159. }
  160. u32 BundleCompiler::version(StringId64 type)
  161. {
  162. CE_ASSERT(sort_map::has(_compilers, type), "Compiler not found");
  163. return sort_map::get(_compilers, type, ResourceTypeData()).version;
  164. }
  165. void BundleCompiler::scan_source_dir(const char* cur_dir)
  166. {
  167. Vector<DynamicString> my_files(default_allocator());
  168. _source_fs.list_files(cur_dir, my_files);
  169. for (u32 i = 0; i < vector::size(my_files); ++i)
  170. {
  171. TempAllocator512 ta;
  172. DynamicString file_i(ta);
  173. if (strcmp(cur_dir, "") != 0)
  174. {
  175. file_i += cur_dir;
  176. file_i += '/';
  177. }
  178. file_i += my_files[i];
  179. if (_source_fs.is_directory(file_i.c_str()))
  180. {
  181. BundleCompiler::scan_source_dir(file_i.c_str());
  182. }
  183. else // Assume a regular file
  184. {
  185. vector::push_back(_files, file_i);
  186. }
  187. }
  188. }
  189. namespace bundle_compiler
  190. {
  191. bool main(bool do_compile, bool do_continue, const char* platform)
  192. {
  193. bool can_proceed = true;
  194. if (do_compile)
  195. {
  196. bool success = bundle_compiler_globals::compiler()->compile_all(platform);
  197. can_proceed = !(!success || !do_continue);
  198. }
  199. return can_proceed;
  200. }
  201. } // namespace bundle_compiler
  202. namespace bundle_compiler_globals
  203. {
  204. char _buffer[sizeof(BundleCompiler)];
  205. BundleCompiler* _compiler = NULL;
  206. void init(const char* source_dir, const char* bundle_dir)
  207. {
  208. #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
  209. _compiler = new (_buffer) BundleCompiler(source_dir, bundle_dir);
  210. #endif
  211. }
  212. void shutdown()
  213. {
  214. _compiler->~BundleCompiler();
  215. _compiler = NULL;
  216. }
  217. BundleCompiler* compiler()
  218. {
  219. return _compiler;
  220. }
  221. } // namespace bundle_compiler_globals
  222. } // namespace crown