Codechange: keep how we convert string <-> JSON private (#11269)

This commit is contained in:
Patric Stout
2023-09-08 19:03:10 +02:00
committed by GitHub
parent d725fa14a2
commit 00f13282a9
5 changed files with 156 additions and 162 deletions

View File

@@ -14,9 +14,22 @@
#include "../script_instance.hpp"
#include "../../string_func.h"
#include <nlohmann/json.hpp>
#include "../../safeguards.h"
/* static */ bool ScriptAdmin::MakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index, int depth)
/**
* Convert a Squirrel structure into a JSON object.
*
* This function is not "static", so it can be tested in unittests.
*
* @param json The resulting JSON object.
* @param vm The VM to operate on.
* @param index The index we are currently working for.
* @param depth The current depth in the squirrel struct.
* @return True iff the conversion was successful.
*/
bool ScriptAdminMakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index, int depth = 0)
{
if (depth == SQUIRREL_MAX_DEPTH) {
ScriptLog::Error("Send parameters can only be nested to 25 deep. No data sent."); // SQUIRREL_MAX_DEPTH = 25
@@ -47,7 +60,7 @@
while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
nlohmann::json tmp;
bool res = MakeJSON(tmp, vm, -1, depth + 1);
bool res = ScriptAdminMakeJSON(tmp, vm, -1, depth + 1);
sq_pop(vm, 2);
if (!res) {
sq_pop(vm, 1);
@@ -72,7 +85,7 @@
std::string key = std::string(buf);
nlohmann::json value;
bool res = MakeJSON(value, vm, -1, depth + 1);
bool res = ScriptAdminMakeJSON(value, vm, -1, depth + 1);
sq_pop(vm, 2);
if (!res) {
sq_pop(vm, 1);
@@ -113,7 +126,7 @@
}
nlohmann::json json;
if (!ScriptAdmin::MakeJSON(json, vm, -1)) {
if (!ScriptAdminMakeJSON(json, vm, -1)) {
sq_pushinteger(vm, 0);
return 1;
}

View File

@@ -11,7 +11,6 @@
#define SCRIPT_ADMIN_HPP
#include "script_object.hpp"
#include <nlohmann/json.hpp>
/**
* Class that handles communication with the AdminPort.
@@ -36,17 +35,6 @@ public:
*/
static bool Send(void *table);
#endif /* DOXYGEN_API */
protected:
/**
* Convert a Squirrel structure into a JSON object.
* @param json The resulting JSON object.
* @param vm The VM to operate on.
* @param index The index we are currently working for.
* @param depth The current depth in the squirrel struct.
* @return True iff the conversion was successful.
*/
static bool MakeJSON(nlohmann::json &data, HSQUIRRELVM vm, SQInteger index, int depth = 0);
};
#endif /* SCRIPT_ADMIN_HPP */

View File

@@ -20,6 +20,8 @@
#include "../../engine_cmd.h"
#include "table/strings.h"
#include <nlohmann/json.hpp>
#include "../../safeguards.h"
bool ScriptEventEnginePreview::IsEngineValid() const
@@ -127,33 +129,12 @@ ScriptEventAdminPort::ScriptEventAdminPort(const std::string &json) :
json(json)
{
}
SQInteger ScriptEventAdminPort::GetObject(HSQUIRRELVM vm)
{
auto json = nlohmann::json::parse(this->json, nullptr, false);
if (!json.is_object()) {
ScriptLog::Error("The root element in the JSON data from AdminPort has to be an object.");
sq_pushnull(vm);
return 1;
}
auto top = sq_gettop(vm);
if (!this->ReadValue(vm, json)) {
/* Rewind the stack, removing anything that might be left on top. */
sq_settop(vm, top);
ScriptLog::Error("Received invalid JSON data from AdminPort.");
sq_pushnull(vm);
return 1;
}
return 1;
}
bool ScriptEventAdminPort::ReadValue(HSQUIRRELVM vm, nlohmann::json &json)
/**
* Convert a JSON part fo Squirrel.
* @param vm The VM used.
* @param json The JSON part to convert to Squirrel.
*/
static bool ScriptEventAdminPortReadValue(HSQUIRRELVM vm, nlohmann::json &json)
{
switch (json.type()) {
case nlohmann::json::value_t::null:
@@ -181,7 +162,7 @@ bool ScriptEventAdminPort::ReadValue(HSQUIRRELVM vm, nlohmann::json &json)
for (auto &[key, value] : json.items()) {
sq_pushstring(vm, key.data(), key.size());
if (!this->ReadValue(vm, value)) {
if (!ScriptEventAdminPortReadValue(vm, value)) {
return false;
}
@@ -193,7 +174,7 @@ bool ScriptEventAdminPort::ReadValue(HSQUIRRELVM vm, nlohmann::json &json)
sq_newarray(vm, 0);
for (auto &value : json) {
if (!this->ReadValue(vm, value)) {
if (!ScriptEventAdminPortReadValue(vm, value)) {
return false;
}
@@ -209,3 +190,28 @@ bool ScriptEventAdminPort::ReadValue(HSQUIRRELVM vm, nlohmann::json &json)
return true;
}
SQInteger ScriptEventAdminPort::GetObject(HSQUIRRELVM vm)
{
auto json = nlohmann::json::parse(this->json, nullptr, false);
if (!json.is_object()) {
ScriptLog::Error("The root element in the JSON data from AdminPort has to be an object.");
sq_pushnull(vm);
return 1;
}
auto top = sq_gettop(vm);
if (!ScriptEventAdminPortReadValue(vm, json)) {
/* Rewind the stack, removing anything that might be left on top. */
sq_settop(vm, top);
ScriptLog::Error("Received invalid JSON data from AdminPort.");
sq_pushnull(vm);
return 1;
}
return 1;
}

View File

@@ -14,8 +14,6 @@
#include "script_goal.hpp"
#include "script_window.hpp"
#include <nlohmann/json.hpp>
/**
* Event Vehicle Crash, indicating a vehicle of yours is crashed.
* It contains the crash site, the crashed vehicle and the reason for the crash.
@@ -912,13 +910,6 @@ public:
private:
std::string json; ///< The JSON string.
/**
* Convert a JSON part fo Squirrel.
* @param vm The VM used.
* @param json The JSON part to convert to Squirrel.
*/
bool ReadValue(HSQUIRRELVM vm, nlohmann::json &json);
};
/**