Skip to content

Commit 6474a15

Browse files
committed
测试2
1 parent f253140 commit 6474a15

File tree

7 files changed

+463
-424
lines changed

7 files changed

+463
-424
lines changed

core/config.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package core
2+
3+
import (
4+
"archive/zip"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
)
9+
10+
// InstConfig 结构体表示 inst.json 的内容
11+
type InstConfig struct {
12+
Version string `json:"version"`
13+
Loader string `json:"loader"`
14+
LoaderVersion string `json:"loaderVersion"`
15+
Download string `json:"download"`
16+
MaxConnections int `json:"maxconnections"`
17+
}
18+
19+
// Config 定义配置文件的结构
20+
type Config struct {
21+
MaxConnections int `json:"maxconnections"`
22+
}
23+
24+
// Library 定义库的结构
25+
type Library struct {
26+
Name string `json:"name"`
27+
Downloads struct {
28+
Artifact struct {
29+
URL string `json:"url"`
30+
Path string `json:"path"`
31+
SHA1 string `json:"sha1"`
32+
} `json:"artifact"`
33+
} `json:"downloads"`
34+
}
35+
36+
// VersionInfo 定义 version.json 文件的结构
37+
type VersionInfo struct {
38+
Libraries []Library `json:"libraries"`
39+
}
40+
41+
// 从 JAR 文件中提取 version.json
42+
func ExtractVersionJson(jarFilePath string) (VersionInfo, error) {
43+
var versionInfo VersionInfo
44+
r, err := zip.OpenReader(jarFilePath)
45+
if err != nil {
46+
return versionInfo, fmt.Errorf("无法打开 JAR 文件: %v", err)
47+
}
48+
defer r.Close()
49+
50+
for _, f := range r.File {
51+
if f.Name == "version.json" {
52+
rc, err := f.Open()
53+
if err != nil {
54+
log.Printf("警告: 无法打开 version.json 文件: %v", err)
55+
continue
56+
}
57+
defer rc.Close()
58+
if err := json.NewDecoder(rc).Decode(&versionInfo); err != nil {
59+
log.Printf("警告: 无法解析 version.json: %v", err)
60+
continue
61+
}
62+
return versionInfo, nil
63+
}
64+
}
65+
for _, f := range r.File {
66+
if f.Name == "install_profile.json" {
67+
rc, err := f.Open()
68+
if err != nil {
69+
return versionInfo, fmt.Errorf("无法打开 install_profile.json 文件: %v", err)
70+
}
71+
defer rc.Close()
72+
73+
if err := json.NewDecoder(rc).Decode(&versionInfo); err != nil {
74+
return versionInfo, fmt.Errorf("无法解析 install_profile.json: %v", err)
75+
}
76+
77+
return versionInfo, nil
78+
}
79+
}
80+
81+
return versionInfo, fmt.Errorf("没有找到文件")
82+
}

core/download.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"os"
8+
"time"
9+
)
10+
11+
// ProgressReader 用于跟踪 io.Reader 的进度
12+
type ProgressReader struct {
13+
Reader io.ReadCloser
14+
Total int64
15+
Current int64
16+
FilePath string
17+
UpdateInterval int64 // 更新间隔,单位秒
18+
lastUpdatedTime int64
19+
}
20+
21+
// Read 实现了 io.Reader 接口
22+
func (pr *ProgressReader) Read(p []byte) (n int, err error) {
23+
n, err = pr.Reader.Read(p)
24+
pr.Current += int64(n)
25+
26+
now := time.Now().Unix()
27+
if now-pr.lastUpdatedTime >= pr.UpdateInterval || err == io.EOF {
28+
pr.lastUpdatedTime = now
29+
percent := float64(pr.Current) / float64(pr.Total) * 100
30+
fmt.Printf("下载进度: %.2f%% (%s)\n", percent, pr.FilePath)
31+
}
32+
return
33+
}
34+
35+
func DownloadFile(url, filePath string) error {
36+
const maxRetries = 3
37+
38+
for i := 0; i < maxRetries; i++ {
39+
resp, err := http.Get(url)
40+
if err != nil {
41+
fmt.Printf("尝试 %d/%d 下载失败: %v\n", i+1, maxRetries, err)
42+
continue
43+
}
44+
defer resp.Body.Close()
45+
46+
if resp.StatusCode == http.StatusNotFound {
47+
fmt.Printf("文件未找到,跳过: %s\n", url)
48+
return nil
49+
}
50+
if resp.StatusCode != http.StatusOK {
51+
fmt.Printf("尝试 %d/%d,HTTP 状态码: %d\n", i+1, maxRetries, resp.StatusCode)
52+
continue
53+
}
54+
55+
// 直接用 ContentLength,省去第二次请求
56+
total := resp.ContentLength
57+
if total <= 0 {
58+
fmt.Println("无法获取文件大小,将不显示进度")
59+
}
60+
61+
outFile, err := os.Create(filePath)
62+
if err != nil {
63+
return fmt.Errorf("创建文件失败: %w", err)
64+
}
65+
defer outFile.Close()
66+
67+
reader := &ProgressReader{
68+
Reader: resp.Body,
69+
Total: total,
70+
FilePath: filePath,
71+
UpdateInterval: 3,
72+
}
73+
74+
if _, err = io.Copy(outFile, reader); err != nil {
75+
fmt.Printf("写入失败 %d/%d: %v\n", i+1, maxRetries, err)
76+
continue
77+
}
78+
fmt.Println("下载完成!")
79+
return nil
80+
}
81+
82+
return fmt.Errorf("多次尝试下载失败 (共 %d 次)", maxRetries)
83+
}

