ソースを参照

Filenames with spaces are dumb, but accept them anyway

Daniele Bartolini 9 年 前
コミット
5eb10cabe2

+ 22 - 0
src/resource/compile_options.h

@@ -12,7 +12,9 @@
 #include "filesystem.h"
 #include "guid.h"
 #include "log.h"
+#include "os.h"
 #include "path.h"
+#include "string_stream.h"
 #include "temp_allocator.h"
 #include "vector.h"
 #include <setjmp.h>
@@ -205,6 +207,26 @@ public:
 		dep += path;
 		vector::push_back(_dependencies, dep);
 	}
+
+	int run_external_compiler(const char* const* argv, StringStream& output)
+	{
+		TempAllocator512 ta;
+		StringStream ss(ta);
+
+		for (s32 i = 1; argv[i] != NULL; ++i)
+		{
+			const char* arg = argv[i];
+			for (; *arg; ++arg)
+			{
+				if (*arg == ' ')
+					ss << '\\';
+				ss << *arg;
+			}
+			ss << ' ';
+		}
+
+		return os::execute_process(argv[0], string_stream::c_str(ss), output);
+	}
 };
 
 } // namespace crown

+ 10 - 8
src/resource/lua_resource.cpp

@@ -8,7 +8,6 @@
 #include "config.h"
 #include "dynamic_string.h"
 #include "lua_resource.h"
-#include "os.h"
 #include "string_stream.h"
 #include "temp_allocator.h"
 
@@ -40,14 +39,17 @@ namespace lua_resource_internal
 		opts.get_absolute_path(path, luasrc);
 		opts.get_temporary_path("lua.bin", luabin);
 
-		StringStream args(ta);
-		args << " " << LUAJIT_FLAGS;
-		args << " " << luasrc.c_str();
-		args << " " << luabin.c_str();
-
 		StringStream output(ta);
-		int exitcode = os::execute_process(LUAJIT_EXE, string_stream::c_str(args), output);
-		RESOURCE_COMPILER_ASSERT(exitcode == 0
+		const char* argv[] =
+		{
+			LUAJIT_EXE,
+			LUAJIT_FLAGS,
+			luasrc.c_str(),
+			luabin.c_str(),
+			NULL
+		};
+		int ec = opts.run_external_compiler(argv, output);
+		RESOURCE_COMPILER_ASSERT(ec == 0
 			, opts
 			, "Failed to compile lua:\n%s"
 			, string_stream::c_str(output)

+ 26 - 16
src/resource/shader_resource.cpp

@@ -9,7 +9,6 @@
 #include "filesystem.h"
 #include "json_object.h"
 #include "map.h"
-#include "os.h"
 #include "resource_manager.h"
 #include "shader_manager.h"
 #include "shader_resource.h"
@@ -430,22 +429,33 @@ namespace shader_resource_internal
 		return SamplerWrap::COUNT;
 	}
 
-	static int run_external_compiler(const char* infile, const char* outfile, const char* varying, const char* type, const char* platform, StringStream& output)
+	static int run_external_compiler(CompileOptions& opts, const char* infile, const char* outfile, const char* varying, const char* type, const char* platform, StringStream& output)
 	{
-		TempAllocator512 ta;
-		StringStream args(ta);
-		args << " -f " << infile;
-		args << " -o " << outfile;
-		args << " --varyingdef " << varying;
-		args << " --type " << type;
-		args << " --platform " << platform;
+		const char* argv[] =
+		{
+			SHADERC_PATH,
+			"-f",
+			infile,
+			"-o",
+			outfile,
+			"--varyingdef",
+			varying,
+			"--type",
+			type,
+			"--platform",
+			platform,
+			NULL,
+			NULL,
+			NULL,
+		};
+
 		if (strcmp("windows", platform) == 0)
 		{
-			args << " --profile ";
-			args << ((strcmp(type, "vertex") == 0) ? "vs_3_0" : "ps_3_0");
+			argv[11] = "--profile";
+			argv[12] = ((strcmp(type, "vertex") == 0) ? "vs_3_0" : "ps_3_0");
 		}
 
-		return os::execute_process(SHADERC_PATH, string_stream::c_str(args), output);
+		return opts.run_external_compiler(argv, output);
 	}
 
 	struct RenderState
@@ -1176,14 +1186,14 @@ namespace shader_resource_internal
 			TempAllocator4096 ta;
 			StringStream output(ta);
 
-			int exitcode = run_external_compiler(_vs_source_path.c_str()
+			int ec = run_external_compiler(_opts, _vs_source_path.c_str()
 				, _vs_compiled_path.c_str()
 				, _varying_path.c_str()
 				, "vertex"
 				, _opts.platform()
 				, output
 				);
-			if (exitcode)
+			if (ec)
 			{
 				delete_temp_files();
 				RESOURCE_COMPILER_ASSERT(false
@@ -1194,14 +1204,14 @@ namespace shader_resource_internal
 			}
 
 			array::clear(output);
-			exitcode = run_external_compiler(_fs_source_path.c_str()
+			ec = run_external_compiler(_opts, _fs_source_path.c_str()
 				, _fs_compiled_path.c_str()
 				, _varying_path.c_str()
 				, "fragment"
 				, _opts.platform()
 				, output
 				);
-			if (exitcode)
+			if (ec)
 			{
 				delete_temp_files();
 				RESOURCE_COMPILER_ASSERT(false

+ 13 - 8
src/resource/texture_resource.cpp

@@ -5,7 +5,6 @@
 
 #include "compile_options.h"
 #include "json_object.h"
-#include "os.h"
 #include "reader_writer.h"
 #include "resource_manager.h"
 #include "sjson.h"
@@ -56,15 +55,21 @@ namespace texture_resource_internal
 		opts.get_absolute_path(name.c_str(), texsrc);
 		opts.get_temporary_path("texture.ktx", texout);
 
-		StringStream args(ta);
-		args << " -f " << texsrc.c_str();
-		args << " -o " << texout.c_str();
-		args << (generate_mips ? " -m " : "");
-		args << (is_normalmap  ? " -n " : "");
+		const char* argv[] =
+		{
+			TEXTUREC_PATH,
+			"-f",
+			texsrc.c_str(),
+			"-o",
+			texout.c_str(),
+			(generate_mips ? "-m" : ""),
+			(is_normalmap  ? "-n" : ""),
+			NULL
+		};
 
 		StringStream output(ta);
-		int exitcode = os::execute_process(TEXTUREC_PATH, string_stream::c_str(args), output);
-		RESOURCE_COMPILER_ASSERT(exitcode == 0
+		int ec = opts.run_external_compiler(argv, output);
+		RESOURCE_COMPILER_ASSERT(ec == 0
 			, opts
 			, "Failed to compile texture:\n%s"
 			, string_stream::c_str(output)