Просмотр исходного кода

Output data_index.sjson to data directory after resource compilation

Daniele Bartolini 8 лет назад
Родитель
Сommit
fe1c7de3e0
2 измененных файлов с 106 добавлено и 85 удалено
  1. 94 77
      src/resource/data_compiler.cpp
  2. 12 8
      src/resource/data_compiler.h

+ 94 - 77
src/resource/data_compiler.cpp

@@ -113,6 +113,7 @@ DataCompiler::DataCompiler(ConsoleServer& cs)
 	, _compilers(default_allocator())
 	, _files(default_allocator())
 	, _globs(default_allocator())
+	, _data_index(default_allocator())
 	, _file_monitor(default_allocator())
 {
 	cs.register_command("compile", console_command_compile, this);
@@ -198,18 +199,6 @@ void DataCompiler::remove_tree(const char* path)
 	}
 }
 
-bool DataCompiler::can_compile(StringId64 type)
-{
-	return hash_map::has(_compilers, type);
-}
-
-void DataCompiler::compile(StringId64 type, const char* path, CompileOptions& opts)
-{
-	CE_ASSERT(hash_map::has(_compilers, type), "Compiler not found");
-
-	hash_map::get(_compilers, type, ResourceTypeData()).compiler(path, opts);
-}
-
 void DataCompiler::scan_source_dir(const char* prefix, const char* cur_dir)
 {
 	Vector<DynamicString> my_files(default_allocator());
@@ -245,68 +234,6 @@ void DataCompiler::scan_source_dir(const char* prefix, const char* cur_dir)
 	}
 }
 
-bool DataCompiler::compile(FilesystemDisk& bundle_fs, const char* type, const char* name, const char* platform)
-{
-	TempAllocator1024 ta;
-	DynamicString path(ta);
-	DynamicString src_path(ta);
-	DynamicString type_str(ta);
-	DynamicString name_str(ta);
-	DynamicString dst_path(ta);
-
-	StringId64 _type(type);
-	StringId64 _name(name);
-
-	// Build source file path
-	src_path += name;
-	src_path += '.';
-	src_path += type;
-
-	// Build compiled file path
-	_type.to_string(type_str);
-	_name.to_string(name_str);
-
-	// Build destination file path
-	dst_path += type_str;
-	dst_path += '-';
-	dst_path += name_str;
-
-	path::join(path, CROWN_DATA_DIRECTORY, dst_path.c_str());
-
-	logi(COMPILER, "%s <= %s", dst_path.c_str(), src_path.c_str());
-
-	if (!can_compile(_type))
-	{
-		loge(COMPILER, "Unknown resource type: '%s'", type);
-		return false;
-	}
-
-	bool success = true;
-
-	Buffer output(default_allocator());
-	array::reserve(output, 4*1024*1024);
-
-	if (!setjmp(_jmpbuf))
-	{
-		CompileOptions opts(*this, bundle_fs, output, platform);
-
-		compile(_type, src_path.c_str(), opts);
-
-		File* outf = bundle_fs.open(path.c_str(), FileOpenMode::WRITE);
-		u32 size = array::size(output);
-		u32 written = outf->write(array::begin(output), size);
-		bundle_fs.close(*outf);
-
-		success = size == written;
-	}
-	else
-	{
-		success = false;
-	}
-
-	return success;
-}
-
 void DataCompiler::map_source_dir(const char* name, const char* source_dir)
 {
 	TempAllocator256 ta;
@@ -422,8 +349,91 @@ bool DataCompiler::compile(const char* bundle_dir, const char* platform)
 		strncpy(name, filename, size);
 		name[size] = '\0';
 
-		if (!compile(bundle_fs, type, name, platform))
+		TempAllocator1024 ta;
+		DynamicString path(ta);
+		DynamicString src_path(ta);
+		DynamicString type_str(ta);
+		DynamicString name_str(ta);
+		DynamicString dst_path(ta);
+
+		StringId64 _type(type);
+		StringId64 _name(name);
+
+		// Build source file path
+		src_path += name;
+		src_path += '.';
+		src_path += type;
+
+		// Build compiled file path
+		_type.to_string(type_str);
+		_name.to_string(name_str);
+
+		// Build destination file path
+		dst_path += type_str;
+		dst_path += '-';
+		dst_path += name_str;
+
+		path::join(path, CROWN_DATA_DIRECTORY, dst_path.c_str());
+
+		logi(COMPILER, "%s", src_path.c_str());
+
+		if (!can_compile(_type))
+		{
+			loge(COMPILER, "Unknown resource type: '%s'", type);
+			break;
+		}
+
+		bool success = true;
+
+		Buffer output(default_allocator());
+		array::reserve(output, 4*1024*1024);
+
+		if (!setjmp(_jmpbuf))
+		{
+			CompileOptions opts(*this, bundle_fs, output, platform);
+
+			hash_map::get(_compilers, _type, ResourceTypeData()).compiler(src_path.c_str(), opts);
+
+			File* outf = bundle_fs.open(path.c_str(), FileOpenMode::WRITE);
+			u32 size = array::size(output);
+			u32 written = outf->write(array::begin(output), size);
+			bundle_fs.close(*outf);
+
+			success = size == written;
+		}
+		else
+		{
+			success = false;
+		}
+
+		if (!success)
+		{
+			loge(COMPILER, "Error");
+		}
+		else
+		{
+			if (!map::has(_data_index, dst_path))
+				map::set(_data_index, dst_path, src_path);
+		}
+	}
+
+	// Write index
+	{
+		File* file = bundle_fs.open("data_index.sjson", FileOpenMode::WRITE);
+		if (!file)
 			return false;
+
+		StringStream ss(default_allocator());
+
+		auto begin = map::begin(_data_index);
+		auto end = map::end(_data_index);
+		for (; begin != end; ++begin)
+		{
+			ss << "\"" << begin->pair.first.c_str() << "\" = \"" << begin->pair.second.c_str() << "\"\n";
+		}
+
+		file->write(string_stream::c_str(ss), strlen32(string_stream::c_str(ss)));
+		bundle_fs.close(*file);
 	}
 
 	return true;
