diff --git a/fclauncher/InstanceManager.go b/fclauncher/InstanceManager.go index b7e97ee..305eb13 100644 --- a/fclauncher/InstanceManager.go +++ b/fclauncher/InstanceManager.go @@ -1,12 +1,16 @@ package main import ( + "bufio" "bytes" "encoding/json" "fmt" "io" "os" "path/filepath" + "runtime" + "strconv" + "strings" ) type Instance struct { @@ -20,6 +24,15 @@ type InstanceManager struct { app *App } +type mmcpack struct { + Components []component +} + +type component struct { + Uid string + Version string +} + func (i *InstanceManager)SearchInstances() { i.instances = []Instance{} dir := i.app.PrismLauncher.GetInstanceDir() @@ -45,6 +58,74 @@ func (i *InstanceManager)SearchInstances() { } } +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) + 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; + } + } + fmt.Printf("MC Version: %s",mc_version) + tokensStr := strings.Split(mc_version, ".") + tokens := []int{0, 0, 0} + tokens[0], _ = strconv.Atoi(tokensStr[0]) + tokens[1], _ = strconv.Atoi(tokensStr[1]) + tokens[2], _ = strconv.Atoi(tokensStr[2]) + javaVer := 8 + if tokens[1] == 17 { + javaVer = 17 + } else if tokens[1] == 18 || tokens[1] == 19 { + javaVer = 17 + } else if tokens[1] > 19 { + if tokens[1] == 20 && tokens[2] < 5 { + javaVer = 17 + } else { + javaVer = 21 + } + } + 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() + buff := new(bytes.Buffer) + io.Copy(buff, f) + sc := bufio.NewScanner(buff) + f, _ = os.OpenFile(confPath, os.O_CREATE|os.O_RDWR, 0755) + plat := "lin" + exe := "java" + if runtime.GOOS == "windows" { + plat = "win" + exe = "Java.exe" + } + confDir, _ := os.UserConfigDir() + found := false + for sc.Scan() { + line := sc.Text() + if strings.HasPrefix(line, "JavaPath=") { + line = fmt.Sprintf("JavaPath=%s/FCLauncher/java/java-%d-%s/bin/%s", confDir, javaVer, plat, exe) + found = true + } + f.WriteString(line+"\n") + } + if !found { + line := fmt.Sprintf("JavaPath=%s/FCLauncher/java/java-%d-%s/bin/%s", confDir, javaVer, plat, exe) + f.WriteString(line+"\n") + f.WriteString("OverrideJavaLocation=true\nOverrideJava=true\n") + } + f.Close() +} + func (i *InstanceManager)InstallModpack(modpack Modpack, instanceName string){ i.app.Status(fmt.Sprintf("Installing %s", modpack.Name)) version := modpack.Versions[len(modpack.Versions)-1] @@ -59,9 +140,34 @@ func (i *InstanceManager)InstallModpack(modpack Modpack, instanceName string){ defer f.Close() data, _ := json.Marshal(instance) f.Write(data) + i.checkJavaVersion(instance) + } func (i *InstanceManager)GetInstances() []Instance{ return i.instances } +func (i *InstanceManager)CheckUpdate(instance Instance){ + i.app.Status("Checking for Updates") + i.app.Modpacks.QuerryModpacks() + pack := i.app.Modpacks.GetModpack(instance.ModpackId) + if pack.Versions[len(pack.Versions)-1].Version == instance.ModpackVersion { + return + } + i.app.Status(fmt.Sprintf("Updating %s", instance.InstanceName)) + version := pack.Versions[len(pack.Versions)-1] + 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) + 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) + defer f.Close() + data, _ := json.Marshal(instance) + f.Write(data) + i.checkJavaVersion(instance) + i.SearchInstances() +} + diff --git a/fclauncher/Modpack.go b/fclauncher/Modpack.go index d82a0a0..5e8f0f0 100644 --- a/fclauncher/Modpack.go +++ b/fclauncher/Modpack.go @@ -26,6 +26,7 @@ type ModpackManager struct { func (m *ModpackManager) QuerryModpacks() { + m.Modpacks = []Modpack{} buff := new(bytes.Buffer) err := HttpDownload("modpacks.json", buff, nil) if err != nil { diff --git a/fclauncher/Prism.go b/fclauncher/Prism.go index 550e30f..425ed4b 100644 --- a/fclauncher/Prism.go +++ b/fclauncher/Prism.go @@ -161,3 +161,10 @@ func (p *Prism)ImportModpack(path string) { } child.Process.Kill() } + +func (p *Prism)LaunchInstance(instance Instance) { + p.app.Status(fmt.Sprintf("Launching %s", instance.InstanceName)) + dir, _ := os.UserConfigDir() + child := exec.Command(filepath.Join(dir, "FCLauncher", "prism", p.getExecutableName()), "-l", instance.InstanceName) + child.Start() +} diff --git a/fclauncher/frontend/src/Instances.svelte b/fclauncher/frontend/src/Instances.svelte index 46ff915..be579fb 100644 --- a/fclauncher/frontend/src/Instances.svelte +++ b/fclauncher/frontend/src/Instances.svelte @@ -1,6 +1,7 @@