mirror of
https://github.com/zs-yg/MCSJ.git
synced 2025-12-06 10:50:42 +08:00
添加管理服务器功能
This commit is contained in:
63
Tools/server_management/EulaAgreer.cs
Normal file
63
Tools/server_management/EulaAgreer.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MCSJ.Tools.ServerManagement
|
||||
{
|
||||
public static class EulaAgreer
|
||||
{
|
||||
public static void AgreeEula()
|
||||
{
|
||||
// 获取服务器列表
|
||||
var servers = ServerManager.GetServerProfiles();
|
||||
if (servers.Count == 0)
|
||||
{
|
||||
Console.WriteLine("没有可用的服务器存档");
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示服务器列表供选择
|
||||
Console.WriteLine("可用的服务器存档:");
|
||||
for (int i = 0; i < servers.Count; i++)
|
||||
{
|
||||
Console.WriteLine($"{i + 1}. {servers[i]}");
|
||||
}
|
||||
|
||||
Console.Write("请选择服务器(输入编号): ");
|
||||
if (!int.TryParse(Console.ReadLine(), out int serverIndex) || serverIndex < 1 || serverIndex > servers.Count)
|
||||
{
|
||||
Console.WriteLine("无效选择");
|
||||
return;
|
||||
}
|
||||
|
||||
string selectedServer = servers[serverIndex - 1];
|
||||
string eulaPath = Path.Combine("profiles", selectedServer, "eula.txt");
|
||||
|
||||
// 检查eula文件是否存在
|
||||
if (!File.Exists(eulaPath))
|
||||
{
|
||||
Console.WriteLine("没有找到eula.txt文件");
|
||||
return;
|
||||
}
|
||||
|
||||
// 读取并修改eula文件
|
||||
string eulaContent = File.ReadAllText(eulaPath);
|
||||
if (eulaContent.Contains("eula=true"))
|
||||
{
|
||||
Console.WriteLine("EULA已经同意,无需修改");
|
||||
return;
|
||||
}
|
||||
|
||||
if (eulaContent.Contains("eula=false"))
|
||||
{
|
||||
eulaContent = eulaContent.Replace("eula=false", "eula=true");
|
||||
File.WriteAllText(eulaPath, eulaContent);
|
||||
Console.WriteLine("已同意EULA");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("无效的eula.txt格式");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
117
Tools/server_management/ScriptGenerator.cs
Normal file
117
Tools/server_management/ScriptGenerator.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MCSJ.Tools.ServerManagement
|
||||
{
|
||||
public static class ScriptGenerator
|
||||
{
|
||||
public static void GenerateScript()
|
||||
{
|
||||
// 获取服务器列表
|
||||
var servers = ServerManager.GetServerProfiles();
|
||||
if (servers.Count == 0)
|
||||
{
|
||||
Console.WriteLine("没有可用的服务器存档");
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示服务器列表供选择
|
||||
Console.WriteLine("可用的服务器存档:");
|
||||
for (int i = 0; i < servers.Count; i++)
|
||||
{
|
||||
Console.WriteLine($"{i + 1}. {servers[i]}");
|
||||
}
|
||||
|
||||
Console.Write("请选择服务器(输入编号): ");
|
||||
if (!int.TryParse(Console.ReadLine(), out int serverIndex) || serverIndex < 1 || serverIndex > servers.Count)
|
||||
{
|
||||
Console.WriteLine("无效选择");
|
||||
return;
|
||||
}
|
||||
|
||||
string selectedServer = servers[serverIndex - 1];
|
||||
string serverPath = Path.Combine("profiles", selectedServer);
|
||||
|
||||
// 检查JRE配置
|
||||
string jreConfigPath = "setup/jre.toml";
|
||||
if (!File.Exists(jreConfigPath) || new FileInfo(jreConfigPath).Length == 0)
|
||||
{
|
||||
Console.WriteLine("没有下载的JRE,请先下载JRE");
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析JRE配置
|
||||
var jreVersions = new Dictionary<string, Dictionary<string, string>>();
|
||||
string currentVersion = null;
|
||||
|
||||
foreach (var line in File.ReadAllLines(jreConfigPath))
|
||||
{
|
||||
if (line.StartsWith("[") && line.EndsWith("]"))
|
||||
{
|
||||
currentVersion = line.Trim('[', ']');
|
||||
jreVersions[currentVersion] = new Dictionary<string, string>();
|
||||
}
|
||||
else if (line.Contains("=") && currentVersion != null)
|
||||
{
|
||||
var parts = line.Split('=');
|
||||
string key = parts[0].Trim();
|
||||
string value = parts[1].Trim().Trim('\'');
|
||||
jreVersions[currentVersion][key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (jreVersions.Count == 0)
|
||||
{
|
||||
Console.WriteLine("没有可用的JRE配置");
|
||||
return;
|
||||
}
|
||||
|
||||
// 选择JRE版本
|
||||
Console.WriteLine("可用的JRE版本:");
|
||||
int versionIndex = 1;
|
||||
var versionList = new List<string>(jreVersions.Keys);
|
||||
foreach (var version in versionList)
|
||||
{
|
||||
Console.WriteLine($"{versionIndex}. {version}");
|
||||
versionIndex++;
|
||||
}
|
||||
|
||||
Console.Write("请选择JRE版本(输入编号): ");
|
||||
if (!int.TryParse(Console.ReadLine(), out int selectedVersionIndex) ||
|
||||
selectedVersionIndex < 1 || selectedVersionIndex > versionList.Count)
|
||||
{
|
||||
Console.WriteLine("无效选择");
|
||||
return;
|
||||
}
|
||||
|
||||
string selectedVersion = versionList[selectedVersionIndex - 1];
|
||||
var versionConfig = jreVersions[selectedVersion];
|
||||
|
||||
if (!versionConfig.ContainsKey("java_path") || !versionConfig.ContainsKey("javaw_path"))
|
||||
{
|
||||
Console.WriteLine("JRE配置不完整");
|
||||
return;
|
||||
}
|
||||
|
||||
// 选择java执行方式
|
||||
Console.WriteLine("选择Java执行方式:");
|
||||
Console.WriteLine("1. java.exe (控制台模式)");
|
||||
Console.WriteLine("2. javaw.exe (无控制台模式)");
|
||||
Console.Write("请选择: ");
|
||||
string javaPath = Console.ReadLine() == "1"
|
||||
? Path.GetFullPath(versionConfig["java_path"])
|
||||
: Path.GetFullPath(versionConfig["javaw_path"]);
|
||||
|
||||
// 生成启动脚本(统一使用反斜杠)
|
||||
string scriptContent = $"@echo off\r\n" +
|
||||
$"cd /D \"{Path.GetFullPath(serverPath).Replace('/', '\\')}\"\r\n" +
|
||||
$"\"{javaPath.Replace('/', '\\')}\" -jar server.jar\r\n" +
|
||||
"pause";
|
||||
|
||||
string scriptPath = Path.Combine(serverPath, "start.bat");
|
||||
File.WriteAllText(scriptPath, scriptContent);
|
||||
Console.WriteLine($"已生成启动脚本: {scriptPath}");
|
||||
}
|
||||
}
|
||||
}
|
||||
59
Tools/server_management/ServerManager.cs
Normal file
59
Tools/server_management/ServerManager.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MCSJ.Tools.ServerManagement
|
||||
{
|
||||
public class ServerManager
|
||||
{
|
||||
private const int SERVER_MANAGEMENT_OPTION = 3;
|
||||
|
||||
public static void ShowMenu()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Console.WriteLine("\n=== 服务器管理 ===");
|
||||
Console.WriteLine("1. 生成启动脚本");
|
||||
Console.WriteLine("2. 启动服务器");
|
||||
Console.WriteLine("3. 同意eula.txt");
|
||||
Console.WriteLine("4. 返回主菜单");
|
||||
Console.Write("请选择: ");
|
||||
|
||||
var choice = Console.ReadLine();
|
||||
switch (choice)
|
||||
{
|
||||
case "1":
|
||||
ScriptGenerator.GenerateScript();
|
||||
break;
|
||||
case "2":
|
||||
ServerStarter.StartServer();
|
||||
break;
|
||||
case "3":
|
||||
EulaAgreer.AgreeEula();
|
||||
break;
|
||||
case "4":
|
||||
return;
|
||||
default:
|
||||
Console.WriteLine("无效选项");
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine("\n按任意键继续...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<string> GetServerProfiles()
|
||||
{
|
||||
var profiles = new List<string>();
|
||||
if (Directory.Exists("profiles"))
|
||||
{
|
||||
foreach (var dir in Directory.GetDirectories("profiles"))
|
||||
{
|
||||
profiles.Add(Path.GetFileName(dir));
|
||||
}
|
||||
}
|
||||
return profiles;
|
||||
}
|
||||
}
|
||||
}
|
||||
77
Tools/server_management/ServerStarter.cs
Normal file
77
Tools/server_management/ServerStarter.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MCSJ.Tools.ServerManagement
|
||||
{
|
||||
public static class ServerStarter
|
||||
{
|
||||
public static void StartServer()
|
||||
{
|
||||
// 获取服务器列表
|
||||
var servers = ServerManager.GetServerProfiles();
|
||||
if (servers.Count == 0)
|
||||
{
|
||||
Console.WriteLine("没有可用的服务器存档");
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示服务器列表供选择
|
||||
Console.WriteLine("可用的服务器存档:");
|
||||
for (int i = 0; i < servers.Count; i++)
|
||||
{
|
||||
Console.WriteLine($"{i + 1}. {servers[i]}");
|
||||
}
|
||||
|
||||
Console.Write("请选择服务器(输入编号): ");
|
||||
if (!int.TryParse(Console.ReadLine(), out int serverIndex) || serverIndex < 1 || serverIndex > servers.Count)
|
||||
{
|
||||
Console.WriteLine("无效选择");
|
||||
return;
|
||||
}
|
||||
|
||||
string selectedServer = servers[serverIndex - 1];
|
||||
string serverPath = Path.Combine("profiles", selectedServer);
|
||||
string batPath = Path.Combine(serverPath, "start.bat");
|
||||
|
||||
// 检查启动脚本是否存在
|
||||
if (!File.Exists(batPath))
|
||||
{
|
||||
Console.WriteLine("没有找到启动脚本,请先生成脚本");
|
||||
return;
|
||||
}
|
||||
|
||||
// 启动服务器
|
||||
try
|
||||
{
|
||||
string fullBatPath = Path.GetFullPath(batPath);
|
||||
string fullServerPath = Path.GetFullPath(serverPath);
|
||||
|
||||
if (!File.Exists(fullBatPath))
|
||||
{
|
||||
Console.WriteLine($"启动脚本不存在: {fullBatPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = fullBatPath,
|
||||
WorkingDirectory = fullServerPath,
|
||||
UseShellExecute = true,
|
||||
CreateNoWindow = false
|
||||
};
|
||||
|
||||
Console.WriteLine($"启动路径: {fullBatPath}");
|
||||
Console.WriteLine($"工作目录: {fullServerPath}");
|
||||
|
||||
Process.Start(startInfo);
|
||||
Console.WriteLine($"已启动服务器: {selectedServer}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"启动服务器失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user