mirror of
https://github.com/zs-yg/MCSJ.git
synced 2025-12-06 19:00:43 +08:00
Compare commits
11 Commits
v0.0.4
...
9e5e290da5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e5e290da5 | ||
|
|
59b7c84310 | ||
|
|
306a7bb309 | ||
|
|
62713817e1 | ||
|
|
1870b066fc | ||
|
|
7d74c603b8 | ||
|
|
aea0742092 | ||
|
|
295791b4f2 | ||
|
|
ff0cf01100 | ||
|
|
b88c7507aa | ||
|
|
969fcdff6e |
19
Program.cs
19
Program.cs
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||
using MCSJ.Tools;
|
||||
using MCSJ.Tools.LogSystem;
|
||||
using MCSJ.Tools.JreDownload;
|
||||
using MCSJ.Tools.ViewJre;
|
||||
|
||||
namespace MCSJ
|
||||
{
|
||||
@@ -15,22 +16,24 @@ namespace MCSJ
|
||||
Console.WriteLine($"日志目录: {logDir}");
|
||||
LogMain.Debug($"日志文件: {LogCreator.GetLogFilePath()}");
|
||||
|
||||
LogMain.Info("MC服务器下载工具启动");
|
||||
LogMain.Info("MC服务器启动工具启动");
|
||||
var httpClient = new HttpClient {
|
||||
Timeout = TimeSpan.FromMinutes(5),
|
||||
DefaultRequestHeaders = { { "User-Agent", "MCSJ-JRE-Downloader" } }
|
||||
};
|
||||
var versionManager = new VersionManager();
|
||||
var downloadService = new DownloadService(versionManager);
|
||||
var jreViewer = new JreViewer();
|
||||
LogMain.Debug("服务初始化完成");
|
||||
|
||||
while (true)
|
||||
{
|
||||
Console.WriteLine("MC服务器下载工具");
|
||||
Console.WriteLine("1. 显示所有版本");
|
||||
Console.WriteLine("2. 下载指定版本");
|
||||
Console.WriteLine("MC服务器启动工具");
|
||||
Console.WriteLine("1. 显示所有服务器版本");
|
||||
Console.WriteLine("2. 下载指定服务器版本");
|
||||
Console.WriteLine("3. 下载JRE");
|
||||
Console.WriteLine("4. 退出");
|
||||
Console.WriteLine("4. 查看已安装的JRE");
|
||||
Console.WriteLine("5. 退出");
|
||||
Console.Write("请选择操作: ");
|
||||
|
||||
var input = Console.ReadLine();
|
||||
@@ -50,7 +53,7 @@ namespace MCSJ
|
||||
LogMain.Info($"版本下载完成: {version}");
|
||||
break;
|
||||
case "3":
|
||||
Console.Write("请输入要下载的JRE版本(如jre8,jre11等): ");
|
||||
Console.Write("请输入要下载的JRE版本(jre8,jre11,jre17/21/25): ");
|
||||
var jreVersion = Console.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(jreVersion))
|
||||
{
|
||||
@@ -63,6 +66,10 @@ namespace MCSJ
|
||||
LogMain.Info($"JRE下载完成: {jreVersion}");
|
||||
break;
|
||||
case "4":
|
||||
jreViewer.DisplayInstalledJres();
|
||||
LogMain.Info("显示已安装的JRE列表");
|
||||
break;
|
||||
case "5":
|
||||
LogMain.Info("程序正常退出");
|
||||
return;
|
||||
default:
|
||||
|
||||
@@ -35,7 +35,11 @@ MCSJ 是一个开源的 Minecraft 服务器管理工具,旨在为用户提供
|
||||
2. 双击运行 MCSJ.exe 启动程序。
|
||||
3. 或者命令行输入MCSJ.exe 启动程序。可以查看是否需要安装运行时
|
||||
|
||||
## 关于作者
|
||||
## 官方镜像仓库
|
||||
|
||||
[gitee镜像仓库](https://gitee.com/chr_super/mcsj)
|
||||
|
||||
## 关于
|
||||
|
||||
MCSJ 项目由 [zs-yg](https://github.com/zs-yg) 开发,欢迎提交 issue 和 PR。
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using MCSJ.Tools.LogSystem;
|
||||
|
||||
namespace MCSJ.Tools
|
||||
{
|
||||
@@ -21,6 +22,7 @@ namespace MCSJ.Tools
|
||||
if (string.IsNullOrWhiteSpace(version))
|
||||
{
|
||||
Console.WriteLine("版本名称不能为空");
|
||||
LogMain.Error("版本名称不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -28,6 +30,7 @@ namespace MCSJ.Tools
|
||||
if (string.IsNullOrEmpty(url))
|
||||
{
|
||||
Console.WriteLine($"版本 {version} 不存在");
|
||||
LogMain.Error($"版本 {version} 不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,10 +50,12 @@ namespace MCSJ.Tools
|
||||
if (!Directory.Exists(profilePath))
|
||||
break;
|
||||
Console.WriteLine($"文件夹 '{targetFolder}' 已存在,请重新输入(直接回车则取消下载):");
|
||||
LogMain.Warn($"文件夹 '{targetFolder}' 已存在,请重新输入(直接回车则取消下载):");
|
||||
}
|
||||
if (Directory.Exists(profilePath))
|
||||
{
|
||||
Console.WriteLine("下载已取消。");
|
||||
LogMain.Info("下载已取消。");
|
||||
return;
|
||||
}
|
||||
Directory.CreateDirectory(profilePath);
|
||||
@@ -59,6 +64,7 @@ namespace MCSJ.Tools
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"开始下载 {version} 到 {profilePath} ...");
|
||||
LogMain.Info($"开始下载 {version} 到 {profilePath} ...");
|
||||
var response = await _httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
@@ -90,10 +96,12 @@ namespace MCSJ.Tools
|
||||
}
|
||||
}
|
||||
Console.WriteLine($"\n{version} 下载完成! 文件已保存到 {jarPath}");
|
||||
LogMain.Info($"{version} 下载完成! 文件已保存到 {jarPath}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"下载失败: {ex.Message}");
|
||||
LogMain.Error($"下载失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MCSJ.Tools.LogSystem;
|
||||
|
||||
namespace MCSJ.Tools
|
||||
{
|
||||
@@ -19,6 +20,7 @@ namespace MCSJ.Tools
|
||||
{
|
||||
var filePath = Path.Combine("resources", "serverlist.txt");
|
||||
Console.WriteLine($"尝试从路径加载版本列表: {Path.GetFullPath(filePath)}");
|
||||
LogMain.Info($"尝试从路径加载版本列表: {Path.GetFullPath(filePath)}");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
@@ -42,7 +44,8 @@ namespace MCSJ.Tools
|
||||
|
||||
if (string.IsNullOrEmpty(version) || string.IsNullOrEmpty(url))
|
||||
{
|
||||
Console.WriteLine($"忽略无效条目: {rawLine} (版本或URL为空)");
|
||||
Console.WriteLine($"忽略无效条目: {rawLine} (版本或URL为空)");
|
||||
LogMain.Warn($"忽略无效条目: {rawLine} (版本或URL为空)");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -51,6 +54,7 @@ namespace MCSJ.Tools
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"忽略无效条目: {rawLine} (缺少冒号分隔或格式不正确)");
|
||||
LogMain.Warn($"忽略无效条目: {rawLine} (缺少冒号分隔或格式不正确)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,12 +64,15 @@ namespace MCSJ.Tools
|
||||
}
|
||||
|
||||
Console.WriteLine($"成功加载 {_versions.Count} 个版本");
|
||||
LogMain.Info($"成功加载 {_versions.Count} 个版本");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"加载版本列表失败: {ex.Message}");
|
||||
Console.WriteLine($"当前工作目录: {Directory.GetCurrentDirectory()}");
|
||||
Console.WriteLine("请确保serverlist.txt每行格式为: 版本名:下载URL (版本名可以包含空格),支持以#开头的注释");
|
||||
LogMain.Error($"加载版本列表失败: {ex.Message}");
|
||||
LogMain.Error($"当前工作目录: {Directory.GetCurrentDirectory()}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,10 +82,12 @@ namespace MCSJ.Tools
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
Console.WriteLine("版本列表文件不存在");
|
||||
LogMain.Error("版本列表文件不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("可用版本列表:");
|
||||
Console.WriteLine("可用版本列表:");
|
||||
LogMain.Info("可用版本列表:");
|
||||
foreach (var version in _versions.Keys)
|
||||
{
|
||||
Console.WriteLine(version);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using MCSJ.Tools.LogSystem;
|
||||
|
||||
namespace MCSJ.Tools.JreDownload
|
||||
{
|
||||
@@ -33,6 +34,7 @@ namespace MCSJ.Tools.JreDownload
|
||||
if (percentage == 100)
|
||||
{
|
||||
Console.WriteLine();
|
||||
LogMain.Info("JRE下载完成");
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO.Compression;
|
||||
using MCSJ.Tools.LogSystem;
|
||||
|
||||
namespace MCSJ.Tools.JreDownload
|
||||
{
|
||||
@@ -21,11 +22,20 @@ namespace MCSJ.Tools.JreDownload
|
||||
|
||||
public async Task DownloadAndSetupJre(string version)
|
||||
{
|
||||
// 0. 检查是否已存在该版本
|
||||
if (CheckJreExists(version))
|
||||
{
|
||||
Console.WriteLine($"JRE {version} 已存在,无需重复下载");
|
||||
LogMain.Info($"JRE {version} 已存在,无需重复下载");
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 读取jrelist.txt获取下载链接
|
||||
var downloadUrl = GetDownloadUrl(version);
|
||||
if (string.IsNullOrEmpty(downloadUrl))
|
||||
{
|
||||
Console.WriteLine($"找不到版本 {version} 的下载链接");
|
||||
LogMain.Error($"找不到版本 {version} 的下载链接");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -35,6 +45,7 @@ namespace MCSJ.Tools.JreDownload
|
||||
if (string.IsNullOrEmpty(tempZipPath))
|
||||
{
|
||||
Console.WriteLine("下载失败");
|
||||
LogMain.Error("下载失败");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -43,6 +54,7 @@ namespace MCSJ.Tools.JreDownload
|
||||
if (!ExtractJre(tempZipPath, jreFolder))
|
||||
{
|
||||
Console.WriteLine("解压失败");
|
||||
LogMain.Error("解压失败");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -53,6 +65,7 @@ namespace MCSJ.Tools.JreDownload
|
||||
if (javaExePath == null || javawExePath == null)
|
||||
{
|
||||
Console.WriteLine("找不到java.exe或javaw.exe");
|
||||
LogMain.Error("找不到java.exe或javaw.exe");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -62,7 +75,8 @@ namespace MCSJ.Tools.JreDownload
|
||||
// 6. 清理临时文件
|
||||
File.Delete(tempZipPath);
|
||||
|
||||
Console.WriteLine($"JRE {version} 安装完成");
|
||||
Console.WriteLine($"JRE {version} 安装完成");
|
||||
LogMain.Info($"JRE {version} 安装完成");
|
||||
}
|
||||
|
||||
private string? GetDownloadUrl(string version)
|
||||
@@ -92,18 +106,22 @@ namespace MCSJ.Tools.JreDownload
|
||||
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
|
||||
{
|
||||
Console.WriteLine($"无效的下载URL: {url}");
|
||||
LogMain.Error($"无效的下载URL: {url}");
|
||||
return null;
|
||||
}
|
||||
|
||||
Console.WriteLine($"正在准备下载 {url}...");
|
||||
LogMain.Info($"正在准备下载 {url}...");
|
||||
var response = await _httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var totalBytes = response.Content.Headers.ContentLength ?? 0;
|
||||
Console.WriteLine($"文件总大小: {totalBytes} 字节");
|
||||
LogMain.Info($"文件总大小: {totalBytes} 字节");
|
||||
long bytesRead = 0;
|
||||
var lastReportTime = DateTime.MinValue;
|
||||
Console.WriteLine("开始下载...");
|
||||
LogMain.Info("开始下载...");
|
||||
|
||||
using (var stream = await response.Content.ReadAsStreamAsync())
|
||||
using (var fileStream = new FileStream(tempPath, FileMode.Create))
|
||||
@@ -132,6 +150,7 @@ namespace MCSJ.Tools.JreDownload
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"下载失败: {ex.Message}");
|
||||
LogMain.Error($"下载失败: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -150,6 +169,7 @@ namespace MCSJ.Tools.JreDownload
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"解压失败: {ex.Message}");
|
||||
LogMain.Error($"解压失败: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -165,6 +185,23 @@ namespace MCSJ.Tools.JreDownload
|
||||
return Directory.GetFiles(folder, exeName, SearchOption.AllDirectories).FirstOrDefault();
|
||||
}
|
||||
|
||||
private bool CheckJreExists(string version)
|
||||
{
|
||||
var tomlPath = Path.Combine(SetupFolder, "jre.toml");
|
||||
if (!File.Exists(tomlPath))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
var content = File.ReadAllText(tomlPath);
|
||||
return content.Contains($"[jre.{version}]");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateJreToml(string version, string javaExePath, string javawExePath)
|
||||
{
|
||||
if (!Directory.Exists(SetupFolder))
|
||||
|
||||
53
Tools/viewjre/JreViewer.cs
Normal file
53
Tools/viewjre/JreViewer.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using MCSJ.Tools.LogSystem;
|
||||
|
||||
namespace MCSJ.Tools.ViewJre
|
||||
{
|
||||
public class JreViewer
|
||||
{
|
||||
private const string SetupFolder = "setup";
|
||||
private const string JreTomlFile = "jre.toml";
|
||||
|
||||
public void DisplayInstalledJres()
|
||||
{
|
||||
var tomlPath = Path.Combine(SetupFolder, JreTomlFile);
|
||||
|
||||
if (!File.Exists(tomlPath))
|
||||
{
|
||||
Console.WriteLine("没有安装任何JRE");
|
||||
LogMain.Info("没有安装任何JRE");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var content = File.ReadAllText(tomlPath);
|
||||
var versions = content.Split('\n')
|
||||
.Where(line => line.StartsWith("[jre."))
|
||||
.Select(line => line.Split('.')[1].Split(']')[0].Trim()) // 精确提取版本号
|
||||
.ToList();
|
||||
|
||||
if (versions.Count == 0)
|
||||
{
|
||||
Console.WriteLine("没有安装任何JRE");
|
||||
LogMain.Info("没有安装任何JRE");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("已安装的JRE版本:");
|
||||
LogMain.Info("已安装的JRE版本:");
|
||||
foreach (var version in versions)
|
||||
{
|
||||
Console.WriteLine(version); // 直接输出版本号,不带前缀
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"读取JRE列表失败: {ex.Message}");
|
||||
LogMain.Error($"读取JRE列表失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,11 @@ ren win-x64 MCSJ-x64
|
||||
ren win-x86 MCSJ-x86
|
||||
rmdir /s /q MCSJ-x64\publish
|
||||
rmdir /s /q MCSJ-x86\publish
|
||||
7z a -t7z MCSJ-x64.7z MCSJ-x64
|
||||
7z a -t7z MCSJ-x86.7z MCSJ-x86
|
||||
7z a -tzip MCSJ-x64.zip MCSJ-x64
|
||||
7z a -tzip MCSJ-x86.zip MCSJ-x86
|
||||
7z a -tzip MCSJ-x86.zip MCSJ-x86
|
||||
7z a -ttar MCSJ-x64.tar MCSJ-x64
|
||||
7z a -ttar MCSJ-x86.tar MCSJ-x86
|
||||
rmdir /s /q MCSJ-x64
|
||||
rmdir /s /q MCSJ-x86
|
||||
@@ -1,3 +1,5 @@
|
||||
1.21.11-pre2:https://piston-data.mojang.com/v1/objects/7f997b55094aa7754f25173aafef657449f02bec/server.jar
|
||||
1.21.11-pre1:https://piston-data.mojang.com/v1/objects/1087f90b4d73209318d87aa8deecfaae24861004/server.jar
|
||||
25w46a:https://piston-data.mojang.com/v1/objects/e61a72ec98fae895ef3e80b05269ae343c42fc0b/server.jar
|
||||
25w45a:https://piston-data.mojang.com/v1/objects/4c0fe96ca002d7049226a740194c8d7114bd5059/server.jar
|
||||
25w44a:https://piston-data.mojang.com/v1/objects/1ade1ebd6affbfed6dbfb2ce8864cf19efed07ba/server.jar
|
||||
|
||||
Reference in New Issue
Block a user