-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMissionJSON.lua
48 lines (39 loc) · 1.32 KB
/
MissionJSON.lua
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
function SeemsLikeValidMissionJSON(missionJson)
-- A very simple sanity check to see if the string provided begins with "{" and ends with "}"
return (type(missionJson) == "string" and string.sub(missionJson, 1, 1) == "{" and string.sub(missionJson, -1, -1) == "}")
end
function IsValidUGC(ugc)
-- A very simple sanity check to see if the flag we set is there,
return (type(ugc) == "table" and ugc.__IsValidUGC)
end
--[[
MissionJSON rules:
1: Objects cannot contain the keys "x", "y" or "z" in which case they are Vector3 data
From this, a logical process for determining the type of an object or array can be found.
Simply check if you contain the key "x", if you do, you're a Vector3!
]]
local TraverseMissionJSON -- Forward Declaration
-- This function assumes me is a value which can be passed to pairs()
TraverseMissionJSON = function(me)
if me.x then
-- If I contain the key "x" then I am Vector3 data
return vec3(me.x, me.y, me.z)
else
for k,v in pairs(me) do
if type(v) == "table" then
me[k] = TraverseMissionJSON(v)
end
end
return me
end
end
function ParseMissionJSON(missionJson)
local output
local success, err = pcall(function()
output = TraverseMissionJSON(json.decode(missionJson))
end)
if type(output) == "table" then
output.__IsValidUGC = success
end
return success and output, err
end