-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkey.go
288 lines (252 loc) · 6.31 KB
/
key.go
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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"path"
"strings"
)
type RESREF struct {
Name [8]byte
}
func NewResref(name string) RESREF {
r := RESREF{}
copy(r.Name[:], []byte(name))
return r
}
func (r *RESREF) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
}
func (r *RESREF) Valid() bool {
return r.String() != ""
}
func (r *RESREF) String() string {
str := strings.Split(string(r.Name[0:]), "\x00")[0]
return str
}
type keyHeader struct {
Signature, Version [4]byte
BifCount uint32
ResourceCount uint32
BifOffset uint32
ResourceOffset uint32
}
type keyBifEntry struct {
Length uint32
OffsetFilename uint32
LengthFilename uint16
FileLocation uint16
}
type keyBifValue struct {
Length uint32
Filename string
FileLocation uint16
}
type keyResourceEntry struct {
Name RESREF
Type uint16
Location uint32
}
type keyUniqueResource struct {
Name string
Type uint16
}
type KEY struct {
header keyHeader
bifs []keyBifValue
resources []keyResourceEntry
r io.ReadSeeker
root string
files map[keyUniqueResource]int
}
var fileTypes = map[string]int{
"bmp": 1,
"mve": 2,
"tga": 3,
"wav": 4,
"wfx": 5,
"plt": 6,
"bam": 1000,
"wed": 1001,
"chu": 1002,
"tis": 1003,
"mos": 1004,
"itm": 1005,
"spl": 1006,
"bcs": 1007,
"ids": 1008,
"cre": 1009,
"are": 1010,
"dlg": 1011,
"2da": 1012,
"gam": 1013,
"sto": 1014,
"wmp": 1015,
"eff": 1016,
"bs": 1017,
"chr": 1018,
"vvc": 1019,
"vef": 1020,
"pro": 1021,
"bio": 1022,
"wbm": 1023,
"fnt": 1024,
"gui": 1026,
"sql": 1027,
"pvrz": 1028,
"glsl": 1029,
"tot": 1030,
"toh": 1031,
"menu": 1032,
"lua": 1033,
"ttf": 1034,
"ini": 2050,
}
var fileTypesExt = map[int]string{}
func init() {
for ext, num := range fileTypes {
fileTypesExt[num] = ext
}
}
func (res *keyResourceEntry) SetBifId(id uint32) {
res.Location = (res.Location & 0xFFFFF) | id<<20
}
func (res *keyResourceEntry) SetResourceId(id uint32) {
res.Location = res.Location&0xFFFFC000 | id&0x3fff
}
func (res *keyResourceEntry) GetBifId() uint32 {
return res.Location >> 20
}
func (res *keyResourceEntry) GetResourceId() uint32 {
return res.Location & 0x3fff
}
func (res *keyResourceEntry) GetTilesetId() uint32 {
return (res.Location & 0x000FC000) >> 14
}
func (res *keyResourceEntry) CleanName() string {
return strings.ToUpper(res.Name.String())
}
func (res *keyResourceEntry) String() string {
return fmt.Sprintf("%s.%s: %d %d", res.CleanName(), fileTypesExt[int(res.Type)], res.GetBifId(), res.GetResourceId())
}
func OpenKEY(r io.ReadSeeker, root string) (*KEY, error) {
key := &KEY{r: r, root: root}
r.Seek(0, os.SEEK_SET)
err := binary.Read(r, binary.LittleEndian, &key.header)
if err != nil {
return nil, err
}
r.Seek(int64(key.header.BifOffset), os.SEEK_SET)
bifs := make([]keyBifEntry, key.header.BifCount)
err = binary.Read(r, binary.LittleEndian, &bifs)
if err != nil {
return nil, err
}
for _, bifEntry := range bifs {
_, err := key.r.Seek(int64(bifEntry.OffsetFilename), os.SEEK_SET)
if err != nil {
return nil, err
}
bufStr := make([]byte, bifEntry.LengthFilename)
nBytes, err := io.ReadAtLeast(key.r, bufStr, int(bifEntry.LengthFilename))
if err != nil {
return nil, err
}
key.bifs = append(key.bifs, keyBifValue{Length: bifEntry.Length, Filename: path.Clean(strings.Replace(strings.Trim(string(bufStr[0:nBytes]), "\000"), "\\", "/", -1)), FileLocation: bifEntry.FileLocation})
}
r.Seek(int64(key.header.ResourceOffset), os.SEEK_SET)
key.resources = make([]keyResourceEntry, key.header.ResourceCount)
err = binary.Read(r, binary.LittleEndian, &key.resources)
if err != nil {
return nil, err
}
key.files = make(map[keyUniqueResource]int)
for idx, res := range key.resources {
kur := keyUniqueResource{Name: res.CleanName(), Type: res.Type}
key.files[kur] = idx
}
return key, nil
}
func (key *KEY) GetBifPath(bifId uint32) (string, error) {
if int(bifId) > len(key.bifs) {
return "", errors.New("Invalid bifId")
}
return key.bifs[bifId].Filename, nil
}
func (key *KEY) TypeToExt(ext uint16) string {
return fileTypesExt[int(ext)]
}
func (key *KEY) ExtToType(ext string) int {
fileExt := strings.Trim(ext, ".")
return fileTypes[fileExt]
}
func (key *KEY) GetResourceName(biffId uint32, resourceId uint32) (string, error) {
nID := uint32((biffId << 20) | (resourceId & 0x3fff))
for _, res := range key.resources {
if res.Location == nID {
name := string(res.CleanName()) + "." + key.TypeToExt(res.Type)
return name, nil
}
}
return "", errors.New("Resource not found")
}
func (key *KEY) Write(w io.Writer) error {
var bifFilenames bytes.Buffer
for _, bv := range key.bifs {
bifFilenames.WriteString(bv.Filename)
bifFilenames.WriteByte(0)
}
h := keyHeader{Signature: [4]byte{'K', 'E', 'Y', ' '}, Version: [4]byte{'V', '1', ' ', ' '}}
h.BifCount = uint32(len(key.bifs))
h.ResourceCount = uint32(len(key.resources))
h.BifOffset = uint32(binary.Size(h))
h.ResourceOffset = h.BifOffset + uint32(binary.Size(keyBifEntry{}))*h.BifCount + uint32(bifFilenames.Len())
err := binary.Write(w, binary.LittleEndian, &h)
if err != nil {
return err
}
var bifs []keyBifEntry
offset := h.ResourceOffset - uint32(bifFilenames.Len())
for _, bv := range key.bifs {
length := len(bv.Filename) + 1
bifs = append(bifs, keyBifEntry{Length: bv.Length, OffsetFilename: offset, LengthFilename: uint16(length), FileLocation: 1})
offset += uint32(length)
}
err = binary.Write(w, binary.LittleEndian, &bifs)
if err != nil {
return err
}
_, err = bifFilenames.WriteTo(w)
if err != nil {
return err
}
err = binary.Write(w, binary.LittleEndian, key.resources)
if err != nil {
return err
}
return nil
}
func (key *KEY) Validate() {
log.Printf("Header: %+v", key.header)
for idx, bif := range key.bifs {
bifPath, _ := key.GetBifPath(uint32(idx))
fmt.Printf("Idx: %d Path: %s Location: %d\n", idx, bifPath, bif.FileLocation)
}
for _, resource := range key.resources {
bifPath, err := key.GetBifPath(resource.GetBifId())
if err != nil {
log.Fatal(err)
}
log.Printf("Res: %s in %s", resource.String(), bifPath)
diskPath := path.Join(key.root, bifPath)
_, err = os.Stat(diskPath)
if err != nil {
//fmt.Printf("Can't find bif: %s\n", diskPath)
}
}
}