-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.lua
175 lines (138 loc) · 4.4 KB
/
pack.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
if ( tonumber(_VERSION:match("%d+%.?%d?")) < 5.3 ) then
error("You need to use at least LUA version 5.3!")
end
local zlib = require("zlib")
local lfs = require("lfs")
local in_path = assert(arg[1], "\n\n[ERR] no input\n")
local out_file = arg[2] or "./DATA7.PAK"
local filter = arg[3] -- ".DAT" for example
if in_path:sub(-1) ~= "\\" then
in_path = in_path .. "\\"
end
local entries = 0
local dirs = 0
local offset = 18
local header = {}
local w
local function uint32(i) return string.pack("<I", i) end
local function uint16(i) return string.pack("<H", i) end
local function uint8(i) return string.pack("B", i) end
local function scan(path)
local count = 0
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
count = count + 1
end
end
local t = {}
local idx = 0
for file in lfs.dir(path) do
if file == "." or file == ".." then
goto next_file
end
-- filter by extension
local fullpath = path .. file
local attr = lfs.attributes(fullpath)
if attr.mode == "file" then
if filter and filter ~= file:sub(-4) then
goto next_file
end
end
idx = idx + 1
entries = entries + 1
local crc, typ, off, size_orig, size, zsize, fname = 0, 8, 0, 0, 0, 0, ""
if attr.mode == "directory" then
fname = file .. "/"
else
for t, f in file:gmatch("(%d+)_(.+)") do
typ = tonumber(t)
fname = f
end
-- read & pack
local r = assert(io.open(fullpath, "rb"))
local data = r:read("*a")
size_orig = r:seek()
r:close()
crc = zlib.crc32()(data)
off = offset
-- deflate only specific types
if typ > 8 then
local eof = false
data, eof, size, zsize = zlib.deflate()(data, "finish")
offset = offset + zsize + 8
else
size = size_orig
offset = offset + size_orig + 8
end
-- append data
w:write(uint32(size))
w:write(uint32(zsize))
w:write(data)
end
local itr = idx .. "\\" .. count
print(("%9s %08X %2d %10d (%8d) %8d <- %8d %s"):format(itr, crc, typ, off, size_orig, size, zsize, fname))
t[idx] = {crc, typ, #fname, fname:gsub("(.)", "%1\x00"), off, size_orig}
::next_file::
end
table.insert(header, t)
end
local function dir(path)
for file in lfs.dir(path) do
if file == "." or file == ".." then
goto next_dir
end
local fullpath = path .. file
local attr = lfs.attributes(fullpath)
if attr.mode == "directory" then
dirs = dirs + 1
fullpath = fullpath .. "\\"
local fp = fullpath:gsub(in_path, "")
print()
print(dirs, fp)
fp = fp:gsub("\\", "/")
table.insert(header, {#fp, fp:gsub("(.)", "%1\x00")})
scan(fullpath)
dir(fullpath)
end
::next_dir::
end
end
--[[ main ]]--------------------------------------------------------------------
w = assert(io.open(out_file, "w+b"))
w:write(uint16(5)) -- version
w:write(uint32(0))
w:write(uint32(0)) -- dummy filenames offset
w:write(uint32(0)) -- dummy filenames sizes
w:write(uint32(0)) -- unknown
-- append files
dir(in_path)
-- filenames data
local filenames_offset = w:seek()
w:write(uint32(entries))
w:write(uint32(dirs))
for k, v in ipairs(header) do
if type(v[1]) == "number" then
--print(table.concat(v, "\t"))
w:write(uint16(v[1])) -- #dirname
w:write(v[2]) -- dirname (utf16le)
else
w:write(uint32(#v)) -- count
for i, j in ipairs(v) do
--print(table.concat(j, "\t"))
w:write(uint32(j[1])) -- crc
w:write(uint8( j[2])) -- type
w:write(uint16(j[3])) -- fname len
w:write( j[4]) -- fname
w:write(uint32(j[5])) -- offset
w:write(uint32(j[6])) -- orig size
end
end
end
local filenames_size = w:seek() - filenames_offset
w:close()
-- update
w = assert(io.open(out_file, "r+b"))
w:seek("set", 6)
w:write(uint32(filenames_offset))
w:write(uint32(filenames_size))
w:close()