-
Notifications
You must be signed in to change notification settings - Fork 8
/
file.go
224 lines (185 loc) · 4.49 KB
/
file.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
package wz
import (
"errors"
"fmt"
"github.com/edsrzf/mmap-go"
"github.com/goinggo/workpool"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
type WZFile struct {
filemap mmap.MMap
versionHash uint32
mainBlob *WZFileBlob
workPool *workpool.WorkPool
FileDescription string
Debug bool
Filename string
Root *WZDirectory
LazyLoading bool
}
func NewFile(filename string) (*WZFile, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
filemap, err := mmap.Map(file, mmap.RDONLY, 0)
if err != nil {
return nil, err
}
wz := new(WZFile)
wz.filemap = filemap
wz.Debug = false
wz.Filename = filename
wz.workPool = workpool.New(runtime.NumCPU()*2, 7000)
wz.mainBlob = NewWZFileBlob(wz.filemap, nil, wz)
wz.LazyLoading = true
return wz, nil
}
func (m *WZFile) debug(args ...interface{}) {
if m.Debug {
fmt.Println(fmt.Sprint("[WZFile: ", m.Filename, "] ", fmt.Sprint(args...)))
}
}
func (m *WZFile) Close() {
m.filemap.Unmap()
}
func (m *WZFile) Parse() {
runtime.GOMAXPROCS(runtime.NumCPU())
m.debug("Starting parsing...")
m.mainBlob.seek(0)
header := m.mainBlob.readASCIIString(4)
m.debug("Header: ", header)
if header != "PKG1" {
panic(errors.New("Not a PKG1/WZ file"))
}
m.mainBlob.skip(8) // Filesize
m.mainBlob.contentsStart = m.mainBlob.readInt32()
m.debug("Content starts at ", m.mainBlob.contentsStart)
m.FileDescription = m.mainBlob.readASCIIZString()
m.debug("File description: ", m.FileDescription)
m.determineVersion()
}
// determineVersion is a bruteforcer on the hash stored inside the
// wz file.
func (m *WZFile) determineVersion() {
m.mainBlob.seek(int64(m.mainBlob.contentsStart))
encryptedVersion := m.mainBlob.readUInt16()
var realVersion uint16 = 0
for {
realVersion++
calcVersion, calcHash := calculateHash(realVersion)
if calcVersion != encryptedVersion {
m.debug("It cannot be version ", realVersion)
} else {
m.debug("It is probably version ", realVersion, "! (hash ", calcHash, ")")
m.versionHash = calcHash
// Now, see if we can actually do something with this version
if dir := m.isParsableWithVersion(); dir != nil {
m.debug("Yes, this is usable!")
m.Root = dir
return
} else {
m.debug("Nope, not the correct version")
continue
}
}
}
}
func (m *WZFile) isParsableWithVersion() (result *WZDirectory) {
defer func() {
if r := recover(); r != nil {
m.debug("Its not this version, reason: ", r)
panic(r)
result = nil
}
}()
dir := NewWZDirectory(filepath.Base(m.Filename), nil)
dir.Parse(m.mainBlob, m.mainBlob.pos())
return dir
}
func (m *WZFile) WaitUntilLoaded() {
for m.workPool.QueuedWork() != 0 {
time.Sleep(100 * time.Millisecond)
}
}
func Fetch(node interface{}, elem string) interface{} {
childNodes := GetChildNodes(node)
node = childNodes[elem]
switch node.(type) {
case *WZVariant:
variant := node.(*WZVariant)
if variant.Type != 9 {
val := variant.Value
switch val.(type) {
case int16:
node = val.(int16)
case int32:
node = val.(int32)
case int64:
node = val.(int64)
case float32:
node = val.(float32)
case float64:
node = val.(float64)
case string:
node = val.(string)
default:
println("WARN: Could not unpack variant with type ", variant.Type)
}
}
}
return node
}
func GetChildNodes(node interface{}) map[string]interface{} {
elements := make(map[string]interface{})
switch node.(type) {
case *WZDirectory:
for name, elem := range node.(*WZDirectory).Directories {
elements[name] = elem
}
for name, elem := range node.(*WZDirectory).Images {
elements[name] = elem
}
case WZProperty:
for name, elem := range node.(WZProperty) {
elements[name] = elem
}
case *WZImage:
img := node.(*WZImage)
img.StartParse()
for name, elem := range img.Properties {
elements[name] = elem
}
case *WZCanvas:
for name, elem := range node.(*WZCanvas).Properties {
elements[name] = elem
}
case *WZVariant:
variant := node.(*WZVariant)
elements = GetChildNodes(variant.Value)
case *WZVector:
obj := node.(*WZVector)
elements["X"] = obj.X
elements["Y"] = obj.Y
case []interface{}:
for idx, elem := range node.([]interface{}) {
elements[strconv.Itoa(idx)] = elem
}
default:
// panic("WAT")
}
return elements
}
func (m *WZFile) GetFromPath(path string) interface{} {
elements := strings.Split(path, "/")
var node interface{} = m.Root
for _, elem := range elements {
node = Fetch(node, elem)
}
return node
}