Update: Translations from eints
[openttd-github.git] / src / script / api / script_admin.cpp
blob858b855231b7aadf13bfca99f9096b85bbf27c59
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file script_admin.cpp Implementation of ScriptAdmin. */
10 #include "../../stdafx.h"
11 #include "script_admin.hpp"
12 #include "script_log.hpp"
13 #include "../../network/network_admin.h"
14 #include "../script_instance.hpp"
15 #include "../../string_func.h"
16 #include "../../3rdparty/nlohmann/json.hpp"
18 #include "../../safeguards.h"
20 /**
21 * Convert a Squirrel structure into a JSON object.
23 * This function is not "static", so it can be tested in unittests.
25 * @param json The resulting JSON object.
26 * @param vm The VM to operate on.
27 * @param index The index we are currently working for.
28 * @param depth The current depth in the squirrel struct.
29 * @return True iff the conversion was successful.
31 bool ScriptAdminMakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index, int depth = 0)
33 if (depth == SQUIRREL_MAX_DEPTH) {
34 ScriptLog::Error("Send parameters can only be nested to 25 deep. No data sent."); // SQUIRREL_MAX_DEPTH = 25
35 return false;
38 switch (sq_gettype(vm, index)) {
39 case OT_INTEGER: {
40 SQInteger res;
41 sq_getinteger(vm, index, &res);
43 json = res;
44 return true;
47 case OT_STRING: {
48 const SQChar *buf;
49 sq_getstring(vm, index, &buf);
51 json = std::string(buf);
52 return true;
55 case OT_ARRAY: {
56 json = nlohmann::json::array();
58 sq_pushnull(vm);
59 while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
60 nlohmann::json tmp;
62 bool res = ScriptAdminMakeJSON(tmp, vm, -1, depth + 1);
63 sq_pop(vm, 2);
64 if (!res) {
65 sq_pop(vm, 1);
66 return false;
69 json.push_back(tmp);
71 sq_pop(vm, 1);
72 return true;
75 case OT_TABLE: {
76 json = nlohmann::json::object();
78 sq_pushnull(vm);
79 while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
80 sq_tostring(vm, -2);
81 const SQChar *buf;
82 sq_getstring(vm, -1, &buf);
83 std::string key = std::string(buf);
84 sq_pop(vm, 1);
86 nlohmann::json value;
87 bool res = ScriptAdminMakeJSON(value, vm, -1, depth + 1);
88 sq_pop(vm, 2);
89 if (!res) {
90 sq_pop(vm, 1);
91 return false;
94 json[key] = value;
96 sq_pop(vm, 1);
97 return true;
100 case OT_BOOL: {
101 SQBool res;
102 sq_getbool(vm, index, &res);
104 json = res ? true : false;
105 return true;
108 case OT_NULL: {
109 json = nullptr;
110 return true;
113 default:
114 ScriptLog::Error("You tried to send an unsupported type. No data sent.");
115 return false;
119 /* static */ SQInteger ScriptAdmin::Send(HSQUIRRELVM vm)
121 if (sq_gettop(vm) - 1 != 1) return sq_throwerror(vm, "wrong number of parameters");
123 if (sq_gettype(vm, 2) != OT_TABLE) {
124 return sq_throwerror(vm, "ScriptAdmin::Send requires a table as first parameter. No data sent.");
127 nlohmann::json json;
128 if (!ScriptAdminMakeJSON(json, vm, -1)) {
129 sq_pushinteger(vm, 0);
130 return 1;
133 NetworkAdminGameScript(json.dump());
135 sq_pushinteger(vm, 1);
136 return 1;