-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua2dat.lua
177 lines (140 loc) · 4.14 KB
/
lua2dat.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
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
if ( tonumber(_VERSION:match("%d+%.?%d?")) < 5.3 ) then
error("You need to use at least LUA version 5.3!")
end
local in_file = assert(arg[1], "\n\n[ERR] no input\n")
local out_file = arg[2] or "OUT.DAT"
local fix_file = arg[3]
-- binary write
local w
local function sint64(v) return string.pack("<i8", v) end
local function uint32(v) return string.pack("<I4", v) end
local function sint32(v) return string.pack("<i4", v) end
local function uint16(v) return string.pack("<H", v) end
local function uint8(v) return string.pack("B", v) end
local function float(v) return string.pack("f", v) end
-- utils
-- Get the OS type
local function get_os_type()
local dir_separator = package.config:sub(1,1)
if ( dir_separator == "\\\\" or dir_separator == "\\") then
return "win"
elseif ( dir_separator == "/" ) then
return "unix"
end
return "unknown"
end
-- Get the path the script is running in
local function get_script_path()
local os_type = get_os_type()
local path_str = debug.getinfo(2, "S").source:sub(2)
if ( os_type == "win" ) then
return path_str:match("(.*[/\\])") or ""
elseif ( os_type == "unix" ) then
return path_str:match("(.*/)") or "."
end
return ""
end
-- path
local script_path = get_script_path()
-- dictionaries
local dict_i = {name = "internal"} -- from current file
local dict_e = {name = "external"} -- from dict_ext
--local dict_p = {name = "parsed"} -- from dict_parsed
local dict_t = {name = "types"} -- from dict_type
local dict_f = {name = "fixes"} -- from optional arg[3] (list_duped_ids_lang.lua)
local dict_u = {name = "unknown"} -- TODO: collect all missed keys
local function read_tag(t, l)
local tag = dict_e[t.n]
if nil == tag then
--print(t.n)
tag = tonumber(t.n:sub(2, -1))
end
w:write(uint32(tag))
if t.vars then
w:write(uint32(#t.vars))
for _, v in ipairs(t.vars) do
local typ = dict_t[v.t]
local name = dict_e[v.n]
if not name then
name = tonumber(v.n:sub(2))
end
local value
if typ == 1 then -- INTEGER
value = sint32(v.v)
elseif typ == 2 then -- FLOAT
value = float(v.v)
elseif typ == 5 then -- STRING
value = uint32(v.v)
elseif typ == 6 then -- BOOL
value = uint32(v.v and 1 or 0)
elseif typ == 7 then -- INT64
value = sint64(v.v)
elseif typ == 8 then -- TRANSLATE
value = uint32(v.v)
end
assert(value)
w:write(uint32(name))
w:write(uint32(typ))
w:write(value)
end
else
w:write(uint32(0))
end
if t.tags then
w:write(uint32(#t.tags))
for _, v in ipairs(t.tags) do
read_tag(v, l+1)
end
else
w:write(uint32(0))
end
end
-------------------------------------------------------------------------------
-- generate types dictionary
local d = dofile(script_path .. "dict_types.lua")
for i = 1, #d, 2 do
dict_t[d[i+1]] = d[i]
end
-- generate external dictionary
d = dofile(script_path .. "dict_external.lua")
for i = 1, #d, 2 do
dict_e[d[i+1]] = d[i]
end
--[=[ update external dictionary
d = dofile(script_path .. "dict_parsed.lua")
for i = 1, #d, 2 do
dict_e[d[i+1]] = d[i]
end
]=]
-- generate fix dictionary
if fix_file then
dict_f = dofile(fix_file)
end
-- generate internal dictionary
local LS = {} -- [idx]=key
local L, content = dofile(in_file)
for k, v in pairs(L) do
if type(k) == "number" then
LS[v[1]] = k
-- try to fix string
local f = dict_f[k]
if f then
L[k][2] = f
end
end
end
-- check
assert(L.size == #LS)
w = assert(io.open(out_file, "w+b"))
w:write(uint8(6))
-- write dictionary
w:write(uint32(L.size))
for i = 1, #LS do
local idx = LS[i]
local str = L[idx][2]
w:write(uint32(idx))
w:write(uint16(#str))
w:write(str)
end
read_tag(content, 0)
w:close()