-
Notifications
You must be signed in to change notification settings - Fork 1
/
d2p.go
79 lines (68 loc) · 2.17 KB
/
d2p.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
package unpack
import "io"
type D2P struct {
Stream io.ReadWriteSeeker
baseOffset uint32
baseLength uint32
indexesOffset uint32
numberIndexes uint32
propertiesOffset uint32
numberProperties uint32
filesPosition map[string]map[string]uint32
files map[string][]byte
properties map[string]string
}
func NewD2P(stream io.ReadWriteSeeker) *D2P {
d2p := &D2P{
Stream: stream,
filesPosition: make(map[string]map[string]uint32),
files: make(map[string][]byte),
properties: make(map[string]string),
}
d2pFileBinary := NewBinaryStream(stream, true)
bytesHeader := string(d2pFileBinary.ReadBytes(2))
if bytesHeader != "\x02\x01" {
panic("Invalid D2P file")
}
stream.Seek(-24, io.SeekEnd)
d2p.baseOffset = d2pFileBinary.ReadUint32()
d2p.baseLength = d2pFileBinary.ReadUint32()
d2p.indexesOffset = d2pFileBinary.ReadUint32()
d2p.numberIndexes = d2pFileBinary.ReadUint32()
d2p.propertiesOffset = d2pFileBinary.ReadUint32()
d2p.numberProperties = d2pFileBinary.ReadUint32()
stream.Seek(int64(d2p.indexesOffset), io.SeekStart)
for i := uint32(0); i < d2p.numberIndexes; i++ {
fileName := d2pFileBinary.ReadString()
offset := d2pFileBinary.ReadUint32()
length := d2pFileBinary.ReadUint32()
d2p.filesPosition[fileName] = map[string]uint32{
"offset": offset + d2p.baseOffset,
"length": length,
}
}
stream.Seek(int64(d2p.propertiesOffset), io.SeekStart)
for i := uint32(0); i < d2p.numberProperties; i++ {
pptyType := d2pFileBinary.ReadString()
pptyValue := d2pFileBinary.ReadString()
d2p.properties[pptyType] = pptyValue
}
for fileName, position := range d2p.filesPosition {
d2p.Stream.Seek(int64(position["offset"]), io.SeekStart)
file := d2pFileBinary.ReadBytes(int(position["length"]))
d2p.files[fileName] = file
}
return d2p
}
func (d2p *D2P) GetFiles() map[string]map[string]interface{} {
toReturn := make(map[string]map[string]interface{})
for fileName, position := range d2p.filesPosition {
object_ := make(map[string]interface{})
object_["position"] = position
if d2p.files != nil {
object_["binary"] = d2p.files[fileName]
}
toReturn[fileName] = object_
}
return toReturn
}