-
Notifications
You must be signed in to change notification settings - Fork 3
/
forge_installer.go
156 lines (142 loc) · 3.73 KB
/
forge_installer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package installer
import (
"context"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
type (
ForgeInstaller struct {
MavenUrl string // Default is "https://maven.minecraftforge.net"
}
)
var DefaultForgeInstaller = &ForgeInstaller{
MavenUrl: "https://maven.minecraftforge.net",
}
var _ Installer = DefaultForgeInstaller
func init() {
Installers["forge"] = DefaultForgeInstaller
}
var v1_17 = Version{
Major: 1,
Minor: 17,
Patch: 0,
}
func (r *ForgeInstaller) Install(path, name string, target string) (installed string, err error) {
return r.InstallWithLoader(path, name, target, "")
}
func (r *ForgeInstaller) InstallWithLoader(path, name string, target string, loader string) (installed string, err error) {
foundVersion := target
if target == "" || target == "latest" || target == "latest-snapshot" {
if target == "latest-snapshot" {
loger.Warn("forge do not support snapshot version")
}
var versions VanillaVersions
loger.Info("Getting minecraft version manifest...")
if versions, err = VanillaIns.GetVersions(); err != nil {
return
}
target = versions.Latest.Release
foundVersion += "(" + target + ")"
}
var lessV1_17 bool
{
var v Version
if v, err = VersionFromString(target); err != nil {
return
}
lessV1_17 = v.Less(v1_17)
}
var version string
if len(loader) == 0 {
version, err = r.GetLatestInstaller(target)
if err != nil {
return
}
} else {
version = target + "-" + loader
}
forgeInstallerUrl, err := url.JoinPath(r.MavenUrl, "net/minecraftforge/forge", version, "forge-"+version+"-installer.jar")
if err != nil {
return
}
loger.Infof("Getting forge server installer %s at %q...", foundVersion, forgeInstallerUrl)
var installerJar string
if installerJar, err = DefaultHTTPClient.DownloadTmp(forgeInstallerUrl, "forge-installer-*.jar", 0644, nil, -1,
downloadingCallback(forgeInstallerUrl)); err != nil {
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
javapath, err := lookJavaPath()
if err != nil {
return
}
cmd := exec.CommandContext(ctx, javapath, "-jar", installerJar, "--installServer")
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
loger.Infof("Running %q...", cmd.String())
if err = cmd.Run(); err != nil {
return
}
if lessV1_17 { // < 1.17 use forge-<minecraft_version>-<loader_version>.jar
installed = filepath.Join(path, name+".jar")
if err = renameIfNotExist("forge-"+version+".jar", installed, 0644); err != nil {
return
}
return
}
// >= 1.17 use run.sh or run.bat
installedSh := filepath.Join(path, name+".sh")
if err = renameIfNotExist("run.sh", installedSh, 0744); err != nil {
return
}
installedBat := filepath.Join(path, name+".bat")
if err = renameIfNotExist("run.bat", installedBat, 0744); err != nil {
return
}
installed = installedSh
if runtime.GOOS == "windows" {
installed = installedBat
}
return
}
func (r *ForgeInstaller) ListVersions(snapshot bool) (versions []string, err error) {
data, err := r.GetInstallerVersions()
if err != nil {
return
}
for _, v := range data.Versioning.Versions {
i := strings.IndexByte(v, '-')
versions = append(versions, v[i+1:])
}
return
}
func (r *ForgeInstaller) GetInstallerVersions() (data MavenMetadata, err error) {
link, err := url.JoinPath(r.MavenUrl, "net/minecraftforge/forge")
if err != nil {
return
}
return GetMavenMetadata(link)
}
func (r *ForgeInstaller) GetLatestInstaller(target string) (version string, err error) {
data, err := r.GetInstallerVersions()
if err != nil {
return
}
tgName := target + "-"
for _, v := range data.Versioning.Versions {
if strings.HasPrefix(v, tgName) {
version = v
break
}
}
if len(version) == 0 {
return "", &VersionNotFoundErr{"forge-" + target}
}
return
}