2
0
Эх сурвалжийг харах

Move console api to console_api.cpp

Daniele Bartolini 9 жил өмнө
parent
commit
32e8c81dc8

+ 98 - 0
src/device/console_api.cpp

@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "bundle_compiler.h"
+#include "console_server.h"
+#include "device.h"
+#include "dynamic_string.h"
+#include "json_object.h"
+#include "lua_environment.h"
+#include "sjson.h"
+#include "string_stream.h"
+#include "temp_allocator.h"
+
+namespace crown
+{
+static void console_command_script(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json)
+{
+	TempAllocator4096 ta;
+	JsonObject obj(ta);
+	DynamicString script(ta);
+	sjson::parse(json, obj);
+	sjson::parse_string(obj["script"], script);
+	device()->lua_environment()->execute_string(script.c_str());
+}
+
+static void console_command_reload(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json)
+{
+	TempAllocator4096 ta;
+	JsonObject obj(ta);
+	sjson::parse(json, obj);
+
+	DynamicString type(ta);
+	DynamicString name(ta);
+	sjson::parse_string(obj["resource_type"], type);
+	sjson::parse_string(obj["resource_name"], name);
+	logi("Reloading resource '%s.%s'", name.c_str(), type.c_str());
+	device()->reload(ResourceId(type.c_str()), ResourceId(name.c_str()));
+	logi("Reloaded resource '%s.%s'", name.c_str(), type.c_str());
+}
+
+static void console_command_pause(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* /*json*/)
+{
+	device()->pause();
+}
+
+static void console_command_unpause(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* /*json*/)
+{
+	device()->unpause();
+}
+
+static void console_command_compile(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));
+	}
+
+	logi("Compiling '%s'", id.c_str());
+	bool succ = device()->bundle_compiler()->compile(bundle_dir.c_str(), platform.c_str());
+
+	if (succ)
+		logi("Compiled '%s'", id.c_str());
+	else
+		loge("Error while compiling '%s'", id.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));
+	}
+}
+
+void load_console_api(ConsoleServer& cs)
+{
+	cs.register_command("script",  console_command_script);
+	cs.register_command("reload",  console_command_reload);
+	cs.register_command("pause",   console_command_pause);
+	cs.register_command("unpause", console_command_unpause);
+	cs.register_command("compile", console_command_compile);
+}
+
+} // namespace crown

+ 13 - 0
src/device/console_api.h

@@ -0,0 +1,13 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "console_server.h"
+
+namespace crown
+{
+/// Loads console API into console server @a cs.
+void load_console_api(ConsoleServer& cs);
+
+} // namespace crown

+ 6 - 13
src/device/console_server.cpp

@@ -140,27 +140,20 @@ void ConsoleServer::process(TCPSocket client, const char* json)
 	JsonObject obj(ta);
 	sjson::parse(json, obj);
 
-	CommandData cd;
-	cd.cmd = NULL;
-	cd.data = NULL;
-	cd = sort_map::get(_commands, sjson::parse_string_id(obj["type"]), cd);
+	CommandFunction cmd = sort_map::get(_commands, sjson::parse_string_id(obj["type"]), (CommandFunction)NULL);
 
-	if (cd.cmd)
-		cd.cmd(cd.data, *this, client, json);
+	if (cmd)
+		cmd(*this, client, json);
 	else
 		error(client, "Unknown command");
 }
 
-void ConsoleServer::register_command(StringId32 type, CommandFunction cmd, void* data)
+void ConsoleServer::register_command(const char* type, CommandFunction cmd)
 {
-	CE_ASSERT(!sort_map::has(_commands, type), "Command type already registered");
+	CE_ASSERT_NOT_NULL(type);
 	CE_ASSERT_NOT_NULL(cmd);
 
-	CommandData cd;
-	cd.cmd = cmd;
-	cd.data = data;
-
-	sort_map::set(_commands, type, cd);
+	sort_map::set(_commands, StringId32(type), cmd);
 	sort_map::sort(_commands);
 }
 

+ 3 - 9
src/device/console_server.h

