-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
345 lines (288 loc) · 8.49 KB
/
main.go
File metadata and controls
345 lines (288 loc) · 8.49 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* Spectrum-Bootstrap - A bootstrap for Minecraft launchers
* Copyright (C) 2023-2024 - Oxodao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
package main
import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
//go:embed bs_settings.json
var BOOTSTRAP_SETTINGS_STR []byte
var basepath *string
var BOOTSTRAP_VERSION = "1"
func init() {
basepath = flag.String("path", "", "The path to store launcher data (i.e. portable-mode)")
}
func main() {
bsVersion, err := strconv.Atoi(BOOTSTRAP_VERSION)
if err != nil {
fmt.Println("Failed to parse bootstrap version to an int!")
fmt.Println("Version found: ", BOOTSTRAP_VERSION)
panic(err)
}
flag.Parse()
app := app.New()
window := app.NewWindow("SpectrumBootstrap")
window.SetFixedSize(true)
window.Resize(fyne.NewSize(600, 200))
go func() {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("fetching_launcher_updates", nil)),
),
)
window.CenterOnScreen()
settings := BootstrapSettings{}
err := json.Unmarshal(BOOTSTRAP_SETTINGS_STR, &settings)
if len(*basepath) > 0 {
settings.LauncherPath = *basepath
}
settings.LauncherPath, err = GetLauncherDirectory(&settings)
if err != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("failed_init", map[string]string{"Err": err.Error()})),
),
)
window.CenterOnScreen()
return
}
window.SetTitle(settings.Brand + " - Bootstrap")
launcherManager, err := GetLauncherManager(&settings)
if err != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("failed_init", map[string]string{"Err": err.Error()})),
),
)
window.CenterOnScreen()
return
}
jvmManager, err := GetJvmManager(&settings, launcherManager.launcherManifest.Java)
if err != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("failed_init", map[string]string{"Err": err.Error()})),
),
)
window.CenterOnScreen()
return
}
// Always attempt to get the legacy JVM manager
jvmManagerLegacy, errLegacy := GetJvmManagerLegacy(&settings, launcherManager.launcherManifest.Java)
if errLegacy != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("failed_init", map[string]string{"Err": errLegacy.Error()})),
),
)
window.CenterOnScreen()
return
}
jvmFilesToDownload, err := jvmManager.ValidateInstallation()
if err != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("failed_init", map[string]string{"Err": err.Error()})),
),
)
window.CenterOnScreen()
return
}
jvmFilesToDownloadLegacy, err := jvmManagerLegacy.ValidateInstallationLegacy()
if err != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("failed_init", map[string]string{"Err": err.Error()})),
),
)
window.CenterOnScreen()
return
}
launcherFilesToDownload, err := launcherManager.ValidateInstallation()
if err != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("failed_init", map[string]string{"Err": err.Error()})),
),
)
window.CenterOnScreen()
return
}
filesToDownload := append(append(jvmFilesToDownload, jvmFilesToDownloadLegacy...), launcherFilesToDownload...)
timeLabel := widget.NewLabel("00:00:00")
mainProgressBar := widget.NewProgressBar()
filenameLabel := widget.NewLabel("-")
fileProgressBar := widget.NewProgressBar()
window.SetContent(container.NewVBox(
widget.NewLabel(Localize("downloading", nil)),
container.NewHBox(
widget.NewLabel(Localize("elapsed_time", nil)),
timeLabel,
),
mainProgressBar,
filenameLabel,
fileProgressBar,
))
processedFiles := 0
// @TODO Make this base on goroutine to download multiple files at once
var wg sync.WaitGroup
downloadedBytes := make([]int64, len(filesToDownload)) // To track downloaded bytes for each file
for i, f := range filesToDownload {
wg.Add(1) // Increment the wait group counter
go func(i int, f Downloadable) {
defer wg.Done() // Decrement the counter when the goroutine completes
err := os.MkdirAll(filepath.Dir(f.Path), os.ModePerm)
if err != nil {
window.SetContent(
container.NewVBox(
widget.NewLabel(Localize("fail_download", map[string]string{"Err": err.Error()})),
),
)
window.CenterOnScreen()
return
}
out, err := os.Create(f.Path)
if ShowError(window, "fail_download", err) {
return
}
defer out.Close()
done := make(chan int64)
go func() {
// Update progress for this specific file
for {
select {
case n := <-done:
downloadedBytes[i] += n
fileProgressBar.SetValue(float64(downloadedBytes[i]) / float64(f.Size))
}
}
}()
// @TODO: 3 Retries per file
req, err := http.NewRequest("GET", f.Url, nil)
if ShowError(window, "fail_download", err) {
return
}
req.Header.Set("User-Agent", "SpectrumBootstrap/"+BOOTSTRAP_VERSION)
resp, err := http.DefaultClient.Do(req)
if ShowError(window, "fail_download", err) {
return
}
defer resp.Body.Close()
n, err := io.Copy(out, resp.Body)
if ShowError(window, "fail_download", err) {
return
}
done <- n // Send the number of bytes downloaded
if f.Executable {
err := os.Chmod(f.Path, os.ModePerm)
if ShowError(window, "fail_download", err) {
return
}
}
processedFiles += 1
mainProgressBar.SetValue(float64(processedFiles) / float64(len(filesToDownload)))
}(i, f)
}
wg.Wait() // Wait for all downloads to complete
// Launching the launcher
executablePath := ""
classpathSeparator := ":"
if runtime.GOOS == "darwin" {
executablePath = "jre.bundle/Contents/Home/bin/java"
} else if runtime.GOOS == "linux" {
executablePath = "bin/java"
} else if runtime.GOOS == "windows" {
executablePath = "bin/javaw.exe"
classpathSeparator = ";"
} else {
panic("How did we get here?")
}
classpath := []string{}
for _, f := range launcherManager.launcherManifest.Files {
if f.Type == "classpath" {
classpath = append(classpath, filepath.Join(settings.LauncherPath, "launcher", f.Path))
}
}
variables := map[string]any{
"osArch": jvmManager.os,
"rootPath": settings.LauncherPath,
"bsVersion": bsVersion,
"isPortable": len(*basepath) > 0,
}
cmdStrArr := []string{
"-classpath",
strings.Join(classpath, classpathSeparator),
launcherManager.launcherManifest.MainClass,
}
for _, arg := range launcherManager.launcherManifest.Args {
val := arg
for k, v := range variables {
val = strings.ReplaceAll(val, "${"+k+"}", fmt.Sprintf("%v", v))
}
cmdStrArr = append(cmdStrArr, val)
}
cmd := exec.Command(
filepath.Join(jvmManager.GetPath(), executablePath),
cmdStrArr...,
)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = settings.LauncherPath
if err = cmd.Start(); err != nil {
fmt.Println("Failed to run the launcher:")
fmt.Println(err)
os.Exit(1)
}
window.Hide()
if err = cmd.Wait(); err != nil {
fmt.Println("Failed to run the launcher:")
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}()
window.ShowAndRun()
}
func ShowError(w fyne.Window, translation string, err error) bool {
if err != nil {
w.SetContent(
container.NewVBox(
widget.NewLabel(Localize(translation, nil)),
widget.NewLabel(err.Error()),
),
)
w.CenterOnScreen()
return true
}
return false
}