bundle_compiler.cpp 3.5 KB

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