Made instances list global

This commit is contained in:
Samuel Walker 2024-11-01 05:55:41 -06:00
parent 9d23d503e2
commit 27c97010ae
5 changed files with 64 additions and 60 deletions

View File

@ -17,22 +17,22 @@ import (
) )
type Instance struct { type Instance struct {
InstanceName string InstanceName string
ModpackId string ModpackId string
ModpackVersion string ModpackVersion string
MinecraftVersion string MinecraftVersion string
ForgeVersion string ForgeVersion string
NeoForgeVersion string NeoForgeVersion string
FabricVersion string FabricVersion string
QuiltVersion string QuiltVersion string
JavaVersion int JavaVersion int
Libraries []string Libraries []string
MainClass string MainClass string
} }
type InstanceManager struct { type InstanceManager struct {
instances []Instance instances []Instance
app *App app *App
} }
type mmcpack struct { type mmcpack struct {
@ -40,11 +40,11 @@ type mmcpack struct {
} }
type component struct { type component struct {
Uid string Uid string
Version string Version string
} }
func (i *InstanceManager)SearchInstances() { func (i *InstanceManager) SearchInstances() {
i.instances = []Instance{} i.instances = []Instance{}
dir, _ := os.UserConfigDir() dir, _ := os.UserConfigDir()
dir = filepath.Join(dir, "FCLauncher", "instances") 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") infoPath := filepath.Join(i.app.PrismLauncher.GetInstanceDir(), instance.InstanceName, "mmc-pack.json")
f, _ := os.OpenFile(infoPath, os.O_RDONLY, 0755) f, _ := os.OpenFile(infoPath, os.O_RDONLY, 0755)
defer f.Close() defer f.Close()
dataStr,_ := io.ReadAll(f) dataStr, _ := io.ReadAll(f)
var data mmcpack var data mmcpack
json.Unmarshal(dataStr, &data) json.Unmarshal(dataStr, &data)
mc_version := "0.0" mc_version := "0.0"
for _, comp := range data.Components { for _, comp := range data.Components {
if comp.Uid == "net.minecraft" { if comp.Uid == "net.minecraft" {
mc_version = comp.Version 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, ".") tokensStr := strings.Split(mc_version, ".")
tokens := []int{0, 0, 0} tokens := []int{0, 0, 0}
tokens[0], _ = strconv.Atoi(tokensStr[0]) tokens[0], _ = strconv.Atoi(tokensStr[0])
@ -103,11 +103,11 @@ func (i *InstanceManager)checkJavaVersion(instance Instance){
javaVer = 21 javaVer = 21
} }
} }
fmt.Printf("Req Java Version: %d",javaVer) fmt.Printf("Req Java Version: %d", javaVer)
if !i.app.Java.CheckJavaVer(javaVer) { if !i.app.Java.CheckJavaVer(javaVer) {
i.app.Java.InstallJavaVer(javaVer) i.app.Java.InstallJavaVer(javaVer)
} }
confPath := filepath.Join(i.app.PrismLauncher.GetInstanceDir(), instance.InstanceName, "instance.cfg") confPath := filepath.Join(i.app.PrismLauncher.GetInstanceDir(), instance.InstanceName, "instance.cfg")
f, _ = os.OpenFile(confPath, os.O_RDONLY, 0755) f, _ = os.OpenFile(confPath, os.O_RDONLY, 0755)
defer f.Close() 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) line = fmt.Sprintf("JavaPath=%s/FCLauncher/java/java-%d-%s/bin/%s", strings.ReplaceAll(confDir, "\\", "/"), javaVer, plat, exe)
found = true found = true
} }
f.WriteString(line+"\n") f.WriteString(line + "\n")
} }
if !found { if !found {
line := fmt.Sprintf("JavaPath=%s/FCLauncher/java/java-%d-%s/bin/%s", strings.ReplaceAll(confDir, "\\", "/"), javaVer, plat, exe) 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.WriteString("OverrideJavaLocation=true\nOverrideJava=true\n")
} }
f.Close() 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)) i.app.Status(fmt.Sprintf("Installing %s", modpack.Name))
version := modpack.Versions[len(modpack.Versions)-1] version := modpack.Versions[len(modpack.Versions)-1]
dname, _ := os.MkdirTemp("", "fclauncher-*") dname, _ := os.MkdirTemp("", "fclauncher-*")
f, _ := os.OpenFile(filepath.Join(dname, instanceName+".mrpack"), os.O_CREATE|os.O_RDWR, 0755) f, _ := os.OpenFile(filepath.Join(dname, instanceName+".mrpack"), os.O_CREATE|os.O_RDWR, 0755)
defer f.Close() 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()) i.app.PrismLauncher.ImportModpack(f.Name())
instance := Instance{InstanceName: instanceName, ModpackVersion: version.Version, ModpackId: modpack.Id} instance := Instance{InstanceName: instanceName, ModpackVersion: version.Version, ModpackId: modpack.Id}
i.instances = append(i.instances, instance) 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() dir, _ := os.UserConfigDir()
err := DownloadAssets(version, filepath.Join(dir, "FCLauncher", "assets"), *i.app) err := DownloadAssets(version, filepath.Join(dir, "FCLauncher", "assets"), *i.app)
if err != nil { if err != nil {
@ -181,12 +181,12 @@ func (i *InstanceManager)InstallVanilla(version string, instanceName string) {
} }
metadata, err := GetVersionMetadata(version) metadata, err := GetVersionMetadata(version)
if err != nil { 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) err = os.MkdirAll(filepath.Join(dir, "FCLauncher", "instances", instanceName, "minecraft"), 0755)
if err != nil { 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} instance := Instance{InstanceName: instanceName, MinecraftVersion: version, JavaVersion: metadata.JavaVersion.MajorVersion, MainClass: metadata.MainClass}
for _, lib := range metadata.Libraries { for _, lib := range metadata.Libraries {
@ -194,20 +194,20 @@ func (i *InstanceManager)InstallVanilla(version string, instanceName string) {
} }
data, err := json.Marshal(instance) data, err := json.Marshal(instance)
if err != nil { 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) f, err := os.OpenFile(filepath.Join(dir, "FCLauncher", "instances", instanceName, "instance.json"), os.O_CREATE|os.O_RDWR, 0755)
if err != nil { if err != nil {
fmt.Printf("unable to open file: %s\n", err) fmt.Printf("unable to open file: %s\n", err)
} }
defer f.Close() defer f.Close()
_, err = f.Write(data) _, err = f.Write(data)
if err != nil { 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) i.instances = append(i.instances, instance)
if !i.app.Java.CheckJavaVer(instance.JavaVersion) { if !i.app.Java.CheckJavaVer(instance.JavaVersion) {
i.app.Status(fmt.Sprintf("Installing Java Version %d", instance.JavaVersion)) i.app.Status(fmt.Sprintf("Installing Java Version %d", instance.JavaVersion))
i.app.Java.InstallJavaVer(instance.JavaVersion) i.app.Java.InstallJavaVer(instance.JavaVersion)
@ -216,11 +216,15 @@ func (i *InstanceManager)InstallVanilla(version string, instanceName string) {
return return
} }
func (i *InstanceManager)GetInstances() []Instance{ func (i *InstanceManager) GetInstances() []string {
return i.instances 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 return
i.app.Status("Checking for Updates") i.app.Status("Checking for Updates")
i.app.Modpacks.QuerryModpacks() i.app.Modpacks.QuerryModpacks()
@ -233,7 +237,7 @@ func (i *InstanceManager)CheckUpdate(instance Instance){
dname, _ := os.MkdirTemp("", "fclauncher-*") dname, _ := os.MkdirTemp("", "fclauncher-*")
f, _ := os.OpenFile(filepath.Join(dname, instance.InstanceName+".mrpack"), os.O_CREATE|os.O_RDWR, 0755) f, _ := os.OpenFile(filepath.Join(dname, instance.InstanceName+".mrpack"), os.O_CREATE|os.O_RDWR, 0755)
defer f.Close() 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()) i.app.PrismLauncher.ImportModpack(f.Name())
instance.ModpackVersion = version.Version instance.ModpackVersion = version.Version
f, _ = os.OpenFile(filepath.Join(i.app.PrismLauncher.GetInstanceDir(), instance.InstanceName, "instance.json"), os.O_CREATE|os.O_RDWR, 0755) 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() i.SearchInstances()
} }
func (i *InstanceManager)GetInstance(instance string) (Instance, error) { func (i *InstanceManager) GetInstance(instance string) (Instance, error) {
instanceObject := Instance{} instanceObject := Instance{}
found := false found := false
for _, inst := range i.instances { for _, inst := range i.instances {
@ -260,7 +264,7 @@ func (i *InstanceManager)GetInstance(instance string) (Instance, error) {
return instanceObject, nil return instanceObject, nil
} }
func (i *InstanceManager)LaunchInstance(instance string) { func (i *InstanceManager) LaunchInstance(instance string) {
dir, err := os.UserConfigDir() dir, err := os.UserConfigDir()
if err != nil { if err != nil {
fmt.Printf("unable to get config directory\n") fmt.Printf("unable to get config directory\n")
@ -288,15 +292,13 @@ func (i *InstanceManager)LaunchInstance(instance string) {
fmt.Printf("Args: %+v", args) fmt.Printf("Args: %+v", args)
child := exec.Command(filepath.Join(dir, "java", fmt.Sprintf("java-%d-%s", instanceObject.JavaVersion, suffix), "bin", execName), 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") child.Dir = filepath.Join(dir, "instances", instance, "minecraft")
wruntime.WindowHide(i.app.Ctx) wruntime.WindowHide(i.app.Ctx)
data, err := child.CombinedOutput() data, err := child.CombinedOutput()
wruntime.WindowShow(i.app.Ctx) wruntime.WindowShow(i.app.Ctx)
fmt.Printf("Command Output: %s\n", data) 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") i.app.Status("Installing Fabric")
instanceObject, err := i.GetInstance(instance) instanceObject, err := i.GetInstance(instance)
if err != nil { if err != nil {
@ -306,7 +308,7 @@ func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) {
if err != nil { if err != nil {
fmt.Printf("unable to get version metadata\n") fmt.Printf("unable to get version metadata\n")
} }
client: client:
for _, lib := range metadata.LauncherMeta.Libraries.Client { for _, lib := range metadata.LauncherMeta.Libraries.Client {
tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator))
pkg := tokens[len(tokens)-2] pkg := tokens[len(tokens)-2]
@ -319,8 +321,8 @@ func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) {
continue client continue client
} }
} }
} }
common: common:
for _, lib := range metadata.LauncherMeta.Libraries.Common { for _, lib := range metadata.LauncherMeta.Libraries.Common {
tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator))
pkg := tokens[len(tokens)-2] pkg := tokens[len(tokens)-2]
@ -333,11 +335,11 @@ func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) {
continue common 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.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.Intermediary.Maven), ProcessMavenFilename(metadata.Intermediary.Maven)))
instanceObject.MainClass = metadata.LauncherMeta.MainClass["client"] instanceObject.MainClass = metadata.LauncherMeta.MainClass["client"]
instanceObject.FabricVersion = fabricVersion instanceObject.FabricVersion = fabricVersion
dir, _ := os.UserConfigDir() 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") i.app.Status("Installing Quilt")
instanceObject, err := i.GetInstance(instance) instanceObject, err := i.GetInstance(instance)
if err != nil { if err != nil {
@ -364,7 +366,7 @@ func (i *InstanceManager)InstallQuilt(instance string, quiltVersion string) {
if err != nil { if err != nil {
fmt.Printf("unable to get version metadata\n") fmt.Printf("unable to get version metadata\n")
} }
client: client:
for _, lib := range metadata.LauncherMeta.Libraries.Client { for _, lib := range metadata.LauncherMeta.Libraries.Client {
tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator))
pkg := tokens[len(tokens)-2] pkg := tokens[len(tokens)-2]
@ -377,8 +379,8 @@ func (i *InstanceManager)InstallQuilt(instance string, quiltVersion string) {
continue client continue client
} }
} }
} }
common: common:
for _, lib := range metadata.LauncherMeta.Libraries.Common { for _, lib := range metadata.LauncherMeta.Libraries.Common {
tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator)) tokens := strings.Split(ProcessMavenPath(lib.Name), string(os.PathSeparator))
pkg := tokens[len(tokens)-2] pkg := tokens[len(tokens)-2]
@ -391,11 +393,11 @@ func (i *InstanceManager)InstallQuilt(instance string, quiltVersion string) {
continue common 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.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.Intermediary.Maven), ProcessMavenFilename(metadata.Intermediary.Maven)))
instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(ProcessMavenPath(metadata.Hashed.Maven), ProcessMavenFilename(metadata.Hashed.Maven))) instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(ProcessMavenPath(metadata.Hashed.Maven), ProcessMavenFilename(metadata.Hashed.Maven)))
instanceObject.MainClass = metadata.LauncherMeta.MainClass["client"] instanceObject.MainClass = metadata.LauncherMeta.MainClass["client"]

