Update readme.md
[openttd-joker.git] / src / script / api / script_admin.cpp
bloba79368d0b731565411ecd16600795de995ac6229
1 /* $Id: script_admin.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
3 /*
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/>.
8 */
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)
23 if (max_depth == 0) {
24 ScriptLog::Error("Send parameters can only be nested to 25 deep. No data sent."); // SQUIRREL_MAX_DEPTH = 25
25 return false;
28 switch (sq_gettype(vm, index)) {
29 case OT_INTEGER: {
30 SQInteger res;
31 sq_getinteger(vm, index, &res);
33 char buf[10];
34 seprintf(buf, lastof(buf), "%d", (int32)res);
35 data = buf;
36 return true;
39 case OT_STRING: {
40 const SQChar *buf;
41 sq_getstring(vm, index, &buf);
43 size_t len = strlen(buf) + 1;
44 if (len >= 255) {
45 ScriptLog::Error("Maximum string length is 254 chars. No data sent.");
46 return false;
49 data = std::string("\"") + buf + "\"";
50 return true;
53 case OT_ARRAY: {
54 data = "[ ";
56 bool first = true;
57 sq_pushnull(vm);
58 while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
59 if (!first) data += ", ";
60 if (first) first = false;
62 std::string tmp;
64 bool res = MakeJSON(vm, -1, max_depth - 1, tmp);
65 sq_pop(vm, 2);
66 if (!res) {
67 sq_pop(vm, 1);
68 return false;
70 data += tmp;
72 sq_pop(vm, 1);
73 data += " ]";
74 return true;
77 case OT_TABLE: {
78 data = "{ ";
80 bool first = true;
81 sq_pushnull(vm);
82 while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
83 if (!first) data += ", ";
84 if (first) first = false;
86 std::string key;
87 std::string value;
89 /* Store the key + value */
90 bool res = MakeJSON(vm, -2, max_depth - 1, key) && MakeJSON(vm, -1, max_depth - 1, value);
91 sq_pop(vm, 2);
92 if (!res) {
93 sq_pop(vm, 1);
94 return false;
96 data += key + ": " + value;
98 sq_pop(vm, 1);
99 data += " }";
100 return true;
103 case OT_BOOL: {
104 SQBool res;
105 sq_getbool(vm, index, &res);
107 if (res) {
108 data = "true";
109 return true;
112 data = "false";
113 return true;
116 case OT_NULL: {
117 data = "null";
118 return true;
121 default:
122 ScriptLog::Error("You tried to send an unsupported type. No data sent.");
123 return false;
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.");
135 std::string json;
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);
142 return 1;
145 NetworkAdminGameScript(json.c_str());
146 #endif /* ENABLE_NETWORK */
148 sq_pushinteger(vm, 1);
149 return 1;