From 27c97010aedad4c65675721802e69f49a1a6125f Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Fri, 1 Nov 2024 05:55:41 -0600 Subject: [PATCH] Made instances list global --- fclauncher/InstanceManager.go | 104 +++++++++--------- fclauncher/frontend/src/Instances.svelte | 12 +- fclauncher/frontend/src/Testpage.svelte | 5 +- fclauncher/frontend/src/global.ts | 1 + .../wailsjs/go/main/InstanceManager.d.ts | 2 +- 5 files changed, 64 insertions(+), 60 deletions(-) diff --git a/fclauncher/InstanceManager.go b/fclauncher/InstanceManager.go index 4b0c0e8..b7113bb 100644 --- a/fclauncher/InstanceManager.go +++ b/fclauncher/InstanceManager.go @@ -17,22 +17,22 @@ import ( ) type Instance struct { - InstanceName string - ModpackId string - ModpackVersion string + InstanceName string + ModpackId string + ModpackVersion string MinecraftVersion string - ForgeVersion string - NeoForgeVersion string - FabricVersion string - QuiltVersion string - JavaVersion int - Libraries []string - MainClass string + ForgeVersion string + NeoForgeVersion string + FabricVersion string + QuiltVersion string + JavaVersion int + Libraries []string + MainClass string } type InstanceManager struct { instances []Instance - app *App + app *App } type mmcpack struct { @@ -40,11 +40,11 @@ type mmcpack struct { } type component struct { - Uid string + Uid string Version string } -func (i *InstanceManager)SearchInstances() { +func (i *InstanceManager) SearchInstances() { i.instances = []Instance{} dir, _ := os.UserConfigDir() dir = filepath.Join(dir, "FCLauncher", "instances") @@ -69,21 +69,21 @@ func (i *InstanceManager)SearchInstances() { } } -func (i *InstanceManager)checkJavaVersion(instance Instance){ +func (i *InstanceManager) checkJavaVersion(instance Instance) { infoPath := filepath.Join(i.app.PrismLauncher.GetInstanceDir(), instance.InstanceName, "mmc-pack.json") f, _ := os.OpenFile(infoPath, os.O_RDONLY, 0755) defer f.Close() - dataStr,_ := io.ReadAll(f) + dataStr, _ := io.ReadAll(f) var data mmcpack json.Unmarshal(dataStr, &data) mc_version := "0.0" for _, comp := range data.Components { if comp.Uid == "net.minecraft" { mc_version = comp.Version - break; + break } } - fmt.Printf("MC Version: %s",mc_version) + fmt.Printf("MC Version: %s", mc_version) tokensStr := strings.Split(mc_version, ".") tokens := []int{0, 0, 0} tokens[0], _ = strconv.Atoi(tokensStr[0]) @@ -103,11 +103,11 @@ func (i *InstanceManager)checkJavaVersion(instance Instance){ javaVer = 21 } } - fmt.Printf("Req Java Version: %d",javaVer) + fmt.Printf("Req Java Version: %d", javaVer) if !i.app.Java.CheckJavaVer(javaVer) { i.app.Java.InstallJavaVer(javaVer) } - + confPath := filepath.Join(i.app.PrismLauncher.GetInstanceDir(), instance.InstanceName, "instance.cfg") f, _ = os.OpenFile(confPath, os.O_RDONLY, 0755) defer f.Close() @@ -129,23 +129,23 @@ func (i *InstanceManager)checkJavaVersion(instance Instance){ line = fmt.Sprintf("JavaPath=%s/FCLauncher/java/java-%d-%s/bin/%s", strings.ReplaceAll(confDir, "\\", "/"), javaVer, plat, exe) found = true } - f.WriteString(line+"\n") + f.WriteString(line + "\n") } if !found { line := fmt.Sprintf("JavaPath=%s/FCLauncher/java/java-%d-%s/bin/%s", strings.ReplaceAll(confDir, "\\", "/"), javaVer, plat, exe) - f.WriteString(line+"\n") + f.WriteString(line + "\n") f.WriteString("OverrideJavaLocation=true\nOverrideJava=true\n") } f.Close() } -func (i *InstanceManager)InstallModpack(modpack Modpack, instanceName string){ +func (i *InstanceManager) InstallModpack(modpack Modpack, instanceName string) { i.app.Status(fmt.Sprintf("Installing %s", modpack.Name)) version := modpack.Versions[len(modpack.Versions)-1] dname, _ := os.MkdirTemp("", "fclauncher-*") f, _ := os.OpenFile(filepath.Join(dname, instanceName+".mrpack"), os.O_CREATE|os.O_RDWR, 0755) defer f.Close() - HttpDownload(modpack.Id + "/" + version.File, f, i.app.Ctx) + HttpDownload(modpack.Id+"/"+version.File, f, i.app.Ctx) i.app.PrismLauncher.ImportModpack(f.Name()) instance := Instance{InstanceName: instanceName, ModpackVersion: version.Version, ModpackId: modpack.Id} i.instances = append(i.instances, instance) @@ -157,7 +157,7 @@ func (i *InstanceManager)InstallModpack(modpack Modpack, instanceName string){ } -func (i *InstanceManager)InstallVanilla(version string, instanceName string) { +func (i *InstanceManager) InstallVanilla(version string, instanceName string) { dir, _ := os.UserConfigDir() err := DownloadAssets(version, filepath.Join(dir, "FCLauncher", "assets"), *i.app) if err != nil { @@ -181,12 +181,12 @@ func (i *InstanceManager)InstallVanilla(version string, instanceName string) { } metadata, err := GetVersionMetadata(version) if err != nil { - fmt.Printf("unable to pull metadata: %s\n", err) + fmt.Printf("unable to pull metadata: %s\n", err) } err = os.MkdirAll(filepath.Join(dir, "FCLauncher", "instances", instanceName, "minecraft"), 0755) if err != nil { - fmt.Printf("unable to create directory: %s\n", err) + fmt.Printf("unable to create directory: %s\n", err) } instance := Instance{InstanceName: instanceName, MinecraftVersion: version, JavaVersion: metadata.JavaVersion.MajorVersion, MainClass: metadata.MainClass} for _, lib := range metadata.Libraries { @@ -194,20 +194,20 @@ func (i *InstanceManager)InstallVanilla(version string, instanceName string) { } data, err := json.Marshal(instance) if err != nil { - fmt.Printf("unable to marshal json data: %s\n", err) + fmt.Printf("unable to marshal json data: %s\n", err) } f, err := os.OpenFile(filepath.Join(dir, "FCLauncher", "instances", instanceName, "instance.json"), os.O_CREATE|os.O_RDWR, 0755) if err != nil { - fmt.Printf("unable to open file: %s\n", err) + fmt.Printf("unable to open file: %s\n", err) } defer f.Close() _, err = f.Write(data) if err != nil { - fmt.Printf("unable to write data: %s\n", err) + fmt.Printf("unable to write data: %s\n", err) } i.instances = append(i.instances, instance) - + if !i.app.Java.CheckJavaVer(instance.JavaVersion) { i.app.Status(fmt.Sprintf("Installing Java Version %d", instance.JavaVersion)) i.app.Java.InstallJavaVer(instance.JavaVersion) @@ -216,11 +216,15 @@ func (i *InstanceManager)InstallVanilla(version string, instanceName string) { return } -func (i *InstanceManager)GetInstances() []Instance{ - return i.instances +func (i *InstanceManager) GetInstances() []string { + names := []string{} + for _, inst := range i.instances { + names = append(names, inst.InstanceName) + } + return names } -func (i *InstanceManager)CheckUpdate(instance Instance){ +func (i *InstanceManager) CheckUpdate(instance Instance) { return i.app.Status("Checking for Updates") i.app.Modpacks.QuerryModpacks() @@ -233,7 +237,7 @@ func (i *InstanceManager)CheckUpdate(instance Instance){ dname, _ := os.MkdirTemp("", "fclauncher-*") f, _ := os.OpenFile(filepath.Join(dname, instance.InstanceName+".mrpack"), os.O_CREATE|os.O_RDWR, 0755) defer f.Close() - HttpDownload(pack.Id + "/" + version.File, f, i.app.Ctx) + HttpDownload(pack.Id+"/"+version.File, f, i.app.Ctx) i.app.PrismLauncher.ImportModpack(f.Name()) instance.ModpackVersion = version.Version f, _ = os.OpenFile(filepath.Join(i.app.PrismLauncher.GetInstanceDir(), instance.InstanceName, "instance.json"), os.O_CREATE|os.O_RDWR, 0755) @@ -244,7 +248,7 @@ func (i *InstanceManager)CheckUpdate(instance Instance){ i.SearchInstances() } -func (i *InstanceManager)GetInstance(instance string) (Instance, error) { +func (i *InstanceManager) GetInstance(instance string) (Instance, error) { instanceObject := Instance{} found := false for _, inst := range i.instances { @@ -260,7 +264,7 @@ func (i *InstanceManager)GetInstance(instance string) (Instance, error) { return instanceObject, nil } -func (i *InstanceManager)LaunchInstance(instance string) { +func (i *InstanceManager) LaunchInstance(instance string) { dir, err := os.UserConfigDir() if err != nil { fmt.Printf("unable to get config directory\n") @@ -288,15 +292,13 @@ func (i *InstanceManager)LaunchInstance(instance string) { fmt.Printf("Args: %+v", args) child := exec.Command(filepath.Join(dir, "java", fmt.Sprintf("java-%d-%s", instanceObject.JavaVersion, suffix), "bin", execName), args...) child.Dir = filepath.Join(dir, "instances", instance, "minecraft") - wruntime.WindowHide(i.app.Ctx) + wruntime.WindowHide(i.app.Ctx) data, err := child.CombinedOutput() wruntime.WindowShow(i.app.Ctx) fmt.Printf("Command Output: %s\n", data) } - - -func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) { +func (i *InstanceManager) InstallFabric(instance string, fabricVersion string) { i.app.Status("Installing Fabric") instanceObject, err := i.GetInstance(instance) if err != nil { @@ -306,7 +308,7 @@ func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) { if err != nil { fmt.Printf("unable to get version metadata\n") } - client: +client: for _, lib := range metadata.LauncherMeta.Libraries.Client { tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) pkg := tokens[len(tokens)-2] @@ -319,8 +321,8 @@ func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) { continue client } } - } - common: + } +common: for _, lib := range metadata.LauncherMeta.Libraries.Common { tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) pkg := tokens[len(tokens)-2] @@ -333,11 +335,11 @@ func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) { continue common } } - } + } instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(ProcessMavenPath(metadata.Loader.Maven), ProcessMavenFilename(metadata.Loader.Maven))) instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(ProcessMavenPath(metadata.Intermediary.Maven), ProcessMavenFilename(metadata.Intermediary.Maven))) - + instanceObject.MainClass = metadata.LauncherMeta.MainClass["client"] instanceObject.FabricVersion = fabricVersion dir, _ := os.UserConfigDir() @@ -354,7 +356,7 @@ func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) { } } -func (i *InstanceManager)InstallQuilt(instance string, quiltVersion string) { +func (i *InstanceManager) InstallQuilt(instance string, quiltVersion string) { i.app.Status("Installing Quilt") instanceObject, err := i.GetInstance(instance) if err != nil { @@ -364,7 +366,7 @@ func (i *InstanceManager)InstallQuilt(instance string, quiltVersion string) { if err != nil { fmt.Printf("unable to get version metadata\n") } - client: +client: for _, lib := range metadata.LauncherMeta.Libraries.Client { tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) pkg := tokens[len(tokens)-2] @@ -377,8 +379,8 @@ func (i *InstanceManager)InstallQuilt(instance string, quiltVersion string) { continue client } } - } - common: + } +common: for _, lib := range metadata.LauncherMeta.Libraries.Common { tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) pkg := tokens[len(tokens)-2] @@ -391,11 +393,11 @@ func (i *InstanceManager)InstallQuilt(instance string, quiltVersion string) { continue common } } - } + } instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(ProcessMavenPath(metadata.Loader.Maven), ProcessMavenFilename(metadata.Loader.Maven))) instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(ProcessMavenPath(metadata.Intermediary.Maven), ProcessMavenFilename(metadata.Intermediary.Maven))) - + instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(ProcessMavenPath(metadata.Hashed.Maven), ProcessMavenFilename(metadata.Hashed.Maven))) instanceObject.MainClass = metadata.LauncherMeta.MainClass["client"] diff --git a/fclauncher/frontend/src/Instances.svelte b/fclauncher/frontend/src/Instances.svelte index 0f10250..63e1549 100644 --- a/fclauncher/frontend/src/Instances.svelte +++ b/fclauncher/frontend/src/Instances.svelte @@ -5,12 +5,12 @@ import {GetQuiltVersions} from '../wailsjs/go/main/Quilt.js' import {GetForgeVersions} from '../wailsjs/go/main/Forge.js' import {onMount} from 'svelte' - import {loading, addingInstance} from './global' + import {loading, addingInstance, instances} from './global' import {slide} from 'svelte/transition' let modpacks: string[] = [] let pack: string - let instances: Instance[] = [] + //let instances: Instance[] = [] let instance: string let name: string = "New Modpack" let loader: string = "none" @@ -29,8 +29,8 @@ updateLoaders() }) GetInstances().then((result) => { - instances = result - instance = instances[0].InstanceName + $instances = result + instance = $instances[0] }) } @@ -109,8 +109,8 @@
diff --git a/fclauncher/frontend/src/Testpage.svelte b/fclauncher/frontend/src/Testpage.svelte index 4d52429..0dff254 100644 --- a/fclauncher/frontend/src/Testpage.svelte +++ b/fclauncher/frontend/src/Testpage.svelte @@ -1,4 +1,5 @@ @@ -6,8 +7,8 @@
diff --git a/fclauncher/frontend/src/global.ts b/fclauncher/frontend/src/global.ts index 8650c9d..2bc5a2f 100644 --- a/fclauncher/frontend/src/global.ts +++ b/fclauncher/frontend/src/global.ts @@ -2,3 +2,4 @@ import { writable } from "svelte/store" export const loading = writable(true) export const addingInstance = writable(false) export const testPage = writable(false) +export const instances = writable([]) \ No newline at end of file diff --git a/fclauncher/frontend/wailsjs/go/main/InstanceManager.d.ts b/fclauncher/frontend/wailsjs/go/main/InstanceManager.d.ts index 0ff3e37..20a7e22 100755 --- a/fclauncher/frontend/wailsjs/go/main/InstanceManager.d.ts +++ b/fclauncher/frontend/wailsjs/go/main/InstanceManager.d.ts @@ -6,7 +6,7 @@ export function CheckUpdate(arg1:main.Instance):Promise; export function GetInstance(arg1:string):Promise; -export function GetInstances():Promise>; +export function GetInstances():Promise>; export function InstallFabric(arg1:string,arg2:string):Promise;