From 164c28d2f60059900cc2fe988985de314e973ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20H=C3=BCbner?= Date: Sat, 19 Oct 2024 15:46:03 +0200 Subject: [PATCH] fix neoforged maven download --- go.mod | 7 +++--- go.sum | 6 +++-- server/neoforge.go | 57 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index fdbb92f..86a4cf4 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,14 @@ module github.com/nothub/mrpack-install -go 1.22.0 - -toolchain go1.23.0 +go 1.23.2 require ( github.com/google/uuid v1.6.0 github.com/nothub/hashutils v0.4.1 + github.com/nothub/semver v0.11.1 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 - golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 + golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c ) require ( diff --git a/go.sum b/go.sum index d99e37d..f474ee4 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/nothub/hashutils v0.4.1 h1:pN4PLPviIXF1V+KA3BPPgrATB4KGozzQQfMAN5ELdas= github.com/nothub/hashutils v0.4.1/go.mod h1:iGq7MeGKpA6EC9kiLQTC/PsjFwpgXbKs6F/lNihRboc= +github.com/nothub/semver v0.11.1 h1:X6WmesSulHrvBxWoXPRlI6cdbHK2xFKgPeeedf88zUA= +github.com/nothub/semver v0.11.1/go.mod h1:gSRy1ydCNu2q4ZsC5rqmlSqMJHic+0bS6ahd2AADGjc= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -65,8 +67,8 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= -golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk= -golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY= +golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY= +golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= diff --git a/server/neoforge.go b/server/neoforge.go index f0b684f..0341de5 100644 --- a/server/neoforge.go +++ b/server/neoforge.go @@ -4,7 +4,10 @@ import ( "fmt" "github.com/nothub/mrpack-install/maven" "github.com/nothub/mrpack-install/web" + "github.com/nothub/semver" "log" + "os/exec" + "strings" ) type NeoForgeInstaller struct { @@ -15,21 +18,65 @@ type NeoForgeInstaller struct { func (inst *NeoForgeInstaller) Install(serverDir string, serverFile string) error { version := inst.NeoForgeVersion if version == "" || version == "latest" { + meta, err := maven.FetchMetadata("https://maven.neoforged.net/releases/net/neoforged/neoforge/maven-metadata.xml") if err != nil { return err } - // TODO: instead of using the latest release, match against the latest - // non-pre-release version that matches the minecraft version - version = meta.Versioning.Release + + mcPrefix := strings.Join(strings.Split(inst.MinecraftVersion, ".")[1:3], ".") + log.Println(mcPrefix) + var versions []string + for _, ver := range meta.Versioning.Versions { + if !strings.HasPrefix(ver, mcPrefix) { + // does not match mc version + continue + } + if strings.Contains(ver, "-") { + // is pre-release version + continue + } + versions = append(versions, ver) + } + + parsed, err := semver.ParseAll(versions) + if err != nil { + return err + } + + var releases []semver.Version + for _, v := range parsed { + if v.IsRelease() { + releases = append(releases, v) + } + } + + sorted := semver.SortDesc(releases) + + if len(sorted) <= 0 { + return fmt.Errorf("no neoforge release version found for mc version %s", inst.MinecraftVersion) + } + + version = sorted[0].String() } u := fmt.Sprintf("https://maven.neoforged.net/releases/net/neoforged/neoforge/%s/neoforge-%s-installer.jar", version, version) - file, err := web.DefaultClient.DownloadFile(u, serverDir, serverFile) + installerFile, err := web.DefaultClient.DownloadFile(u, serverDir, fmt.Sprintf("neoforge-%s-installer.jar", version)) if err != nil { return err } - log.Println("Server jar downloaded to:", file) + log.Println("Installer downloaded to:", installerFile) + + cmd := exec.Command("java", "-jar", installerFile, "--install-server", serverDir) + log.Println("Executing command:", cmd.String()) + if err = cmd.Run(); err != nil { + return err + } + + if serverFile != "" { + log.Println("ignoring --server-file option for neoforged server installation!") + } + return nil }