aboutsummaryrefslogtreecommitdiff
path: root/primedev/scripts/scriptjson.cpp
blob: 8959bf47120dc2abcf359f4f03b6876ba0a43e2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "squirrel/squirrel.h"

#include "rapidjson/error/en.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

#ifdef _MSC_VER
#undef GetObject // fuck microsoft developers
#endif

template <ScriptContext context> void
DecodeJsonArray(HSquirrelVM* sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* arr)
{
	g_pSquirrel<context>->newarray(sqvm, 0);

	for (auto& itr : arr->GetArray())
	{
		switch (itr.GetType())
		{
		case rapidjson::kObjectType:
			DecodeJsonTable<context>(sqvm, &itr);
			g_pSquirrel<context>->arrayappend(sqvm, -2);
			break;
		case rapidjson::kArrayType:
			DecodeJsonArray<context>(sqvm, &itr);
			g_pSquirrel<context>->arrayappend(sqvm, -2);
			break;
		case rapidjson::kStringType:
			g_pSquirrel<context>->pushstring(sqvm, itr.GetString(), -1);
			g_pSquirrel<context>->arrayappend(sqvm, -2);
			break;
		case rapidjson::kTrueType:
		case rapidjson::kFalseType:
			g_pSquirrel<context>->pushbool(sqvm, itr.GetBool());
			g_pSquirrel<context>->arrayappend(sqvm, -2);
			break;
		case rapidjson::kNumberType:
			if (itr.IsDouble() || itr.IsFloat())
				g_pSquirrel<context>->pushfloat(sqvm, itr.GetFloat());
			else
				g_pSquirrel<context>->pushinteger(sqvm, itr.GetInt());
			g_pSquirrel<context>->arrayappend(sqvm, -2);
			break;
		case rapidjson::kNullType:
			break;
		}
	}
}

template <ScriptContext context> void
DecodeJsonTable(HSquirrelVM* sqvm, rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* obj)
{
	g_pSquirrel<context>->newtable(sqvm);

	for (auto itr = obj->MemberBegin(); itr != obj->MemberEnd(); itr++)
	{
		switch (itr->value.GetType())
		{
		case rapidjson::kObjectType:
			g_pSquirrel<context>->pushstring(sqvm, itr->name.GetString(), -1);
			DecodeJsonTable<context>(
				sqvm, (rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>*)&itr->value);
			g_pSquirrel<context>->newslot(sqvm, -3, false);
			break;
		case rapidjson::kArrayType:
			g_pSquirrel<context>->pushstring(sqvm, itr->name.GetString(), -1);
			DecodeJsonArray<context>(
				sqvm, (rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>*)&itr->value);
			g_pSquirrel<context>->newslot(sqvm, -3, false);
			break;
		case rapidjson::kStringType:
			g_pSquirrel<context>->pushstring(sqvm, itr->name.GetString(), -1);
			g_pSquirrel<context>->pushstring(sqvm, itr->value.GetString(), -1);

			g_pSquirrel<context>->newslot(sqvm, -3, false);
			break;
		case rapidjson::kTrueType:
		case rapidjson::kFalseType:
			g_pSquirrel<context>->pushstring(sqvm, itr->name.GetString(), -1);
			g_pSquirrel<context>->pushbool(sqvm, itr->value.GetBool());
			g_pSquirrel<context>->newslot(sqvm, -3, false);
			break;
		case rapidjson::kNumberType:
			if (itr->value.IsDouble() || itr->value.IsFloat())
			{
				g_pSquirrel<context>->pushstring(sqvm, itr->name.GetString(), -1);
				g_pSquirrel<context>->pushfloat(sqvm, itr->value.GetFloat());
			}
			else
			{
				g_pSquirrel<context>->pushstring(sqvm, itr->name.GetString(), -1);
				g_pSquirrel<context>->pushinteger(sqvm, itr->value.GetInt());
			}
			g_pSquirrel<context>->newslot(sqvm, -3, false);
			break;
		case rapidjson::kNullType:
			break;
		}
	}
}

