bundle_compiler.cpp 3.8 KB

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