Daniele Bartolini 9 rokov pred
rodič
commit
a97d13ba62

+ 56 - 4
src/device/device.cpp

@@ -168,6 +168,37 @@ static void console_command_unpause(void* /*data*/, ConsoleServer& /*cs*/, TCPSo
 	device()->unpause();
 }
 
+static void console_command_compile(void* data, ConsoleServer& cs, TCPSocket client, const char* json)
+{
+	TempAllocator4096 ta;
+	JsonObject obj(ta);
+	sjson::parse(json, obj);
+
+	DynamicString id(ta);
+	DynamicString bundle_dir(ta);
+	DynamicString platform(ta);
+	sjson::parse_string(obj["id"], id);
+	sjson::parse_string(obj["bundle_dir"], bundle_dir);
+	sjson::parse_string(obj["platform"], platform);
+
+	{
+		TempAllocator512 ta;
+		StringStream ss(ta);
+		ss << "{\"type\":\"compile\",\"id\":\"" << id.c_str() << "\",\"start\":true}";
+		cs.send(client, string_stream::c_str(ss));
+	}
+
+	BundleCompiler* bc = (BundleCompiler*)data;
+	bool succ = bc->compile(bundle_dir.c_str(), platform.c_str());
+
+	{
+		TempAllocator512 ta;
+		StringStream ss(ta);
+		ss << "{\"type\":\"compile\",\"id\":\"" << id.c_str() << "\",\"success\":" << (succ ? "true" : "false") << "}";
+		cs.send(client, string_stream::c_str(ss));
+	}
+}
+
 Device::Device(const DeviceOptions& opts)
 	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
 	, _device_options(opts)
@@ -394,9 +425,9 @@ void Device::run()
 	bool do_continue = true;
 
 #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
-	if (_device_options._do_compile)
+	if (_device_options._do_compile || _device_options._server)
 	{
-		_bundle_compiler = CE_NEW(_allocator, BundleCompiler)(_device_options._source_dir, _device_options._bundle_dir, *_console_server);
+		_bundle_compiler = CE_NEW(_allocator, BundleCompiler)();
 		_bundle_compiler->register_compiler(RESOURCE_TYPE_SCRIPT,           RESOURCE_VERSION_SCRIPT,           lur::compile);
 		_bundle_compiler->register_compiler(RESOURCE_TYPE_TEXTURE,          RESOURCE_VERSION_TEXTURE,          txr::compile);
 		_bundle_compiler->register_compiler(RESOURCE_TYPE_MESH,             RESOURCE_VERSION_MESH,             mhr::compile);
@@ -413,7 +444,27 @@ void Device::run()
 		_bundle_compiler->register_compiler(RESOURCE_TYPE_SPRITE_ANIMATION, RESOURCE_VERSION_SPRITE_ANIMATION, sar::compile);
 		_bundle_compiler->register_compiler(RESOURCE_TYPE_CONFIG,           RESOURCE_VERSION_CONFIG,           cor::compile);
 
-		do_continue = _bundle_compiler->run(_device_options._server) && _device_options._do_continue;
+		_bundle_compiler->scan(_device_options._source_dir);
+
+		if (_device_options._server)
+		{
+			_console_server->register_command(StringId32("compile"), console_command_compile, _bundle_compiler);
+			_console_server->listen(CROWN_DEFAULT_COMPILER_PORT, false);
+
+			while (true)
+			{
+				_console_server->update();
+				os::sleep(60);
+			}
+		}
+		else
+		{
+			const char* source_dir = _device_options._source_dir;
+			const char* bundle_dir = _device_options._bundle_dir;
+			const char* platform = _device_options._platform;
+			do_continue = _bundle_compiler->compile(bundle_dir, platform);
+			do_continue = do_continue && _device_options._do_continue;
+		}
 	}
 #endif // CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
 
@@ -428,7 +479,8 @@ void Device::run()
 			char buf[1024];
 			bundle_dir = os::getcwd(buf, sizeof(buf));
 		}
-		_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(default_allocator(), bundle_dir);
+		_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(default_allocator());
+		((DiskFilesystem*)_bundle_filesystem)->set_prefix(bundle_dir);
 		if (!_bundle_filesystem->exists(bundle_dir))
 			_bundle_filesystem->create_directory(bundle_dir);
 

+ 8 - 2
src/device/device_options.cpp

@@ -19,10 +19,16 @@ static void help(const char* msg = NULL)
 	}
 
 	CE_LOGI(
-		"Usage: crown [options]\n"
+		"The Flexible Game Engine\n"
+		"Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.\n"
+		"License: https://github.com/taylor001/crown/blob/master/LICENSE\n"
 		"\n"
-		"Options:\n"
+		"Complete documentation available at https://taylor001.github.io/crown/manual.html\n"
+		"\n"
+		"Usage:\n"
+		"  crown [options]\n"
 		"\n"
+		"Options:\n"
 		"  -h --help                  Display this help.\n"
 		"  -v --version               Display engine version.\n"
 		"  --source-dir <path>        Use <path> as the source directory for resource compilation.\n"

+ 19 - 48
src/resource/bundle_compiler.cpp

@@ -52,44 +52,17 @@ public:
 	}
 };
 
