Skip to content
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 #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ This is a minecraft server online installer
You can use this cli to easily install minecraft server.
We also support some types of servers and modpacks

| Server Type | Support |
|-------------|---------|
| Vanilla | true |
| Fabric | true |
| Forge | true |
| Quilt | true |
| Spigot | true |
| PaperMC | TODO |
| ArcLight | TODO |
| Server Type | Support |
|--------------|---------|
| Vanilla | true |
| Fabric | true |
| Forge | true |
| Quilt | true |
| Spigot | true |
| PaperMC | true |
| ArcLight | true |
| Mohist | TODO |
| Catserver | TODO |

| Modpack Type | Support |
|--------------|---------|
Expand All @@ -51,7 +53,7 @@ Flags:
the version of the server need to be installed, default is the latest (default "latest")
Args:
<server_type> string
type of the server [fabric forge quilt spigot vanilla] (default "vanilla" )
type of the server [fabric forge quilt spigot vanilla papermc arclight] (default "vanilla" )
<modpack_file> filepath | URL
the modpack's local path or an URL. If it's an URL, installer will download the modpack first
```
Expand Down Expand Up @@ -80,6 +82,12 @@ minecraft_installer -name minecraft_server -version 1.16.5 -server forge
minecraft_installer -name minecraft_server -version 1.19.2 -server fabric -path server
```

```sh
# Install papermc 1.14.4 server into server/minecraft_server.jar
minecraft_installer -name minecraft_server -version 1.14.4 -server papermc
# papermc and arclight installation will automatically generate and rename different directories for different builder.
```

### Install modpacks

```sh
Expand All @@ -105,8 +113,8 @@ minecraft_installer versions
minecraft_installer -version snapshot versions
```


## TODO

- [ ] PaperMC
- [ ] Search modpacks from modrinth
- [ ] Configurable proxy
17 changes: 15 additions & 2 deletions README_zh.MD
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
| Forge | 是 |
| Quilt | 是 |
| Spigot | 是 |
| PaperMC | 进行中 |
| ArcLight | 进行中 |
| PaperMC | 是 |
| ArcLight | 是 |
| Mohist | 计划中/否 |
| Catserver | 计划中/否 |

| 整合包类型 | 支持 |
|--------------|----------|
Expand Down Expand Up @@ -76,6 +78,12 @@ minecraft_installer -name minecraft_server -version 1.16.5 -server forge
minecraft_installer -name minecraft_server -version 1.19.2 -server fabric -path server
```

```sh
# 将 papermc 1.14.4 服务端下载到 server/minecraft_server.jar
minecraft_installer -name minecraft_server -version 1.14.4 -server papermc
# 注:papermc以及arclight服务器因build区别会区分到不同文件夹中,执行中会自动建立并命名此server文件夹,更加方便识别
```

### 安装整合包

```sh
Expand All @@ -100,3 +108,8 @@ minecraft_installer versions
```sh
minecraft_installer -version snapshot versions
```

## 计划中
Fellsoul marked this conversation as resolved.
Show resolved Hide resolved

- [ ] 从modrinth搜寻整合包
- [ ] 代理相关
189 changes: 189 additions & 0 deletions arclight_installer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package installer

import (
"context"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)

