-
Notifications
You must be signed in to change notification settings - Fork 6
/
espParser.lua
411 lines (353 loc) · 12.1 KB
/
espParser.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
--[[
espParser 0.5
By Jakob https://github.com/JakobCh
Mostly using: https://en.uesp.net/morrow/tech/mw_esm.txt
Updates will probably break your shit right now in the early stages.
Almost all record/subrecord data isn't parsed.
Things that are currently parsed:
Cells - espParser.files["Morrowind.esm"].cells
Installation:
1. Put this file and struct.lua ( https://github.com/iryont/lua-struct ) in /server/scripts/custom/
2. Add "require("custom.espParser")" to /server/scripts/customScripts.lua
3. Create a folder called "esps" in /server/data/custom/
4. Place your esp/esm files in the new folder (/server/data/custom/esps/)
5. Change the "files" table a couple lines down to match your files
(6. Check the espParserTest.lua file for examples)
]]
local files = {
"Morrowind.esm",
"Tribunal.esm",
"Bloodmoon.esm"
}
require "custom.struct" -- Requires https://github.com/iryont/lua-struct
--Global
espParser = {}
--print(debug.getinfo(2, "S").source:sub(2))
--Stream class
espParser.Stream = {}
espParser.Stream.__index = espParser.Stream
function espParser.Stream:create(data)
local newobj = {}
setmetatable(newobj, espParser.Stream)
newobj.data = data
newobj.pointer = 1
return newobj
end
function espParser.Stream:len()
return string.len(self.data)
end
function espParser.Stream:read(amount)
local temp = string.sub(self.data, self.pointer, self.pointer+amount-1)
self.pointer = self.pointer + amount
return temp
end
function espParser.Stream:sub(start, send)
local temp = string.sub(self.data, start, send)
return temp
end
--Record class
espParser.Record = {}
espParser.Record.__index = espParser.Record
function espParser.Record:create(stream)
local newobj = {}
setmetatable(newobj, espParser.Record)
newobj.name = stream:read(4)
newobj.size = struct.unpack( "i", stream:read(4) )
newobj.header1 = struct.unpack( "i", stream:read(4) )
newobj.flags = struct.unpack( "i", stream:read(4) )
newobj.data = stream:read(newobj.size)
newobj.subRecords = {}
--get subrecords
local st = espParser.Stream:create(newobj.data)
while st.pointer < st:len() do
table.insert(newobj.subRecords, espParser.SubRecord:create(st) )
end
return newobj
end
function espParser.Record:getSubRecordsByName(name)
local out = {}
for _, subrecord in pairs(self.subRecords) do
if subrecord.name == name then
table.insert(out, subrecord)
end
end
return out
end
--SubRecord class
espParser.SubRecord = {}
espParser.SubRecord.__index = espParser.SubRecord
function espParser.SubRecord:create(stream)
local newobj = {}
setmetatable(newobj, espParser.SubRecord)
newobj.name = stream:read(4)
newobj.size = struct.unpack( "i", stream:read(4) )
newobj.data = stream:read(newobj.size)
--print("Creating subrecord with name: " .. tostring(newobj.name))
return newobj
end
--helper functions
espParser.getRecords = function(filename, recordName)
local out = {}
for i,record in pairs(espParser.rawFiles[filename]) do
if record.name == recordName then
table.insert(out, record)
end
end
return out
end
espParser.getSubRecords = function(filename, recordName, subRecordName)
local out = {}
for _,record in pairs(espParser.rawFiles[filename]) do
if record.name == recordName then
for _, subrecord in pairs(record.subRecords) do
if subrecord.name == subRecordName then
table.insert(out, subrecord)
end
end
end
end
return out
end
espParser.getAllRecords = function(recordName)
local out = {}
for filename,records in pairs(espParser.files) do
for _,record in pairs(records) do
if record.name == recordName then
table.insert(out, record)
end
end
end
return out
end
espParser.getAllSubRecords = function(recordName, subRecordName)
local out = {}
for filename,records in pairs(espParser.files) do
for _,record in pairs(records) do
if record.name == recordName then
for _, subrecord in pairs(record.subRecords) do
if subrecord.name == subRecordName then
table.insert(out, subrecord)
end
end
end
end
end
return out
end
espParser.rawFiles = {} --contains each .esp file as a key (raw Records and subrecords)
espParser.files = {} --contains each .esp file as a key (parsed)
--TODO have a merged one that carry over changes depending on the loadorder
espParser.subrecordParseHelper = function(obj, dataTypes, subrecord)
if dataTypes[subrecord.name] ~= nil then
if type(dataTypes[subrecord.name][1]) == "table" then
local stream = espParser.Stream:create( subrecord.data )
for _, ty in pairs(dataTypes[subrecord.name][1]) do
if dataTypes[subrecord.name][2] == nil then --assign directly to the object
obj[ ty[2] ] = struct.unpack( ty[1], stream:read(4) )
else --put the values in a table
if obj[ dataTypes[subrecord.name][2] ] == nil then
obj[ dataTypes[subrecord.name][2] ] = {}
end
obj[ dataTypes[subrecord.name][2] ][ ty[2] ] = struct.unpack( ty[1], stream:read(4) )
end
end
else
obj[ dataTypes[subrecord.name][2] ] = struct.unpack( dataTypes[subrecord.name][1], subrecord.data )
end
end
return obj
end
espParser.parseCells = function(filename) --filename already loaded in espParser.rawFiles
local records = espParser.getRecords(filename, "CELL")
if espParser.files[filename] == nil then
espParser.files[filename] = {}
end
espParser.files[filename].cells = {}
--lenghts of data types
local lenTable = {
i = 4,
f = 4,
}
local dataTypes = {
Unique = {
{"NAME", "s", "name"}, --cell description
{"DATA", {
{"i", "flags"},
{"i", "gridX"},
{"i", "gridY"}
}},
{"INTV", "i", "water"}, --water height stored in a int (didn't know about this one until I checked the openmw source, no idea why theres 2 of them)
{"WHGT", "f", "water"}, --water height stored in a float
{"AMBI", {
{"i", "ambientColor"},
{"i", "sunlightColor"},
{"i", "fogColor"},
{"f", "fogDensity"}
}},
{"RGNN", "s", "region"}, --the region name like "Azura's Coast" used for weather and stuff
{"NAM5", "i", "mapColor"},
{"NAM0", "i", "refNumCounter"} --when you add a new object to the cell in the editor it gets this refNum then this variable is incremented
},
Multi = {
{"NAME", "s", "refId"},
{"XSCL", "f", "scale"},
{"DELE", "i", "deleted"}, --rip my boi
{"DNAM", "s", "destCell"}, --the name of the cell the door takes you too
{"FLTV", "i", "lockLevel"}, --door lock level
{"KNAM", "s", "key"}, --key refId
{"TNAM", "s", "trap"}, --trap spell refId
{"UNAM", "B", "referenceBlocked"},
{"ANAM", "s", "owner"}, --the npc owner or the item
{"BNAM", "s", "globalVariable"}, -- Global variable for use in scripts?
{"INTV", "i", "charge"}, --current charge?
{"NAM9", "i", "goldValue"}, --https://github.com/OpenMW/openmw/blob/dcd381049c3b7f9779c91b2f6b0f1142aff44c4a/components/esm/cellref.cpp#L163
{"XSOL", "s", "soul"},
{"CNAM", "s", "faction"}, --faction who owns the item
{"INDX", "i", "factionRank"}, --what rank you need to be in the faction to pick it up without stealing?
{"XCHG", "i", "enchantmentCharge"}, --max charge?
{"DODT", {
{"f", "XPos"},
{"f", "YPos"},
{"f", "ZPos"},
{"f", "XRot"},
{"f", "YRot"},
{"f", "ZRot"}
}, "doorDest"}, --the position the door takes you too
{"DATA", {
{"f", "XPos"},
{"f", "YPos"},
{"f", "ZPos"},
{"f", "XRot"},
{"f", "YRot"},
{"f", "ZRot"}
}, "pos"} --the position of the object
}
}
for _, record in pairs(records) do
local cell = {}
for _, dType in pairs(dataTypes.Unique) do
local tempData = record:getSubRecordsByName(dType[1])[1]
if tempData ~= nil then
if type(dType[2]) == "table" then
local stream = espParser.Stream:create( tempData.data )
for _, ddType in pairs(dType[2]) do
cell[ddType[2]] = struct.unpack( ddType[1], stream:read(4) )
end
else
--print(dType[2], tempData.data)
cell[dType[3]] = struct.unpack( dType[2], tempData.data )
end
end
end
if cell.region ~= nil or cell.name == "" then --its a external cell
cell.isExterior = true
cell.name = cell.gridX .. ", " .. cell.gridY
else --its a internal cell
cell.isExterior = false
end
cell.objects = {}
local currentIndex = nil
for _, subrecord in pairs(record.subRecords) do
if subrecord.name == "FRMR" then
currentIndex = struct.unpack( "i", subrecord.data )
cell.objects[currentIndex] = {}
cell.objects[currentIndex].refNum = currentIndex
cell.objects[currentIndex].scale = 1 --just a default
end
for _, dType in pairs(dataTypes.Multi) do
if subrecord.name == dType[1] and currentIndex ~= nil then --if its a subrecord in dataTypes.Multi
if type(dType[2]) == "table" then --there are several values in this data
local stream = espParser.Stream:create( subrecord.data )
for _, ddType in pairs(dType[2]) do --go thrue every value that we want out of this data
if dType[3] ~= nil then --store the values in a table
if cell.objects[currentIndex][dType[3]] == nil then
cell.objects[currentIndex][dType[3]] = {}
end
cell.objects[currentIndex][dType[3]][ddType[2]] = struct.unpack( ddType[1], stream:read( lenTable[ddType[1]] ) )
--print("cell.objects[currentIndex]" .. dType[3] .. "][" .. ddType[2] .. "]")
else --store the values directly in the cell
cell.objects[currentIndex][ddType[2]] = struct.unpack( ddType[1], lenTable[ddType[1]] )
end
end
else -- theres only one value in the data
cell.objects[currentIndex][dType[3]] = struct.unpack( dType[2], subrecord.data )
end
end
end
end
espParser.files[filename].cells[cell.name] = cell
end
end
espParser.parseStatics = function(filename)
local records = espParser.getRecords(filename, "STAT")
if espParser.files[filename] == nil then
espParser.files[filename] = {}
end
espParser.files[filename].statics = {}
for _, record in pairs(records) do
local refId = struct.unpack( "s", record:getSubRecordsByName("NAME")[1].data )
local model = struct.unpack( "s", record:getSubRecordsByName("MODL")[1].data )
espParser.files[filename].statics[refId] = {}
espParser.files[filename].statics[refId].refId = refId
espParser.files[filename].statics[refId].model = model
end
end
espParser.parseMiscs = function(filename)
local records = espParser.getRecords(filename, "MISC")
if espParser.files[filename] == nil then
espParser.files[filename] = {}
end
espParser.files[filename].miscs = {}
local dataTypes = {
NAME = {"s", "refId"},
MODL = {"s", "model"},
FNAM = {"s", "name"},
MCDT = {
{
{"f", "weight"},
{"i", "value"},
{"i", "unknown"}
}
},
SCRI = {"s", "script"},
ITEX = {"s", "icon"},
DELE = {"i", "deleted"}
}
for _, record in pairs(records) do
local refId = struct.unpack( "s", record:getSubRecordsByName("NAME")[1].data )
espParser.files[filename].miscs[refId] = {}
for _, subrecord in pairs(record.subRecords) do
espParser.files[filename].miscs[refId] = espParser.subrecordParseHelper(espParser.files[filename].miscs[refId], dataTypes, subrecord)
end
end
end
espParser.addEsp = function(filename)
local currentFile = filename
--print(tes3mp.GetDataPath() .. "\\custom\\esps\\" .. currentFile)
local f
f = io.open(tes3mp.GetDataPath() .. "\\custom\\esps\\" .. currentFile, "rb") --open file handler (windows)
if f == nil then
f = io.open(tes3mp.GetDataPath() .. "/custom/esps/" .. currentFile, "rb") --open file handler (linux)
end
if f == nil then return false end --could not open the file
local mainStream = espParser.Stream:create(f:read("*a")) --read all
espParser.rawFiles[currentFile] = {}
while mainStream.pointer < mainStream:len() do
local r = espParser.Record:create(mainStream)
table.insert(espParser.rawFiles[currentFile], r)
end
espParser.parseCells(currentFile)
espParser.parseStatics(currentFile)
espParser.parseMiscs(currentFile)
return true
end
-- Load all the files in the config
tes3mp.LogMessage(enumerations.log.INFO, "[espParser] Loading files...")
for i,name in pairs(files) do
if espParser.addEsp(name) then
tes3mp.LogMessage(enumerations.log.INFO, "[espParser] Loaded: " .. name)
else
tes3mp.LogMessage(enumerations.log.INFO, "[espParser] Failed to load: " .. name)
end
end
tes3mp.LogMessage(enumerations.log.INFO, "[espParser] Finished!")