实现下载列表中版本的功能

This commit is contained in:
zsyg
2025-10-17 21:46:26 +08:00
committed by GitHub
parent 05e7a4fe6e
commit c72ecb8847
5 changed files with 217 additions and 0 deletions

20
MCSJ.csproj Normal file
View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<Content Include="resources\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

45
Program.cs Normal file
View File

@@ -0,0 +1,45 @@
using System;
using System.Threading.Tasks;
using MCSJ.Tools;
namespace MCSJ
{
class Program
{
static async Task Main(string[] args)
{
var versionManager = new VersionManager();
var downloadService = new DownloadService(versionManager);
while (true)
{
Console.WriteLine("MC服务器下载工具");
Console.WriteLine("1. 显示所有版本");
Console.WriteLine("2. 下载指定版本");
Console.WriteLine("3. 退出");
Console.Write("请选择操作: ");
var input = Console.ReadLine();
switch (input)
{
case "1":
versionManager.DisplayAllVersions();
break;
case "2":
Console.Write("请输入要下载的版本名称: ");
var version = Console.ReadLine();
await downloadService.DownloadVersion(version);
break;
case "3":
return;
default:
Console.WriteLine("无效输入,请重新选择");
break;
}
Console.WriteLine();
}
}
}
}

72
Tools/DownloadService.cs Normal file
View File

@@ -0,0 +1,72 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace MCSJ.Tools
{
public class DownloadService
{
private readonly VersionManager _versionManager;
private readonly HttpClient _httpClient;
public DownloadService(VersionManager versionManager)
{
_versionManager = versionManager;
_httpClient = new HttpClient();
}
public async Task DownloadVersion(string version)
{
var url = _versionManager.GetDownloadUrl(version);
if (url == null)
{
Console.WriteLine($"版本 {version} 不存在");
return;
}
try
{
Console.WriteLine($"开始下载 {version}...");
var response = await _httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
var totalBytes = response.Content.Headers.ContentLength ?? -1;
var downloadedBytes = 0L;
var buffer = new byte[8192];
var isMoreToRead = true;
using (var stream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream($"{version}.jar", FileMode.Create, FileAccess.Write))
{
while (isMoreToRead)
{
var read = await stream.ReadAsync(buffer, 0, buffer.Length);
if (read == 0)
{
isMoreToRead = false;
}
else
{
await fileStream.WriteAsync(buffer, 0, read);
downloadedBytes += read;
if (totalBytes > 0)
{
var progress = (double)downloadedBytes / totalBytes * 100;
Console.Write($"\r下载进度: {progress:F2}%");
}
}
}
}
Console.WriteLine($"\n{version} 下载完成!");
}
catch (Exception ex)
{
Console.WriteLine($"下载失败: {ex.Message}");
}
}
}
}

75
Tools/VersionManager.cs Normal file
View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace MCSJ.Tools
{
public class VersionManager
{
private readonly Dictionary<string, string> _versions = new();
public VersionManager()
{
LoadVersions();
}
private void LoadVersions()
{
try
{
var filePath = Path.Combine("resources", "serverlist.txt");
Console.WriteLine($"尝试从路径加载版本列表: {Path.GetFullPath(filePath)}");
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"服务器列表文件不存在: {filePath}");
}
var content = File.ReadAllText(filePath);
var entries = content.Split(new[] {' ', '\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var entry in entries)
{
var colonIndex = entry.IndexOf(':');
if (colonIndex > 0 && colonIndex < entry.Length - 1)
{
var version = entry.Substring(0, colonIndex);
var url = entry.Substring(colonIndex + 1);
_versions[version] = url;
}
else
{
Console.WriteLine($"忽略无效条目: {entry} (缺少冒号分隔或格式不正确)");
}
}
if (_versions.Count == 0)
{
throw new Exception("没有找到有效的版本条目");
}
Console.WriteLine($"成功加载 {_versions.Count} 个版本");
}
catch (Exception ex)
{
Console.WriteLine($"加载版本列表失败: {ex.Message}");
Console.WriteLine($"当前工作目录: {Directory.GetCurrentDirectory()}");
Console.WriteLine("请确保serverlist.txt格式为: 版本名:下载URL (每行一个或空格分隔)");
}
}
public void DisplayAllVersions()
{
Console.WriteLine("可用版本列表:");
foreach (var version in _versions.Keys)
{
Console.WriteLine(version);
}
}
public string GetDownloadUrl(string version)
{
return _versions.TryGetValue(version, out var url) ? url : null;
}
}
}

5
resources/serverlist.txt Normal file
View File

@@ -0,0 +1,5 @@
vanilla1.21.10:https://file.mslmc.cn/mirrors/vanilla/95495a7f485eedd84ce928cef5e223b757d2f764/server.jar
vanilla1.21.9:https://file.mslmc.cn/mirrors/vanilla/11e54c2081420a4d49db3007e66c80a22579ff2a/server.jar
vanilla1.21.8:https://file.mslmc.cn/mirrors/vanilla/6bce4ef400e4efaa63a13d5e6f6b500be969ef81/server.jar
vanilla1.21.7:https://file.mslmc.cn/mirrors/vanilla/05e4b48fbc01f0385adb74bcff9751d34552486c/server.jar
vanilla1.21.6:https://file.mslmc.cn/mirrors/vanilla/6e64dcabba3c01a7271b4fa6bd898483b794c59b/server.jar