Browse Source

Delete console_api.*

Daniele Bartolini 9 years ago
parent
commit
8f9e241db6
5 changed files with 57 additions and 96 deletions
  1. 0 69
      src/device/console_api.cpp
  2. 0 13
      src/device/console_api.h
  3. 54 10
      src/device/device.cpp
  4. 3 1
      src/device/device.h
  5. 0 3
      src/resource/data_compiler.cpp

+ 0 - 69
src/device/console_api.cpp

@@ -1,69 +0,0 @@
-/*
- * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "console_server.h"
-#include "data_compiler.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, void* /*user_data*/)
-{
-	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(ConsoleServer& cs, TCPSocket client, const char* json, void* /*user_data*/)
-{
-	TempAllocator4096 ta;
-	JsonObject obj(ta);
-	JsonArray args(ta);
-
-	sjson::parse(json, obj);
-	sjson::parse_array(obj["args"], args);
-
-	DynamicString cmd(ta);
-	sjson::parse_string(args[0], cmd);
-
-	if (cmd == "pause")
-		device()->pause();
-	else if (cmd == "unpause")
-		device()->unpause();
-	else if (cmd == "reload")
-	{
-		if (array::size(args) != 3)
-		{
-			cs.error(client, "Usage: reload type name");
-			return;
-		}
-
-		DynamicString type(ta);
-		DynamicString name(ta);
-		sjson::parse_string(args[1], type);
-		sjson::parse_string(args[2], name);
-
-		device()->reload(ResourceId(type.c_str()), ResourceId(name.c_str()));
-	}
-}
-
-void load_console_api(ConsoleServer& cs)
-{
-	cs.register_command("script",  console_command_script, NULL);
-	cs.register_command("command", console_command, NULL);
-}
-
-} // namespace crown

+ 0 - 13
src/device/console_api.h

@@ -1,13 +0,0 @@
-/*
- * Copyright (c) 2012-2017 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

+ 54 - 10
src/device/device.cpp

@@ -7,8 +7,6 @@
 #include "audio.h"
 #include "config.h"
 #include "config_resource.h"
-#include "console_api.h"
-#include "console_server.h"
 #include "console_server.h"
 #include "device.h"
 #include "device_event_queue.h"
@@ -142,10 +140,56 @@ private:
 	ProxyAllocator _allocator;
 };
 
-Device::Device(const DeviceOptions& opts)
+static void console_command_script(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json, void* user_data)
+{
+	TempAllocator4096 ta;
+	JsonObject obj(ta);
+	DynamicString script(ta);
+
+	sjson::parse(json, obj);
+	sjson::parse_string(obj["script"], script);
+
+	((Device*)user_data)->_lua_environment->execute_string(script.c_str());
+}
+
+static void console_command(ConsoleServer& cs, TCPSocket client, const char* json, void* user_data)
+{
+	TempAllocator4096 ta;
+	JsonObject obj(ta);
+	JsonArray args(ta);
+
+	sjson::parse(json, obj);
+	sjson::parse_array(obj["args"], args);
+
+	DynamicString cmd(ta);
+	sjson::parse_string(args[0], cmd);
+
+	if (cmd == "pause")
+		device()->pause();
+	else if (cmd == "unpause")
+		device()->unpause();
+	else if (cmd == "reload")
+	{
+		if (array::size(args) != 3)
+		{
+			cs.error(client, "Usage: reload type name");
+			return;
+		}
+
+		DynamicString type(ta);
+		DynamicString name(ta);
+		sjson::parse_string(args[1], type);
+		sjson::parse_string(args[2], name);
+
+		((Device*)user_data)->reload(ResourceId(type.c_str()), ResourceId(name.c_str()));
+	}
+}
+
+Device::Device(const DeviceOptions& opts, ConsoleServer& cs)
 	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
 	, _device_options(opts)
 	, _boot_config(default_allocator())
+	, _console_server(&cs)
 	, _bundle_filesystem(NULL)
 	, _last_log(NULL)
 	, _resource_loader(NULL)
@@ -282,10 +326,10 @@ bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16&
 
 void Device::run()
 {
-	console_server_globals::init();
-	load_console_api(*console_server());
+	_console_server->register_command("script",  console_command_script, this);
+	_console_server->register_command("command", console_command, this);
 
-	console_server()->listen(_device_options._console_port, _device_options._wait_console);
+	_console_server->listen(_device_options._console_port, _device_options._wait_console);
 
 	namespace cor = config_resource_internal;
 	namespace ftr = font_resource_internal;
@@ -422,7 +466,7 @@ void Device::run()
 		_time_since_start += _last_delta_time;
 
 		profiler_globals::clear();
-		console_server()->update();
+		_console_server->update();
 
 		RECORD_FLOAT("device.dt", _last_delta_time);
 		RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
@@ -489,8 +533,6 @@ void Device::run()
 
 	profiler_globals::shutdown();
 
-	console_server_globals::shutdown();
-
 	_allocator.clear();
 }
 
@@ -643,10 +685,12 @@ Device* _device = NULL;
 void run(const DeviceOptions& opts)
 {
 	CE_ASSERT(_device == NULL, "Crown already initialized");
-	_device = new (_buffer) Device(opts);
+	console_server_globals::init();
+	_device = new (_buffer) Device(opts, *console_server());
 	_device->run();
 	_device->~Device();
 	_device = NULL;
+	console_server_globals::shutdown();
 }
 
 Device* device()

+ 3 - 1
src/device/device.h

@@ -9,6 +9,7 @@
 #include "boot_config.h"
 #include "compiler_types.h"
 #include "config.h"
+#include "console_server.h"
 #include "container_types.h"
 #include "device_options.h"
 #include "display.h"
@@ -39,6 +40,7 @@ struct Device
 
 	const DeviceOptions& _device_options;
 	BootConfig _boot_config;
+	ConsoleServer* _console_server;
 	Filesystem* _bundle_filesystem;
 	File* _last_log;
 	ResourceLoader* _resource_loader;
@@ -68,7 +70,7 @@ struct Device
 
 public:
 
-	Device(const DeviceOptions& opts);
+	Device(const DeviceOptions& opts, ConsoleServer& cs);
 
 	/// Runs the engine.
 	void run();

+ 0 - 3
src/resource/data_compiler.cpp

@@ -7,7 +7,6 @@
 #include "compile_options.h"
 #include "config.h"
 #include "config_resource.h"
-#include "console_api.h"
 #include "console_server.h"
 #include "data_compiler.h"
 #include "device_options.h"
@@ -532,8 +531,6 @@ int main_data_compiler(int argc, char** argv)
 		return EXIT_FAILURE;
 
 	console_server_globals::init();
-	load_console_api(*console_server());
-
 	console_server()->listen(CROWN_DEFAULT_COMPILER_PORT, opts._wait_console);
 
 	namespace cor = config_resource_internal;