-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Papermc arclight #1
Changes from all commits
f51a0e4
d8c50dc
f7e297f
4e38ddf
197373b
850ed81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,195 @@ | ||||||||||
package installer | ||||||||||
|
||||||||||
import ( | ||||||||||
"context" | ||||||||||
"os" | ||||||||||
"os/exec" | ||||||||||
"path/filepath" | ||||||||||
"strconv" | ||||||||||
"strings" | ||||||||||
) | ||||||||||
|
||||||||||
type ( | ||||||||||
ArclightInstaller struct { | ||||||||||
} | ||||||||||
|
||||||||||
ArclightRelease struct { | ||||||||||
Assets []ArclightAssets `json:"assets"` | ||||||||||
IsExpired bool | ||||||||||
PublishTime string `json:"published_at"` | ||||||||||
} | ||||||||||
|
||||||||||
ArclightAssets struct { | ||||||||||
AssetsUrl string `json:"url"` | ||||||||||
AssetsName string `json:"name"` | ||||||||||
DownloadUrl string `json:"browser_download_url"` | ||||||||||
} | ||||||||||
) | ||||||||||
|
||||||||||
var DefaultArclightInstaller = &ArclightInstaller{} | ||||||||||
|
||||||||||
var _ Installer = DefaultArclightInstaller | ||||||||||
|
||||||||||
func init() { | ||||||||||
Installers["arclight"] = DefaultArclightInstaller | ||||||||||
} | ||||||||||
|
||||||||||
func (r *ArclightInstaller) Install(path, name string, target string) (installed string, err error) { | ||||||||||
return r.InstallWithLoader(path, name, target, "") | ||||||||||
} | ||||||||||
|
||||||||||
func (r *ArclightInstaller) InstallWithLoader(path, name string, target string, loader string) (installed string, err error) { | ||||||||||
data, err := r.GetInstallerVersions() | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好的 |
||||||||||
if err != nil { | ||||||||||
return "", err | ||||||||||
} | ||||||||||
if len(loader) == 0 { | ||||||||||
allVersions := r.GetOnlyVersions(data) | ||||||||||
if target == "latest" { | ||||||||||
loader, err = r.GetLatestVersion() | ||||||||||
if err != nil { | ||||||||||
return "", err | ||||||||||
} | ||||||||||
goto DownloadPart | ||||||||||
} | ||||||||||
for i := 0; i < len(allVersions); i += 1 { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
或者直接
Suggested change
|
||||||||||
if allVersions[i] == target { | ||||||||||
loader = target | ||||||||||
goto DownloadPart | ||||||||||
} | ||||||||||
} | ||||||||||
loger.Info("not find the suitable builder, the version should be included in the following list:") | ||||||||||
for i := 0; i < len(allVersions); i += 1 { | ||||||||||
if data[allVersions[i]].IsExpired == true { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为什么我还是能遇到 == true 这种操作 😓
Suggested change
|
||||||||||
loger.Info("versions:", allVersions[i], " EXPIRED, DO NOT SUPPORT") | ||||||||||
} else { | ||||||||||
loger.Info("versions:", allVersions[i]) | ||||||||||
} | ||||||||||
} | ||||||||||
return "", &VersionNotFoundErr{target} | ||||||||||
} | ||||||||||
DownloadPart: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Label 用全大写字母+下划线,另外,能不用goto就别用 |
||||||||||
ExactDownloadeName := data[loader].Assets[0].AssetsName | ||||||||||
ArclightInstallerUrl := data[loader].Assets[0].DownloadUrl | ||||||||||
if data[loader].IsExpired == true { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你不能假设loader存在于data里
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 疏忽了 |
||||||||||
loger.Fatal("Sorry, the one you choose has already expired, try another version.") | ||||||||||
return "", &VersionNotFoundErr{target} | ||||||||||
} | ||||||||||
var buildJar string | ||||||||||
if buildJar, err = DefaultHTTPClient.DownloadDirect(ArclightInstallerUrl, ExactDownloadeName, downloadingCallback(ArclightInstallerUrl)); err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
installed, err = r.Runbuilder(buildJar, ExactDownloadeName, path) | ||||||||||
if err != nil { | ||||||||||
loger.Info("an error occurred while running the server jar file, but you can still do that manually.") | ||||||||||
loger.Error(err) | ||||||||||
} | ||||||||||
return | ||||||||||
} | ||||||||||
|
||||||||||
func (r *ArclightInstaller) ListVersions(snapshot bool) (versions []string, err error) { | ||||||||||
data, err := r.GetInstallerVersions() | ||||||||||
if err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
var dataVersions []string = r.GetOnlyVersions(data) | ||||||||||
for _, v := range dataVersions { | ||||||||||
versions = append(versions, v) | ||||||||||
} | ||||||||||
return | ||||||||||
} | ||||||||||
|
||||||||||
func (r *ArclightInstaller) GetLatestVersion() (version string, err error) { | ||||||||||
data, err := r.GetInstallerVersions() | ||||||||||
if err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
var dataVersions []string = r.GetOnlyVersions(data) | ||||||||||
var v0, v1 Version | ||||||||||
for _, v := range dataVersions { | ||||||||||
if v1, err = VersionFromString(v); err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
if v0.Less(v1) { | ||||||||||
v0 = v1 | ||||||||||
} | ||||||||||
} | ||||||||||
version = v0.String() | ||||||||||
return | ||||||||||
} | ||||||||||
|
||||||||||
func (r *ArclightInstaller) GetInstallerVersions() (map[string]ArclightRelease, error) { | ||||||||||
data := make(map[string]ArclightRelease) | ||||||||||
link := "https://api.github.com/repos/IzzelAliz/Arclight/releases" | ||||||||||
var releases []ArclightRelease | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
err := DefaultHTTPClient.GetJson(link, &releases) | ||||||||||
if err != nil { | ||||||||||
return data, err | ||||||||||
} | ||||||||||
for i := 0; i < len(releases); i += 1 { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
details := strings.Split(releases[i].Assets[0].AssetsName, "-") | ||||||||||
//details should be ["arclight","forge","{VERSION}","{BUILDNUM}.jar"], so append value of index 2 | ||||||||||
timeDetails := strings.Split(releases[i].PublishTime, "-") | ||||||||||
//time should be "{YEAR}-{MONTH}-{DATE}T{CLOCK}}" | ||||||||||
year, err := strconv.Atoi(timeDetails[0]) | ||||||||||
if err != nil { | ||||||||||
return data, err | ||||||||||
} | ||||||||||
month, err := strconv.Atoi(timeDetails[1]) | ||||||||||
if err != nil { | ||||||||||
return data, err | ||||||||||
} | ||||||||||
if year < 2024 || (year == 2024 && month < 2) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 判断是否过期,因为在2024.2月以前的所有构建都失效了,而且想不到比这样判断更好的方法了,就直接这样硬编码了 |
||||||||||
releases[i].IsExpired = true | ||||||||||
} else { | ||||||||||
releases[i].IsExpired = false | ||||||||||
} | ||||||||||
if len(data[details[2]].Assets) == 0 { | ||||||||||
data[details[2]] = releases[i] | ||||||||||
} | ||||||||||
//to get the newest builder for each version | ||||||||||
} | ||||||||||
return data, err | ||||||||||
} | ||||||||||
|
||||||||||
func (r *ArclightInstaller) GetOnlyVersions(data map[string]ArclightRelease) (versions []string) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
for k := range data { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
versions = append(versions, k) | ||||||||||
} | ||||||||||
return | ||||||||||
} | ||||||||||
|
||||||||||
func (r *ArclightInstaller) Runbuilder(buildJar string, ExactDownloadName string, path string) (installed string, err error) { | ||||||||||
currentDir, err := os.Getwd() | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
if err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
serverDirectory := filepath.Join(currentDir, "server-"+ExactDownloadName[0:len(ExactDownloadName)-4]) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要出现 |
||||||||||
os.RemoveAll(serverDirectory) | ||||||||||
err = os.MkdirAll(serverDirectory, os.ModePerm) | ||||||||||
if err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
err = os.Rename(buildJar, filepath.Join(serverDirectory, ExactDownloadName)) | ||||||||||
if err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
buildJar = filepath.Join(serverDirectory, ExactDownloadName) | ||||||||||
loger.Info("Server jar file is successfully installed in path: " + buildJar) | ||||||||||
ctx, cancel := context.WithCancel(context.Background()) | ||||||||||
defer cancel() | ||||||||||
javapath, err := lookJavaPath() | ||||||||||
if err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
cmd := exec.CommandContext(ctx, javapath, "-jar", buildJar) | ||||||||||
cmd.Dir = filepath.Join(path, "server-"+ExactDownloadName[0:len(ExactDownloadName)-4]) | ||||||||||
cmd.Stdout = os.Stdout | ||||||||||
cmd.Stderr = os.Stdout | ||||||||||
loger.Infof("Running %q...", cmd.String()) | ||||||||||
if err = cmd.Run(); err != nil { | ||||||||||
return | ||||||||||
} | ||||||||||
installed = buildJar + "\n" | ||||||||||
return | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
#### Adds | ||
|
||
- Add the support of Papermc server downloading and available versions inquiry | ||
- Add the support of Arclight server downloading and available versions inquiry | ||
|
||
#### Changes | ||
|
||
- Support new types of server: Papermc, Arclight |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1 @@ | ||
package main | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usage 太长,所以单放一个文件 |
||
const UsageText = ` | ||
minecraft_installer [...flags] <server_type> | ||
minecraft_installer [...flags] modpack <modpack_file> | ||
minecraft_installer [...flags] versions [<server_type>] | ||
|
||
Example: | ||
Install servers: | ||
minecraft_installer -name minecraft_server -version 1.7.10 vanilla | ||
Install minecraft 1.7.10 vanilla server into minecraft_server.jar | ||
minecraft_installer -name minecraft_server -version 1.19.2 forge | ||
Install minecraft 1.19.2 forge server into current directory and the executable is minecraft_server.sh | ||
Hint: forge installer will make run scripts for the minecraft version that higher or equal than 1.17 | ||
for version that less than 1.17, you still need to use 'java -jar' to run the server | ||
minecraft_installer -name minecraft_server -version 1.19.2 -output server fabric | ||
Install minecraft 1.19.2 fabric server into server/minecraft_server.jar | ||
Install modpacks: | ||
minecraft_installer -name modpack_server modpack /path/to/modrinth-modpack.mrpack | ||
Install the modpack from local to the current directory | ||
Hint: Only support modrinth modpack for now, curseforge is in progress | ||
minecraft_installer -name modpack_server modpack 'https://cdn-raw.modrinth.com/data/sl6XzkCP/versions/i4agaPF2/Automation%20v3.3.mrpack' | ||
Install the modpack from internet to the current directory | ||
Hint: if you want to install modpack from the internet, | ||
you must add the prefixs [https://, http://] | ||
List Versions: | ||
minecraft_installer versions | ||
List all vanilla versions but without snapshots | ||
minecraft_installer -version snapshot versions | ||
List all vanilla versions include snapshots | ||
` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.