Răsfoiți Sursa

Scan source directory recursively and create bundle dir if does not exist

Daniele Bartolini 12 ani în urmă
părinte
comite
5ee3b71656
2 a modificat fișierele cu 51 adăugiri și 6 ștergeri
  1. 42 4
      engine/compilers/BundleCompiler.cpp
  2. 9 2
      engine/compilers/BundleCompiler.h

+ 42 - 4
engine/compilers/BundleCompiler.cpp

@@ -32,6 +32,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Log.h"
 #include "Resource.h"
 #include "Path.h"
+#include "DiskFilesystem.h"
+#include "TempAllocator.h"
 
 namespace crown
 {
@@ -44,9 +46,15 @@ BundleCompiler::BundleCompiler()
 //-----------------------------------------------------------------------------
 bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir)
 {
-	// Get list of all files from source dir
 	Vector<DynamicString> files(default_allocator());
-	os::list_files(source_dir, files);
+	BundleCompiler::scan(source_dir, "", files);
+
+	// Create bundle dir if does not exist
+	DiskFilesystem temp;
+	if (!temp.is_directory(bundle_dir) || !temp.is_file(bundle_dir))
+	{
+		temp.create_directory(bundle_dir);
+	}
 
 	// Compile all resources
 	for (uint32_t i = 0; i < files.size(); i++)
@@ -63,7 +71,7 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir)
 
 		char out_name[1024];
 		snprintf(out_name, 1024, "%.8X%.8X", resource_name_hash, resource_type_hash);
-		Log::i("%s <= %s\n", out_name, filename.c_str());
+		Log::i("%s <= %s", out_name, filename.c_str());
 
 		bool result = false;
 		if (resource_type_hash == TEXTURE_TYPE)
@@ -76,7 +84,8 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir)
 		}
 		else
 		{
-			continue;
+			Log::e("Oops, unknown resource type!");
+			return false;
 		}
 
 		if (!result)
@@ -88,4 +97,33 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir)
 	return true;
 }
 
+void BundleCompiler::scan(const char* source_dir, const char* cur_dir, Vector<DynamicString>& files)
+{
+	Vector<DynamicString> my_files(default_allocator());
+
+	DiskFilesystem fs(source_dir);
+	fs.list_files(cur_dir, my_files);
+
+	for (uint32_t i = 0; i < my_files.size(); i++)
+	{
+		DynamicString file_i(default_allocator());
+
+		if (string::strcmp(cur_dir, "") != 0)
+		{
+			file_i += cur_dir;
+			file_i += '/';
+		}
+		file_i += my_files[i];
+
+		if (fs.is_directory(file_i.c_str()))
+		{
+			BundleCompiler::scan(source_dir, file_i.c_str(), files);
+		}
+		else // Assume a regular file
+		{
+			files.push_back(file_i);
+		}
+	}
+}
+
 } // namespace crown

+ 9 - 2
engine/compilers/BundleCompiler.h

@@ -28,6 +28,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "TGACompiler.h"
 #include "LuaCompiler.h"
+#include "DynamicString.h"
+#include "Vector.h"
+#include "DiskFilesystem.h"
 
 namespace crown
 {
@@ -36,9 +39,13 @@ class BundleCompiler
 {
 public:
 
-				BundleCompiler();
+	BundleCompiler();
 
-	bool		compile(const char* bundle_dir, const char* source_dir);
+	bool compile(const char* bundle_dir, const char* source_dir);
+
+private:
+
+	static void scan(const char* source_dir, const char* cur_dir, Vector<DynamicString>& files);
 
 private: