1
0
Fork 0

Codechange: [Network] Let admin-game script use std::string

pull/9312/head
rubidium42 2021-05-14 17:33:29 +02:00 committed by rubidium42
parent 29f2bd27c4
commit e58581f1f8
5 changed files with 15 additions and 22 deletions

View File

@ -514,11 +514,9 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_GAMESCRIPT(Pack
{ {
if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED); if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
char json[NETWORK_GAMESCRIPT_JSON_LENGTH]; std::string json = p->Recv_string(NETWORK_GAMESCRIPT_JSON_LENGTH);
p->Recv_string(json, sizeof(json)); DEBUG(net, 6, "[admin] GameScript JSON from '%s' (%s): %s", this->admin_name.c_str(), this->admin_version.c_str(), json.c_str());
DEBUG(net, 6, "[admin] GameScript JSON from '%s' (%s): %s", this->admin_name.c_str(), this->admin_version.c_str(), json);
Game::NewEvent(new ScriptEventAdminPort(json)); Game::NewEvent(new ScriptEventAdminPort(json));
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
@ -561,12 +559,12 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendConsole(const char *origi
* Send GameScript JSON output. * Send GameScript JSON output.
* @param json The JSON string. * @param json The JSON string.
*/ */
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendGameScript(const char *json) NetworkRecvStatus ServerNetworkAdminSocketHandler::SendGameScript(const std::string_view json)
{ {
/* At the moment we cannot transmit anything larger than MTU. So we limit /* At the moment we cannot transmit anything larger than MTU. So we limit
* the maximum amount of json data that can be sent. Account also for * the maximum amount of json data that can be sent. Account also for
* the trailing \0 of the string */ * the trailing \0 of the string */
if (strlen(json) + 1 >= NETWORK_GAMESCRIPT_JSON_LENGTH) return NETWORK_RECV_STATUS_OKAY; if (json.size() + 1 >= NETWORK_GAMESCRIPT_JSON_LENGTH) return NETWORK_RECV_STATUS_OKAY;
Packet *p = new Packet(ADMIN_PACKET_SERVER_GAMESCRIPT); Packet *p = new Packet(ADMIN_PACKET_SERVER_GAMESCRIPT);
@ -941,7 +939,7 @@ void NetworkAdminConsole(const char *origin, const char *string)
* Send GameScript JSON to the admin network (if they did opt in for the respective update). * Send GameScript JSON to the admin network (if they did opt in for the respective update).
* @param json The JSON data as received from the GameScript. * @param json The JSON data as received from the GameScript.
*/ */
void NetworkAdminGameScript(const char *json) void NetworkAdminGameScript(const std::string_view json)
{ {
for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
if (as->update_frequency[ADMIN_UPDATE_GAMESCRIPT] & ADMIN_FREQUENCY_AUTOMATIC) { if (as->update_frequency[ADMIN_UPDATE_GAMESCRIPT] & ADMIN_FREQUENCY_AUTOMATIC) {

View File

@ -64,7 +64,7 @@ public:
NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const std::string &msg, int64 data); NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const std::string &msg, int64 data);
NetworkRecvStatus SendRcon(uint16 colour, const char *command); NetworkRecvStatus SendRcon(uint16 colour, const char *command);
NetworkRecvStatus SendConsole(const char *origin, const char *command); NetworkRecvStatus SendConsole(const char *origin, const char *command);
NetworkRecvStatus SendGameScript(const char *json); NetworkRecvStatus SendGameScript(const std::string_view json);
NetworkRecvStatus SendCmdNames(); NetworkRecvStatus SendCmdNames();
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp); NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp);
NetworkRecvStatus SendRconEnd(const char *command); NetworkRecvStatus SendRconEnd(const char *command);
@ -110,7 +110,7 @@ void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_i
void NetworkAdminUpdate(AdminUpdateFrequency freq); void NetworkAdminUpdate(AdminUpdateFrequency freq);
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string); void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string);
void NetworkAdminConsole(const char *origin, const char *string); void NetworkAdminConsole(const char *origin, const char *string);
void NetworkAdminGameScript(const char *json); void NetworkAdminGameScript(const std::string_view json);
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp); void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp);
#endif /* NETWORK_ADMIN_H */ #endif /* NETWORK_ADMIN_H */

View File

@ -139,7 +139,7 @@
return 1; return 1;
} }
NetworkAdminGameScript(json.c_str()); NetworkAdminGameScript(json);
sq_pushinteger(vm, 1); sq_pushinteger(vm, 1);
return 1; return 1;

View File

@ -118,23 +118,18 @@ bool ScriptEventCompanyAskMerger::AcceptMerger()
return ScriptObject::DoCommand(0, this->owner, 0, CMD_BUY_COMPANY); return ScriptObject::DoCommand(0, this->owner, 0, CMD_BUY_COMPANY);
} }
ScriptEventAdminPort::ScriptEventAdminPort(const char *json) : ScriptEventAdminPort::ScriptEventAdminPort(const std::string &json) :
ScriptEvent(ET_ADMIN_PORT), ScriptEvent(ET_ADMIN_PORT),
json(stredup(json)) json(json)
{ {
} }
ScriptEventAdminPort::~ScriptEventAdminPort()
{
free(this->json);
}
#define SKIP_EMPTY(p) while (*(p) == ' ' || *(p) == '\n' || *(p) == '\r') (p)++; #define SKIP_EMPTY(p) while (*(p) == ' ' || *(p) == '\n' || *(p) == '\r') (p)++;
#define RETURN_ERROR(stack) { ScriptLog::Error("Received invalid JSON data from AdminPort."); if (stack != 0) sq_pop(vm, stack); return nullptr; } #define RETURN_ERROR(stack) { ScriptLog::Error("Received invalid JSON data from AdminPort."); if (stack != 0) sq_pop(vm, stack); return nullptr; }
SQInteger ScriptEventAdminPort::GetObject(HSQUIRRELVM vm) SQInteger ScriptEventAdminPort::GetObject(HSQUIRRELVM vm)
{ {
const char *p = this->json; const char *p = this->json.c_str();
if (this->ReadTable(vm, p) == nullptr) { if (this->ReadTable(vm, p) == nullptr) {
sq_pushnull(vm); sq_pushnull(vm);
@ -168,7 +163,8 @@ const char *ScriptEventAdminPort::ReadString(HSQUIRRELVM vm, const char *p)
p++; p++;
} }
sq_pushstring(vm, value, p - value); size_t len = p - value;
sq_pushstring(vm, value, len);
p++; // Step past the end-of-string marker (") p++; // Step past the end-of-string marker (")
return p; return p;

View File

@ -837,8 +837,7 @@ public:
/** /**
* @param json The JSON string which got sent. * @param json The JSON string which got sent.
*/ */
ScriptEventAdminPort(const char *json); ScriptEventAdminPort(const std::string &json);
~ScriptEventAdminPort();
/** /**
* Convert an ScriptEvent to the real instance. * Convert an ScriptEvent to the real instance.
@ -853,7 +852,7 @@ public:
SQInteger GetObject(HSQUIRRELVM vm); SQInteger GetObject(HSQUIRRELVM vm);
private: private:
char *json; ///< The JSON string. std::string json; ///< The JSON string.
/** /**
* Read a table from a JSON string. * Read a table from a JSON string.