core/installer.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"runtime"
10+
"strings"
11+
)
12+
13+
func RunInstaller(installerPath string, loader string, version string, loaderVersion string, Download string) error {
14+
var cmd *exec.Cmd
15+
if Download == "bmclapi" {
16+
if loader == "forge" {
17+
cmd = exec.Command("java", "-jar", installerPath, "--installServer", "--mirror", "https://bmclapi2.bangbang93.com/maven/")
18+
} else if loader == "fabric" {
19+
cmd = exec.Command(
20+
"java", "-jar", installerPath, "server",
21+
"-mavenurl", "https://bmclapi2.bangbang93.com/maven/",
22+
"-metaurl", "https://bmclapi2.bangbang93.com/fabric-meta/",
23+
"-mcversion", version,
24+
"-loader", loaderVersion,
25+
)
26+
} else {
27+
cmd = exec.Command("java", "-jar", installerPath)
28+
}
29+
} else {
30+
if loader == "forge" {
31+
cmd = exec.Command("java", "-jar", installerPath, "--installServer")
32+
} else if loader == "fabric" {
33+
cmd = exec.Command(
34+
"java", "-jar", installerPath, "server",
35+
"-mcversion", version,
36+
"-loader", loaderVersion,
37+
)
38+
} else {
39+
cmd = exec.Command("java", "-jar", installerPath)
40+
}
41+
}
42+
43+
stdout, err := cmd.StdoutPipe()
44+
if err != nil {
45+
return fmt.Errorf("无法获取标准输出: %v", err)
46+
}
47+
stderr, err := cmd.StderrPipe()
48+
if err != nil {
49+
return fmt.Errorf("无法获取标准错误: %v", err)
50+
}
51+
if err := cmd.Start(); err != nil {
52+
return fmt.Errorf("启动命令失败: %v", err)
53+
}
54+
go func() {
55+
io.Copy(os.Stdout, stdout)
56+
}()
57+
go func() {
58+
io.Copy(os.Stderr, stderr)
59+
}()
60+
61+
if err := cmd.Wait(); err != nil {
62+
return fmt.Errorf("命令执行失败: %v", err)
63+
}
64+
return nil
65+
}
66+
67+
func FindJava() (string, bool) {
68+
if runtime.GOOS == "linux" {
69+
simpfun := "/usr/bin/jdk/jdk1.8.0_361/bin/java"
70+
if _, err := os.Stat(simpfun); err == nil {
71+
return simpfun, true
72+
}
73+
}
74+
75+
javaHome := os.Getenv("JAVA_HOME")
76+
if javaHome != "" {
77+
javaPath := filepath.Join(javaHome, "bin", "java")
78+
if _, err := os.Stat(javaPath); err == nil {
79+
return javaPath, false
80+
}
81+
}
82+
83+
cmd := exec.Command("java", "-version")
84+
output, err := cmd.CombinedOutput()
85+
if err == nil && strings.Contains(string(output), "version") {
86+
return "java", false
87+
}
88+
89+
return "", false
90+
}

0 commit comments

Comments
 (0)