type (
ArclightInstaller struct {}

ArclightAssets struct {
AssetsUrl string `json:"url"`
AssetsName string `json:"name"`
DownloadUrl string `json:"browser_download_url"`
}
Fellsoul marked this conversation as resolved.
Show resolved Hide resolved

ArclightRelease struct {
Assets []ArclightAssets `json:"assets"`
IsExpired bool
PublishTime string `json:"published_at"`
Fellsoul marked this conversation as resolved.
Show resolved Hide resolved
}
)

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) {
versions, err := r.GetInstallerVersions()
if err != nil {
return "", err
}
if len(loader) == 0 {
var alreadyFind bool = false
allVersions := r.GetOnlyVersions(versions)
if target == "latest" {
loader, err = r.GetLatestVersion()
if err != nil {
return "", err
}
alreadyFind = true
}
for _, version := range allVersions {
if version == target {
loader = target
alreadyFind = true
}
}
if !alreadyFind {
loger.Info("not find the suitable builder, the version should be included in the following list:")
for _, version := range allVersions {
if versions[version].IsExpired {
loger.Info("versions:", version, " EXPIRED, DO NOT SUPPORT")
} else {
loger.Info("versions:", version)
}
}
return "", &VersionNotFoundErr{target}
}
}
exactDownloadeName := versions[loader].Assets[0].AssetsName
ArclightInstallerUrl := versions[loader].Assets[0].DownloadUrl
if version, ok := versions[loader]; ok && version.IsExpired {
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) ([]string, error) {
additionalVersions, err := r.GetInstallerVersions()
if err != nil {
return nil, err
}
return r.GetOnlyVersions(additionalVersions), err
}

func (r *ArclightInstaller) GetLatestVersion() (version string, err error) {
additionalVersions, err := r.GetInstallerVersions()
if err != nil {
return
}
var dataVersions []string = r.GetOnlyVersions(additionalVersions)
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) {
additionalVersions := make(map[string]ArclightRelease)
link := "https://api.github.com/repos/IzzelAliz/Arclight/releases"
var releases []*ArclightRelease
err := DefaultHTTPClient.GetJson(link, &releases)
if err != nil {
return additionalVersions, err
}
for _, release := range releases {
details := strings.Split(release.Assets[0].AssetsName, "-")
// details should be ["arclight","forge","{VERSION}","{BUILDNUM}.jar"], so append value of index 2
layout := "2006-01-02T14:41:48Z"
timeDetails, err := time.Parse(layout, release.PublishTime)
if err != nil{
return additionalVersions, err
}
year := timeDetails.Year()
month := timeDetails.Month()
expiredYear := 2024
expiredMonth := time.February
if year < expiredYear || (year == expiredYear && month < expiredMonth) {
release.IsExpired = true
} else {
release.IsExpired = false
}
if len(additionalVersions[details[2]].Assets) == 0 {
additionalVersions[details[2]] = *release
}
// to get the newest builder for each version
}
return additionalVersions, err
}

func (r *ArclightInstaller) GetOnlyVersions(additionalVersions map[string]ArclightRelease) (versions []string) {
for k, _ := range additionalVersions {
versions = append(versions, k)
}
return
}

func (r *ArclightInstaller) Runbuilder(buildJar string, ExactDownloadName string, path string) (installed string, err error) {
const SUFFIX_LENGTH int = 4
NameWithoutSuffix := ExactDownloadName[0 : len(ExactDownloadName)-SUFFIX_LENGTH]
serverDirectory := filepath.Join(".", "server-"+NameWithoutSuffix)
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)-SUFFIX_LENGTH])
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
loger.Infof("Running %q...", cmd.String())
if err = cmd.Run(); err != nil {
return
}
installed = buildJar
return
}
9 changes: 9 additions & 0 deletions changelogs/v1.2.4.MD
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
4 changes: 4 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func main() {
installed, err = installer.DefaultFabricInstaller.InstallWithLoader(InstallPath, ExecutableName, minecraft, fabric)
} else if quilt, ok := pack.Deps["quilt-loader"]; ok {
installed, err = installer.DefaultQuiltInstaller.InstallWithLoader(InstallPath, ExecutableName, minecraft, quilt)
} else if papermc, ok := pack.Deps["papermc-loader"]; ok {
installed, err = installer.DefaultQuiltInstaller.InstallWithLoader(InstallPath, ExecutableName, minecraft, papermc)
} else if arclight, ok := pack.Deps["arclight-loader"]; ok {
installed, err = installer.DefaultQuiltInstaller.InstallWithLoader(InstallPath, ExecutableName, minecraft, arclight)
} else if mok {
installed, err = installer.VanillaIns.Install(InstallPath, ExecutableName, minecraft)
} else {
Expand Down
35 changes: 35 additions & 0 deletions httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -193,3 +194,37 @@ func (c *HTTPClient) PostForm(url string, form url.Values) (res *http.Response,
return c.Post(url, "application/x-www-form-urlencoded",
strings.NewReader(formStr))
}

func (c *HTTPClient) DownloadDirect(url string, ExactDownloadeName string, cb DlCallback) (installed string, err error) {
resp, err := http.Head(url)
if err != nil {
return
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
resp, err = http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
filename := filepath.Base(url)
f, err := os.OpenFile(filename, syscall.O_CREAT, 0644)
if err != nil {
return
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
if err == io.EOF {
return
}
}
cpath, err := os.Getwd()
if err != nil {
return
}
installed = filepath.Join(cpath, ExactDownloadeName)
return
}
Loading