almost have launching working

This commit is contained in:
Samuel Walker 2024-10-26 18:14:12 -06:00
parent 4bcc2703d6
commit bce2b04d3e
8 changed files with 142 additions and 4 deletions

View File

@ -1,12 +1,16 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strconv"
"strings"
) )
type Instance struct { type Instance struct {
@ -20,6 +24,15 @@ type InstanceManager struct {
app *App app *App
} }
type mmcpack struct {
Components []component
}
type component struct {
Uid string
Version string
}
func (i *InstanceManager)SearchInstances() { func (i *InstanceManager)SearchInstances() {
i.instances = []Instance{} i.instances = []Instance{}
dir := i.app.PrismLauncher.GetInstanceDir() 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){ 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]
@ -59,9 +140,34 @@ func (i *InstanceManager)InstallModpack(modpack Modpack, instanceName string){
defer f.Close() defer f.Close()
data, _ := json.Marshal(instance) data, _ := json.Marshal(instance)
f.Write(data) f.Write(data)
i.checkJavaVersion(instance)
} }
func (i *InstanceManager)GetInstances() []Instance{ func (i *InstanceManager)GetInstances() []Instance{
return i.instances 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()
}

View File

@ -26,6 +26,7 @@ type ModpackManager struct {
func (m *ModpackManager) QuerryModpacks() { func (m *ModpackManager) QuerryModpacks() {
m.Modpacks = []Modpack{}
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
err := HttpDownload("modpacks.json", buff, nil) err := HttpDownload("modpacks.json", buff, nil)
if err != nil { if err != nil {

View File

@ -161,3 +161,10 @@ func (p *Prism)ImportModpack(path string) {
} }
child.Process.Kill() 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()
}

View File

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import {GetModpacks, GetModpack} from '../wailsjs/go/main/ModpackManager.js' import {GetModpacks, GetModpack} from '../wailsjs/go/main/ModpackManager.js'
import {InstallModpack, GetInstances} from '../wailsjs/go/main/InstanceManager.js' import {InstallModpack, GetInstances, CheckUpdate} from '../wailsjs/go/main/InstanceManager.js'
import {LaunchInstance} from '../wailsjs/go/main/Prism.js'
import {onMount} from 'svelte' import {onMount} from 'svelte'
import {loading} from './global.ts' import {loading} from './global.ts'
@ -11,7 +12,7 @@
let addingInstance: boolean = false let addingInstance: boolean = false
let name: string = "New Modpack" let name: string = "New Modpack"
onMount(() => { function updateLists(){
GetModpacks().then((result) => { GetModpacks().then((result) => {
modpacks = result modpacks = result
pack = modpacks[0].Id pack = modpacks[0].Id
@ -21,16 +22,26 @@
instances = result instances = result
instance = instances[0] instance = instances[0]
}) })
}
onMount(() => {
updateLists()
}) })
function onclick(event) { function onclick(event) {
$loading = true
CheckUpdate(instance).then(() => {
LaunchInstance(instance).then(() => {
Quit()
})
})
} }
function install(){ function install(){
$loading = true $loading = true
GetModpack(pack).then((result) => { GetModpack(pack).then((result) => {
InstallModpack(result, name).then(() => { InstallModpack(result, name).then(() => {
onMount() updateLists()
addingInstance = false addingInstance = false
$loading = false $loading = false
}) })

View File

@ -2,6 +2,8 @@
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
import {main} from '../models'; import {main} from '../models';
export function CheckUpdate(arg1:main.Instance):Promise<void>;
export function GetInstances():Promise<Array<main.Instance>>; export function GetInstances():Promise<Array<main.Instance>>;
export function InstallModpack(arg1:main.Modpack,arg2:string):Promise<void>; export function InstallModpack(arg1:main.Modpack,arg2:string):Promise<void>;

View File

@ -2,6 +2,10 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
export function CheckUpdate(arg1) {
return window['go']['main']['InstanceManager']['CheckUpdate'](arg1);
}
export function GetInstances() { export function GetInstances() {
return window['go']['main']['InstanceManager']['GetInstances'](); return window['go']['main']['InstanceManager']['GetInstances']();
} }

View File

@ -1,5 +1,6 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
import {main} from '../models';
export function CheckInstalled():Promise<boolean>; export function CheckInstalled():Promise<boolean>;
@ -8,3 +9,5 @@ export function GetInstanceDir():Promise<string>;
export function ImportModpack(arg1:string):Promise<void>; export function ImportModpack(arg1:string):Promise<void>;
export function Install():Promise<void>; export function Install():Promise<void>;
export function LaunchInstance(arg1:main.Instance):Promise<void>;

View File

@ -17,3 +17,7 @@ export function ImportModpack(arg1) {
export function Install() { export function Install() {
return window['go']['main']['Prism']['Install'](); return window['go']['main']['Prism']['Install']();
} }
export function LaunchInstance(arg1) {
return window['go']['main']['Prism']['LaunchInstance'](arg1);
}