-
Notifications
You must be signed in to change notification settings - Fork 0
/
PakGo.go
109 lines (86 loc) · 2.14 KB
/
PakGo.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
package PakGo
import (
"bytes"
"encoding/binary"
"errors"
"os"
"reflect"
"strings"
)
type pakHeader struct {
Id [4]byte
Offset uint32
Size uint32
}
type pakFileEntry struct {
Name [56]byte
Offset uint32
Size uint32
}
type pakFileEntryNative struct {
Name string
Offset int64
Size int
}
type PakFile struct {
filetable []pakFileEntryNative
filehandle *os.File
}
func (pak PakFile) fileid(filename string, filetable []pakFileEntryNative) (int, error) {
for id, file := range filetable {
if filename == file.Name {
return id, nil
}
}
return -1, errors.New("the system cannot find the file specified")
}
func (pak PakFile) ReadFile(filename string) ([]byte, error) {
i, err := pak.fileid(filename, pak.filetable)
if err != nil {
return nil, err
}
data := make([]byte, pak.filetable[i].Size)
pak.filehandle.Seek(pak.filetable[i].Offset, 0)
pak.filehandle.Read(data)
return data, nil
}
func (pak PakFile) PakClose() {
pak.filehandle.Close()
}
func PakLoad(filename string) (PakFile, error) {
var filetable []pakFileEntryNative
f, err := os.Open(filename)
if err != nil {
return PakFile{}, err
}
header := pakHeader{}
binary.Read(f, binary.LittleEndian, &header.Id)
binary.Read(f, binary.LittleEndian, &header.Offset)
binary.Read(f, binary.LittleEndian, &header.Size)
validheader := []byte{'P', 'A', 'C', 'K'}
valid := bytes.Compare(validheader, header.Id[:])
if valid != 0 {
return PakFile{}, errors.New("not a valid .PAK file")
}
size := reflect.TypeOf(pakFileEntry{}).Size()
filecount := int(header.Size / uint32(size))
f.Seek(int64(header.Offset), 0)
for i := 1; i <= filecount; i++ {
file := pakFileEntry{}
binary.Read(f, binary.LittleEndian, &file.Name)
binary.Read(f, binary.LittleEndian, &file.Offset)
binary.Read(f, binary.LittleEndian, &file.Size)
filename := string(file.Name[:])
filename = string(file.Name[:strings.Index(filename, "\x00")])
nativefile := pakFileEntryNative{
Name: filename,
Offset: int64(file.Offset),
Size: int(file.Size),
}
filetable = append(filetable, nativefile)
}
return PakFile{
filetable: filetable,
filehandle: f,
}, nil
}