-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminecraftforge.go
153 lines (134 loc) · 3.92 KB
/
minecraftforge.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
package ops
import (
"errors"
"github.com/anaskhan96/soup"
"io"
"net/http"
"strings"
)
func DownloadMinecraftForge(version string, forgeVersion string, experimental bool) ([]byte, string, error) {
log("attempting to download", version, "using minecraft forge html parsing")
slog("downloading minecraft version list")
resp, err := soup.Get("https://files.minecraftforge.net/net/minecraftforge/forge/index.html")
if err != nil {
return nil, "", err
}
log("retrieved minecraft version list")
slog("getting version lists")
log("parsing minecraft version list")
doc := soup.HTMLParse(resp)
var versionIndexes []string
elems := doc.FindAll("a")
for _, link := range elems {
href := link.Attrs()["href"]
if strings.Contains(href, "index") && versionRegex.MatchString(href) {
versionIndexes = append(versionIndexes, "https://files.minecraftforge.net/net/minecraftforge/forge/"+href)
}
}
versionIndexes = append(versionIndexes, "https://files.minecraftforge.net/net/minecraftforge/forge/index_"+doc.Find("li", "class", "elem-active").Text()+".html")
log("parsed all minecraft versions")
var index string
if version == "latest" {
if experimental {
index = versionIndexes[0]
} else if !experimental {
index = versionIndexes[len(versionIndexes)-1]
}
} else {
found := false
for i := range versionIndexes {
if strings.Contains(versionIndexes[i], version) {
found = true
index = "https://files.minecraftforge.net/net/minecraftforge/forge/index_" + version + ".html"
break
}
}
if !found {
return nil, "", errors.New("could not locate version in version list")
}
}
version = versionRegex.FindStringSubmatch(index)[0]
log("using version", version)
log("downloading forge version list")
resp, err = soup.Get(index)
if err != nil {
return nil, "", err
}
log("retrieved forge version list")
log("parsing forge version list")
doc = soup.HTMLParse(resp)
indexOfVersionListIfLatestAndExperimental := 0
if forgeVersion == "latest" && !experimental {
elems = doc.FindAll("td", "class", "download-version")
for i := range elems {
if len(elems[i].Children()) == 3 {
if strings.Contains(elems[i].Children()[1].Attrs()["class"], "recommended") {
indexOfVersionListIfLatestAndExperimental = i
break
}
}
}
}
elems = doc.FindAll("div", "class", "info-tooltip")
var jarSha1, jarUrl string
for i := range elems {
e := elems[i]
if !strings.Contains(e.HTML(), "installer.jar") {
continue
}
sha1 := strings.Trim(e.Children()[6].HTML(), " \n")
url := e.Children()[8].Attrs()["href"]
if forgeVersion == "latest" {
if experimental {
jarUrl = url
jarSha1 = sha1
break
} else {
if i/3 == indexOfVersionListIfLatestAndExperimental {
jarUrl = url
jarSha1 = sha1
break
}
}
} else {
if strings.Contains(url, forgeVersion) {
jarUrl = url
jarSha1 = sha1
}
break
}
}
log("parsed all forge versions")
slog("downloading server installer")
res, err := http.Get(jarUrl)
if err != nil {
return nil, "", err
}
log("downloaded server installer")
defer func() { _ = res.Body.Close() }()
jar, err := io.ReadAll(res.Body)
//log("jarfile size:", len(jar))
//log("minecraft version number:", version)
if printChecksum {
post("sha1 checksum:", jarSha1)
}
post("the jar is an installer, not a server jar!")
return jar, version, nil
}
func GetVersionListMinecraftForge() ([]string, error) {
resp, err := soup.Get("https://files.minecraftforge.net/net/minecraftforge/forge/index.html")
if err != nil {
return nil, err
}
doc := soup.HTMLParse(resp)
var versions []string
elems := doc.FindAll("a")
for _, link := range elems {
href := link.Attrs()["href"]
if strings.Contains(href, "index") && versionRegex.MatchString(href) {
versions = append(versions, versionRegex.FindStringSubmatch(href)[0])
}
}
versions = append([]string{doc.Find("li", "class", "elem-active").Text()}, versions...)
return versions, nil
}