fabric installing, sorta...

This commit is contained in:
Samuel Walker 2024-10-31 16:23:38 -06:00
parent d0384b1778
commit 859bf812cd
16 changed files with 183 additions and 176 deletions

View File

@ -26,6 +26,8 @@ type Instance struct {
FabricVersion string FabricVersion string
QuiltVersion string QuiltVersion string
JavaVersion int JavaVersion int
Libraries []string
MainClass string
} }
type InstanceManager struct { type InstanceManager struct {
@ -187,9 +189,10 @@ func (i *InstanceManager)InstallVanilla(version string, instanceName string) {
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}
for _, lib := range metadata.Libraries {
instance := Instance{InstanceName: instanceName, MinecraftVersion: version, JavaVersion: metadata.JavaVersion.MajorVersion} instance.Libraries = append(instance.Libraries, lib.Downloads.Artifact.Path)
}
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)
@ -242,11 +245,7 @@ func (i *InstanceManager)CheckUpdate(instance Instance){
i.SearchInstances() i.SearchInstances()
} }
func (i *InstanceManager)LaunchInstance(instance string) { func (i *InstanceManager)GetInstance(instance string) (Instance, error) {
dir, err := os.UserConfigDir()
if err != nil {
fmt.Printf("unable to get config directory\n")
}
instanceObject := Instance{} instanceObject := Instance{}
found := false found := false
for _, inst := range i.instances { for _, inst := range i.instances {
@ -257,7 +256,19 @@ func (i *InstanceManager)LaunchInstance(instance string) {
} }
} }
if !found { if !found {
fmt.Printf("unable to find instance %s\n", instance) return Instance{}, fmt.Errorf("unable to find instance %s\n", instance)
}
return instanceObject, nil
}
func (i *InstanceManager)LaunchInstance(instance string) {
dir, err := os.UserConfigDir()
if err != nil {
fmt.Printf("unable to get config directory\n")
}
instanceObject, err := i.GetInstance(instance)
if err != nil {
fmt.Printf("Unable to find instance\n")
} }
execName := "java" execName := "java"
suffix := "lin" suffix := "lin"
@ -271,7 +282,7 @@ func (i *InstanceManager)LaunchInstance(instance string) {
fmt.Printf("unable to authenticate: %s\n", err) fmt.Printf("unable to authenticate: %s\n", err)
return return
} }
args, err := GetOnlineLaunchArgs(instanceObject.MinecraftVersion, filepath.Join(dir, "lib"), filepath.Join(dir, "bin"), filepath.Join(dir, "assets"), filepath.Join(dir, "instances", instance, "minecraft"), auth) args, err := GetOnlineLaunchArgs(instanceObject.MinecraftVersion, instanceObject, filepath.Join(dir, "lib"), filepath.Join(dir, "bin"), filepath.Join(dir, "assets"), filepath.Join(dir, "instances", instance, "minecraft"), auth)
if err != nil { if err != nil {
fmt.Printf("unable to get launch args: %s\n", err) fmt.Printf("unable to get launch args: %s\n", err)
} }
@ -283,3 +294,64 @@ func (i *InstanceManager)LaunchInstance(instance string) {
fmt.Printf("Command Output: %s\n", data) fmt.Printf("Command Output: %s\n", data)
} }
func (i *InstanceManager)InstallFabric(instance string, fabricVersion string) {
i.app.Status("Installing Fabric")
instanceObject, err := i.GetInstance(instance)
if err != nil {
fmt.Printf("Instance does not exist\n")
}
metadata, err := GetFabricMetadata(instanceObject.MinecraftVersion, fabricVersion)
if err != nil {
fmt.Printf("unable to get version metadata\n")
}
for _, lib := range metadata.LauncherMeta.Libraries.Client {
tokens := strings.SplitN(lib.Name, ".", 2)
org1 := tokens[0]
tokens = strings.Split(tokens[1], ":")
org2 := tokens[0]
pack := tokens[1]
version := tokens[2]
instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(org1, org2, pack, version, pack+"-"+version+".jar"))
}
for _, lib := range metadata.LauncherMeta.Libraries.Common {
tokens := strings.SplitN(lib.Name, ".", 2)
org1 := tokens[0]
tokens = strings.Split(tokens[1], ":")
org2 := tokens[0]
pack := tokens[1]
version := tokens[2]
instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(org1, org2, pack, version, pack+"-"+version+".jar"))
}
tokens := strings.SplitN(metadata.Loader.Maven, ".", 2)
org1 := tokens[0]
tokens = strings.Split(tokens[1], ":")
org2 := tokens[0]
pack := tokens[1]
version := tokens[2]
instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(org1, org2, pack, version, pack+"-"+version+".jar"))
tokens = strings.SplitN(metadata.Intermediary.Maven, ".", 2)
org1 = tokens[0]
tokens = strings.Split(tokens[1], ":")
org2 = tokens[0]
pack = tokens[1]
version = tokens[2]
instanceObject.Libraries = append(instanceObject.Libraries, filepath.Join(org1, org2, pack, version, pack+"-"+version+".jar"))
instanceObject.MainClass = metadata.LauncherMeta.MainClass["client"]
instanceObject.FabricVersion = fabricVersion
dir, _ := os.UserConfigDir()
InstallFabricLibs(instanceObject.MinecraftVersion, fabricVersion, filepath.Join(dir, "FCLauncher", "lib"))
f, _ := os.OpenFile(filepath.Join(dir, "FCLauncher", "instances", instance, "instance.json"), os.O_CREATE|os.O_RDWR, 0755)
data, _ := json.Marshal(instanceObject)
defer f.Close()
f.Write(data)
for ind, inst := range i.instances {
if inst.InstanceName == instance {
i.instances[ind] = instanceObject
break
}
}
}

