-
Notifications
You must be signed in to change notification settings - Fork 1
/
ankabuffer.go
176 lines (152 loc) · 4.73 KB
/
ankabuffer.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
package ankabuffer
import (
"fmt"
"strconv"
"github.com/dofusdude/ankabuffer/AnkamaGames"
)
type Chunk struct {
Hash string `json:"hash"`
Offset int64 `json:"offset"`
Size int64 `json:"size"`
Done bool `json:"done"`
}
type File struct {
Name string `json:"name"`
Size int64 `json:"size"`
Hash string `json:"hash"`
Chunks []Chunk `json:"chunks"`
Executable bool `json:"executable"`
Symlink string `json:",omitempty"`
ReverseBundles []string `json:"reverse_bundles"` // bundles containing chunks or entire file that are needed to reconstruct this file
}
type Bundle struct {
Hash string `json:"hash"`
Chunks []Chunk `json:"chunks"`
}
type Fragment struct {
Name string `json:"name"`
Files map[string]File `json:"files"`
Bundles []Bundle `json:"bundles"`
}
type Manifest struct {
GameVersion string `json:"game_version"`
Fragments map[string]Fragment `json:"fragments"`
}
type iHashFile interface {
Hash(j int) byte
HashLength() int
}
func getHash[T iHashFile](file T) string {
hash := ""
for i := 0; i < file.HashLength(); i++ {
hash += fmt.Sprintf("%02s", strconv.FormatInt(int64(file.Hash(i)), 16))
}
return hash
}
func ParseManifest(data []byte, gameVersion string) (*Manifest, error) {
flatbManifest := AnkamaGames.GetRootAsManifest(data, 0)
manifest := Manifest{}
manifest.GameVersion = gameVersion
manifest.Fragments = make(map[string]Fragment)
// for each file, find the unique bundles that contain chunks needed for the file
// so for first iteration, we save the bundle hash for each chunk hash
bundleLookup := make(map[string]string)
for i := 0; i < flatbManifest.FragmentsLength(); i++ {
fragment := AnkamaGames.Fragment{}
flatbManifest.Fragments(&fragment, i)
fragmentJson := Fragment{}
fragmentJson.Files = make(map[string]File)
fragmentJson.Name = string(fragment.Name())
fragmentJson.Bundles = make([]Bundle, fragment.BundlesLength())
for j := 0; j < fragment.BundlesLength(); j++ {
bundle := AnkamaGames.Bundle{}
fragment.Bundles(&bundle, j)
bundleJson := Bundle{}
bundleJson.Hash = getHash(&bundle)
bundleJson.Chunks = make([]Chunk, bundle.ChunksLength())
for k := 0; k < bundle.ChunksLength(); k++ {
chunk := AnkamaGames.Chunk{}
bundle.Chunks(&chunk, k)
chunkJson := Chunk{}
chunkJson.Hash = getHash(&chunk)
chunkJson.Offset = chunk.Offset()
chunkJson.Size = chunk.Size()
bundleJson.Chunks[k] = chunkJson
bundleLookup[chunkJson.Hash] = bundleJson.Hash // prepare backlink
}
fragmentJson.Bundles[j] = bundleJson
}
for j := 0; j < fragment.FilesLength(); j++ {
file := AnkamaGames.File{}
fragment.Files(&file, j)
fileJson := File{}
fileJson.Name = string(file.Name())
fileJson.Size = file.Size()
fileJson.Hash = getHash(&file)
fileJson.Chunks = make([]Chunk, file.ChunksLength())
for k := 0; k < file.ChunksLength(); k++ {
chunk := AnkamaGames.Chunk{}
file.Chunks(&chunk, k)
chunkJson := Chunk{}
chunkJson.Hash = getHash(&chunk)
chunkJson.Offset = chunk.Offset()
chunkJson.Size = chunk.Size()
fileJson.Chunks[k] = chunkJson
}
fileJson.Executable = file.Executable() == 1
if file.Symlink() != nil {
fileJson.Symlink = string(file.Symlink())
}
fragmentJson.Files[fileJson.Name] = fileJson
}
manifest.Fragments[fragmentJson.Name] = fragmentJson
}
// backlink bundles to files
for _, fragment := range manifest.Fragments {
for fileIdx, file := range fragment.Files {
realFile := file
bundles := NewSet[string]()
// look for file hash in all chunks
if len(file.Chunks) == 0 {
if bundleHash, ok := bundleLookup[file.Hash]; ok {
bundles.Add(bundleHash)
} else {
return nil, fmt.Errorf("File hash %s not found in any bundle", file.Hash)
}
} else {
for _, chunk := range file.Chunks {
if bundleHash, ok := bundleLookup[chunk.Hash]; ok {
bundles.Add(bundleHash)
} else {
return nil, fmt.Errorf("Chunk hash %s for file not found in any bundle", chunk.Hash)
}
}
}
if bundles.Size() == 0 {
realFile.ReverseBundles = nil
} else {
realFile.ReverseBundles = bundles.Slice()
}
fragment.Files[fileIdx] = realFile
}
}
return &manifest, nil
}
func GetNeededBundles(files []File) []string {
bundles := NewSet[string]()
for _, file := range files {
if file.ReverseBundles != nil {
bundles.AddMulti(file.ReverseBundles...)
}
}
return bundles.Slice()
}
func GetBundleHashMap(manifest *Manifest) map[string]Bundle {
bundleHashMap := make(map[string]Bundle)
for _, fragment := range manifest.Fragments {
for _, bundle := range fragment.Bundles {
bundleHashMap[bundle.Hash] = bundle
}
}
return bundleHashMap
}