commands now work

This commit is contained in:
darkrose 2017-05-30 16:32:08 +10:00
parent 02462fe854
commit 5d519799e6
7 changed files with 80 additions and 11 deletions

View File

@ -2,11 +2,34 @@
#include <stdarg.h> #include <stdarg.h>
#ifndef SERVER
#include "client.h"
#endif
#include "server.h" #include "server.h"
#include "environment.h" #include "environment.h"
#include "player.h" #include "player.h"
#include "sha1.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) int bridge_server_get_status(command_context_t *ctx, char* buff, int size)
{ {
if (!ctx) if (!ctx)

View File

@ -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 */ /* 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); m_chat_queue.push_back(message);
} }

View File

@ -134,10 +134,33 @@ static int command_set_detect(char* name)
return 1; 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) static int command_send(char* name, array_t *args)
{ {
command_print(NULL, 0, CN_WARN, "Server-side command not sent: '%s'",name); char* str;
int r;
str = array_join(args," ", 0);
if (!str)
return 1; return 1;
r = command_send_str(name,str);
free(str);
return r;
} }
static int command_set(command_context_t *ctx, array_t *args) 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) { if (!s->client && !ctx) {
command_send(name,args); mutex_unlock(command_data.mutex);
command_send_str(name,value);
return 0; return 0;
} }

View File

@ -173,6 +173,8 @@ int command_setpassword(command_context_t *ctx, array_t *args);
#ifdef __cplusplus #ifdef __cplusplus
} }
#include <string> #include <string>
class Client;
void bridge_register_client(Client *c);
std::string bridge_config_get(char* name); std::string bridge_config_get(char* name);
#endif #endif
@ -182,6 +184,7 @@ std::string bridge_config_get(char* name);
#else #else
#define EXTERNC #define EXTERNC
#endif #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_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_notify_player(command_context_t *ctx, char* name, char* str, ...);
EXTERNC int bridge_server_settime(command_context_t *ctx, uint32_t time); EXTERNC int bridge_server_settime(command_context_t *ctx, uint32_t time);

View File

@ -124,11 +124,16 @@ public:
if (fields["text"] == L"") if (fields["text"] == L"")
return; return;
if (fields["text"][0] == L'/') {
std::string m = wide_to_narrow(fields["text"]);
command_exec(NULL,(char*)m.c_str());
}else{
// Send to others // Send to others
m_client->sendChatMessage(fields["text"]); m_client->sendChatMessage(fields["text"]);
// Show locally // Show locally
m_client->addChatMessage(fields["text"]); m_client->addChatMessage(fields["text"]);
} }
}
std::string getForm() std::string getForm()
{ {
@ -686,6 +691,8 @@ void the_game(
MapDrawControl draw_control; MapDrawControl draw_control;
Client client(device, password, draw_control, sound); Client client(device, password, draw_control, sound);
bridge_register_client(&client);
drawLoadingScreen(device,narrow_to_wide(gettext("Resolving address..."))); drawLoadingScreen(device,narrow_to_wide(gettext("Resolving address...")));
uint16_t port = config_get_int("world.server.port"); uint16_t port = config_get_int("world.server.port");
if (!port) if (!port)
@ -703,6 +710,7 @@ void the_game(
{ {
errorstream<<"Couldn't resolve address"<<std::endl; errorstream<<"Couldn't resolve address"<<std::endl;
error_message = narrow_to_wide(gettext("Couldn't resolve address")); error_message = narrow_to_wide(gettext("Couldn't resolve address"));
bridge_register_client(NULL);
return; return;
} }
@ -781,6 +789,7 @@ void the_game(
error_message = narrow_to_wide(gettext("Connection timed out.")); error_message = narrow_to_wide(gettext("Connection timed out."));
errorstream<<"Timed out."<<std::endl; errorstream<<"Timed out."<<std::endl;
} }
bridge_register_client(NULL);
return; return;
} }
@ -788,8 +797,10 @@ void the_game(
Create the camera node Create the camera node
*/ */
Camera camera(smgr, draw_control, &client); Camera camera(smgr, draw_control, &client);
if (!camera.successfullyCreated(error_message)) if (!camera.successfullyCreated(error_message)) {
bridge_register_client(NULL);
return; return;
}
f32 camera_yaw = 0; // "right/left" f32 camera_yaw = 0; // "right/left"
f32 camera_pitch = 0; // "up/down" f32 camera_pitch = 0; // "up/down"
@ -2371,6 +2382,8 @@ void the_game(
clear_particles(); clear_particles();
bridge_register_client(NULL);
/* /*
Draw a "shutting down" screen, which will be shown while the map Draw a "shutting down" screen, which will be shown while the map
generator and other stuff quits generator and other stuff quits

View File

@ -1190,7 +1190,7 @@ int main(int argc, char *argv[])
// Save settings // Save settings
config_set("client.name", (char*)wide_to_narrow(menudata.name).c_str()); config_set("client.name", (char*)wide_to_narrow(menudata.name).c_str());
config_set("world.admin.name",(char*)wide_to_narrow(menudata.name).c_str()); config_set("world.server.admin",(char*)wide_to_narrow(menudata.name).c_str());
config_save(NULL,NULL,NULL); config_save(NULL,NULL,NULL);
@ -1281,6 +1281,8 @@ int main(int argc, char *argv[])
} // Menu-game loop } // Menu-game loop
config_save(NULL,NULL,NULL);
delete input; delete input;
if (sound != NULL) if (sound != NULL)

View File

@ -4541,7 +4541,11 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
ctx.bridge_player = player; ctx.bridge_player = player;
if (command_exec(&ctx,(char*)message.c_str())) { if (command_exec(&ctx,(char*)message.c_str())) {
if (ctx.flags) {
line += narrow_to_wide(ctx.reply);
}else{
line += L"Server: Invalid command"; line += L"Server: Invalid command";
}
send_to_sender = true; send_to_sender = true;
}else if (ctx.flags) { }else if (ctx.flags) {
std::wstring reply(narrow_to_wide(ctx.reply)); std::wstring reply(narrow_to_wide(ctx.reply));