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/>.
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"
17 #include "../../safeguards.h"
19 /* static */ bool ScriptAdmin::MakeJSON(HSQUIRRELVM vm
, SQInteger index
, int max_depth
, std::string
&data
)
22 ScriptLog::Error("Send parameters can only be nested to 25 deep. No data sent."); // SQUIRREL_MAX_DEPTH = 25
26 switch (sq_gettype(vm
, index
)) {
29 sq_getinteger(vm
, index
, &res
);
32 seprintf(buf
, lastof(buf
), "%d", (int32
)res
);
39 sq_getstring(vm
, index
, &buf
);
41 size_t len
= strlen(buf
) + 1;
43 ScriptLog::Error("Maximum string length is 254 chars. No data sent.");
47 data
= std::string("\"") + buf
+ "\"";
56 while (SQ_SUCCEEDED(sq_next(vm
, index
- 1))) {
57 if (!first
) data
+= ", ";
58 if (first
) first
= false;
62 bool res
= MakeJSON(vm
, -1, max_depth
- 1, tmp
);
80 while (SQ_SUCCEEDED(sq_next(vm
, index
- 1))) {
81 if (!first
) data
+= ", ";
82 if (first
) first
= false;
87 /* Store the key + value */
88 bool res
= MakeJSON(vm
, -2, max_depth
- 1, key
) && MakeJSON(vm
, -1, max_depth
- 1, value
);
94 data
+= key
+ ": " + value
;
103 sq_getbool(vm
, index
, &res
);
120 ScriptLog::Error("You tried to send an unsupported type. No data sent.");
125 /* static */ SQInteger
ScriptAdmin::Send(HSQUIRRELVM vm
)
127 if (sq_gettop(vm
) - 1 != 1) return sq_throwerror(vm
, "wrong number of parameters");
129 if (sq_gettype(vm
, 2) != OT_TABLE
) {
130 return sq_throwerror(vm
, "ScriptAdmin::Send requires a table as first parameter. No data sent.");
134 ScriptAdmin::MakeJSON(vm
, -1, SQUIRREL_MAX_DEPTH
, json
);
136 if (json
.length() > NETWORK_GAMESCRIPT_JSON_LENGTH
) {
137 ScriptLog::Error("You are trying to send a table that is too large to the AdminPort. No data sent.");
138 sq_pushinteger(vm
, 0);
142 NetworkAdminGameScript(json
);
144 sq_pushinteger(vm
, 1);