From bca9bec6d6a5283e42e6a08f21a6c17092ef3cbf Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Thu, 24 Oct 2024 19:48:19 -0600 Subject: [PATCH] install prism, and display status in ui --- fclauncher/Prism.go | 87 +++++++++++++++++++++++++- fclauncher/app.go | 20 ++++-- fclauncher/frontend/src/App.svelte | 2 +- fclauncher/frontend/src/Client.svelte | 1 - fclauncher/frontend/src/Loading.svelte | 29 +++++++++ fclauncher/go.mod | 11 ++-- fclauncher/go.sum | 18 +++--- fclauncher/https.go | 13 +++- 8 files changed, 156 insertions(+), 25 deletions(-) diff --git a/fclauncher/Prism.go b/fclauncher/Prism.go index ab748f8..4918834 100644 --- a/fclauncher/Prism.go +++ b/fclauncher/Prism.go @@ -1,12 +1,21 @@ package main import ( + "archive/tar" + "bytes" + "compress/gzip" + "context" + "io" "os" "path/filepath" + "runtime" + + "github.com/zhyee/zipstream" ) type Prism struct { Instances []Instance + ctx context.Context } type Instance struct { @@ -26,6 +35,82 @@ func (Prism) CheckInstalled() bool { } } -func (Prism) Install() { +func (p *Prism) Install() { + suffix := "lin.tar.gz" + if runtime.GOOS == "windows" { + suffix = "win.zip" + } + buff := new(bytes.Buffer) + HttpDownload("prism/prism-"+suffix, buff, p.ctx) + path, _ := os.UserConfigDir() + os.MkdirAll(filepath.Join(path, "FCLauncher", "prism"), 0755) + if runtime.GOOS == "windows" { + zr := zipstream.NewReader(buff) + for { + entry, err := zr.GetNextEntry() + if err == io.EOF { + break + } + if err != nil { + return + } + target := filepath.Join(path, "FCLauncher", "prism", entry.Name) + if !entry.IsDir() { + rc, err := entry.Open() + if err != nil { + return + } + f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, entry.FileInfo().Mode()) + if err != nil { + return + } + if _, err := io.Copy(f, rc); err != nil { + return + } + f.Close() + rc.Close() + } else { + if _, err := os.Stat(target); err != nil { + if err := os.MkdirAll(target, 0755); err != nil { + return + } + } + } + } + } else { + gzip, _ := gzip.NewReader(buff) + defer gzip.Close() + tr := tar.NewReader(gzip) + out: + for { + header, err := tr.Next() + switch { + case err == io.EOF: + break out + case err != nil: + return + case header == nil: + continue + } + target := filepath.Join(path, "FCLauncher", "prism", header.Name) + switch header.Typeflag { + case tar.TypeDir: + if _, err := os.Stat(target); err != nil { + if err := os.MkdirAll(target, 0755); err != nil { + return + } + } + case tar.TypeReg: + f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) + if err != nil { + return + } + if _, err := io.Copy(f, tr); err != nil { + return + } + f.Close() + } + } + } } diff --git a/fclauncher/app.go b/fclauncher/app.go index d429130..746638d 100644 --- a/fclauncher/app.go +++ b/fclauncher/app.go @@ -6,6 +6,8 @@ import ( "encoding/json" "fmt" "io" + + "github.com/wailsapp/wails/v2/pkg/runtime" ) // App struct @@ -38,7 +40,7 @@ func (a *App) Greet(name string) string { func (a *App) GetModpacks() []string { buff := new(bytes.Buffer) - err := HttpDownload("modpacks.json", buff) + err := HttpDownload("modpacks.json", buff, nil) if err != nil { fmt.Println(err) } @@ -57,14 +59,20 @@ func (a *App) GetModpacks() []string { } func (a *App) CheckPrerequisites() { - a.prism = Prism{} + a.status("Checking Prism") + a.prism = Prism{ctx: a.ctx} res := a.prism.CheckInstalled() - fmt.Printf("Checking if prism is installed: ") if res { - fmt.Println("Yes") + a.status("Prism OK") } else { - fmt.Println("No") - fmt.Println("Installing Prism") + a.status("Prism MISSING") + a.status("Installing Prism") a.prism.Install() + a.status("Prism Installed") } } + +func (a *App) status(status string) { + fmt.Printf("LOG: %s\n", status) + runtime.EventsEmit(a.ctx, "status", status) +} diff --git a/fclauncher/frontend/src/App.svelte b/fclauncher/frontend/src/App.svelte index f581d65..b26cf7d 100644 --- a/fclauncher/frontend/src/App.svelte +++ b/fclauncher/frontend/src/App.svelte @@ -8,7 +8,7 @@ let loading: boolean = true onMount(() => { - CheckPrerequisites().then(() => loading = false) + CheckPrerequisites().then(() => loading = true) }) diff --git a/fclauncher/frontend/src/Client.svelte b/fclauncher/frontend/src/Client.svelte index 09e0acd..5cb4bcc 100644 --- a/fclauncher/frontend/src/Client.svelte +++ b/fclauncher/frontend/src/Client.svelte @@ -5,7 +5,6 @@ let modpacks: string[] = [] onMount(() => { - console.log("mounted") GetModpacks().then((result) => { modpacks = result }) diff --git a/fclauncher/frontend/src/Loading.svelte b/fclauncher/frontend/src/Loading.svelte index e69de29..4526f51 100644 --- a/fclauncher/frontend/src/Loading.svelte +++ b/fclauncher/frontend/src/Loading.svelte @@ -0,0 +1,29 @@ + + +
+

