From 8efd3a5631d67b823a1bff61ea1fc39b1f86d519 Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Fri, 25 Oct 2024 20:46:06 -0600 Subject: [PATCH] Added support for installing java versions --- fclauncher/Java.go | 121 +++++++++++++++++++++++++ fclauncher/app.go | 12 +++ fclauncher/frontend/src/App.svelte | 3 + fclauncher/frontend/src/Loading.svelte | 2 +- fclauncher/main.go | 2 +- 5 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 fclauncher/Java.go diff --git a/fclauncher/Java.go b/fclauncher/Java.go new file mode 100644 index 0000000..98a5265 --- /dev/null +++ b/fclauncher/Java.go @@ -0,0 +1,121 @@ +package main + +import ( + "archive/tar" + "bytes" + "compress/gzip" + "context" + "fmt" + "io" + "os" + "path/filepath" + "runtime" + "strings" + + "github.com/zhyee/zipstream" +) + +type Java struct { + ctx context.Context +} + +func (Java)CheckJavaVer(version int) bool{ + suffix := "lin" + if runtime.GOOS == "windows" { + suffix = "win" + } + path, _ := os.UserConfigDir() + path = filepath.Join(path, "FCLauncher", "java", fmt.Sprintf("java-%d-%s", version, suffix)) + + _, err := os.Stat(path) + if err == nil { + return true + } + return false + +} + +func (j *Java)InstallJavaVer(version int) { + suffix := "lin.tar.gz" + if runtime.GOOS == "windows" { + suffix = "win.zip" + } + buff := new(bytes.Buffer) + HttpDownload("java/"+fmt.Sprintf("java-%d-%s", version, suffix), buff, j.ctx) + path, _ := os.UserConfigDir() + suffix = "lin" + if runtime.GOOS == "windows" { + suffix = "win" + } + + path = filepath.Join(path, "FCLauncher", "java", fmt.Sprintf("java-%d-%s", version, suffix)) + os.MkdirAll(path, 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, strings.SplitN(entry.Name, string(os.PathSeparator), 2)[1]) + 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, strings.SplitN(header.Name, string(os.PathSeparator), 2)[1]) + 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 746638d..a5d4c0b 100644 --- a/fclauncher/app.go +++ b/fclauncher/app.go @@ -14,6 +14,7 @@ import ( type App struct { ctx context.Context prism Prism + java Java } type Modpack struct { @@ -70,6 +71,17 @@ func (a *App) CheckPrerequisites() { a.prism.Install() a.status("Prism Installed") } + a.status("Checking Java 21") + a.java = Java{ctx: a.ctx} + res = a.java.CheckJavaVer(21) + if res { + a.status("Java 21 OK") + } else { + a.status("Java 21 MISSING") + a.status("Installing Java 21") + a.java.InstallJavaVer(21) + a.status("Java 21 Installed") + } } func (a *App) status(status string) { diff --git a/fclauncher/frontend/src/App.svelte b/fclauncher/frontend/src/App.svelte index b26cf7d..df0e6b5 100644 --- a/fclauncher/frontend/src/App.svelte +++ b/fclauncher/frontend/src/App.svelte @@ -6,17 +6,20 @@ import { onMount } from 'svelte' let loading: boolean = true + let width: int = 10 onMount(() => { CheckPrerequisites().then(() => loading = true) }) +
{#if loading} +
tes
{:else} {/if} diff --git a/fclauncher/frontend/src/Loading.svelte b/fclauncher/frontend/src/Loading.svelte index 4526f51..76fb9d4 100644 --- a/fclauncher/frontend/src/Loading.svelte +++ b/fclauncher/frontend/src/Loading.svelte @@ -26,4 +26,4 @@ {#if downloading}

{completed}MB / {total}MB

{/if} -
\ No newline at end of file + diff --git a/fclauncher/main.go b/fclauncher/main.go index 8264058..d88a32e 100644 --- a/fclauncher/main.go +++ b/fclauncher/main.go @@ -2,7 +2,7 @@ package main import ( "embed" - + "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver"