From 5d519799e63480c4dffe5c7b3f7bf9a0d73e46b5 Mon Sep 17 00:00:00 2001 From: darkrose Date: Tue, 30 May 2017 16:32:08 +1000 Subject: [PATCH] commands now work --- src/bridge_temp.cpp | 23 +++++++++++++++++++++++ src/client.cpp | 2 +- src/command.c | 30 +++++++++++++++++++++++++++--- src/common.h | 3 +++ src/game.cpp | 23 ++++++++++++++++++----- src/main.cpp | 4 +++- src/server.cpp | 6 +++++- 7 files changed, 80 insertions(+), 11 deletions(-) diff --git a/src/bridge_temp.cpp b/src/bridge_temp.cpp index 610cb97..bd3d540 100644 --- a/src/bridge_temp.cpp +++ b/src/bridge_temp.cpp @@ -2,11 +2,34 @@ #include +#ifndef SERVER +#include "client.h" +#endif #include "server.h" #include "environment.h" #include "player.h" #include "sha1.h" +#ifndef SERVER +static Client *bridge_client = NULL; + +void bridge_register_client(Client *c) +{ + bridge_client = c; +} + +int bridge_client_send_msg(char* str) +{ + if (!str) + return 1; + std::wstring m = narrow_to_wide(str); + + bridge_client->sendChatMessage(m); + + return 0; +} +#endif + int bridge_server_get_status(command_context_t *ctx, char* buff, int size) { if (!ctx) diff --git a/src/client.cpp b/src/client.cpp index 857f840..66c0119 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1237,7 +1237,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) } /* because I can't remember which random stream prints to stdout */ - printf("CHAT: %s\n",wide_to_narrow(message).c_str()); + vlprint(CN_CHAT,(char*)wide_to_narrow(message).c_str()); m_chat_queue.push_back(message); } diff --git a/src/command.c b/src/command.c index cf96b77..435ec2b 100644 --- a/src/command.c +++ b/src/command.c @@ -134,10 +134,33 @@ static int command_set_detect(char* name) return 1; } +static int command_send_str(char* name, char* args) +{ +#ifndef SERVER + char buff[1024]; + + if (snprintf(buff,1024,"/%s %s",name,args) >= 1024) + return 1; + + return bridge_client_send_msg(buff); +#else + command_print(NULL, 0, CN_WARN, "Server-side command not sent: '%s'",name); +#endif + return 1; +} + static int command_send(char* name, array_t *args) { - command_print(NULL, 0, CN_WARN, "Server-side command not sent: '%s'",name); - return 1; + char* str; + int r; + + str = array_join(args," ", 0); + if (!str) + return 1; + + r = command_send_str(name,str); + free(str); + return r; } static int command_set(command_context_t *ctx, array_t *args) @@ -363,7 +386,8 @@ int command_apply(command_context_t *ctx, char* name, char* value) } if (!s->client && !ctx) { - command_send(name,args); + mutex_unlock(command_data.mutex); + command_send_str(name,value); return 0; } diff --git a/src/common.h b/src/common.h index 24aeef2..29963d4 100644 --- a/src/common.h +++ b/src/common.h @@ -173,6 +173,8 @@ int command_setpassword(command_context_t *ctx, array_t *args); #ifdef __cplusplus } #include +class Client; +void bridge_register_client(Client *c); std::string bridge_config_get(char* name); #endif @@ -182,6 +184,7 @@ std::string bridge_config_get(char* name); #else #define EXTERNC #endif +EXTERNC int bridge_client_send_msg(char* str); EXTERNC int bridge_server_get_status(command_context_t *ctx, char* buff, int size); EXTERNC int bridge_server_notify_player(command_context_t *ctx, char* name, char* str, ...); EXTERNC int bridge_server_settime(command_context_t *ctx, uint32_t time); diff --git a/src/game.cpp b/src/game.cpp index 73f644b..6fc194d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -124,10 +124,15 @@ public: if (fields["text"] == L"") return; - // Send to others - m_client->sendChatMessage(fields["text"]); - // Show locally - m_client->addChatMessage(fields["text"]); + if (fields["text"][0] == L'/') { + std::string m = wide_to_narrow(fields["text"]); + command_exec(NULL,(char*)m.c_str()); + }else{ + // Send to others + m_client->sendChatMessage(fields["text"]); + // Show locally + m_client->addChatMessage(fields["text"]); + } } std::string getForm() @@ -686,6 +691,8 @@ void the_game( MapDrawControl draw_control; Client client(device, password, draw_control, sound); + bridge_register_client(&client); + drawLoadingScreen(device,narrow_to_wide(gettext("Resolving address..."))); uint16_t port = config_get_int("world.server.port"); if (!port) @@ -703,6 +710,7 @@ void the_game( { errorstream<<"Couldn't resolve address"<