{stat}

+ {#if downloading} +

{completed}MB / {total}MB

+ {/if} +
\ No newline at end of file diff --git a/fclauncher/go.mod b/fclauncher/go.mod index c256acb..d47cb1f 100644 --- a/fclauncher/go.mod +++ b/fclauncher/go.mod @@ -1,6 +1,6 @@ module fclauncher -go 1.21 +go 1.22.4 toolchain go1.23.2 @@ -14,23 +14,24 @@ require ( github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect github.com/labstack/echo/v4 v4.10.2 // indirect github.com/labstack/gommon v0.4.0 // indirect - github.com/leaanthony/go-ansi-parser v1.6.0 // indirect + github.com/leaanthony/go-ansi-parser v1.6.1 // indirect github.com/leaanthony/gosod v1.0.3 // indirect github.com/leaanthony/slicer v1.6.0 // indirect github.com/leaanthony/u v1.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/rivo/uniseg v0.4.4 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/samber/lo v1.38.1 // indirect github.com/tkrajina/go-reflector v0.5.6 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect github.com/wailsapp/go-webview2 v1.0.16 // indirect github.com/wailsapp/mimetype v1.4.1 // indirect + github.com/zhyee/zipstream v0.0.0-20230625125559-133d8d1afaa0 golang.org/x/crypto v0.23.0 // indirect - golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect + golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect diff --git a/fclauncher/go.sum b/fclauncher/go.sum index 6ed0576..b3caafc 100644 --- a/fclauncher/go.sum +++ b/fclauncher/go.sum @@ -17,8 +17,8 @@ github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8 github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= github.com/leaanthony/debme v1.2.1 h1:9Tgwf+kjcrbMQ4WnPcEIUcQuIZYqdWftzZkBr+i/oOc= github.com/leaanthony/debme v1.2.1/go.mod h1:3V+sCm5tYAgQymvSOfYQ5Xx2JCr+OXiD9Jkw3otUjiA= -github.com/leaanthony/go-ansi-parser v1.6.0 h1:T8TuMhFB6TUMIUm0oRrSbgJudTFw9csT3ZK09w0t4Pg= -github.com/leaanthony/go-ansi-parser v1.6.0/go.mod h1:+vva/2y4alzVmmIEpk9QDhA7vLC5zKDTRwfZGOp3IWU= +github.com/leaanthony/go-ansi-parser v1.6.1 h1:xd8bzARK3dErqkPFtoF9F3/HgN8UQk0ed1YDKpEz01A= +github.com/leaanthony/go-ansi-parser v1.6.1/go.mod h1:+vva/2y4alzVmmIEpk9QDhA7vLC5zKDTRwfZGOp3IWU= github.com/leaanthony/gosod v1.0.3 h1:Fnt+/B6NjQOVuCWOKYRREZnjGyvg+mEhd1nkkA04aTQ= github.com/leaanthony/gosod v1.0.3/go.mod h1:BJ2J+oHsQIyIQpnLPjnqFGTMnOZXDbvWtRCSG7jGxs4= github.com/leaanthony/slicer v1.5.0/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY= @@ -33,8 +33,8 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= -github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -42,8 +42,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= -github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -63,10 +63,12 @@ github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhw github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o= github.com/wailsapp/wails/v2 v2.9.2 h1:Xb5YRTos1w5N7DTMyYegWaGukCP2fIaX9WF21kPPF2k= github.com/wailsapp/wails/v2 v2.9.2/go.mod h1:uehvlCwJSFcBq7rMCGfk4rxca67QQGsbg5Nm4m9UnBs= +github.com/zhyee/zipstream v0.0.0-20230625125559-133d8d1afaa0 h1:BcjUUYzMORs7sJtOCWLXaelG7woHMN1QEs4yCB1QZ48= +github.com/zhyee/zipstream v0.0.0-20230625125559-133d8d1afaa0/go.mod h1:aaGtAo3dTqYtHjcliPNlyXMIIodvGm8y6uK2KMTYHrk= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= -golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= diff --git a/fclauncher/https.go b/fclauncher/https.go index 9360166..74528b6 100644 --- a/fclauncher/https.go +++ b/fclauncher/https.go @@ -1,14 +1,16 @@ package main import ( - "fmt" + "context" "io" "net/http" + + "github.com/wailsapp/wails/v2/pkg/runtime" ) const BlockSize = 1024 -func HttpDownload(path string, out io.Writer) error { +func HttpDownload(path string, out io.Writer, ctx context.Context) error { res, err := http.Get("https://gitea.piwalker.net/fclauncher/" + path) if err != nil { return err @@ -18,10 +20,15 @@ func HttpDownload(path string, out io.Writer) error { for read < res.ContentLength { count, err := io.CopyN(out, res.Body, BlockSize) read += count - fmt.Printf("Downloaded %dMB / %dMB\n", read/(1024*1024), res.ContentLength/(1024*1024)) if err != nil { break } + if ctx != nil { + runtime.EventsEmit(ctx, "download", read, res.ContentLength) + } + } + if ctx != nil { + runtime.EventsEmit(ctx, "download_complete") } return nil }