View File

@ -1,10 +1,15 @@
package main package main
import ( import (
"crypto/sha1"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
"path/filepath"
"strings"
) )
type Fabric struct { type Fabric struct {
@ -23,6 +28,7 @@ type FabricDefinition struct {
type FabricLibrary struct { type FabricLibrary struct {
Name string Name string
Url string Url string
Sha1 string
} }
type FabricLibraries struct { type FabricLibraries struct {
@ -52,6 +58,58 @@ func (Fabric)GetFabricVersions(mcVersion string) ([]FabricVersion, error) {
data, _ := io.ReadAll(resp.Body) data, _ := io.ReadAll(resp.Body)
versions := []FabricVersion{} versions := []FabricVersion{}
json.Unmarshal(data, &versions) json.Unmarshal(data, &versions)
fmt.Printf("fabric versions found for %s: %+v\n", mcVersion, versions)
return versions, nil return versions, nil
} }
func GetFabricMetadata(mcVersion string, fabricVersion string) (FabricVersion, error) {
versions, err := Fabric{}.GetFabricVersions(mcVersion)
if err != nil {
return FabricVersion{}, fmt.Errorf("unable to download versions manifest: %e\n", err)
}
for _, version := range versions {
if version.Loader.Version == fabricVersion {
return version, nil
}
}
return FabricVersion{}, fmt.Errorf("Unable to find requested version.\n")
}
func InstallLib(lib FabricLibrary, libDir string) {
tokens := strings.SplitN(lib.Name, ".", 2)
org1 := tokens[0]
tokens = strings.Split(tokens[1], ":")
org2 := tokens[0]
pack := tokens[1]
version := tokens[2]
path := filepath.Join(org1, org2, pack, version, pack+"-"+version+".jar")
if _, err := os.Stat(filepath.Join(libDir, path)); err == nil {
f, _ := os.OpenFile(filepath.Join(libDir, path), os.O_RDONLY, 0755)
defer f.Close()
data, _ := io.ReadAll(f)
sha := sha1.Sum(data)
if hex.EncodeToString(sha[:20]) == lib.Sha1 {
return
}
}
resp, err := http.Get(lib.Url+path)
if err != nil {
return
}
defer resp.Body.Close()
os.MkdirAll(filepath.Join(libDir, org1, org2, pack, version), 0755)
f, _ := os.OpenFile(filepath.Join(libDir, path), os.O_CREATE|os.O_RDWR, 0755)
defer f.Close()
io.Copy(f, resp.Body)
}
func InstallFabricLibs(mcVersion string, fabricVersion string, libDir string) {
metadata, _ := GetFabricMetadata(mcVersion, fabricVersion)
for _, lib := range metadata.LauncherMeta.Libraries.Client {
InstallLib(lib, libDir)
}
for _, lib := range metadata.LauncherMeta.Libraries.Common {
InstallLib(lib, libDir)
}
InstallLib(FabricLibrary{Name: metadata.Loader.Maven, Sha1: "", Url: "https://maven.fabricmc.net/"}, libDir)
InstallLib(FabricLibrary{Name: metadata.Intermediary.Maven, Sha1: "", Url: "https://maven.fabricmc.net/"}, libDir)
}

View File

@ -1,6 +1,5 @@
<script lang="ts"> <script lang="ts">
import {GetModpacks, GetModpack} from '../wailsjs/go/main/ModpackManager.js' import {InstallVanilla, LaunchInstance, GetInstances, InstallFabric, CheckUpdate} from '../wailsjs/go/main/InstanceManager.js'
import {InstallVanilla, LaunchInstance, GetInstances, CheckUpdate} from '../wailsjs/go/main/InstanceManager.js'
import {GetVersions} from '../wailsjs/go/main/App.js' import {GetVersions} from '../wailsjs/go/main/App.js'
import {GetFabricVersions} from '../wailsjs/go/main/Fabric.js' import {GetFabricVersions} from '../wailsjs/go/main/Fabric.js'
import {onMount} from 'svelte' import {onMount} from 'svelte'
@ -32,6 +31,7 @@
function updateLoaders(){ function updateLoaders(){
GetFabricVersions(pack).then((result) => { GetFabricVersions(pack).then((result) => {
fab_versions = []
result.forEach((ver) => { result.forEach((ver) => {
fab_versions.push(ver.Loader.Version) fab_versions.push(ver.Loader.Version)
}) })
@ -53,9 +53,20 @@
function install(){ function install(){
$loading = true $loading = true
InstallVanilla(pack, name).then(() => { InstallVanilla(pack, name).then(() => {
addingInstance = false switch (loader){
$loading = false case "none":
updateLists() addingInstance = false
$loading = false
updateLists()
break
case "fabric":
InstallFabric(name, fabric_ver).then(() => {
addingInstance = false
$loading = false
updateLists()
})
}
}) })
} }

View File

@ -4,8 +4,12 @@ import {main} from '../models';
export function CheckUpdate(arg1:main.Instance):Promise<void>; export function CheckUpdate(arg1:main.Instance):Promise<void>;
export function GetInstance(arg1:string):Promise<main.Instance>;
export function GetInstances():Promise<Array<main.Instance>>; export function GetInstances():Promise<Array<main.Instance>>;
export function InstallFabric(arg1:string,arg2:string):Promise<void>;
export function InstallModpack(arg1:main.Modpack,arg2:string):Promise<void>; export function InstallModpack(arg1:main.Modpack,arg2:string):Promise<void>;
export function InstallVanilla(arg1:string,arg2:string):Promise<void>; export function InstallVanilla(arg1:string,arg2:string):Promise<void>;

View File

@ -6,10 +6,18 @@ export function CheckUpdate(arg1) {
return window['go']['main']['InstanceManager']['CheckUpdate'](arg1); return window['go']['main']['InstanceManager']['CheckUpdate'](arg1);
} }
export function GetInstance(arg1) {
return window['go']['main']['InstanceManager']['GetInstance'](arg1);
}
export function GetInstances() { export function GetInstances() {
return window['go']['main']['InstanceManager']['GetInstances'](); return window['go']['main']['InstanceManager']['GetInstances']();
} }
export function InstallFabric(arg1, arg2) {
return window['go']['main']['InstanceManager']['InstallFabric'](arg1, arg2);
}
export function InstallModpack(arg1, arg2) { export function InstallModpack(arg1, arg2) {
return window['go']['main']['InstanceManager']['InstallModpack'](arg1, arg2); return window['go']['main']['InstanceManager']['InstallModpack'](arg1, arg2);
} }

View File

@ -23,6 +23,7 @@ export namespace main {
export class FabricLibrary { export class FabricLibrary {
Name: string; Name: string;
Url: string; Url: string;
Sha1: string;
static createFrom(source: any = {}) { static createFrom(source: any = {}) {
return new FabricLibrary(source); return new FabricLibrary(source);
@ -32,6 +33,7 @@ export namespace main {
if ('string' === typeof source) source = JSON.parse(source); if ('string' === typeof source) source = JSON.parse(source);
this.Name = source["Name"]; this.Name = source["Name"];
this.Url = source["Url"]; this.Url = source["Url"];
this.Sha1 = source["Sha1"];
} }
} }
export class FabricLibraries { export class FabricLibraries {
@ -147,6 +149,8 @@ export namespace main {
FabricVersion: string; FabricVersion: string;
QuiltVersion: string; QuiltVersion: string;
JavaVersion: number; JavaVersion: number;
Libraries: string[];
MainClass: string;
static createFrom(source: any = {}) { static createFrom(source: any = {}) {
return new Instance(source); return new Instance(source);
@ -163,6 +167,8 @@ export namespace main {
this.FabricVersion = source["FabricVersion"]; this.FabricVersion = source["FabricVersion"];
this.QuiltVersion = source["QuiltVersion"]; this.QuiltVersion = source["QuiltVersion"];
this.JavaVersion = source["JavaVersion"]; this.JavaVersion = source["JavaVersion"];
this.Libraries = source["Libraries"];
this.MainClass = source["MainClass"];
} }
} }
export class Version { export class Version {

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -516,7 +516,7 @@ func DownloadExecutable(mcVersion string, binDir string, a App) error {
} }
func GetBaseLaunchArgs(mcVersion string, libDir string, binDir string, assetDir string, gameDir string) ([]string, error) { func GetBaseLaunchArgs(mcVersion string, instance Instance, libDir string, binDir string, assetDir string, gameDir string) ([]string, error) {
args := []string{} args := []string{}
metadata, err := GetVersionMetadata(mcVersion) metadata, err := GetVersionMetadata(mcVersion)
if err != nil { if err != nil {
@ -547,15 +547,15 @@ func GetBaseLaunchArgs(mcVersion string, libDir string, binDir string, assetDir
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
separater = ";" separater = ";"
} }
for _, lib := range metadata.Libraries { for _, lib := range instance.Libraries {
arg += filepath.Join(libDir, lib.Downloads.Artifact.Path) + separater arg += filepath.Join(libDir, lib) + separater
} }
arg += filepath.Join(binDir, mcVersion, "client.jar") arg += filepath.Join(binDir, mcVersion, "client.jar")
args = append(args, arg) args = append(args, arg)
args = append(args, metadata.MainClass) args = append(args, instance.MainClass)
for _, val := range searchArgs { for _, val := range searchArgs {
switch val { switch val {
@ -584,8 +584,8 @@ func GetBaseLaunchArgs(mcVersion string, libDir string, binDir string, assetDir
} }
func GetOfflineLaunchArgs(mcVersion string, libDir string, binDir string, assetDir string, gameDir string, playerName string) ([]string, error) { func GetOfflineLaunchArgs(mcVersion string, instance Instance, libDir string, binDir string, assetDir string, gameDir string, playerName string) ([]string, error) {
args, err := GetBaseLaunchArgs(mcVersion, libDir, binDir, assetDir, gameDir) args, err := GetBaseLaunchArgs(mcVersion, instance, libDir, binDir, assetDir, gameDir)
if err != nil { if err != nil {
return []string{}, fmt.Errorf("GatOfflineLaunchArgs: %e\n", err) return []string{}, fmt.Errorf("GatOfflineLaunchArgs: %e\n", err)
} }
@ -607,8 +607,8 @@ func GetOfflineLaunchArgs(mcVersion string, libDir string, binDir string, assetD
return args, nil return args, nil
} }
func GetOnlineLaunchArgs(mcVersion string, libDir string, binDir string, assetDir string, gameDir string, auth LauncherAuth) ([]string, error) { func GetOnlineLaunchArgs(mcVersion string, instance Instance, libDir string, binDir string, assetDir string, gameDir string, auth LauncherAuth) ([]string, error) {
args, err := GetBaseLaunchArgs(mcVersion, libDir, binDir, assetDir, gameDir) args, err := GetBaseLaunchArgs(mcVersion, instance, libDir, binDir, assetDir, gameDir)
if err != nil { if err != nil {
return []string{}, fmt.Errorf("GatOfflineLaunchArgs: %e\n", err) return []string{}, fmt.Errorf("GatOfflineLaunchArgs: %e\n", err)
} }