-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
328 lines (259 loc) · 6.92 KB
/
app.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"context"
"fmt"
"log"
"os/exec"
"path/filepath"
"strings"
"github.com/fly2z/aeromod/internal/config"
"github.com/fly2z/aeromod/internal/msfs"
"github.com/fly2z/aeromod/utils"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
const (
CONFIG_COMMUNITY_FOLDER = "community_folder"
CONFIG_MOD_FOLDER = "mod_folder"
CONFIG_ENABLE_ON_INSTALL = "enable_on_install"
)
type App struct {
ctx context.Context
config *config.AppConfig
setupComplete bool
msfsClient *msfs.Client
}
func NewApp(config *config.AppConfig) *App {
return &App{config: config}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
err := a.createMSFSClient()
if err != nil {
return
}
a.setupComplete = true
}
func (a *App) reloadConfig() error {
err := config.Load(a.config)
if err != nil {
return err
}
if err := a.createMSFSClient(); err != nil {
log.Printf("failed to update msfs client: %v\n", err)
}
return nil
}
func (a *App) createMSFSClient() error {
communityFolder := a.config.CommunityFolder
if communityFolder == "" {
return fmt.Errorf("community folder path cannot be empty")
}
modFolder := a.config.ModFolder
if modFolder == "" {
return fmt.Errorf("mod folder path cannot be empty")
}
a.msfsClient = msfs.NewClient(communityFolder, modFolder)
return nil
}
func (a *App) IsSetupComplete() bool {
return a.setupComplete
}
func (a *App) GetSettings() config.AppConfig {
return *a.config
}
func (a *App) SetSetting(key string, val interface{}) error {
err := config.Set(key, val)
if err != nil {
return err
}
return a.reloadConfig()
}
func (a *App) FindSimCommunityFolder() (string, error) {
path, found := msfs.FindSimCommunityFolder()
if !found {
return "", fmt.Errorf("failed to find sim community directory")
}
return path, nil
}
func (a *App) CompleteSetup(communityPath, modPath string) error {
if communityPath == "" {
return fmt.Errorf("community folder path cannot be empty")
}
if modPath == "" {
return fmt.Errorf("mod folder path cannot be empty")
}
if err := config.Set(CONFIG_COMMUNITY_FOLDER, communityPath); err != nil {
return err
}
if err := config.Set(CONFIG_MOD_FOLDER, modPath); err != nil {
return err
}
if err := a.reloadConfig(); err != nil {
return fmt.Errorf("failed to reload config")
}
a.setupComplete = true
return nil
}
func (a *App) GetModNames() []string {
if a.msfsClient == nil {
return []string{}
}
return a.msfsClient.GetModNames()
}
func (a *App) GetAllMods() []msfs.Mod {
if a.msfsClient == nil {
return []msfs.Mod{}
}
var mods []msfs.Mod
modNames := a.msfsClient.GetModNames()
for _, name := range modNames {
enabled, err := a.msfsClient.IsModEnabled(name)
if err != nil {
log.Printf("failed to check mod status: %v\n", err)
mods = append(mods, msfs.Mod{Name: name, Type: "SCENERY", Enabled: false})
continue
}
manifest, err := a.msfsClient.ParsePackageManifest(name)
if err != nil {
log.Printf("failed to parse mod manifest: %v\n", err)
mods = append(mods, msfs.Mod{Name: name, Type: "UNKNOWN", Enabled: enabled})
continue
}
ct := strings.ToUpper(strings.TrimSpace(manifest.ContentType))
if ct == "" {
mods = append(mods, msfs.Mod{Name: name, Type: "UNKNOWN", Enabled: enabled})
continue
}
mods = append(mods, msfs.Mod{Name: name, Type: ct, Enabled: enabled})
}
return mods
}
func (a *App) GetModManifest(packageName string) msfs.PackageManifest {
if a.msfsClient == nil {
return msfs.PackageManifest{}
}
manifest, err := a.msfsClient.ParsePackageManifest(packageName)
if err != nil {
log.Printf("failed to parse package manifest: %v\n", err)
return msfs.PackageManifest{}
}
return *manifest
}
func (a *App) EnableMod(modName string) error {
if a.msfsClient == nil {
return fmt.Errorf("msfs client not initialized")
}
if err := a.msfsClient.EnableMod(modName); err != nil {
return fmt.Errorf("error enabling mod: %w", err)
}
return nil
}
func (a *App) DisableMod(modName string) error {
if a.msfsClient == nil {
return fmt.Errorf("msfs client not initialized")
}
if err := a.msfsClient.DisableMod(modName); err != nil {
return fmt.Errorf("error disabling mod: %w", err)
}
return nil
}
func (a *App) UninsallMod(modName string) error {
if a.msfsClient == nil {
return fmt.Errorf("msfs client not initialized")
}
enabled, err := a.msfsClient.IsModEnabled(modName)
if err != nil {
log.Printf("failed to check mod status: %v\n", err)
return fmt.Errorf("failed to check mod status: %w", err)
}
if enabled {
err := a.msfsClient.DisableMod(modName)
if err != nil {
return fmt.Errorf("error disabling mod: %w", err)
}
}
if err := a.msfsClient.UninstallMod(modName); err != nil {
return fmt.Errorf("error uninstalling mod: %w", err)
}
return nil
}
func (a *App) OpenDirectoryDialog(title string) (string, error) {
dirPath, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: title,
})
if err != nil {
return "", fmt.Errorf("failed to open directory dialog: %w", err)
}
return dirPath, nil
}
func (a *App) GetModThumbnail(modName string) (string, error) {
if a.msfsClient == nil {
return "", fmt.Errorf("msfs client not initialized")
}
thumbnail, err := a.msfsClient.GetModThumbnailBase64(modName)
if err != nil {
return "", fmt.Errorf("failed to get mod thumbnail: %w", err)
}
return thumbnail, nil
}
func (a *App) InstallMod() (bool, error) {
archive, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Choose Mod",
})
if err != nil {
return false, fmt.Errorf("failed to open directory dialog: %w", err)
}
if archive == "" {
return false, nil
}
ext := filepath.Ext(archive)
if ext != ".zip" {
return false, fmt.Errorf("unsupported archive format")
}
archiveDest := strings.TrimSuffix(archive, ext)
err = utils.Unzip(archive, archiveDest)
if err != nil {
return false, err
}
defer func() {
err := utils.Rmdir(archiveDest)
if err != nil {
log.Printf("failed to cleanup extracted archive: %s\n", filepath.Base(archiveDest))
}
}()
mods, err := msfs.FindMods(archiveDest)
if err != nil {
return false, fmt.Errorf("failed to find mods: %w", err)
}
if len(mods) < 1 {
return false, nil
}
for _, m := range mods {
name := filepath.Base(m)
err := utils.CopyDir(m, filepath.Join(a.config.ModFolder, name))
if err != nil {
log.Printf("failed to move mod: %v\n", err)
continue
}
if a.config.EnableOnInstall {
err = a.EnableMod(name)
if err != nil {
log.Printf("failed to enable mod: %v\n", err)
}
}
}
return true, nil
}
func (a *App) VerifyMod(name string) ([]msfs.VerificationResult, error) {
result, err := a.msfsClient.VerifyMod(name)
if err != nil {
log.Println(err)
return []msfs.VerificationResult{}, err
}
return result, nil
}
func (a *App) RevealMod(name string) error {
cmd := exec.Command("explorer", a.msfsClient.GetModPath(name))
// cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return cmd.Run()
}