-
Notifications
You must be signed in to change notification settings - Fork 3
/
vanilla_installer.go
150 lines (137 loc) · 4.36 KB
/
vanilla_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
package installer
import (
"path/filepath"
"time"
)
type (
DownloadInfo struct {
Sha1 string `json:"sha1"`
Size int64 `json:"size"`
Url string `json:"url"`
}
AssetIndex struct {
DownloadInfo
Id string `json:"id"`
TotalSize int64 `json:"totalSize"`
}
JavaVersion struct {
Component string `json:"component"`
MajorVersion int `json:"majorVersion"`
}
LibraryDownloadInfo struct {
DownloadInfo
Path string `json:"path"`
}
LibraryDownloads struct {
Artifact *LibraryDownloadInfo `json:"artifact,omitempty"`
Classifiers map[string]LibraryDownloadInfo `json:"classifiers,omitempty"`
}
LibraryRule map[string]any // TODO
LibraryInfo struct {
Name string `json:"name"`
Downloads LibraryDownloads `json:"downloads"`
Rules []LibraryRule `json:"rules,omitempty"`
Extract map[string]any `json:"extract,omitempty"`
Natives map[string]string `json:"natives,omitempty"`
}
VanillaVersion struct {
Id string `json:"id"`
AssetIndex AssetIndex `json:"assetIndex"`
Assets string `json:"assets"`
ComplianceLevel int `json:"complianceLevel"`
Downloads map[string]DownloadInfo `json:"downloads"`
JavaVersion JavaVersion `json:"javaVersion"`
Libraries []LibraryInfo `json:"libraries"`
Logging map[string]any `json:"logging"` // TODO
MainClass string `json:"mainClass"`
MinecraftArguments string `json:"minecraftArguments"`
MinimumLauncherVersion int `json:"minimumLauncherVersion"`
ReleaseTime time.Time `json:"releaseTime"`
Time time.Time `json:"time"`
Type string `json:"type"`
}
VanillaLatestInfo struct {
Release string `json:"release"`
Snapshot string `json:"snapshot"`
}
VanillaVersionInfo struct {
Id string `json:"id"`
Type string `json:"type"`
Url string `json:"url"`
Time time.Time `json:"time"`
ReleaseTime time.Time `json:"releaseTime"`
}
VanillaVersions struct {
Latest VanillaLatestInfo `json:"latest"`
Versions []VanillaVersionInfo `json:"versions"`
}
VanillaInstaller struct {
ManifestUrl string // Default is "https://launchermeta.mojang.com/mc/game/version_manifest.json"
}
)
var _ Installer = (*VanillaInstaller)(nil)
var VanillaIns = &VanillaInstaller{
ManifestUrl: "https://launchermeta.mojang.com/mc/game/version_manifest.json",
}
func init() {
Installers["vanilla"] = VanillaIns
}
func (r *VanillaInstaller) Install(path, name string, target string) (installed string, err error) {
var res VanillaVersions
loger.Info("Getting minecraft version manifest...")
if res, err = r.GetVersions(); err != nil {
return
}
foundVersion := target
if target == "" || target == "latest" {
target = res.Latest.Release
foundVersion += "(" + target + ")"
} else if target == "latest-snapshot" {
target = res.Latest.Snapshot
foundVersion += "(" + target + ")"
}
for _, v := range res.Versions {
if v.Id == target {
var version VanillaVersion
loger.Infof("Getting minecraft version %q...", v.Url)
if version, err = r.GetVersion(v.Url); err != nil {
return
}
info, ok := version.Downloads["server"]
if !ok {
return "", &AssetNotFoundErr{foundVersion, "server.jar"}
}
installed = filepath.Join(path, name+".jar")
if err = DefaultHTTPClient.Download(info.Url, installed, 0644, nil, -1,
downloadingCallback(info.Url)); err != nil {
return
}
return installed, nil
}
}
return "", &VersionNotFoundErr{foundVersion}
}
func (r *VanillaInstaller) ListVersions(snapshot bool) (versions []string, err error) {
vs, err := r.GetVersions()
if err != nil {
return
}
for _, v := range vs.Versions {
if v.Type == "release" || snapshot {
versions = append(versions, v.Id)
}
}
return
}
func (r *VanillaInstaller) GetVersions() (res VanillaVersions, err error) {
if err = DefaultHTTPClient.GetJson(r.ManifestUrl, &res); err != nil {
return
}
return
}
func (r *VanillaInstaller) GetVersion(url string) (res VanillaVersion, err error) {
if err = DefaultHTTPClient.GetJson(url, &res); err != nil {
return
}
return
}