View File

@ -5,12 +5,12 @@
import {GetQuiltVersions} from '../wailsjs/go/main/Quilt.js' import {GetQuiltVersions} from '../wailsjs/go/main/Quilt.js'
import {GetForgeVersions} from '../wailsjs/go/main/Forge.js' import {GetForgeVersions} from '../wailsjs/go/main/Forge.js'
import {onMount} from 'svelte' import {onMount} from 'svelte'
import {loading, addingInstance} from './global' import {loading, addingInstance, instances} from './global'
import {slide} from 'svelte/transition' import {slide} from 'svelte/transition'
let modpacks: string[] = [] let modpacks: string[] = []
let pack: string let pack: string
let instances: Instance[] = [] //let instances: Instance[] = []
let instance: string let instance: string
let name: string = "New Modpack" let name: string = "New Modpack"
let loader: string = "none" let loader: string = "none"
@ -29,8 +29,8 @@
updateLoaders() updateLoaders()
}) })
GetInstances().then((result) => { GetInstances().then((result) => {
instances = result $instances = result
instance = instances[0].InstanceName instance = $instances[0]
}) })
} }
@ -109,8 +109,8 @@
<main> <main>
<select bind:value={instance} name="pack">Select a Modpack: <select bind:value={instance} name="pack">Select a Modpack:
{#each instances as instance} {#each $instances as instance}
<option value={instance.InstanceName}>{instance.InstanceName}</option> <option value={instance}>{instance}</option>
{/each} {/each}
</select> </select>
<button on:click={onclick}>Launch</button> <button on:click={onclick}>Launch</button>

View File

@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import {instances} from './global'
var testArray = ["test","test2","test3"]; var testArray = ["test","test2","test3"];
</script> </script>
@ -6,8 +7,8 @@
<main> <main>
<div class="testLinks"> <div class="testLinks">
{#each {length: testArray.length} as _, i} {#each $instances as i}
<li>{testArray[i]}</li> <li>{i}</li>
{/each} {/each}
</div> </div>
</main> </main>

View File

@ -2,3 +2,4 @@ import { writable } from "svelte/store"
export const loading = writable(true) export const loading = writable(true)
export const addingInstance = writable(false) export const addingInstance = writable(false)
export const testPage = writable(false) export const testPage = writable(false)
export const instances = writable([])

View File

@ -6,7 +6,7 @@ export function CheckUpdate(arg1:main.Instance):Promise<void>;
export function GetInstance(arg1:string):Promise<main.Instance>; export function GetInstance(arg1:string):Promise<main.Instance>;
export function GetInstances():Promise<Array<main.Instance>>; export function GetInstances():Promise<Array<string>>;
export function InstallFabric(arg1:string,arg2:string):Promise<void>; export function InstallFabric(arg1:string,arg2:string):Promise<void>;