-
Notifications
You must be signed in to change notification settings - Fork 0
/
luavmt.lua
343 lines (342 loc) · 8.45 KB
/
luavmt.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
local tostring, tonumber, find, type, pairs, ipairs = tostring, tonumber, string.find, type, pairs, ipairs
local util_TableToJSON, file_Read, Vector = util.TableToJSON, file.Read, Vector
--[[Lua Valve key value structure standalone parser for Garry's mod/ UnkN ©2022]]
module("luavmt", package.seeall)
local numchars = {["."] = true, ["-"] = true}
local strpat = "[%w%d%$<%?\\/_%-]"
for i = 0, 9 do
numchars[tostring(i)] = true
end
local function readNumber(str, offset)
local len = str:len()
local found = false
for i = offset, len do
local char = str[i]
if numchars[char] then
if not found then
found = i
end
elseif found then
return tonumber(str:sub(found, i - 1)), i
end
end
if found then
return tonumber(str:sub(found, len)), len
end
return 0, len
end
function readKey(str, offset, len)
local found = false
local tp = "nil"
for i = offset or 1, len do
local char = str[i]
if tp == "nil" then
if numchars[char] then
tp = "number"
found = i
elseif char == "\"" then
tp = "stringquotes"
found = i
elseif find(char, strpat) then
if char == "/" then
local nc = str[i + 1]
if nc == "/" then
tp = "commentline"
found = i + 2
elseif nc == "*" then
tp = "commentlines"
found = i + 2
end
else
tp = "string"
found = i
end
elseif char == "[" then
tp = "vector"
found = i
elseif char == "{" then
tp = "table"
return {tp, readKeyValues(str, i + 1, len)}
--[[elseif char == "/" then
if str[i + 1] == "/" then
tp = "comment"
found = i + 2
end]]
end
elseif tp == "number" and not numchars[char] then
if find(char, strpat) then
tp = "string"
elseif found then
return {tp, tonumber(str:sub(found, i - 1)), i + 1}
end
elseif tp == "stringquotes" then
if char == "\"" then
if found then
str = str:sub(found + 1, i - 1)
local num = tonumber(str)
if num then
return {"number", num, i + 1}
else
return {tp, str, i + 1}
end
end
elseif char == "[" then
tp = "vectorquoted"
found = i
end
elseif tp == "string" then
if found and not find(char, strpat) then
str = str:sub(found, i - 1)
local num = tonumber(str)
if num then
return {"number", num, i + 1}
else
return {tp, str, i + 1}
end
end
elseif (tp == "vector" and char == "]") or (tp == "vectorquoted" and char == "\"") then
if found then
local vec = str:sub(found + 1, i - 1)
local x, y, z = readNumber(vec, 1)
local ln = vec:len()
if y < ln then
y, z = readNumber(vec, y)
if z < ln then
z = readNumber(vec, z)
else
z = 0
end
else
y = 0
end
return {tp, Vector(x, y, z), i + 2}
end
elseif tp == "commentline" and char == "\n" then
tp = "nil"
found = i + 1
elseif tp == "commentlines" and char == "*" and str[i + 1] == "/" then
tp = "nil"
found = i + 1
end
end
if tp == "number" then
return {tp, tonumber(str:sub(found)), len}
elseif tp == "string" then
str = str:sub(found)
local num = tonumber(str)
if num then
return {"number", num, len}
else
return {tp, str, len}
end
end
return {tp, nil, len}
end
function readKeyValues(str, prevpos, len)
len = len or str:len()
prevpos = prevpos or 1
local level = 1
local comment = false
-- lazy to rewrite this to correct O(n) syntax analyzer, cuz spent much time on fixing bugs
for k = prevpos, len do
local char = str[k]
if comment then
if comment == "line" then
if char == "\n" then
comment = false
end
elseif comment == "lines" then
if char == "*" and str[k + 1] == "/" then
comment = false
k = k + 1
end
end
else
if char == "{" then
level = level + 1
elseif char == "}" then
level = level - 1
if level == 0 then
len = k
break
end
elseif char == "/" then
local nc = str[k + 1]
if nc == "/" then
comment = "line"
elseif nc == "*" then
comment = "lines"
end
end
end
end
local out = {}
for i = 1, 1000 do -- better than "while true do" if something is bad
local key = readKey(str, prevpos, len)
if not key[2] then break end
local value = readKey(str, key[3], len)
if not value[2] then break end
prevpos = value[3]
out[key[2]] = value[2]
if prevpos >= len then break end
end
return out, prevpos
end
function toTable(vmt, gpath)
local filec = file_Read(vmt, gpath or "GAME")
if not filec then return {} end
--print(filec)
local keyvalues, len = readKeyValues(filec)
return keyvalues
end
function toJSON(vmt, gpath)
return util_TableToJSON(toTable(vmt, gpath))
end
texturekeys = {}
for k, v in ipairs({
"ambientoccltexture",
"basetexture",
"basetexture2",
"blendmodulatetexture",
"blurtexture", -- shader
"crackmaterial",
"fallbackmaterial", -- dxlevel
"fbtexture", -- shader
"bumpmap",
"bumpmap2",
"corneatexture",
"detail",
"dudvmap",
"envmap",
"envmapmask",
"envmapmask2",
"flashlighttexture",
"fresnelrangestexture",
"include", -- include vmt, that can contain textures
"iris",
"lightwarptexture",
"material", -- proxy
"modelmaterial",
"normalmap",
"notooltexture",
"phongexponenttexture",
"phongwarptexture",
"pixshader", -- shader
"reflecttexture",
"refracttexture",
"refracttinttexture",
"selfillummask",
"texture2",
"tooltexture",
"woundcutouttexture", -- L4D2
--[[--CSGO shaders doesn't work in Garry's mod
"sourcemrtrendertarget",
"aotexture",
"exptexture",
"grungetexture",
"maskstexture",
"painttexture",
"posttexture",
"surfacetexture",
"weartexture",
]]
}) do texturekeys[v] = true end
local function storeTexture(textures, vals)
for k, v in pairs(vals) do
if type(v) == "string" then
k = k:lower()
if (k[1] == "$" or k[1] == "%") then
k = k:sub(2)
end
if texturekeys[k] then
textures[#textures + 1] = v:gsub("\\\\?", "/"):lower() -- textures are not case sensitive, better to lowercase it, also we should replace windows path to linux like
end
elseif type(v) == "table" then
storeTexture(textures, v)
end
end
end
function isRelativePath(path)
if path:sub(1, 11) == "materials/" then
return true
end
local ext = path:sub(-3)
if ext == "vmt" or ext == "vtf" then
return true
end
end
function getTextures(vmt, gpath)
local tab = toTable(vmt, gpath)
local textures = {}
for shadername, vals in pairs(tab) do
if not vals then
return textures
end
local ok, err = pcall(storeTexture, textures, vals)
if not ok then
print(vmt, gpath)
ErrorNoHalt(err .. "\n")
print(util.TableToJSON(val))
end
end
return textures
end
function test(path, gpath)
--print(path)
local files, fold = file.Find(path .. "*", gpath)
for k,v in pairs(fold) do
if v == "debug" then
continue
end
test(path .. v .. "/", gpath)
end
for i, j in pairs(files) do
if string.GetExtensionFromFilename(j) ~= "vmt" then
continue
else
j = string.StripExtension(j)
end
--print(path .. j)
local mounttext = luavmt.getTextures(path .. j .. ".vmt", gpath)
if #mounttext == 0 then
print(luavmt.toJSON(path .. j .. ".vmt", gpath))
print("File " .. path .. j .. ".vmt doesn't contain textures, possible bug or texture should be like it.")
end
--[[for i, j in pairs(mounttext) do
materials[j:gsub("^materials/",""):gsub(".vmt",""):gsub(".vtf","")] = false
end]]
for k, v in pairs(Material(path .. j .. ".vmt"):GetKeyValues()) do
if TypeID(v) == TYPE_TEXTURE then
if k[1] == "$" or k[1] == "%" then
k = k:sub(2)
end
if not luavmt.texturekeys[k] then
print(k, "Missing texture key that gmod supports!!!")
end
end
--[[if TypeID(v) == TYPE_STRING and mat ~= fixpath(v) then
if materials[v] ~= nil then
materials[v] = true
else
PrintTable(materials)
error(path .. j)
end
elseif TypeID(v) == TYPE_TEXTURE and mat ~= v:GetName() then
if materials[(v:GetName())] ~= nil then
materials[(v:GetName())] = true
else
print(k, v:GetName())
PrintTable(materials)
error(path .. j .. "@")
end
end]]
end
--[[for k, v in pairs(materials) do
if v == false then
--PrintTable(materials)
--error(path .. j)
end
end]]
end
end
-- luavmt.test("materials/", "GAME")