diff options
author | rubidium42 <rubidium@openttd.org> | 2021-05-14 17:33:29 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-05-30 10:15:22 +0200 |
commit | e58581f1f825da4d318b0b09fc12726788e95ccc (patch) | |
tree | 9d99b3939df44d3afd43fde4ac396095af5bf617 /src | |
parent | 29f2bd27c44ef51f8af1299cf16012a4fa89cc61 (diff) | |
download | openttd-e58581f1f825da4d318b0b09fc12726788e95ccc.tar.xz |
Codechange: [Network] Let admin-game script use std::string
Diffstat (limited to 'src')
-rw-r--r-- | src/network/network_admin.cpp | 12 | ||||
-rw-r--r-- | src/network/network_admin.h | 4 | ||||
-rw-r--r-- | src/script/api/script_admin.cpp | 2 | ||||
-rw-r--r-- | src/script/api/script_event_types.cpp | 14 | ||||
-rw-r--r-- | src/script/api/script_event_types.hpp | 5 |
5 files changed, 15 insertions, 22 deletions
diff --git a/src/network/network_admin.cpp b/src/network/network_admin.cpp index c58996711..82c9881bf 100644 --- a/src/network/network_admin.cpp +++ b/src/network/network_admin.cpp @@ -514,11 +514,9 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_GAMESCRIPT(Pack { 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); + DEBUG(net, 6, "[admin] GameScript JSON from '%s' (%s): %s", this->admin_name.c_str(), this->admin_version.c_str(), json.c_str()); Game::NewEvent(new ScriptEventAdminPort(json)); return NETWORK_RECV_STATUS_OKAY; @@ -561,12 +559,12 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendConsole(const char *origi * Send GameScript JSON output. * @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 * the maximum amount of json data that can be sent. Account also for * 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); @@ -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). * @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()) { if (as->update_frequency[ADMIN_UPDATE_GAMESCRIPT] & ADMIN_FREQUENCY_AUTOMATIC) { diff --git a/src/network/network_admin.h b/src/network/network_admin.h index 0e443d08f..189acb6f9 100644 --- a/src/network/network_admin.h +++ b/src/network/network_admin.h @@ -64,7 +64,7 @@ public: NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const std::string &msg, int64 data); NetworkRecvStatus SendRcon(uint16 colour, 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 SendCmdLogging(ClientID client_id, const CommandPacket *cp); NetworkRecvStatus SendRconEnd(const char *command); @@ -110,7 +110,7 @@ void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_i void NetworkAdminUpdate(AdminUpdateFrequency freq); void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, 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); #endif /* NETWORK_ADMIN_H */ diff --git a/src/script/api/script_admin.cpp b/src/script/api/script_admin.cpp index 8f7371266..24aa0bcaf 100644 --- a/src/script/api/script_admin.cpp +++ b/src/script/api/script_admin.cpp @@ -139,7 +139,7 @@ return 1; } - NetworkAdminGameScript(json.c_str()); + NetworkAdminGameScript(json); sq_pushinteger(vm, 1); return 1; diff --git a/src/script/api/script_event_types.cpp b/src/script/api/script_event_types.cpp index 84d6d99cd..3cc5287eb 100644 --- a/src/script/api/script_event_types.cpp +++ b/src/script/api/script_event_types.cpp @@ -118,23 +118,18 @@ bool ScriptEventCompanyAskMerger::AcceptMerger() 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), - json(stredup(json)) + json(json) { } -ScriptEventAdminPort::~ScriptEventAdminPort() -{ - free(this->json); -} - #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; } SQInteger ScriptEventAdminPort::GetObject(HSQUIRRELVM vm) { - const char *p = this->json; + const char *p = this->json.c_str(); if (this->ReadTable(vm, p) == nullptr) { sq_pushnull(vm); @@ -168,7 +163,8 @@ const char *ScriptEventAdminPort::ReadString(HSQUIRRELVM vm, const char *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 (") return p; diff --git a/src/script/api/script_event_types.hpp b/src/script/api/script_event_types.hpp index b6ed7bfbc..3ed6c5b10 100644 --- a/src/script/api/script_event_types.hpp +++ b/src/script/api/script_event_types.hpp @@ -837,8 +837,7 @@ public: /** * @param json The JSON string which got sent. */ - ScriptEventAdminPort(const char *json); - ~ScriptEventAdminPort(); + ScriptEventAdminPort(const std::string &json); /** * Convert an ScriptEvent to the real instance. @@ -853,7 +852,7 @@ public: SQInteger GetObject(HSQUIRRELVM vm); private: - char *json; ///< The JSON string. + std::string json; ///< The JSON string. /** * Read a table from a JSON string. |