@@ -16,17 +16,11 @@ namespace crown
 /// @ingroup Device
 class ConsoleServer
 {
-	typedef void (*CommandFunction)(void* data, ConsoleServer& cs, TCPSocket client, const char* json);
-
-	struct CommandData
-	{
-		CommandFunction cmd;
-		void* data;
-	};
+	typedef void (*CommandFunction)(ConsoleServer& cs, TCPSocket client, const char* json);
 
 	TCPSocket _server;
 	Vector<TCPSocket> _clients;
-	SortMap<StringId32, CommandData> _commands;
+	SortMap<StringId32, CommandFunction> _commands;
 
 	void add_client(TCPSocket socket);
 	ReadResult update_client(TCPSocket client);
@@ -59,7 +53,7 @@ public:
 	void success(TCPSocket client, const char* msg);
 
 	/// Registers the command @a type.
-	void register_command(StringId32 type, CommandFunction cmd, void* data);
+	void register_command(const char* type, CommandFunction cmd);
 };
 
 } // namespace crown

+ 4 - 78
src/device/device.cpp

@@ -8,6 +8,7 @@
 #include "bundle_compiler.h"
 #include "config.h"
 #include "config_resource.h"
+#include "console_api.h"
 #include "console_server.h"
 #include "device.h"
 #include "file.h"
@@ -139,77 +140,6 @@ private:
 	ProxyAllocator _allocator;
 };
 
-static void console_command_script(void* /*data*/, ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json)
-{
-	TempAllocator4096 ta;
-	JsonObject obj(ta);
-	DynamicString script(ta);
-	sjson::parse(json, obj);
-	sjson::parse_string(obj["script"], script);
-	device()->lua_environment()->execute_string(script.c_str());
-}
-
-static void console_command_reload(void* data, ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json)
-{
-	TempAllocator4096 ta;
-	JsonObject obj(ta);
-	sjson::parse(json, obj);
-
-	DynamicString type(ta);
-	DynamicString name(ta);
-	sjson::parse_string(obj["resource_type"], type);
-	sjson::parse_string(obj["resource_name"], name);
-	logi("Reloading resource '%s.%s'", name.c_str(), type.c_str());
-	((Device*)data)->reload(ResourceId(type.c_str()), ResourceId(name.c_str()));
-	logi("Reloaded resource '%s.%s'", name.c_str(), type.c_str());
-}
-
-static void console_command_pause(void* /*data*/, ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* /*json*/)
-{
-	device()->pause();
-}
-
-static void console_command_unpause(void* /*data*/, ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* /*json*/)
-{
-	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));
-	}
-
-	logi("Compiling '%s'", id.c_str());
-	bool succ = ((BundleCompiler*)data)->compile(bundle_dir.c_str(), platform.c_str());
-
-	if (succ)
-		logi("Compiled '%s'", id.c_str());
-	else
-		loge("Error while compiling '%s'", id.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)
@@ -360,6 +290,7 @@ bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16&
 void Device::run()
 {
 	_console_server = CE_NEW(_allocator, ConsoleServer)(default_allocator());
+	load_console_api(*_console_server);
 
 	namespace pcr = physics_config_resource;
 	namespace phr = physics_resource;
@@ -403,7 +334,6 @@ void Device::run()
 
 		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)
@@ -424,6 +354,8 @@ void Device::run()
 
 	if (do_continue)
 	{
+		_console_server->listen(_device_options._console_port, _device_options._wait_console);
+
 #if CROWN_PLATFORM_ANDROID
 		_bundle_filesystem = CE_NEW(_allocator, FilesystemApk)(default_allocator(), const_cast<AAssetManager*>((AAssetManager*)_device_options._asset_manager));
 #else
@@ -441,12 +373,6 @@ void Device::run()
 		_last_log = _bundle_filesystem->open(CROWN_LAST_LOG, FileOpenMode::WRITE);
 #endif // CROWN_PLATFORM_ANDROID
 
-		_console_server->register_command(StringId32("script"), console_command_script, NULL);
-		_console_server->register_command(StringId32("reload"), console_command_reload, this);
-		_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);
-
 		logi("Initializing Crown Engine %s...", version());
 
 		profiler_globals::init();