-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
310 lines (250 loc) · 8.95 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
package main
import (
"bufio"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
cp "github.com/otiai10/copy"
"github.com/sqweek/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) DomReady(ctx context.Context) {
debugLog("Dom ready!")
runtime.EventsEmit(ctx, "AVAILABLE_CONVERTERS", ConvertersMap)
runtime.EventsEmit(ctx, "DOM_READY", map[string]interface{}{
"game": getGameInstallDirectory(),
})
var updateState, updateMessage = checkForUpdate(a)
var updateType = ""
debugLog(fmt.Sprintf("Updatestate: %d, updatemessage: %s", updateState, updateMessage))
if updateState == 0 {
updateType = "none"
} else if updateState == 1 {
updateType = "available"
} else {
updateType = "error"
}
var emitData = map[string]interface{}{
"state": updateType,
"message": updateMessage,
}
runtime.EventsEmit(ctx, "UPDATE_CHECK_INFO", map[string]interface{}{
"data": emitData,
})
}
func (a *App) ChooseManifest() map[string]interface{} {
filename, err := dialog.File().Filter("Mod Manifest (manifest.json)", "json").Title("Choose Input Content Pack manifest.json").Load()
if err != nil {
debugLog("failed to select file")
return map[string]interface{}{
"filename": nil,
"content": nil,
}
}
manifest, err := os.ReadFile(filename)
if err != nil {
debugLog("failed to os.ReadFile")
return map[string]interface{}{
"filename": nil,
"content": nil,
}
}
debugLog(fmt.Sprintf("Chose %s / Manifest: <<%s>>", filename, string(manifest)))
return map[string]interface{}{
"filename": filename,
"content": string(manifest),
}
}
func (a *App) ConvertMod(manifest map[string]interface{}, converterName string, manifestPath string) string {
var converter = ConvertersMap[converterName]
debugLog("converting mod using " + converter.Name)
if !CheckCompatibleGame(converter) {
return "error|This converter is not compatible with your game install!"
}
if !converter.SupportsManifest(manifest["content"].(map[string]interface{})) {
return "error|This converter is not compatible with this content pack!"
}
var downloadDirName = fmt.Sprintf("%s_%s", converter.Name, randomString(5))
var downloadDir = filepath.Join(localFilesRootDirectory, downloadDirName)
debugLog(converter.GitFile)
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", fmt.Sprintf("[GUI] Cloning %s", converter.Name))
if _, err := os.Stat(gitExecutable); errors.Is(err, os.ErrNotExist) {
debugLog(fmt.Sprintf("%s is not found. (ConvertMod)", gitExecutable))
return "error|Git is not installed! Try restart the prorgam."
}
var gitCloneCmd = runSimpleCommand(fmt.Sprintf(
"& \"%s\" clone %s \"%s\" --recurse-submodules", gitExecutable, converter.GitFile, downloadDir,
))
if err := gitCloneCmd.Start(); err != nil {
return "error|git clone failed to start with error " + err.Error()
}
// Wait for the command to finish
if err := gitCloneCmd.Wait(); err != nil {
return "error|git clone completed with error " + err.Error()
}
// time.Sleep(30 * time.Second) // pause for debugging.
var modFolder = filepath.Dir(manifestPath)
var inputFolder = createFolderIfNeeded(filepath.Join(downloadDir, converter.InputDirectory))
var outputFolder = createFolderIfNeeded(filepath.Join(downloadDir, converter.OutputDirectory))
if converter.IsPython {
err := updatePackageResolution(filepath.Join(downloadDir, converter.MainFile))
if err != nil {
debugLog(fmt.Sprintf("failed to update package resolution %s", err.Error()))
return fmt.Sprintf("error|Failed change directory with error %s", err.Error())
}
}
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", fmt.Sprintf("[GUI] Copying %s to input directory",
manifest["content"].(map[string]interface{})["Name"].(string)))
_ = cp.Copy(modFolder, inputFolder)
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", "[GUI] Installing dependencies...")
var reqsCmd *exec.Cmd
if converter.IsPython {
reqsCmd = runSimpleCommand(fmt.Sprintf(
"& \"%s\" -m pip install -r \"%s\"", pyExecutable, filepath.Join(downloadDir, converter.RequirementsFile),
))
} else if converter.Name == "Convert To Fashion Sense JA ONLY" {
reqsCmd = runSimpleCommand(fmt.Sprintf(
"& \"%s\" install", npmExecutable,
))
reqsCmd.Dir = downloadDir
}
reqsStdout, err := reqsCmd.StdoutPipe()
if err != nil {
debugLog(fmt.Sprintf("Error creating stdout pipe: %v\n", err))
}
reqsSterr, err := reqsCmd.StderrPipe()
if err != nil {
debugLog(fmt.Sprintf("Error creating stderr pipe: %v\n", err))
}
if err := reqsCmd.Start(); err != nil {
var r = "error|Dependencies installation failed to start with error " + err.Error()
debugLog(r)
return r
}
reqsOutScanner := bufio.NewScanner(reqsStdout)
reqsErrScanner := bufio.NewScanner(reqsSterr)
for reqsOutScanner.Scan() {
debugLog("stdout:" + reqsOutScanner.Text())
if strings.Contains(reqsOutScanner.Text(), "Successfully installed") {
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", "[GUI] "+reqsOutScanner.Text())
}
}
for reqsErrScanner.Scan() {
debugLog("stderr:" + reqsErrScanner.Text())
}
converter.ModifyConfig(filepath.Join(downloadDir, "config.json"))
if err := reqsCmd.Wait(); err != nil {
var r = "error|Dependencies installation completed with error " + err.Error()
debugLog(fmt.Sprintf("Failed to install dependencies %v // %s", err, err.Error()))
debugLog(r)
return r
}
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", fmt.Sprintf("[GUI] Running %s", converter.Name))
old_wd, _ := os.Getwd()
var chdir1_err = os.Chdir(downloadDir)
if chdir1_err != nil {
return fmt.Sprintf("error|Failed change directory with error %s", err.Error())
}
wd, wde := os.Getwd()
debugLog(fmt.Sprintf("PWD: %s - possible err %s", wd, wde))
var mainRunCmd *exec.Cmd
if converter.IsPython {
mainRunCmd = runSimpleCommand(fmt.Sprintf(
"& \"%s\" -u \"%s\" %s", pyExecutable, converter.MainFile, converter.ExtraArgs,
))
} else if converter.Name == "Convert To Fashion Sense JA ONLY" {
var conversionType string
if checkExists(filepath.Join(modFolder, "Hats")) {
debugLog("converter type hats")
conversionType = "hat"
} else if checkExists(filepath.Join(modFolder, "Shirts")) {
debugLog("converter type shirts")
conversionType = "shirt"
} else {
debugLog("converter type failed to find type. just will ignore and hope it works")
}
var authorName = manifest["content"].(map[string]interface{})["Author"].(string)
mainRunCmd = runSimpleCommand(fmt.Sprintf(
"& \"%s\" %s --conversionType=%s --authorName=%s", nodeExecutable, converter.MainFile, conversionType, authorName,
))
mainRunCmd.Dir = downloadDir
}
mainStdout, err := mainRunCmd.StdoutPipe()
if err != nil {
debugLog(fmt.Sprintf("Error creating stdout pipe: %v\n", err))
}
mainStdoutScanner := bufio.NewScanner(mainStdout)
mainSterr, err := mainRunCmd.StderrPipe()
if err != nil {
debugLog(fmt.Sprintf("Error creating sterr pipe: %v\n", err))
}
mainStderrScanner := bufio.NewScanner(mainSterr)
if err := mainRunCmd.Start(); err != nil {
var r = fmt.Sprintf("error|%s failed to start with error %s", converter.Name, err.Error())
debugLog(r)
return r
}
for mainStdoutScanner.Scan() {
var txt = mainStdoutScanner.Text()
debugLog(txt)
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", txt)
}
for mainStderrScanner.Scan() {
var txt = mainStderrScanner.Text()
debugLog(txt)
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", "ERR: "+txt)
}
if err := mainRunCmd.Wait(); err != nil {
var r = fmt.Sprintf("error|%s completed with error %s", converter.Name, err.Error())
debugLog(r)
return r
}
var chdir2_err = os.Chdir(old_wd)
if chdir2_err != nil {
var r = fmt.Sprintf("error|Failed change directory with error %s", err.Error())
debugLog(r)
return r
}
var newOutputFolder = createFolderIfNeeded(filepath.Join(localFilesRootDirectory, "conversions",
fmt.Sprintf("%s-converted-%s_%s",
manifest["content"].(map[string]interface{})["UniqueID"].(string), converter.Name, randomString(8),
),
))
showFolder(newOutputFolder)
_ = cp.Copy(outputFolder, newOutputFolder)
var zipPath = newOutputFolder + ".zip"
var zipErr = zipFolder(outputFolder, zipPath)
if zipErr != nil {
debugLog("Failed to zip mod")
debugLog(zipErr.Error())
} else {
debugLog(fmt.Sprintf("Completed mod conversion to zip: %s", zipPath))
runtime.EventsEmit(a.ctx, "CONVERTER_MOD_DONE", zipPath)
}
runtime.EventsEmit(a.ctx, "OUTPUT_CONVERTER_TEXT", fmt.Sprintf("[GUI] %s completed!", converter.Name))
os.RemoveAll(downloadDir) // Clean up afterwards
return "success|Conversion complete! The converted mod has been opened in explorer.."
}
func (a *App) ShowFolderInExplorer(path string) {
showFolder(path)
}
func (a *App) ShowDebugLogsFolder() {
showFolder(logDirectory)
}