-
Notifications
You must be signed in to change notification settings - Fork 8
/
directory.go
127 lines (107 loc) · 2.75 KB
/
directory.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
package wz
import (
"fmt"
)
type WZDirectoryLoader struct {
Directory *WZDirectory
FileBlob *WZFileBlob
Offset int64
}
func (m *WZDirectoryLoader) DoWork(workRoutine int) {
m.Directory.Parse(m.FileBlob, m.Offset)
}
type WZImageLoader struct {
Image *WZImage
FileBlob *WZFileBlob
Offset int64
}
func (m *WZImageLoader) DoWork(workRoutine int) {
m.Image.Parse(m.FileBlob, m.Offset)
}
type WZDirectory struct {
*WZSimpleNode
Directories map[string]*WZDirectory
Images map[string]*WZImage
}
func NewWZDirectory(name string, parent *WZSimpleNode) *WZDirectory {
node := new(WZDirectory)
node.WZSimpleNode = NewWZSimpleNode(name, parent)
node.Directories = make(map[string]*WZDirectory)
node.Images = make(map[string]*WZImage)
return node
}
func (m *WZDirectory) Parse(file *WZFileBlob, offset int64) {
file.seek(offset)
entries := file.readWZInt()
var i int32 = 0
for ; i < entries; i++ {
elementType := file.readByte()
var name string = ""
switch elementType {
case 1:
someData := file.readBytes(10)
m.debug(file, "WZDirectory::Parse found type 1: ", someData)
continue // What does these 10 bytes contain?
case 2: // UOL basically
subOffset := int64(file.readInt32() + file.contentsStart)
file.peekFor(func() {
file.seek(subOffset)
elementType = file.readByte()
name = file.readWZString(m.GetPath())
})
break
case 3, 4:
name = file.readWZString(m.GetPath())
break
default:
panic(fmt.Sprint("Unknown type in directory? ", elementType))
return
}
/*size := */ file.readWZInt() // Blob size
file.readWZInt() // Checksum?
dataOffset := int64(file.readWZOffset())
curpos := file.pos()
if elementType == 3 {
newDir := NewWZDirectory(name, m.WZSimpleNode)
m.Directories[name] = newDir
if true {
work := new(WZDirectoryLoader)
work.Directory = newDir
work.FileBlob = file.Copy()
work.Offset = dataOffset
if err := file.workPool.PostWork("directory loader", work); err != nil {
fmt.Println("ERROR ", err)
}
} else {
newDir.Parse(file, dataOffset)
}
} else {
img := NewWZImage(name, m.WZSimpleNode)
m.Images[name] = img
if !file.file.LazyLoading {
if false {
// Goroutine spamming
subfile := file.CopySliced(int(dataOffset))
img.Parse(subfile, 0)
} else if true {
// Workpool
work := new(WZImageLoader)
work.Image = img
work.FileBlob = file.Copy()
work.Offset = dataOffset
if err := file.workPool.PostWork("image loader", work); err != nil {
fmt.Println("ERROR ", err)
}
} else {
// Sync loading
img.Parse(file, dataOffset)
}
} else {
img.parseFuncInfo = func() {
img.Parse(file, dataOffset)
}
}
}
file.seek(curpos)
}
}