bundle_compiler.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "file.h"
  13. #include "font_resource.h"
  14. #include "level_resource.h"
  15. #include "log.h"
  16. #include "lua_resource.h"
  17. #include "material_resource.h"
  18. #include "mesh_resource.h"
  19. #include "os.h"
  20. #include "package_resource.h"
  21. #include "path.h"
  22. #include "physics_resource.h"
  23. #include "shader_resource.h"
  24. #include "sort_map.h"
  25. #include "sound_resource.h"
  26. #include "sprite_resource.h"
  27. #include "temp_allocator.h"
  28. #include "texture_resource.h"
  29. #include "unit_resource.h"
  30. #include "vector.h"
  31. #include <setjmp.h>
  32. namespace crown
  33. {
  34. class LineReader
  35. {
  36. const char* _str;
  37. const u32 _len;
  38. u32 _pos;
  39. public:
  40. LineReader(const char* str)
  41. : _str(str)
  42. , _len(strlen32(str))
  43. , _pos(0)
  44. {
  45. }
  46. void read_line(DynamicString& line)
  47. {
  48. const char* s = &_str[_pos];
  49. const char* nl = strnl(s);
  50. _pos += u32(nl - s);
  51. line.set(s, nl - s);
  52. }
  53. bool eof()
  54. {
  55. return _str[_pos] == '\0';
  56. }
  57. };
  58. BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
  59. : _source_fs(default_allocator(), source_dir)
  60. , _bundle_fs(default_allocator(), bundle_dir)
  61. , _compilers(default_allocator())
  62. , _files(default_allocator())
  63. , _globs(default_allocator())
  64. {
  65. namespace pcr = physics_config_resource;
  66. namespace phr = physics_resource;
  67. namespace pkr = package_resource;
  68. namespace sdr = sound_resource;
  69. namespace mhr = mesh_resource;
  70. namespace utr = unit_resource;
  71. namespace txr = texture_resource;
  72. namespace mtr = material_resource;
  73. namespace lur = lua_resource;
  74. namespace ftr = font_resource;
  75. namespace lvr = level_resource;
  76. namespace spr = sprite_resource;
  77. namespace shr = shader_resource;
  78. namespace sar = sprite_animation_resource;
  79. namespace cor = config_resource;
  80. register_resource_compiler(RESOURCE_TYPE_SCRIPT, RESOURCE_VERSION_SCRIPT, lur::compile);
  81. register_resource_compiler(RESOURCE_TYPE_TEXTURE, RESOURCE_VERSION_TEXTURE, txr::compile);
  82. register_resource_compiler(RESOURCE_TYPE_MESH, RESOURCE_VERSION_MESH, mhr::compile);
  83. register_resource_compiler(RESOURCE_TYPE_SOUND, RESOURCE_VERSION_SOUND, sdr::compile);
  84. register_resource_compiler(RESOURCE_TYPE_UNIT, RESOURCE_VERSION_UNIT, utr::compile);
  85. register_resource_compiler(RESOURCE_TYPE_SPRITE, RESOURCE_VERSION_SPRITE, spr::compile);
  86. register_resource_compiler(RESOURCE_TYPE_PACKAGE, RESOURCE_VERSION_PACKAGE, pkr::compile);
  87. register_resource_compiler(RESOURCE_TYPE_PHYSICS, RESOURCE_VERSION_PHYSICS, phr::compile);
  88. register_resource_compiler(RESOURCE_TYPE_MATERIAL, RESOURCE_VERSION_MATERIAL, mtr::compile);
  89. register_resource_compiler(RESOURCE_TYPE_PHYSICS_CONFIG, RESOURCE_VERSION_PHYSICS_CONFIG, pcr::compile);
  90. register_resource_compiler(RESOURCE_TYPE_FONT, RESOURCE_VERSION_FONT, ftr::compile);
  91. register_resource_compiler(RESOURCE_TYPE_LEVEL, RESOURCE_VERSION_LEVEL, lvr::compile);
  92. register_resource_compiler(RESOURCE_TYPE_SHADER, RESOURCE_VERSION_SHADER, shr::compile);
  93. register_resource_compiler(RESOURCE_TYPE_SPRITE_ANIMATION, RESOURCE_VERSION_SPRITE_ANIMATION, sar::compile);
  94. register_resource_compiler(RESOURCE_TYPE_CONFIG, RESOURCE_VERSION_CONFIG, cor::compile);
  95. _bundle_fs.create_directory(bundle_dir);
  96. if (!_bundle_fs.exists(CROWN_DATA_DIRECTORY))
  97. _bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
  98. scan_source_dir("");
  99. TempAllocator512 ta;
  100. vector::push_back(_globs, DynamicString("*.tmp", ta));
  101. vector::push_back(_globs, DynamicString("*.wav", ta));
  102. vector::push_back(_globs, DynamicString("*.ogg", ta));
  103. vector::push_back(_globs, DynamicString("*.png", ta));
  104. vector::push_back(_globs, DynamicString("*.tga", ta));
  105. vector::push_back(_globs, DynamicString("*.dds", ta));
  106. vector::push_back(_globs, DynamicString("*.ktx", ta));
  107. vector::push_back(_globs, DynamicString("*.pvr", ta));
  108. vector::push_back(_globs, DynamicString("*.swn", ta)); // VIM swap file.
  109. vector::push_back(_globs, DynamicString("*.swo", ta)); // VIM swap file.
  110. vector::push_back(_globs, DynamicString("*.swp", ta)); // VIM swap file.
  111. vector::push_back(_globs, DynamicString("*~", ta));
  112. vector::push_back(_globs, DynamicString(".*", ta));
  113. if (_source_fs.exists(CROWN_BUNDLEIGNORE))
  114. {
  115. File& file = *_source_fs.open(CROWN_BUNDLEIGNORE, FileOpenMode::READ);
  116. const u32 size = file.size();
  117. char* data = (char*)default_allocator().allocate(size + 1);
  118. file.read(data, size);
  119. data[size] = '\0';
  120. _source_fs.close(file);
  121. LineReader lr(data);
  122. while (!lr.eof())
  123. {
  124. TempAllocator512 ta;
  125. DynamicString line(ta);
  126. lr.read_line(line);
  127. line.trim();
  128. if (line.empty() || line.has_prefix("#"))
  129. continue;
  130. vector::push_back(_globs, line);
  131. }
  132. default_allocator().deallocate(data);
  133. }
  134. }
  135. bool BundleCompiler::compile(const char* type, const char* name, const char* platform)
  136. {
  137. StringId64 _type(type);
  138. StringId64 _name(name);
  139. TempAllocator512 alloc;
  140. DynamicString path(alloc);
  141. TempAllocator512 alloc2;
  142. DynamicString src_path(alloc2);
  143. src_path += name;
  144. src_path += ".";
  145. src_path += type;
  146. char res_name[1 + 2*StringId64::STRING_LENGTH];
  147. _type.to_string(res_name);
  148. res_name[16] = '-';
  149. _name.to_string(res_name + 17);
  150. path::join(CROWN_DATA_DIRECTORY, res_name, path);
  151. CE_LOGI("%s <= %s.%s", res_name, name, type);
  152. bool success = true;
  153. jmp_buf buf;
  154. Buffer output(default_allocator());
  155. array::reserve(output, 4*1024*1024);
  156. if (!setjmp(buf))
  157. {
  158. CompileOptions opts(_source_fs, output, platform, &buf);
  159. compile(_type, src_path.c_str(), opts);
  160. File* outf = _bundle_fs.open(path.c_str(), FileOpenMode::WRITE);
  161. u32 size = array::size(output);
  162. u32 written = outf->write(array::begin(output), size);
  163. _bundle_fs.close(*outf);
  164. success = size == written;
  165. }
  166. else
  167. {
  168. success = false;
  169. }
  170. return success;
  171. }
  172. bool BundleCompiler::compile_all(const char* platform)
  173. {
  174. for (u32 i = 0; i < vector::size(_files); ++i)
  175. {
  176. const char* filename = _files[i].c_str();
  177. bool ignore = false;
  178. for (u32 gg = 0; gg < vector::size(_globs); ++gg)
  179. {
  180. const char* glob = _globs[gg].c_str();
  181. if (wildcmp(glob, filename))
  182. {
  183. ignore = true;
  184. break;
  185. }
  186. }
  187. const char* type = path::extension(filename);
  188. if (ignore || type == NULL)
  189. continue;
  190. char name[256];
  191. const u32 size = u32(type - filename - 1);
  192. strncpy(name, filename, size);
  193. name[size] = '\0';
  194. if (!compile(type, name, platform))
  195. return false;
  196. }
  197. return true;
  198. }
  199. void BundleCompiler::register_resource_compiler(StringId64 type, u32 version, CompileFunction compiler)
  200. {
  201. CE_ASSERT(!sort_map::has(_compilers, type), "Type already registered");
  202. CE_ASSERT_NOT_NULL(compiler);
  203. ResourceTypeData rtd;
  204. rtd.version = version;
  205. rtd.compiler = compiler;
  206. sort_map::set(_compilers, type, rtd);
  207. sort_map::sort(_compilers);
  208. }
  209. void BundleCompiler::compile(StringId64 type, const char* path, CompileOptions& opts)
  210. {
  211. CE_ASSERT(sort_map::has(_compilers, type), "Compiler not found");
  212. sort_map::get(_compilers, type, ResourceTypeData()).compiler(path, opts);
  213. }
  214. u32 BundleCompiler::version(StringId64 type)
  215. {
  216. CE_ASSERT(sort_map::has(_compilers, type), "Compiler not found");
  217. return sort_map::get(_compilers, type, ResourceTypeData()).version;
  218. }
  219. void BundleCompiler::scan_source_dir(const char* cur_dir)
  220. {
  221. Vector<DynamicString> my_files(default_allocator());
  222. _source_fs.list_files(cur_dir, my_files);
  223. for (u32 i = 0; i < vector::size(my_files); ++i)
  224. {
  225. TempAllocator512 ta;
  226. DynamicString file_i(ta);
  227. if (strcmp(cur_dir, "") != 0)
  228. {
  229. file_i += cur_dir;
  230. file_i += '/';
  231. }
  232. file_i += my_files[i];
  233. if (_source_fs.is_directory(file_i.c_str()))
  234. {
  235. BundleCompiler::scan_source_dir(file_i.c_str());
  236. }
  237. else // Assume a regular file
  238. {
  239. vector::push_back(_files, file_i);
  240. }
  241. }
  242. }
  243. } // namespace crown