1 /* $Id: script_admin.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file script_admin.cpp Implementation of ScriptAdmin. */
12 #include "../../stdafx.h"
13 #include "script_admin.hpp"
14 #include "script_log.hpp"
15 #include "../../network/network_admin.h"
16 #include "../script_instance.hpp"
17 #include "../../string_func.h"
19 #include "../../safeguards.h"
21 /* static */ bool ScriptAdmin::MakeJSON(HSQUIRRELVM vm
, SQInteger index
, int max_depth
, std::string
&data
)
24 ScriptLog::Error("Send parameters can only be nested to 25 deep. No data sent."); // SQUIRREL_MAX_DEPTH = 25
28 switch (sq_gettype(vm
, index
)) {
31 sq_getinteger(vm
, index
, &res
);
34 seprintf(buf
, lastof(buf
), "%d", (int32
)res
);
41 sq_getstring(vm
, index
, &buf
);
43 size_t len
= strlen(buf
) + 1;
45 ScriptLog::Error("Maximum string length is 254 chars. No data sent.");
49 data
= std::string("\"") + buf
+ "\"";
58 while (SQ_SUCCEEDED(sq_next(vm
, index
- 1))) {
59 if (!first
) data
+= ", ";
60 if (first
) first
= false;
64 bool res
= MakeJSON(vm
, -1, max_depth
- 1, tmp
);
82 while (SQ_SUCCEEDED(sq_next(vm
, index
- 1))) {
83 if (!first
) data
+= ", ";
84 if (first
) first
= false;
89 /* Store the key + value */
90 bool res
= MakeJSON(vm
, -2, max_depth
- 1, key
) && MakeJSON(vm
, -1, max_depth
- 1, value
);
96 data
+= key
+ ": " + value
;
105 sq_getbool(vm
, index
, &res
);
122 ScriptLog::Error("You tried to send an unsupported type. No data sent.");
127 /* static */ SQInteger
ScriptAdmin::Send(HSQUIRRELVM vm
)
129 if (sq_gettop(vm
) - 1 != 1) return sq_throwerror(vm
, "wrong number of parameters");
131 if (sq_gettype(vm
, 2) != OT_TABLE
) {
132 return sq_throwerror(vm
, "ScriptAdmin::Send requires a table as first parameter. No data sent.");
136 ScriptAdmin::MakeJSON(vm
, -1, SQUIRREL_MAX_DEPTH
, json
);
138 #ifdef ENABLE_NETWORK
139 if (json
.length() > NETWORK_GAMESCRIPT_JSON_LENGTH
) {
140 ScriptLog::Error("You are trying to send a table that is too large to the AdminPort. No data sent.");
141 sq_pushinteger(vm
, 0);
145 NetworkAdminGameScript(json
.c_str());
146 #endif /* ENABLE_NETWORK */
148 sq_pushinteger(vm
, 1);