bundle_compiler.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "bundle_compiler.h"
  24. #include "vector.h"
  25. #include "dynamic_string.h"
  26. #include "allocator.h"
  27. #include "os.h"
  28. #include "log.h"
  29. #include "resource.h"
  30. #include "path.h"
  31. #include "disk_filesystem.h"
  32. #include "temp_allocator.h"
  33. #include <inttypes.h>
  34. namespace crown
  35. {
  36. namespace mesh_resource { extern void compile(Filesystem&, const char*, File*); }
  37. namespace texture_resource { extern void compile(Filesystem&, const char*, File*); }
  38. namespace package_resource { extern void compile(Filesystem&, const char*, File*); }
  39. namespace lua_resource { extern void compile(Filesystem&, const char*, File*); }
  40. namespace physics_resource { extern void compile(Filesystem&, const char*, File*); }
  41. namespace physics_config_resource { extern void compile(Filesystem&, const char*, File*); }
  42. namespace unit_resource { extern void compile(Filesystem&, const char*, File*); }
  43. namespace sound_resource { extern void compile(Filesystem&, const char*, File*); }
  44. namespace sprite_resource { extern void compile(Filesystem&, const char*, File*); }
  45. namespace material_resource { extern void compile(Filesystem&, const char*, File*); }
  46. namespace font_resource { extern void compile(Filesystem&, const char*, File*); }
  47. namespace level_resource { extern void compile(Filesystem&, const char*, File*); }
  48. //-----------------------------------------------------------------------------
  49. BundleCompiler::BundleCompiler()
  50. {
  51. }
  52. //-----------------------------------------------------------------------------
  53. bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir, const char* resource)
  54. {
  55. Vector<DynamicString> files(default_allocator());
  56. if (resource == NULL)
  57. {
  58. BundleCompiler::scan(source_dir, "", files);
  59. DiskFilesystem temp;
  60. temp.create_directory(bundle_dir);
  61. // Copy crown.config to bundle dir
  62. DiskFilesystem src_fs(source_dir);
  63. DiskFilesystem dst_fs(bundle_dir);
  64. if (src_fs.is_file("crown.config"))
  65. {
  66. File* src = src_fs.open("crown.config", FOM_READ);
  67. File* dst = dst_fs.open("crown.config", FOM_WRITE);
  68. src->copy_to(*dst, src->size());
  69. src_fs.close(src);
  70. dst_fs.close(dst);
  71. }
  72. else
  73. {
  74. CE_LOGD("'crown.config' does not exist.");
  75. return false;
  76. }
  77. }
  78. else
  79. {
  80. DynamicString filename(default_allocator());
  81. filename = resource;
  82. vector::push_back(files, filename);
  83. }
  84. // Compile all resources
  85. for (uint32_t i = 0; i < vector::size(files); i++)
  86. {
  87. if (files[i].ends_with(".tga"))
  88. continue;
  89. if (files[i].ends_with(".dds"))
  90. continue;
  91. if (files[i].starts_with("."))
  92. continue;
  93. if (files[i].ends_with(".config"))
  94. continue;
  95. const char* filename = files[i].c_str();
  96. char filename_extension[512];
  97. char filename_without_extension[512];
  98. path::extension(filename, filename_extension, 512);
  99. path::filename_without_extension(filename, filename_without_extension, 512);
  100. const ResourceId name(filename_extension, filename_without_extension);
  101. char out_name[512];
  102. snprintf(out_name, 512, "%.16"PRIx64"-%.16"PRIx64, name.type, name.name);
  103. CE_LOGI("%s <= %s", out_name, filename);
  104. DiskFilesystem root_fs(source_dir);
  105. DiskFilesystem dest_fs(bundle_dir);
  106. // Open destination file
  107. File* out_file = dest_fs.open(out_name, FOM_WRITE);
  108. if (out_file)
  109. {
  110. if (name.type == MESH_TYPE)
  111. {
  112. mesh_resource::compile(root_fs, filename, out_file);
  113. }
  114. else if (name.type == TEXTURE_TYPE)
  115. {
  116. texture_resource::compile(root_fs, filename, out_file);
  117. }
  118. else if (name.type == LUA_TYPE)
  119. {
  120. lua_resource::compile(root_fs, filename, out_file);
  121. }
  122. else if(name.type == SOUND_TYPE)
  123. {
  124. sound_resource::compile(root_fs, filename, out_file);
  125. }
  126. else if(name.type == SPRITE_TYPE)
  127. {
  128. sprite_resource::compile(root_fs, filename, out_file);
  129. }
  130. else if (name.type == PACKAGE_TYPE)
  131. {
  132. package_resource::compile(root_fs, filename, out_file);
  133. }
  134. else if (name.type == UNIT_TYPE)
  135. {
  136. unit_resource::compile(root_fs, filename, out_file);
  137. }
  138. else if (name.type == PHYSICS_TYPE)
  139. {
  140. physics_resource::compile(root_fs, filename, out_file);
  141. }
  142. else if (name.type == MATERIAL_TYPE)
  143. {
  144. material_resource::compile(root_fs, filename, out_file);
  145. }
  146. else if (name.type == PHYSICS_CONFIG_TYPE)
  147. {
  148. physics_config_resource::compile(root_fs, filename, out_file);
  149. }
  150. else if (name.type == FONT_TYPE)
  151. {
  152. font_resource::compile(root_fs, filename, out_file);
  153. }
  154. else if (name.type == LEVEL_TYPE)
  155. {
  156. level_resource::compile(root_fs, filename, out_file);
  157. }
  158. else
  159. {
  160. CE_LOGE("Oops, unknown resource type!");
  161. return false;
  162. }
  163. dest_fs.close(out_file);
  164. }
  165. }
  166. return true;
  167. }
  168. void BundleCompiler::scan(const char* source_dir, const char* cur_dir, Vector<DynamicString>& files)
  169. {
  170. Vector<DynamicString> my_files(default_allocator());
  171. DiskFilesystem fs(source_dir);
  172. fs.list_files(cur_dir, my_files);
  173. for (uint32_t i = 0; i < vector::size(my_files); i++)
  174. {
  175. DynamicString file_i(default_allocator());
  176. if (string::strcmp(cur_dir, "") != 0)
  177. {
  178. file_i += cur_dir;
  179. file_i += '/';
  180. }
  181. file_i += my_files[i];
  182. if (fs.is_directory(file_i.c_str()))
  183. {
  184. BundleCompiler::scan(source_dir, file_i.c_str(), files);
  185. }
  186. else // Assume a regular file
  187. {
  188. vector::push_back(files, file_i);
  189. }
  190. }
  191. }
  192. } // namespace crown