Skip to content

Commit bfb3e3b

Browse files
committed
fix: 修复版本获取
1 parent 3bbc4ef commit bfb3e3b

File tree

2 files changed

+92
-22
lines changed

2 files changed

+92
-22
lines changed

core/installer.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,32 @@ func RunInstaller(installerPath string, loader string, version string, loaderVer
3636
}
3737
var cmd *exec.Cmd
3838
if Download == "bmclapi" {
39-
if loader == "forge" || loader == "neoforge" {
39+
if loader == "forge" {
40+
cmd = exec.Command(javaPath, "-jar", installerPath, "--installServer", "--mirror", "https://bmclapi2.bangbang93.com/maven/")
41+
} else if loader == "neoforge" {
4042
cmd = exec.Command(javaPath, "-jar", installerPath, "--installServer", "--mirror", "https://bmclapi2.bangbang93.com/maven/")
4143
} else if loader == "fabric" {
42-
args := []string{javaPath, "-jar", installerPath, "server"}
43-
if version != "latest" {
44-
args = append(args, "-mcversion", version)
45-
}
46-
if loaderVersion != "latest" {
47-
args = append(args, "-loader", loaderVersion)
48-
}
49-
cmd = exec.Command(args[0], args[1:]...)
44+
cmd = exec.Command(
45+
javaPath, "-jar", installerPath, "server",
46+
"-mavenurl", "https://bmclapi2.bangbang93.com/maven/",
47+
"-metaurl", "https://bmclapi2.bangbang93.com/fabric-meta/",
48+
"-mcversion", version,
49+
"-loader", loaderVersion,
50+
)
5051
} else {
5152
cmd = exec.Command(javaPath, "-jar", installerPath)
5253
}
5354
} else {
54-
if loader == "forge" || loader == "neoforge" {
55+
if loader == "forge" {
56+
cmd = exec.Command(javaPath, "-jar", installerPath, "--installServer")
57+
} else if loader == "neoforge" {
5558
cmd = exec.Command(javaPath, "-jar", installerPath, "--installServer")
5659
} else if loader == "fabric" {
57-
args := []string{javaPath, "-jar", installerPath, "server"}
58-
59-
if version != "latest" {
60-
args = append(args, "-mcversion", version)
61-
}
62-
if loaderVersion != "latest" {
63-
args = append(args, "-loader", loaderVersion)
64-
}
65-
cmd = exec.Command(args[0], args[1:]...)
60+
cmd = exec.Command(
61+
javaPath, "-jar", installerPath, "server",
62+
"-mcversion", version,
63+
"-loader", loaderVersion,
64+
)
6665
} else {
6766
cmd = exec.Command(javaPath, "-jar", installerPath)
6867
}

pkg/fabric.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,103 @@
11
package pkg
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"log"
7+
"net/http"
68
"path/filepath"
79

810
"github.com/autoinst/AutoInstall/core"
911
)
1012

1113
func FabricB(config core.InstConfig, simpfun bool, mise bool) {
12-
var installerURL string
13-
installerURL = "https://maven.fabricmc.net/net/fabricmc/fabric-installer/1.0.1/fabric-installer-1.0.1.jar"
14+
if config.Version == "latest" {
15+
latestRelease, err := FetchLatestFabricMinecraftVersion()
16+
if err != nil {
17+
log.Println("获取最新我的世界版本失败:", err)
18+
return
19+
}
20+
config.Version = latestRelease
21+
stableLoader, err := FetchLatestStableFabricLoaderVersion()
22+
if err != nil {
23+
log.Println("获取最新 Fabric Loader 版本失败:", err)
24+
return
25+
}
26+
config.LoaderVersion = stableLoader
27+
}
28+
if config.LoaderVersion == "latest" {
29+
stableLoader, err := FetchLatestStableFabricLoaderVersion()
30+
if err != nil {
31+
log.Println("获取最新 Fabric Loader 版本失败:", err)
32+
return
33+
}
34+
config.LoaderVersion = stableLoader
35+
}
36+
37+
installerURL := "https://maven.fabricmc.net/net/fabricmc/fabric-installer/1.0.1/fabric-installer-1.0.1.jar"
1438
installerPath := filepath.Join("./.autoinst/cache", "fabric-installer-1.0.1.jar")
1539
fmt.Println("当前为 fabric 加载器,正在下载:", installerURL)
1640
if err := core.DownloadFile(installerURL, installerPath); err != nil {
1741
log.Println("下载 fabric 失败:", err)
1842
return
1943
}
2044
fmt.Println("fabric 安装器下载完成:", installerPath)
45+
2146
librariesDir := "./libraries"
2247
if err := DownloadServerJar(config.Version, config.Loader, librariesDir); err != nil {
23-
log.Println("下载mc服务端失败:", err)
48+
log.Println("下载 mc 服务端失败:", err)
2449
return
2550
}
2651
fmt.Println("服务端下载完成")
52+
2753
if err := core.RunInstaller(installerPath, config.Loader, config.Version, config.LoaderVersion, config.Download, simpfun, mise); err != nil {
2854
log.Println("运行安装器失败:", err)
2955
return
3056
}
3157
core.RunScript(config.Version, config.Loader, config.LoaderVersion, simpfun, mise)
3258
}
59+
60+
func FetchLatestFabricMinecraftVersion() (string, error) {
61+
resp, err := http.Get("https://launchermeta.mojang.com/mc/game/version_manifest.json")
62+
if err != nil {
63+
return "", err
64+
}
65+
defer resp.Body.Close()
66+
67+
var result struct {
68+
Latest struct {
69+
Release string `json:"release"`
70+
} `json:"latest"`
71+
}
72+
73+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
74+
return "", err
75+
}
76+
77+
return result.Latest.Release, nil
78+
}
79+
80+
func FetchLatestStableFabricLoaderVersion() (string, error) {
81+
resp, err := http.Get("https://meta.fabricmc.net/v2/versions/loader")
82+
if err != nil {
83+
return "", err
84+
}
85+
defer resp.Body.Close()
86+
87+
var versions []struct {
88+
Version string `json:"version"`
89+
Stable bool `json:"stable"`
90+
}
91+
92+
if err := json.NewDecoder(resp.Body).Decode(&versions); err != nil {
93+
return "", err
94+
}
95+
96+
for _, v := range versions {
97+
if v.Stable {
98+
return v.Version, nil
99+
}
100+
}
101+
102+
return "", fmt.Errorf("未找到稳定版本的Fabric Loader")
103+
}

0 commit comments

Comments
 (0)