Daniele Bartolini 9 tahun lalu
induk
melakukan
68e5d587a3
3 mengubah file dengan 36 tambahan dan 15 penghapusan
  1. 11 9
      src/device/device.cpp
  2. 17 4
      src/resource/bundle_compiler.cpp
  3. 8 2
      src/resource/bundle_compiler.h

+ 11 - 9
src/device/device.cpp

@@ -392,13 +392,6 @@ void Device::run()
 	profiler_globals::init();
 
 	_console_server = CE_NEW(_allocator, ConsoleServer)(default_allocator());
-	_console_server->register_command(StringId32("script"), console_command_script, NULL);
-	_console_server->register_command(StringId32("reload"), console_command_reload, NULL);
-	_console_server->register_command(StringId32("pause"), console_command_pause, NULL);
-	_console_server->register_command(StringId32("unpause"), console_command_unpause, NULL);
-	_console_server->listen(_device_options._console_port, _device_options._wait_console);
-
-	bool do_continue = true;
 
 	namespace pcr = physics_config_resource;
 	namespace phr = physics_resource;
@@ -416,10 +409,12 @@ void Device::run()
 	namespace sar = sprite_animation_resource;
 	namespace cor = config_resource;
 
+	bool do_continue = true;
+
 #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
 	if (_device_options._do_compile)
 	{
-		_bundle_compiler = CE_NEW(_allocator, BundleCompiler)(_device_options._source_dir, _device_options._bundle_dir);
+		_bundle_compiler = CE_NEW(_allocator, BundleCompiler)(_device_options._source_dir, _device_options._bundle_dir, *_console_server);
 		_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);
@@ -435,12 +430,19 @@ void Device::run()
 		_bundle_compiler->register_compiler(RESOURCE_TYPE_SHADER,           RESOURCE_VERSION_SHADER,           shr::compile);
 		_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->compile_all(_device_options._platform) && _device_options._do_continue;
+
+		do_continue = _bundle_compiler->run(_device_options._server) && _device_options._do_continue;
 	}
 #endif // CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
 
 	if (do_continue)
 	{
+		_console_server->register_command(StringId32("script"), console_command_script, NULL);
+		_console_server->register_command(StringId32("reload"), console_command_reload, NULL);
+		_console_server->register_command(StringId32("pause"), console_command_pause, NULL);
+		_console_server->register_command(StringId32("unpause"), console_command_unpause, NULL);
+		_console_server->listen(_device_options._console_port, _device_options._wait_console);
+
 		CE_LOGI("Initializing Crown Engine %s...", version());
 
 		_resource_loader  = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);

+ 17 - 4
src/resource/bundle_compiler.cpp

@@ -73,16 +73,19 @@ static void console_command_compile(void* data, ConsoleServer& cs, TCPSocket cli
 		cs.error(client, "Failed to compile resource.");
 }
 
-BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
+BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir, ConsoleServer& cs)
 	: _source_fs(default_allocator(), source_dir)
 	, _bundle_fs(default_allocator(), bundle_dir)
 	, _compilers(default_allocator())
 	, _files(default_allocator())
 	, _globs(default_allocator())
-	, _console_server(default_allocator())
+	, _console_server(&cs)
 {
 	_bundle_fs.create_directory(bundle_dir);
+}
 
+bool BundleCompiler::run(bool server)
+{
 	if (!_bundle_fs.exists(CROWN_DATA_DIRECTORY))
 		_bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
 
@@ -131,8 +134,18 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 		default_allocator().deallocate(data);
 	}
 
-	_console_server.register_command(StringId32("compile"), console_command_compile, this);
-	_console_server.listen(CROWN_DEFAULT_COMPILER_PORT, false);
+	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;
 }
 
 bool BundleCompiler::compile(const char* type, const char* name, const char* platform)

+ 8 - 2
src/resource/bundle_compiler.h

@@ -27,7 +27,7 @@ class BundleCompiler
 	SortMap<StringId64, ResourceTypeData> _compilers;
 	Vector<DynamicString> _files;
 	Vector<DynamicString> _globs;
-	ConsoleServer _console_server;
+	ConsoleServer* _console_server;
 
 	void compile(StringId64 type, const char* path, CompileOptions& opts);
 	// Returns the version of the compiler for @a type.
@@ -35,8 +35,14 @@ class BundleCompiler
 
 public:
 
-	BundleCompiler(const char* source_dir, const char* bundle_dir);
+	BundleCompiler(const char* source_dir, const char* bundle_dir, ConsoleServer& cs);
 
+	/// 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);
 
 	/// Compiles all the resources found in @a source_dir and puts them in @a bundle_dir.