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

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

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;
}
}
}