bundle_compiler.cpp 4.5 KB

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