-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
370 lines (298 loc) · 8.59 KB
/
init.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
-- reslister --
--local lfs = require'lfs'
-- uncomment for debug
--if not pcall(require,'vstruct') and not package.path:find([[;.\?\init.lua]],1,true) then
-- package.path=package.path..[[;.\?\init.lua]]
--end
require'minigcompat'
require'binfuncs'
require'mdlinspect'
local function wrap(ok, ...)
if ok then return ... end
print""
print("FAIL", ...)
end
local function SAFE(func, ...)
return wrap(xpcall(func, debug.traceback, ...))
end
local fmt_bspzip = false
local t = {...}
for i=#t,1,-1 do
if t[i]:lower()=="--format=bspzip" then
table.remove(t,i)
fmt_bspzip = true
end
end
local file, respath,out = unpack(t)
if not file and not respath then
print[[Usage: reslister [--format=bspzip] "c:\mymap\map.vmf" "c:\mymap\resources" "c:\mymap\reslist.txt"]]
return
end
out = out or [[reslist.txt]]
local function respath_has(relpath)
local p = ("/%s"):format(relpath)
p=p:gsub("[%\\%/][%\\%/]?","%/")
local p = ("%s%s"):format(respath, p)
--print(p)
if p:find"mapdata/sprit" then error(p) end
if os.rename(p, p) then return p end
end
local processors = {}
local reslist = {}
function _G.RESADD(path)
path=path:gsub("[%\\%/][%\\%/]?","%/")
path=path:lower()
reslist[path] = true
end
local parsers = {}
local warned = {}
local function PARSE(path)
local ext = path:match'[^%.]+$'
local func = parsers[ext:lower()]
if func then
func(path)
else
if not warned[ext] then
warned[ext] = true
print("did not parse", ext)
end
end
end
-- processors for map format entries --
local function dogeneric(path)
local full = respath_has(path)
if full then
RESADD(path)
PARSE(full)
return true
end
end
-- parsers for different filetypes --
local vmt_key_whitelist = {}
local w= {'basetexture','basetexture2','ambientoccltexture','ambientocclusiontexture',
'emissiveblendbasetexture','emissiveblendtexture','emissiveblendflowtexture','emissiveblendtexture',
'phongexponenttexture','texture', 'detail','selfillumtexture',
'blendmodulatetexture', 'bumpmap',
'normalmap', 'parallaxmap', 'heightmap',
'selfillummask', 'lightwarptexture',
'envmap', 'envmapmask', 'displacementmap',
'reflecttexture', 'refracttexture',
'refracttinttexture', 'dudvmap',
'bottommaterial', 'underwateroverlay'}
for k, v in next, w do
vmt_key_whitelist[v] = true
end
local function vmtlineparse(l)
local L = l
-- keys we are interested in start with $...
local pos = l:find("$", 1, true)
if not pos then return end
-- ... but not all keys start with $
if l:sub(1, pos):find"[a-zA-Z]" then return end
-- fuzzy comments
local commentpos = l:find("//", 1, true)
if commentpos and commentpos > pos then return end
-- keys we are interested in start with this pattern
local _, stoppos, key = l:find("([a-zA-Z]+)", pos + 1)
if not key then
print("???", l)
return
end
local likely_texture_check=false
key = key:lower()
if not vmt_key_whitelist[key] then
if key:find"texture$" then
print("Unknown texture key in a material",key)
likely_texture_check=true
else
return
end
end
-- cleanup rest of line
l = l:sub(stoppos + 1, -1):gsub("[\r\n]*$", ''):gsub("^[\t%s]+", ""):gsub("[\t%s]+$", "")
-- attempt finding value using various patterns
local val = l:match([["%s%s?"([^"]+)"]]) or l:match'"([^"]+)"%s*$'
val = val or l
assert(not val:find('"', 1, true))
if likely_texture_check and not val:find("/",2,true) then
print("\t Discarding key",key,"value invalid for a texture:",val)
return
end
--print("process",val)
if key:find("material", 1, true) then
processors.material(val)
else
processors.texture(val)
end
end
function parsers:vtf()end -- no
function parsers:phy()end
function parsers:vvd()end
function parsers:vtx()end
function parsers:vmt()
for l in io.lines(self) do
vmtlineparse(l)
end
end
local function parse_mdl(f, fp)
local mdl, err = mdlinspect.Open(f)
if not mdl then
print("FAIL", fp, err)
end
mdl:ParseHeader()
local mdls = mdl:IncludedModels()
for k, v in next, mdls do
print("NOT IMPLEMENTED", k, v)
end
local materials = mdl:Textures()
local paths = mdl:TextureDirs()
for _,matdat in next,materials do
local material = matdat[1]
local found
for _,path in next,paths do
local found1 = dogeneric(("materials/%s/%s.vmt"):format(path,material))
local found2 = dogeneric(("materials/%s/%s.vtf"):format(path,material))
local found3 = dogeneric(("materials/%s/%s.hdr.vtf"):format(path,material))
if found1 or found2 or found3 then
--print("FOUND",path,material)
found = true
break
end
end
if not found then
print("notfound",fp,material)
end
end
end
function parsers:mdl()
local f = assert(io.open(self, 'rb'))
SAFE(parse_mdl, f, self)
f:close()
end
function processors:material()
local path = ("materials/%s.vmt"):format(self)
if dogeneric(path) then return end
assert(not self:lower():find("%.[a-z][a-z][a-z]$",-4),self)
end
function processors:texture()
if dogeneric("materials/"..self) then return end
if not self:lower():find("%.[a-z][a-z][a-z]$",-4) then
local gotvmt = dogeneric(("materials/%s.vmt"):format(self))
local gotvtf = dogeneric(("materials/%s.vtf"):format(self))
local gotvtfhdr = dogeneric(("materials/%s.hdr.vtf"):format(self))
if gotvmt or gotvtf or gotvtfhdr then return end
if dogeneric(("materials/%s.jpg"):format(self)) then return end
if dogeneric(("materials/%s.png"):format(self)) then return end
end
end
function processors:message()
if self:find"%.[a-zA-Z][a-zA-Z][a-zA-Z]$" then
if dogeneric(("sound/%s"):format(self)) then return end
else
local found = dogeneric(("sound/%s.wav"):format(self)) or dogeneric(("sound/%s.mp3"):format(self)) or dogeneric(("sound/%s.ogg"):format(self))
if found then return end
end
end
--TODO?? find .wav/.ogg/.mp3 instead
processors.noise1 = processors.message
processors.noise2 = processors.message
processors.soundcloseoverride = processors.message
processors.soundmoveoverride = processors.message
processors.stopsound = processors.message
processors.startsound = processors.message
processors.movesound = processors.message
function processors:model()
-- TODO: may be vmf and .spr, otherwise always mdl and models/
local normal = self:find'^models/.*%.mdl$'
if normal then
local path = self
if dogeneric(path) then
path = path:sub(1,-5)
dogeneric(path..'.phy')
dogeneric(path..'.dx80.vtx')
dogeneric(path..'.dx90.vtx')
dogeneric(path..'.sw.vtx')
dogeneric(path..'.vvd')
end
else
assert(not self:lower():find("%.mdl$"))
if dogeneric("materials/"..self) then
print("but okay",self)
return
end
end
end
local res = {}
local function parsevmfline(l)
local pos = l:find('" "', 3, true)
if not pos then return end
local left = l:sub(2, pos):lower()
if not (left:find("material", 1, true) or left:find("texture", 1, true) or left:find("model", 1, true) or left:find("sound", 1, true) or left:find("message", 1, true)) then
return
end
local right = l:sub(pos + 3, -1)
local firstquote = left:find('"', 1, true)
local key = left:sub(firstquote + 1, #left - 1)
local val = right:gsub('"[^"]*$', '')
--print(key,val)
local t = res[key]
if not t then
t = {}
res[key] = t
end
t[val:lower()] = true
end
local function process_found()
for restype, paths in next, res do
restype = restype:lower()
local processor_func = processors[restype]
if processor_func then
for path, _ in next, paths do
processor_func(path)
end
end
end
end
local function main()
print"Parsing VMF"
for l in io.lines(file) do
parsevmfline(l)
end
print"Processing lists..."
process_found()
print"Writing list..."
local t = {}
for k, v in next, reslist do
t[#t+1] = k
end
print(#t, "resources")
table.sort(t)
local f = io.open(out,'wb')
for k, v in next, t do
f:write(v..'\n')
if fmt_bspzip then
f:write(v..'\n')
end
end
f:close()
print"Calculating sizes..."
local szsum = 0
for path, _ in next, reslist do
local f = assert(io.open(respath_has(path),'rb'))
local sz = f:seek("end")
szsum = szsum + sz
reslist[path]=sz
f:close()
end
print("Size:",math.floor(szsum/1000/1000),"MB uncompressed")
local t = {}
for k,v in next,reslist do
t[#t+1] = {k,v}
end
table.sort(t,function(a,b) return a[2]<b[2] end)
print"Top 10: "
for i=#t,math.max(1,#t-10),-1 do
print("",math.floor(t[i][2]/1000),"KB",t[i][1])
end
end
main()