@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
31
31
#include " tool.h"
32
32
#include " serverobject.h"
33
33
#include " mapgen.h"
34
+ #include " json/json.h"
34
35
35
36
struct EnumString es_TileAnimationType[] =
36
37
{
@@ -998,3 +999,84 @@ bool read_schematic(lua_State *L, int index, DecoSchematic *dschem, Server *serv
998
999
999
1000
return true ;
1000
1001
}
1002
+
1003
+ /* *****************************************************************************/
1004
+ // Returns depth of json value tree
1005
+ static int push_json_value_getdepth (const Json::Value &value)
1006
+ {
1007
+ if (!value.isArray () && !value.isObject ())
1008
+ return 1 ;
1009
+
1010
+ int maxdepth = 0 ;
1011
+ for (Json::Value::const_iterator it = value.begin ();
1012
+ it != value.end (); ++it) {
1013
+ int elemdepth = push_json_value_getdepth (*it);
1014
+ if (elemdepth > maxdepth)
1015
+ maxdepth = elemdepth;
1016
+ }
1017
+ return maxdepth + 1 ;
1018
+ }
1019
+ // Recursive function to convert JSON --> Lua table
1020
+ static bool push_json_value_helper (lua_State *L, const Json::Value &value,
1021
+ int nullindex)
1022
+ {
1023
+ switch (value.type ()) {
1024
+ case Json::nullValue:
1025
+ default :
1026
+ lua_pushvalue (L, nullindex);
1027
+ break ;
1028
+ case Json::intValue:
1029
+ lua_pushinteger (L, value.asInt ());
1030
+ break ;
1031
+ case Json::uintValue:
1032
+ lua_pushinteger (L, value.asUInt ());
1033
+ break ;
1034
+ case Json::realValue:
1035
+ lua_pushnumber (L, value.asDouble ());
1036
+ break ;
1037
+ case Json::stringValue:
1038
+ {
1039
+ const char *str = value.asCString ();
1040
+ lua_pushstring (L, str ? str : " " );
1041
+ }
1042
+ break ;
1043
+ case Json::booleanValue:
1044
+ lua_pushboolean (L, value.asInt ());
1045
+ break ;
1046
+ case Json::arrayValue:
1047
+ lua_newtable (L);
1048
+ for (Json::Value::const_iterator it = value.begin ();
1049
+ it != value.end (); ++it) {
1050
+ push_json_value_helper (L, *it, nullindex);
1051
+ lua_rawseti (L, -2 , it.index () + 1 );
1052
+ }
1053
+ break ;
1054
+ case Json::objectValue:
1055
+ lua_newtable (L);
1056
+ for (Json::Value::const_iterator it = value.begin ();
1057
+ it != value.end (); ++it) {
1058
+ const char *str = it.memberName ();
1059
+ lua_pushstring (L, str ? str : " " );
1060
+ push_json_value_helper (L, *it, nullindex);
1061
+ lua_rawset (L, -3 );
1062
+ }
1063
+ break ;
1064
+ }
1065
+ return true ;
1066
+ }
1067
+ // converts JSON --> Lua table; returns false if lua stack limit exceeded
1068
+ // nullindex: Lua stack index of value to use in place of JSON null
1069
+ bool push_json_value (lua_State *L, const Json::Value &value, int nullindex)
1070
+ {
1071
+ if (nullindex < 0 )
1072
+ nullindex = lua_gettop (L) + 1 + nullindex;
1073
+
1074
+ int depth = push_json_value_getdepth (value);
1075
+
1076
+ // The maximum number of Lua stack slots used at each recursion level
1077
+ // of push_json_value_helper is 2, so make sure there a depth * 2 slots
1078
+ if (lua_checkstack (L, depth * 2 ))
1079
+ return push_json_value_helper (L, value, nullindex);
1080
+ else
1081
+ return false ;
1082
+ }
0 commit comments