bundle_compiler.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "bundle_compiler.h"
  7. #include "vector.h"
  8. #include "dynamic_string.h"
  9. #include "allocator.h"
  10. #include "os.h"
  11. #include "log.h"
  12. #include "path.h"
  13. #include "disk_filesystem.h"
  14. #include "compile_options.h"
  15. #include "resource_registry.h"
  16. #include "resource_id.h"
  17. #include "temp_allocator.h"
  18. namespace crown
  19. {
  20. BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
  21. : _source_fs(source_dir)
  22. , _bundle_fs(bundle_dir)
  23. {
  24. DiskFilesystem temp;
  25. temp.create_directory(bundle_dir);
  26. }
  27. bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum platform)
  28. {
  29. ResourceId id(type, name);
  30. TempAllocator512 alloc;
  31. DynamicString path(alloc);
  32. TempAllocator512 alloc2;
  33. DynamicString src_path(alloc2);
  34. src_path += name;
  35. src_path += ".";
  36. src_path += type;
  37. char res_name[64];
  38. id.to_string(res_name);
  39. path::join(CROWN_DATA_DIRECTORY, res_name, path);
  40. CE_LOGI("%s <= %s.%s", res_name, name, type);
  41. File* outf = _bundle_fs.open(path.c_str(), FOM_WRITE);
  42. CompileOptions opts(_source_fs, outf, platform);
  43. resource_on_compile(id.type, src_path.c_str(), opts);
  44. _bundle_fs.close(outf);
  45. return true;
  46. }
  47. bool BundleCompiler::compile_all(Platform::Enum platform)
  48. {
  49. Vector<DynamicString> files(default_allocator());
  50. BundleCompiler::scan("", files);
  51. if (!_source_fs.is_file("crown.config"))
  52. {
  53. CE_LOGD("'crown.config' does not exist.");
  54. return false;
  55. }
  56. File* src = _source_fs.open("crown.config", FOM_READ);
  57. File* dst = _bundle_fs.open("crown.config", FOM_WRITE);
  58. src->copy_to(*dst, src->size());
  59. _source_fs.close(src);
  60. _bundle_fs.close(dst);
  61. if (!_bundle_fs.exists("data"))
  62. _bundle_fs.create_directory("data");
  63. // Compile all resources
  64. for (uint32_t i = 0; i < vector::size(files); i++)
  65. {
  66. if (files[i].ends_with(".tga")
  67. || files[i].ends_with(".dds")
  68. || files[i].ends_with(".sh")
  69. || files[i].ends_with(".sc")
  70. || files[i].starts_with(".")
  71. || files[i].ends_with(".config")
  72. || files[i].ends_with(".tmp")
  73. || files[i].ends_with(".wav"))
  74. continue;
  75. const char* filename = files[i].c_str();
  76. char type[256];
  77. char name[256];
  78. path::extension(filename, type, 256);
  79. path::filename_without_extension(filename, name, 256);
  80. compile(type, name, platform);
  81. }
  82. return true;
  83. }
  84. void BundleCompiler::scan(const char* cur_dir, Vector<DynamicString>& files)
  85. {
  86. Vector<DynamicString> my_files(default_allocator());
  87. _source_fs.list_files(cur_dir, my_files);
  88. for (uint32_t i = 0; i < vector::size(my_files); i++)
  89. {
  90. DynamicString file_i(default_allocator());
  91. if (strcmp(cur_dir, "") != 0)
  92. {
  93. file_i += cur_dir;
  94. file_i += '/';
  95. }
  96. file_i += my_files[i];
  97. if (_source_fs.is_directory(file_i.c_str()))
  98. {
  99. BundleCompiler::scan(file_i.c_str(), files);
  100. }
  101. else // Assume a regular file
  102. {
  103. vector::push_back(files, file_i);
  104. }
  105. }
  106. }
  107. namespace bundle_compiler
  108. {
  109. bool main(bool do_compile, bool do_continue, Platform::Enum platform)
  110. {
  111. if (do_compile)
  112. {
  113. bool ok = bundle_compiler_globals::compiler()->compile_all(platform);
  114. if (!ok || !do_continue)
  115. {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. } // namespace bundle_compiler
  122. namespace bundle_compiler_globals
  123. {
  124. char _buffer[sizeof(BundleCompiler)];
  125. BundleCompiler* _compiler = NULL;
  126. void init(const char* source_dir, const char* bundle_dir)
  127. {
  128. #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
  129. _compiler = new (_buffer) BundleCompiler(source_dir, bundle_dir);
  130. #endif
  131. }
  132. void shutdown()
  133. {
  134. _compiler->~BundleCompiler();
  135. _compiler = NULL;
  136. }
  137. BundleCompiler* compiler()
  138. {
  139. return _compiler;
  140. }
  141. } // namespace bundle_compiler_globals
  142. } // namespace crown