Daniele Bartolini преди 10 години
родител
ревизия
93b22a0135
променени са 4 файла, в които са добавени 40 реда и са изтрити 22 реда
  1. 33 7
      src/resource/compile_options.h
  2. 1 1
      src/resource/lua_resource.cpp
  3. 5 13
      src/resource/shader_resource.cpp
  4. 1 1
      src/resource/texture_resource.cpp

+ 33 - 7
src/resource/compile_options.h

@@ -12,6 +12,7 @@
 #include "path.h"
 #include "temp_allocator.h"
 #include "vector.h"
+#include "array.h"
 #include <setjmp.h>
 
 #define RESOURCE_COMPILER_ASSERT(condition, opts, msg, ...) do { if (!(condition))\
@@ -19,8 +20,16 @@
 
 namespace crown
 {
-struct CompileOptions
+class CompileOptions
 {
+	Filesystem& _fs;
+	Buffer& _output;
+	const char* _platform;
+	jmp_buf* _jmpbuf;
+	Vector<DynamicString> _dependencies;
+
+public:
+
 	CompileOptions(Filesystem& fs, Buffer& output, const char* platform, jmp_buf* buf)
 		: _fs(fs)
 		, _output(output)
@@ -58,6 +67,29 @@ struct CompileOptions
 		return file_exists(path.c_str());
 	}
 
+	Buffer read_temporary(const char* path)
+	{
+		File* file = _fs.open(path, FileOpenMode::READ);
+		u32 size = file->size();
+		Buffer buf(default_allocator());
+		array::resize(buf, size);
+		file->read(array::begin(buf), size);
+		_fs.close(*file);
+		return buf;
+	}
+
+	void write_temporary(const char* path, const char* data, u32 size)
+	{
+		File* file = _fs.open(path, FileOpenMode::WRITE);
+		file->write(data, size);
+		_fs.close(*file);
+	}
+
+	void write_temporary(const char* path, const Buffer& data)
+	{
+		write_temporary(path, array::begin(data), array::size(data));
+	}
+
 	Buffer read(const char* path)
 	{
 		add_dependency(path);
@@ -113,12 +145,6 @@ struct CompileOptions
 		DynamicString dep(path, ta);
 		vector::push_back(_dependencies, dep);
 	}
-
-	Filesystem& _fs;
-	Buffer& _output;
-	const char* _platform;
-	jmp_buf* _jmpbuf;
-	Vector<DynamicString> _dependencies;
 };
 
 } // namespace crown

+ 1 - 1
src/resource/lua_resource.cpp

@@ -55,7 +55,7 @@ namespace lua_resource
 			, c_str(output)
 			);
 
-		Buffer blob = opts.read(luabin.c_str());
+		Buffer blob = opts.read_temporary(luabin.c_str());
 		opts.delete_file(luabin.c_str());
 
 		LuaResource lr;

+ 5 - 13
src/resource/shader_resource.cpp

@@ -1169,17 +1169,9 @@ namespace shader_resource
 			fs_code << shader._code.c_str();
 			fs_code << shader._fs_code.c_str();
 
-			File* vs_file = _opts._fs.open(_vs_source_path.c_str(), FileOpenMode::WRITE);
-			vs_file->write(c_str(vs_code), array::size(vs_code));
-			_opts._fs.close(*vs_file);
-
-			File* fs_file = _opts._fs.open(_fs_source_path.c_str(), FileOpenMode::WRITE);
-			fs_file->write(c_str(fs_code), array::size(fs_code));
-			_opts._fs.close(*fs_file);
-
-			File* varying_file = _opts._fs.open(_varying_path.c_str(), FileOpenMode::WRITE);
-			varying_file->write(shader._varying.c_str(), shader._varying.length());
-			_opts._fs.close(*varying_file);
+			_opts.write_temporary(_vs_source_path.c_str(), vs_code);
+			_opts.write_temporary(_fs_source_path.c_str(), fs_code);
+			_opts.write_temporary(_varying_path.c_str(), shader._varying.c_str(), shader._varying.length());
 
 			TempAllocator4096 ta;
 			StringStream output(ta);
@@ -1219,8 +1211,8 @@ namespace shader_resource
 					);
 			}
 
-			Buffer tmpvs = _opts.read(_vs_compiled_path.c_str());
-			Buffer tmpfs = _opts.read(_fs_compiled_path.c_str());
+			Buffer tmpvs = _opts.read_temporary(_vs_compiled_path.c_str());
+			Buffer tmpfs = _opts.read_temporary(_fs_compiled_path.c_str());
 
 			delete_temp_files();
 

+ 1 - 1
src/resource/texture_resource.cpp

@@ -70,7 +70,7 @@ namespace texture_resource
 			, c_str(output)
 			);
 
-		Buffer blob = opts.read(texout.c_str());
+		Buffer blob = opts.read_temporary(texout.c_str());
 		opts.delete_file(texout.c_str());
 
 		// Write DDS