@@ -443,9 +453,16 @@ void DataCompiler::register_compiler(StringId64 type, u32 version, CompileFuncti
 
 u32 DataCompiler::version(StringId64 type)
 {
-	CE_ASSERT(hash_map::has(_compilers, type), "Compiler not found");
+	ResourceTypeData rtd;
+	rtd.version = COMPILER_NOT_FOUND;
+	rtd.compiler = NULL;
 
-	return hash_map::get(_compilers, type, ResourceTypeData()).version;
+	return hash_map::get(_compilers, type, rtd).version;
+}
+
+bool DataCompiler::can_compile(StringId64 type)
+{
+	return hash_map::has(_compilers, type);
 }
 
 void DataCompiler::error(const char* msg, va_list args)

+ 12 - 8
src/resource/data_compiler.h

@@ -30,6 +30,7 @@ class DataCompiler
 	HashMap<StringId64, ResourceTypeData> _compilers;
 	Vector<DynamicString> _files;
 	Vector<DynamicString> _globs;
+	Map<DynamicString, DynamicString> _data_index;
 	FileMonitor _file_monitor;
 	jmp_buf _jmpbuf;
 
@@ -37,13 +38,8 @@ class DataCompiler
 	void add_tree(const char* path);
 	void remove_file(const char* path);
 	void remove_tree(const char* path);
-
 	void scan_source_dir(const char* prefix, const char* path);
 
-	bool can_compile(StringId64 type);
-	void compile(StringId64 type, const char* path, CompileOptions& opts);
-	bool compile(FilesystemDisk& bundle_fs, const char* type, const char* name, const char* platform);
-
 	void filemonitor_callback(FileMonitorEvent::Enum fme, bool is_dir, const char* path, const char* path_renamed);
 	static void filemonitor_callback(void* thiz, FileMonitorEvent::Enum fme, bool is_dir, const char* path_original, const char* path_modified);
 
@@ -55,22 +51,30 @@ public:
 	void map_source_dir(const char* name, const char* source_dir);
 	void source_dir(const char* resource_name, DynamicString& source_dir);
 
+	/// Adds a @a glob pattern to ignore when scanning the source directory.
 	void add_ignore_glob(const char* glob);
 
-	/// Scans source tree for resources.
+	/// Scans source directory for resources.
 	void scan();
 
-	/// Compiles all the resources found in the source tree and puts them in @a bundle_dir.
+	/// Compiles all the resources found in the source directory and puts them in @a bundle_dir.
 	/// Returns true on success, false otherwise.
 	bool compile(const char* bundle_dir, const char* platform);
 
 	/// Registers the resource @a compiler for the given resource @a type and @a version.
 	void register_compiler(StringId64 type, u32 version, CompileFunction compiler);
 
-	// Returns the version of the compiler for @a type.
+	/// Returns whether there is a compiler for the resource @a type.
+	bool can_compile(StringId64 type);
+
+	/// Returns the version of the compiler for @a type or COMPILER_NOT_FOUND if no compiler
+	/// is found.
 	u32 version(StringId64 type);
 
+	///
 	void error(const char* msg, va_list args);
+
+	static const u32 COMPILER_NOT_FOUND = UINT32_MAX;
 };
 
 int main_data_compiler(int argc, char** argv);