-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdumpmod.lua
More file actions
executable file
·146 lines (119 loc) · 3.54 KB
/
dumpmod.lua
File metadata and controls
executable file
·146 lines (119 loc) · 3.54 KB
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
#!/usr/local/bin/lua
local do_unbundle = true
local json = require 'dkjson'
local fs = require 'lfs'
local janky = require 'jankybundle'
local json_keyorder = require 'keyorder'
function is_nil_or_empty(str)
return str == nil or str:len() == 0
end
function write_file(name, content)
local tmp = io.open(name, 'w')
if not tmp then
print(name, fs.currentdir())
error('failed to open file ' .. name .. ' in dir ' .. fs.currentdir())
end
tmp:write(content)
tmp:close()
end
function write_luaxmlstate(obj)
if not is_nil_or_empty(obj.LuaScript) then
if do_unbundle then
local unbundle_code = janky.tryunbundle(obj.LuaScript)
if unbundle_code then
write_file('script.lua', unbundle_code)
else
write_file('script.lua', obj.LuaScript)
end
else
write_file('script.lua', obj.LuaScript)
end
end
if not is_nil_or_empty(obj.XmlUI) then write_file('ui.xml', obj.XmlUI) end
if not is_nil_or_empty(obj.LuaScriptState) then
local script_state_obj = json.decode(obj.LuaScriptState)
local keyorder = keyorder_for_placement_bag_state(script_state_obj)
write_file('state.json',
json.encode(script_state_obj,
{indent = true, keyorder = keyorder}))
end
obj.LuaScript = ""
obj.LuaScriptState = ""
obj.XmlUI = ""
end
function sanitized(str)
return str:gsub('[^%a%d-._]', '_')
end
function safe_name(key, obj)
assert(type(key) == 'string')
return ('%s-%s-%s'):format(
#(obj.Nickname) > 0 and sanitized(obj.Nickname) or '',
#(obj.Name) > 0 and sanitized(obj.Name) or '',
key)
end
function recursive_write_luaxmlstate(obj)
local States = obj.States
obj.States = nil
local ContainedObjects = obj.ContainedObjects
if obj.ContainedObjects then
obj.ContainedObjects = {}
end
write_luaxmlstate(obj)
if obj.Name == 'Deck' then
setmetatable(obj.CustomDeck, {__jsonorder = keyorder_for_CustomDeck(obj.CustomDeck)})
end
write_file('object.json', json.encode(obj, {indent = true, keyorder = json_keyorder}))
if ContainedObjects then
fs.mkdir('ContainedObjects')
fs.chdir('ContainedObjects')
for i,v in ipairs(ContainedObjects) do
local safe_dir_name = safe_name(tostring(i), v)
fs.mkdir(safe_dir_name)
fs.chdir(safe_dir_name)
recursive_write_luaxmlstate(v)
fs.chdir('..')
end
fs.chdir('..')
end
if States then
fs.mkdir('States')
fs.chdir('States')
for k,v in pairs(States) do
local safe_dir_name = safe_name(k, v)
fs.mkdir(safe_dir_name)
fs.chdir(safe_dir_name)
recursive_write_luaxmlstate(v)
fs.chdir('..')
end
fs.chdir('..')
end
end
---
local args = {...}
if #args == 0 then
print('Usage: lua dumpmod.lua <mod_file.json> <output_dir>')
os.exit(0)
end
local input_json_filename = args[1]
local output_dir = args[2]
local dest_mode = fs.attributes(output_dir, 'mode')
if dest_mode then
error('destination exists. Delete it before running dumpmod.lua')
end
local file = io.open(input_json_filename):read('a')
local game_object = json.decode(file)
fs.mkdir(output_dir)
fs.chdir(output_dir)
fs.mkdir('ObjectStates')
fs.chdir('ObjectStates')
for i,obj in ipairs(game_object.ObjectStates) do
local safe_dir_name = safe_name(tostring(i), obj)
fs.mkdir(safe_dir_name)
fs.chdir(safe_dir_name)
recursive_write_luaxmlstate(obj)
fs.chdir('..')
end
fs.chdir('..')
write_luaxmlstate(game_object)
game_object.ObjectStates = {}
write_file('global.json', json.encode(game_object, {indent = true, keyorder = json_keyorder}))