-static void console_command_compile(void* data, ConsoleServer& cs, TCPSocket client, const char* json)
-{
-	TempAllocator4096 ta;
-	JsonObject obj(ta);
-	DynamicString type(ta);
-	DynamicString name(ta);
-	DynamicString platform(ta);
-	sjson::parse(json, obj);
-	sjson::parse_string(obj["resource_type"], type);
-	sjson::parse_string(obj["resource_name"], name);
-	sjson::parse_string(obj["platform"], platform);
-
-	BundleCompiler* bc = (BundleCompiler*)data;
-	bool succ = bc->compile(type.c_str(), name.c_str(), platform.c_str());
-
-	if (succ)
-		cs.success(client, "Resource compiled.");
-	else
-		cs.error(client, "Failed to compile resource.");
-}
-
-BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir, ConsoleServer& cs)
-	: _source_fs(default_allocator(), source_dir)
-	, _bundle_fs(default_allocator(), bundle_dir)
+BundleCompiler::BundleCompiler()
+	: _source_fs(default_allocator())
 	, _compilers(default_allocator())
 	, _files(default_allocator())
 	, _globs(default_allocator())
-	, _console_server(&cs)
 {
-	_bundle_fs.create_directory(bundle_dir);
 }
 
-bool BundleCompiler::run(bool server)
+void BundleCompiler::scan(const char* source_dir)
 {
-	if (!_bundle_fs.exists(CROWN_DATA_DIRECTORY))
-		_bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
-
-	scan_source_dir("");
+	_source_fs.set_prefix(source_dir);
 
 	TempAllocator512 ta;
 	vector::push_back(_globs, DynamicString("*.tmp", ta));
@@ -134,21 +107,10 @@ bool BundleCompiler::run(bool server)
 		default_allocator().deallocate(data);
 	}
 
-	bool success = compile_all("linux");
-
-	if (server)
-	{
-		_console_server->register_command(StringId32("compile"), console_command_compile, this);
-		_console_server->listen(CROWN_DEFAULT_COMPILER_PORT, true);
-
-		while (true)
-			_console_server->update();
-	}
-
-	return success;
+	scan_source_dir("");
 }
 
-bool BundleCompiler::compile(const char* type, const char* name, const char* platform)
+bool BundleCompiler::compile(DiskFilesystem& bundle_fs, const char* type, const char* name, const char* platform)
 {
 	StringId64 _type(type);
 	StringId64 _name(name);
@@ -187,10 +149,10 @@ bool BundleCompiler::compile(const char* type, const char* name, const char* pla
 	{
 		CompileOptions opts(_source_fs, output, platform, &buf);
 		compile(_type, src_path.c_str(), opts);
-		File* outf = _bundle_fs.open(path.c_str(), FileOpenMode::WRITE);
+		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);
+		bundle_fs.close(*outf);
 		success = size == written;
 	}
 	else
@@ -201,8 +163,17 @@ bool BundleCompiler::compile(const char* type, const char* name, const char* pla
 	return success;
 }
 
-bool BundleCompiler::compile_all(const char* platform)
+bool BundleCompiler::compile(const char* bundle_dir, const char* platform)
 {
+	// Create bundle dir if necessary
+	DiskFilesystem bundle_fs(default_allocator());
+	bundle_fs.set_prefix(bundle_dir);
+	bundle_fs.create_directory("");
+
+	if (!bundle_fs.exists(CROWN_DATA_DIRECTORY))
+		bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
+
+	// Compile all changed resources
 	for (u32 i = 0; i < vector::size(_files); ++i)
 	{
 		const char* filename = _files[i].c_str();
@@ -229,7 +200,7 @@ bool BundleCompiler::compile_all(const char* platform)
 		strncpy(name, filename, size);
 		name[size] = '\0';
 
-		if (!compile(type, name, platform))
+		if (!compile(bundle_fs, type, name, platform))
 			return false;
 	}
 

+ 9 - 17
src/resource/bundle_compiler.h

@@ -6,7 +6,6 @@
 #pragma once
 
 #include "compile_options.h"
-#include "console_server.h"
 #include "container_types.h"
 #include "disk_filesystem.h"
 
@@ -23,37 +22,30 @@ class BundleCompiler
 	};
 
 	DiskFilesystem _source_fs;
-	DiskFilesystem _bundle_fs;
 	SortMap<StringId64, ResourceTypeData> _compilers;
 	Vector<DynamicString> _files;
 	Vector<DynamicString> _globs;
-	ConsoleServer* _console_server;
 
 	void compile(StringId64 type, const char* path, CompileOptions& opts);
-	// Returns the version of the compiler for @a type.
-	u32 version(StringId64 type);
+	void scan_source_dir(const char* path);
+	bool compile(DiskFilesystem& bundle_fs, const char* type, const char* name, const char* platform);
 
 public:
 
-	BundleCompiler(const char* source_dir, const char* bundle_dir, ConsoleServer& cs);
+	BundleCompiler();
 
-	/// Runs the compiler. If @a server is true, it listens for compile
-	/// requests on port CROWN_DEFAULT_SERVER_PORT.
-	bool run(bool server);
-
-	/// Compiles the resource (@a type, @a name) for the given @a platform.
-	/// Returns true on success, false otherwise.
-	bool compile(const char* type, const char* name, const char* platform);
+	/// Scans @a source_dir for resources.
+	void scan(const char* source_dir);
 
 	/// Compiles all the resources found in @a source_dir and puts them in @a bundle_dir.
 	/// Returns true on success, false otherwise.
-	bool compile_all(const char* platform);
-
-	/// Scans the source directory for resources.
-	void scan_source_dir(const char* path);
+	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.
+	u32 version(StringId64 type);
 };
 
 } // namespace crown