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

This commit is contained in:
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);
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) {

View File

@ -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 */

View File

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

View File

@ -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;

View File

@ -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.