Repository navigation
Expand file tree
/
Copy pathpack.lua
More file actions
165 lines (125 loc) · 3.87 KB
/
Copy pathpack.lua
File metadata and controls
165 lines (125 loc) · 3.87 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
assert("Lua 5.3" == _VERSION)
local zlib = require("zlib")
local lfs = require("lfs")
local in_path = arg[1] or "."
local out_file = arg[2] or "DATA2.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 = 14
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, fname = 0, 8, 0, 0, ""
if attr.mode == "directory" then
fname = file .. "/"
else
for t, f in file:gmatch("(%d+)_(.+)") do
typ = t
fname = f
end
-- read & pack
local r = assert(io.open(fullpath, "rb"))
local data = r:read("*a")
r:close()
crc = zlib.crc32()(data)
local eof, zsize = false, 0
data, eof, size, zsize = zlib.deflate()(data, "finish")
off = offset
offset = offset + zsize + 8
-- add packed data
w:write(uint32(size))
w:write(uint32(zsize))
w:write(data)
end
local itr = idx .. "\\" .. count
print(("%9s %08X %2d %10d %8d %s"):format(itr, crc, typ, off, size, fname))
t[idx] = {crc, typ, #fname, fname:gsub("(.)", "%1\x00"), off, size}
::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)
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(uint32(1)) -- header
w:write(uint16(0))
w:write(uint32(0)) -- filenames offset
w:write(uint32(16777344)) -- flags 0x01000080
-- packed files data
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])) -- typ
w:write(uint16(j[3])) -- len
w:write( j[4]) -- fname
w:write(uint32(j[5])) -- offset
w:write(uint32(j[6])) -- unpacked
end
end
end
w:close()
-- update
w = assert(io.open(out_file, "r+b"))
w:seek("set", 6)
w:write(uint32(filenames_offset))
w:close()