FCLauncher/fclauncher/fabric.go

146 lines
3.7 KiB
Go
Raw Permalink Normal View History

2024-10-31 14:55:58 -06:00
package main
import (
2024-11-26 10:22:37 -07:00
"bytes"
2024-10-31 16:23:38 -06:00
"crypto/sha1"
"encoding/hex"
2024-10-31 14:55:58 -06:00
"encoding/json"
"fmt"
"io"
"net/http"
2024-10-31 16:23:38 -06:00
"os"
"path/filepath"
"strings"
2024-11-26 10:22:37 -07:00
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
2024-10-31 14:55:58 -06:00
)
type Fabric struct {
}
type FabricDefinition struct {
Separator string
2024-11-26 10:22:37 -07:00
Build int
Maven string
Version string
Stable bool
2024-10-31 14:55:58 -06:00
}
type FabricLibrary struct {
Name string
2024-11-26 10:22:37 -07:00
Url string
2024-10-31 16:23:38 -06:00
Sha1 string
2024-10-31 14:55:58 -06:00
}
type FabricLibraries struct {
Client []FabricLibrary
Common []FabricLibrary
Server []FabricLibrary
}
type FabricMeta struct {
2024-11-26 10:22:37 -07:00
Version int
2024-10-31 14:55:58 -06:00
Libraries FabricLibraries
MainClass map[string]string
}
type FabricVersion struct {
2024-11-26 10:22:37 -07:00
Loader FabricDefinition
2024-10-31 14:55:58 -06:00
Intermediary FabricDefinition
LauncherMeta FabricMeta
}
2024-11-26 10:22:37 -07:00
func (Fabric) GetFabricVersions(mcVersion string) ([]FabricVersion, error) {
resp, err := http.Get("https://meta.fabricmc.net/v2/versions/loader/" + mcVersion)
2024-10-31 14:55:58 -06:00
if err != nil {
return []FabricVersion{}, fmt.Errorf("Unable to pull fabric version manifest: %s\n", err)
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
versions := []FabricVersion{}
json.Unmarshal(data, &versions)
return versions, nil
}
2024-10-31 16:23:38 -06:00
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")
}
2024-11-26 10:22:37 -07:00
func InstallLib(lib FabricLibrary, libDir string, a *App) {
a.Status(fmt.Sprintf("Checking %s\n", lib.Name))
2024-10-31 16:46:12 -06:00
path := filepath.Join(ProcessMavenPath(lib.Name), ProcessMavenFilename(lib.Name))
2024-10-31 16:23:38 -06:00
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
}
}
2024-11-26 10:22:37 -07:00
a.Status(fmt.Sprintf("Downloading %s\n", lib.Name))
2024-11-26 16:49:32 -07:00
unixPath := strings.ReplaceAll(path, "\\", "/")
resp, err := http.Get(lib.Url + unixPath)
2024-10-31 16:23:38 -06:00
if err != nil {
return
}
defer resp.Body.Close()
2024-11-26 10:22:37 -07:00
buff := new(bytes.Buffer)
downloaded := 0
for {
count, err := io.CopyN(buff, resp.Body, BlockSize)
if err == io.EOF {
downloaded += int(count)
break
}
if err != nil {
fmt.Printf("Error Downloading libs: %e\n", err)
return
}
downloaded += int(count)
wruntime.EventsEmit(a.Ctx, "download", downloaded, resp.ContentLength)
}
2024-10-31 16:46:12 -06:00
os.MkdirAll(filepath.Join(libDir, ProcessMavenPath(lib.Name)), 0755)
2024-10-31 16:23:38 -06:00
f, _ := os.OpenFile(filepath.Join(libDir, path), os.O_CREATE|os.O_RDWR, 0755)
defer f.Close()
2024-11-26 10:22:37 -07:00
io.Copy(f, buff)
wruntime.EventsEmit(a.Ctx, "download_complete")
2024-10-31 16:23:38 -06:00
}
2024-11-26 10:22:37 -07:00
func InstallFabricLibs(mcVersion string, fabricVersion string, libDir string, a *App) {
2024-10-31 16:23:38 -06:00
metadata, _ := GetFabricMetadata(mcVersion, fabricVersion)
for _, lib := range metadata.LauncherMeta.Libraries.Client {
2024-11-26 10:22:37 -07:00
InstallLib(lib, libDir, a)
}
2024-10-31 16:23:38 -06:00
for _, lib := range metadata.LauncherMeta.Libraries.Common {
2024-11-26 10:22:37 -07:00
InstallLib(lib, libDir, a)
}
InstallLib(FabricLibrary{Name: metadata.Loader.Maven, Sha1: "", Url: "https://maven.fabricmc.net/"}, libDir, a)
InstallLib(FabricLibrary{Name: metadata.Intermediary.Maven, Sha1: "", Url: "https://maven.fabricmc.net/"}, libDir, a)
2024-10-31 16:23:38 -06:00
}
2024-10-31 16:46:12 -06:00
func ProcessMavenPath(maven string) string {
tokens := strings.Split(maven, ":")
path := filepath.Join(strings.Split(tokens[0], ".")...)
pack := tokens[1]
version := tokens[2]
path = filepath.Join(path, pack, version)
return path
}
func ProcessMavenFilename(maven string) string {
tokens := strings.Split(maven, ":")
pack := tokens[1]
version := tokens[2]
2024-11-26 10:22:37 -07:00
return pack + "-" + version + ".jar"
2024-10-31 16:46:12 -06:00
}