-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwdc1.lua
More file actions
181 lines (148 loc) · 5.5 KB
/
Copy pathwdc1.lua
File metadata and controls
181 lines (148 loc) · 5.5 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
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
--[[
Minimal WDC1 writer/reader for {id:FileDataID, path:string} tables.
Emits exactly the layout the WraithEngine DB2 reader (Engine/src/Features/DB2/DB2.cpp)
decodes: single section, no compression, inline id at field 0, one string field. A decoded
record is {uint32 id; uint32 pathOffset}; the path is resolved by reading the NUL-terminated
string at (string block start + pathOffset).
Layout written, in order:
- WDC1 header (84 bytes)
- field_structure[2] (4 bytes each = 8 bytes)
- records[N] (8 bytes each: uint32 id, uint32 pathOffset)
- string block (offset 0 = empty string, then deduped NUL-terminated paths)
- field_storage_info[2] (24 bytes each = 48 bytes)
Uses the `bit` library for little-endian packing; everything else is
plain loops, string.char and dicts so it stays close to the original.
]]
local bit = require("bit")
local band, rshift = bit.band, bit.rshift
local schar, sbyte = string.char, string.byte
local ssub, sfind = string.sub, string.find
local tconcat = table.concat
local sformat = string.format
local M = {}
local WDC1_MAGIC = 0x31434457 -- 'WDC1'
M.WDC1_MAGIC = WDC1_MAGIC
local HEADER_SIZE = 84
local FIELD_COUNT = 2
local RECORD_SIZE = 8
local FIELD_STORAGE_INFO_SIZE = 48 -- 2 fields * 24 bytes
local function u32le(v)
return schar(band(v, 0xFF), band(rshift(v, 8), 0xFF), band(rshift(v, 16), 0xFF), band(rshift(v, 24), 0xFF))
end
local function u16le(v)
return schar(band(v, 0xFF), band(rshift(v, 8), 0xFF))
end
-- int16 little-endian: identical low-16 bytes as u16 for our (small, non-negative) values.
local function i16le(v)
return u16le(band(v, 0xFFFF))
end
local function pack_header(n, string_size, min_id, max_id)
return tconcat({
u32le(WDC1_MAGIC), -- magic
u32le(n), -- record_count
u32le(FIELD_COUNT), -- field_count
u32le(RECORD_SIZE), -- record_size
u32le(string_size), -- string_table_size
u32le(0), -- table_hash
u32le(0), -- layout_hash
u32le(min_id), -- min_id
u32le(max_id), -- max_id
u32le(0), -- locale
u32le(0), -- copy_table_size
u16le(0), -- flags (0 = fixed-size records, no offset map)
u16le(0), -- id_index (id is field 0)
u32le(FIELD_COUNT), -- total_field_count
u32le(RECORD_SIZE), -- bitpacked_data_offset (== record_size for inline data)
u32le(0), -- lookup_column_count
u32le(0), -- offset_map_offset
u32le(0), -- id_list_size (0 = id is inline, not a separate list)
u32le(FIELD_STORAGE_INFO_SIZE),
u32le(0), -- common_data_size
u32le(0), -- pallet_data_size
u32le(0), -- relationship_data_size
})
end
local function pack_field_info(offset_bits)
return u16le(offset_bits) .. u16le(32) .. u32le(0) .. u32le(0) .. u32le(0) .. u32le(0) .. u32le(0)
end
function M.write_wdc1(out_path, rows)
local n = #rows
local order = {}
for i = 1, n do order[i] = i end
table.sort(order, function(a, b)
local ia, ib = rows[a][1], rows[b][1]
if ia ~= ib then return ia < ib end
return a < b
end)
local min_id, max_id
if n > 0 then
min_id = rows[order[1]][1]
max_id = rows[order[n]][1]
else
min_id = 0
max_id = 0
end
local string_parts = { "\0" }
local string_len = 1
local seen = {}
local record_parts = {}
for k = 1, n do
local row = rows[order[k]]
local fdid = row[1]
local path = row[2]
local offset = seen[path]
if offset == nil then
offset = string_len
seen[path] = offset
string_parts[#string_parts + 1] = path
string_parts[#string_parts + 1] = "\0"
string_len = string_len + #path + 1
end
record_parts[k] = u32le(fdid) .. u32le(offset)
end
local string_block = tconcat(string_parts)
local string_size = #string_block
local records = tconcat(record_parts)
local header = pack_header(n, string_size, min_id, max_id)
local field_struct = i16le(0) .. u16le(0) .. i16le(0) .. u16le(4)
local field_info = pack_field_info(0) .. pack_field_info(32)
local f = assert(io.open(out_path, "wb"))
f:write(header)
f:write(field_struct)
f:write(records)
f:write(string_block)
f:write(field_info)
f:close()
return n, string_size
end
local function u32le_at(s, off)
local a, b, c, d = sbyte(s, off + 1, off + 4)
return a + b * 256 + c * 65536 + d * 16777216
end
function M.read_wdc1_sample(path, count)
count = count or 5
local f = assert(io.open(path, "rb"))
local data = f:read("*a")
f:close()
local magic = u32le_at(data, 0)
assert(magic == WDC1_MAGIC, sformat("bad magic 0x%08X", magic))
local rec_count = u32le_at(data, 4)
local field_count = u32le_at(data, 8)
local rec_size = u32le_at(data, 12)
local records_off = HEADER_SIZE + field_count * 4
local strings_off = records_off + rec_count * rec_size
local out = {}
local limit = count
if rec_count < limit then limit = rec_count end
for i = 0, limit - 1 do
local roff = records_off + i * rec_size
local fdid = u32le_at(data, roff)
local path_offset = u32le_at(data, roff + 4)
local start = strings_off + path_offset
local nul = sfind(data, "\0", start + 1, true)
local text = ssub(data, start + 1, nul - 1)
out[#out + 1] = { fdid, text }
end
return out, rec_count
end
return M