template <ScriptContext context> void EncodeJSONTable(
	SQTable* table,
	rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* obj,
	rapidjson::MemoryPoolAllocator<SourceAllocator>& allocator)
{
	for (int i = 0; i < table->_numOfNodes; i++)
	{
		tableNode* node = &table->_nodes[i];
		if (node->key._Type == OT_STRING)
		{
			rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>> newObj(rapidjson::kObjectType);
			rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>> newArray(rapidjson::kArrayType);

			switch (node->val._Type)
			{
			case OT_STRING:
				obj->AddMember(
					rapidjson::StringRef(node->key._VAL.asString->_val), rapidjson::StringRef(node->val._VAL.asString->_val), allocator);
				break;
			case OT_INTEGER:
				obj->AddMember(rapidjson::StringRef(node->key._VAL.asString->_val), node->val._VAL.asInteger, allocator);
				break;
			case OT_FLOAT:
				obj->AddMember(rapidjson::StringRef(node->key._VAL.asString->_val), node->val._VAL.asFloat, allocator);
				break;
			case OT_BOOL:
				if (node->val._VAL.asInteger)
				{
					obj->AddMember(rapidjson::StringRef(node->key._VAL.asString->_val), true, allocator);
				}
				else
				{
					obj->AddMember(rapidjson::StringRef(node->key._VAL.asString->_val), false, allocator);
				}
				break;
			case OT_TABLE:
				EncodeJSONTable<context>(node->val._VAL.asTable, &newObj, allocator);
				obj->AddMember(rapidjson::StringRef(node->key._VAL.asString->_val), newObj, allocator);
				break;
			case OT_ARRAY:
				EncodeJSONArray<context>(node->val._VAL.asArray, &newArray, allocator);
				obj->AddMember(rapidjson::StringRef(node->key._VAL.asString->_val), newArray, allocator);
				break;
			default:
				spdlog::warn("SQ_EncodeJSON: squirrel type {} not supported", SQTypeNameFromID(node->val._Type));
				break;
			}
		}
	}
}

template <ScriptContext context> void EncodeJSONArray(
	SQArray* arr,
	rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>* obj,
	rapidjson::MemoryPoolAllocator<SourceAllocator>& allocator)
{
	for (int i = 0; i < arr->_usedSlots; i++)
	{
		SQObject* node = &arr->_values[i];

		rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>> newObj(rapidjson::kObjectType);
		rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>> newArray(rapidjson::kArrayType);

		switch (node->_Type)
		{
		case OT_STRING:
			obj->PushBack(rapidjson::StringRef(node->_VAL.asString->_val), allocator);
			break;
		case OT_INTEGER:
			obj->PushBack(node->_VAL.asInteger, allocator);
			break;
		case OT_FLOAT:
			obj->PushBack(node->_VAL.asFloat, allocator);
			break;
		case OT_BOOL:
			if (node->_VAL.asInteger)
				obj->PushBack(rapidjson::StringRef("true"), allocator);
			else
				obj->PushBack(rapidjson::StringRef("false"), allocator);
			break;
		case OT_TABLE:
			EncodeJSONTable<context>(node->_VAL.asTable, &newObj, allocator);
			obj->PushBack(newObj, allocator);
			break;
		case OT_ARRAY:
			EncodeJSONArray<context>(node->_VAL.asArray, &newArray, allocator);
			obj->PushBack(newArray, allocator);
			break;
		default:
			spdlog::info("SQ encode Json type {} not supported", SQTypeNameFromID(node->_Type));
		}
	}
}

ADD_SQFUNC(
	"table",
	DecodeJSON,
	"string json, bool fatalParseErrors = false",
	"converts a json string to a squirrel table",
	ScriptContext::UI | ScriptContext::CLIENT | ScriptContext::SERVER)
{
	const char* pJson = g_pSquirrel<context>->getstring(sqvm, 1);
	const bool bFatalParseErrors = g_pSquirrel<context>->getbool(sqvm, 2);

	rapidjson_document doc;
	doc.Parse(pJson);
	if (doc.HasParseError())
	{
		g_pSquirrel<context>->newtable(sqvm);

		std::string sErrorString = fmt::format(
			"Failed parsing json file: encountered parse error \"{}\" at offset {}",
			GetParseError_En(doc.GetParseError()),
			doc.GetErrorOffset());

		if (bFatalParseErrors)
		{
			g_pSquirrel<context>->raiseerror(sqvm, sErrorString.c_str());
			return SQRESULT_ERROR;
		}

		spdlog::warn(sErrorString);
		return SQRESULT_NOTNULL;
	}

	DecodeJsonTable<context>(sqvm, (rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<SourceAllocator>>*)&doc);
	return SQRESULT_NOTNULL;
}

ADD_SQFUNC(
	"string",
	EncodeJSON,
	"table data",
	"converts a squirrel table to a json string",
	ScriptContext::UI | ScriptContext::CLIENT | ScriptContext::SERVER)
{
	rapidjson_document doc;
	doc.SetObject();

	// temp until this is just the func parameter type
	HSquirrelVM* vm = (HSquirrelVM*)sqvm;
	SQTable* table = vm->_stackOfCurrentFunction[1]._VAL.asTable;
	EncodeJSONTable<context>(table, &doc, doc.GetAllocator());

	rapidjson::StringBuffer buffer;
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
	doc.Accept(writer);
	const char* pJsonString = buffer.GetString();

	g_pSquirrel<context>->pushstring(sqvm, pJsonString, -1);
	return SQRESULT_NOTNULL;
}