-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
mpqexplorer.go
218 lines (175 loc) · 5.37 KB
/
mpqexplorer.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
// Package hsmpqexplorer contains an implementation of a MPQ archive explorer,
// which displays the archive contents as a tree.
package hsmpqexplorer
import (
"log"
"os"
"path"
"path/filepath"
"strings"
"sync"
g "github.com/ianling/giu"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
"github.com/OpenDiablo2/HellSpawner/hscommon"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsproject"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsstate"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsutil"
"github.com/OpenDiablo2/HellSpawner/hsconfig"
"github.com/OpenDiablo2/HellSpawner/hswidget"
"github.com/OpenDiablo2/HellSpawner/hswindow/hstoolwindow"
)
const (
mainWindowW, mainWindowH = 300, 400
)
// MPQExplorerFileSelectedCallback represents file selected callback
type MPQExplorerFileSelectedCallback func(path *hscommon.PathEntry)
// MPQExplorer represents a mpq explorer
type MPQExplorer struct {
*hstoolwindow.ToolWindow
config *hsconfig.Config
project *hsproject.Project
fileSelectedCallback MPQExplorerFileSelectedCallback
nodeCache []g.Widget
filesToOverwrite []fileToOverwrite
}
type fileToOverwrite struct {
Path string
Data []byte
}
// Create creates a new explorer
func Create(fileSelectedCallback MPQExplorerFileSelectedCallback, config *hsconfig.Config, x, y float32) (*MPQExplorer, error) {
result := &MPQExplorer{
ToolWindow: hstoolwindow.New("MPQ Explorer", hsstate.ToolWindowTypeMPQExplorer, x, y),
fileSelectedCallback: fileSelectedCallback,
config: config,
}
if w, h := result.CurrentSize(); w == 0 || h == 0 {
result.Size(mainWindowW, mainWindowH)
}
return result, nil
}
// SetProject sets explorer's project
func (m *MPQExplorer) SetProject(project *hsproject.Project) {
m.project = project
}
// Build builds an explorer
func (m *MPQExplorer) Build() {
if m.project == nil {
return
}
needToShowOverwritePrompt := len(m.filesToOverwrite) > 0
if needToShowOverwritePrompt {
m.IsOpen(&needToShowOverwritePrompt).Layout(g.Layout{
g.PopupModal("Overwrite File?").IsOpen(&needToShowOverwritePrompt).Layout(g.Layout{
g.Label("File at " + m.filesToOverwrite[0].Path + " already exists. Overwrite?"),
g.Row(
g.Button("Overwrite").OnClick(func() {
success := hsutil.CreateFileAtPath(m.filesToOverwrite[0].Path, m.filesToOverwrite[0].Data)
if success {
m.project.InvalidateFileStructure()
}
m.filesToOverwrite = m.filesToOverwrite[1:]
}),
g.Button("Cancel").OnClick(func() {
m.filesToOverwrite = m.filesToOverwrite[1:]
}),
),
}),
})
return
}
m.IsOpen(&m.Visible).
Size(mainWindowW, mainWindowH).
Layout(g.Layout{
g.Child("MpqExplorerContent").
Border(false).
Flags(g.WindowFlagsHorizontalScrollbar).
Layout(m.GetMpqTreeNodes()...),
})
}
// GetMpqTreeNodes returns mpq tree
func (m *MPQExplorer) GetMpqTreeNodes() []g.Widget {
if m.nodeCache != nil {
return m.nodeCache
}
wg := sync.WaitGroup{}
result := make([]g.Widget, len(m.project.AuxiliaryMPQs))
wg.Add(len(m.project.AuxiliaryMPQs))
for mpqIndex := range m.project.AuxiliaryMPQs {
go func(idx int) {
fullPath := filepath.Join(m.config.AuxiliaryMpqPath, m.project.AuxiliaryMPQs[idx])
mpq, err := d2mpq.FromFile(fullPath)
if err != nil {
log.Printf("failed to load mpq: %s", fullPath)
}
if mpq != nil {
nodes := m.project.GetMPQFileNodes(mpq, m.config)
result[idx] = m.renderNodes(nodes)
}
wg.Done()
}(mpqIndex)
}
wg.Wait()
m.nodeCache = result
return result
}
func (m *MPQExplorer) renderNodes(pathEntry *hscommon.PathEntry) g.Widget {
if !pathEntry.IsDirectory {
id := generatePathEntryID(pathEntry)
return g.Layout{
g.Selectable(pathEntry.Name + id),
hswidget.OnDoubleClick(func() { m.fileSelectedCallback(pathEntry) }),
g.ContextMenu("Context" + id).Layout(g.Layout{
g.Selectable("Copy to Project").OnClick(func() {
m.copyToProject(pathEntry)
}),
}),
}
}
widgets := make([]g.Widget, len(pathEntry.Children))
hscommon.SortPaths(pathEntry)
wg := sync.WaitGroup{}
wg.Add(len(pathEntry.Children))
for childIdx := range pathEntry.Children {
go func(idx int) {
widgets[idx] = m.renderNodes(pathEntry.Children[idx])
wg.Done()
}(childIdx)
}
wg.Wait()
return g.TreeNode(pathEntry.Name).Layout(widgets...)
}
func (m *MPQExplorer) copyToProject(pathEntry *hscommon.PathEntry) {
data, err := pathEntry.GetFileBytes()
if err != nil {
log.Printf("failed to read file %s when copying to project: %s", pathEntry.FullPath, err)
return
}
pathToFile := pathEntry.FullPath
if strings.HasPrefix(pathEntry.FullPath, "data") {
// strip "data" from the beginning of the path if it exists
pathToFile = pathToFile[4:]
}
pathToFile = path.Join(m.project.GetProjectFileContentPath(), pathToFile)
pathToFile = strings.ReplaceAll(pathToFile, "\\", "/")
if _, err := os.Stat(pathToFile); err == nil {
// file already exists
fileInfo := fileToOverwrite{
Path: pathToFile,
Data: data,
}
m.filesToOverwrite = append(m.filesToOverwrite, fileInfo)
return
}
success := hsutil.CreateFileAtPath(pathToFile, data)
if success {
m.project.InvalidateFileStructure()
}
}
func generatePathEntryID(pathEntry *hscommon.PathEntry) string {
return "##MPQExplorerNode_" + pathEntry.FullPath
}
// Reset resets the explorer
func (m *MPQExplorer) Reset() {
m.nodeCache = nil
}