FCLauncher/fclauncher/quilt.go

135 lines
3.6 KiB
Go
Raw Permalink Normal View History

2024-10-31 18:21:09 -06:00
package main
import (
2024-11-26 10:22:37 -07:00
"bytes"
2024-10-31 18:21:09 -06:00
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
2024-11-26 10:22:37 -07:00
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
2024-10-31 18:21:09 -06:00
)
type Quilt struct {
}
type QuiltDefinition struct {
Separator string
2024-11-26 10:22:37 -07:00
Build int
Maven string
Version string
Stable bool
2024-10-31 18:21:09 -06:00
}
type QuiltLibrary struct {
Name string
2024-11-26 10:22:37 -07:00
Url string
2024-10-31 18:21:09 -06:00
Sha1 string
}
type QuiltLibraries struct {
Client []QuiltLibrary
Common []QuiltLibrary
Server []QuiltLibrary
}
type QuiltMeta struct {
2024-11-26 10:22:37 -07:00
Version int
2024-10-31 18:21:09 -06:00
Libraries QuiltLibraries
MainClass map[string]string
}
type QuiltVersion struct {
2024-11-26 10:22:37 -07:00
Loader QuiltDefinition
2024-10-31 18:21:09 -06:00
Intermediary QuiltDefinition
2024-11-26 10:22:37 -07:00
Hashed QuiltDefinition
2024-10-31 18:21:09 -06:00
LauncherMeta QuiltMeta
}
2024-11-26 10:22:37 -07:00
func (Quilt) GetQuiltVersions(mcVersion string) ([]QuiltVersion, error) {
resp, err := http.Get("https://meta.quiltmc.org/v3/versions/loader/" + mcVersion)
2024-10-31 18:21:09 -06:00
if err != nil {
return []QuiltVersion{}, fmt.Errorf("Unable to pull quilt version manifest: %s\n", err)
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
versions := []QuiltVersion{}
json.Unmarshal(data, &versions)
return versions, nil
}
func GetQuiltMetadata(mcVersion string, quiltVersion string) (QuiltVersion, error) {
versions, err := Quilt{}.GetQuiltVersions(mcVersion)
if err != nil {
return QuiltVersion{}, fmt.Errorf("unable to download versions manifest: %e\n", err)
}
for _, version := range versions {
if version.Loader.Version == quiltVersion {
return version, nil
}
}
return QuiltVersion{}, fmt.Errorf("Unable to find requested version.\n")
}
2024-11-26 10:22:37 -07:00
func InstallQuiltLib(lib QuiltLibrary, libDir string, a *App) {
a.Status(fmt.Sprintf("Checking %s\n", lib.Name))
2024-10-31 18:21:09 -06:00
path := filepath.Join(ProcessMavenPath(lib.Name), ProcessMavenFilename(lib.Name))
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))
resp, err := http.Get(lib.Url + path)
2024-10-31 18:21:09 -06:00
if err != nil {
fmt.Printf("unable to find library: %s\n", lib.Url+path)
return
}
defer resp.Body.Close()
2024-10-31 18:57:54 -06:00
if resp.StatusCode != 200 {
fmt.Printf("unable to find library: %s\n", lib.Url+path)
return
}
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 18:21:09 -06:00
os.MkdirAll(filepath.Join(libDir, ProcessMavenPath(lib.Name)), 0755)
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 18:21:09 -06:00
}
2024-11-26 10:22:37 -07:00
func InstallQuiltLibs(mcVersion string, quiltVersion string, libDir string, a *App) {
2024-10-31 18:21:09 -06:00
metadata, _ := GetQuiltMetadata(mcVersion, quiltVersion)
for _, lib := range metadata.LauncherMeta.Libraries.Client {
2024-11-26 10:22:37 -07:00
InstallQuiltLib(lib, libDir, a)
}
2024-10-31 18:21:09 -06:00
for _, lib := range metadata.LauncherMeta.Libraries.Common {
2024-11-26 10:22:37 -07:00
InstallQuiltLib(lib, libDir, a)
}
InstallQuiltLib(QuiltLibrary{Name: metadata.Loader.Maven, Sha1: "", Url: "https://maven.quiltmc.org/repository/release/"}, libDir, a)
InstallQuiltLib(QuiltLibrary{Name: metadata.Intermediary.Maven, Sha1: "", Url: "https://maven.fabricmc.net/"}, libDir, a)
InstallQuiltLib(QuiltLibrary{Name: metadata.Hashed.Maven, Sha1: "", Url: "https://maven.quiltmc.org/repository/release/"}, libDir, a)
2024-10-31 18:21:09 -06:00
}