BundleCompiler.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <inttypes.h>
  24. #include "BundleCompiler.h"
  25. #include "Vector.h"
  26. #include "DynamicString.h"
  27. #include "Allocator.h"
  28. #include "OS.h"
  29. #include "Log.h"
  30. #include "Resource.h"
  31. #include "Path.h"
  32. #include "DiskFilesystem.h"
  33. #include "TempAllocator.h"
  34. namespace crown
  35. {
  36. //-----------------------------------------------------------------------------
  37. BundleCompiler::BundleCompiler()
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir, const char* resource)
  42. {
  43. Vector<DynamicString> files(default_allocator());
  44. if (resource == NULL)
  45. {
  46. BundleCompiler::scan(source_dir, "", files);
  47. // Create bundle dir if does not exist
  48. DiskFilesystem temp;
  49. if (!temp.is_directory(bundle_dir) || !temp.is_file(bundle_dir))
  50. {
  51. temp.create_directory(bundle_dir);
  52. }
  53. // Copy crown.config to bundle dir
  54. DiskFilesystem src_fs(source_dir);
  55. DiskFilesystem dst_fs(bundle_dir);
  56. if (src_fs.is_file("crown.config"))
  57. {
  58. File* src = src_fs.open("crown.config", FOM_READ);
  59. File* dst = dst_fs.open("crown.config", FOM_WRITE);
  60. src->copy_to(*dst, src->size());
  61. src_fs.close(src);
  62. dst_fs.close(dst);
  63. }
  64. else
  65. {
  66. Log::d("'crown.config' does not exist.");
  67. return false;
  68. }
  69. }
  70. else
  71. {
  72. DynamicString filename(default_allocator());
  73. filename = resource;
  74. files.push_back(filename);
  75. }
  76. // Compile all resources
  77. for (uint32_t i = 0; i < files.size(); i++)
  78. {
  79. const char* filename = files[i].c_str();
  80. uint64_t filename_hash = hash::murmur2_64(filename, string::strlen(filename), 0);
  81. char filename_extension[32];
  82. path::extension(filename, filename_extension, 32);
  83. uint32_t resource_type_hash = hash::murmur2_32(filename_extension, string::strlen(filename_extension), 0);
  84. char out_name[65];
  85. snprintf(out_name, 65, "%"PRIx64"", filename_hash);
  86. // Skip crown.config file
  87. if (resource_type_hash == CONFIG_TYPE)
  88. {
  89. continue;
  90. }
  91. Log::i("%s <= %s", out_name, filename);
  92. Log::flush();
  93. bool result = false;
  94. if (resource_type_hash == MESH_TYPE)
  95. {
  96. result = m_mesh.compile(source_dir, bundle_dir, filename, out_name);
  97. }
  98. else if (resource_type_hash == TEXTURE_TYPE)
  99. {
  100. result = m_texture.compile(source_dir, bundle_dir, filename, out_name);
  101. }
  102. else if (resource_type_hash == LUA_TYPE)
  103. {
  104. result = m_lua.compile(source_dir, bundle_dir, filename, out_name);
  105. }
  106. else if (resource_type_hash == PACKAGE_TYPE)
  107. {
  108. result = m_package.compile(source_dir, bundle_dir, filename, out_name);
  109. }
  110. else
  111. {
  112. Log::e("Oops, unknown resource type!");
  113. return false;
  114. }
  115. if (!result)
  116. {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. void BundleCompiler::scan(const char* source_dir, const char* cur_dir, Vector<DynamicString>& files)
  123. {
  124. Vector<DynamicString> my_files(default_allocator());
  125. DiskFilesystem fs(source_dir);
  126. fs.list_files(cur_dir, my_files);
  127. for (uint32_t i = 0; i < my_files.size(); i++)
  128. {
  129. DynamicString file_i(default_allocator());
  130. if (string::strcmp(cur_dir, "") != 0)
  131. {
  132. file_i += cur_dir;
  133. file_i += '/';
  134. }
  135. file_i += my_files[i];
  136. if (fs.is_directory(file_i.c_str()))
  137. {
  138. BundleCompiler::scan(source_dir, file_i.c_str(), files);
  139. }
  140. else // Assume a regular file
  141. {
  142. files.push_back(file_i);
  143. }
  144. }
  145. }
  146. } // namespace crown