Skip to content

Commit ca1cc9b

Browse files
authored
Merge pull request #83 from AMagicPear/feature/game
optimize: 优雅!优雅!还是tmd优雅!
2 parents 3a6955c + 337b962 commit ca1cc9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1367
-1376
lines changed

PCL.Neo.Core/FileExtension.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using PCL.Neo.Core.Utils;
2+
using System.Diagnostics;
3+
using System.Runtime.InteropServices;
4+
using System.Runtime.Versioning;
5+
6+
namespace PCL.Neo.Core;
7+
8+
/// <summary>
9+
/// 一些文件操作和下载请求之类的
10+
/// </summary>
11+
public static class FileExtension
12+
{
13+
/// <summary>
14+
/// 校验文件流与SHA-1是否匹配
15+
/// </summary>
16+
/// <param name="fileStream">文件流</param>
17+
/// <param name="sha1">SHA-1</param>
18+
/// <returns>是否匹配</returns>
19+
public static async Task<bool> CheckSha1(this FileStream fileStream, string sha1)
20+
{
21+
using var sha1Provider = System.Security.Cryptography.SHA1.Create();
22+
fileStream.Position = 0; // 重置文件流位置
23+
var computedHash = await sha1Provider.ComputeHashAsync(fileStream);
24+
var computedHashString = Convert.ToHexStringLower(computedHash);
25+
return string.Equals(computedHashString, sha1, StringComparison.OrdinalIgnoreCase);
26+
}
27+
28+
/// <summary>
29+
/// 在 Unix 系统中给予可执行文件运行权限
30+
/// </summary>
31+
/// <param name="path">文件路径</param>
32+
[SupportedOSPlatform(nameof(OSPlatform.OSX))]
33+
[SupportedOSPlatform(nameof(OSPlatform.Linux))]
34+
public static void SetFileExecutableUnix(this string path)
35+
{
36+
if (SystemUtils.Os is SystemUtils.RunningOs.Windows) return;
37+
try
38+
{
39+
var currentMode = File.GetUnixFileMode(path);
40+
var newMode = currentMode | UnixFileMode.UserExecute | UnixFileMode.GroupExecute |
41+
UnixFileMode.OtherExecute;
42+
File.SetUnixFileMode(path, newMode);
43+
}
44+
catch (Exception e)
45+
{
46+
Console.WriteLine($"无法设置可执行权限:{e.Message}");
47+
throw;
48+
}
49+
}
50+
51+
/// <summary>
52+
/// 从某个地方抄来的很像 C 语言风格的解压 LZMA 压缩算法的函数
53+
/// </summary>
54+
/// <param name="inStream">被压缩的文件流</param>
55+
/// <param name="outputFile">输出文件路径</param>
56+
/// <returns>解压后的文件流</returns>
57+
public static FileStream? DecompressLZMA(this FileStream inStream, string outputFile)
58+
{
59+
inStream.Position = 0;
60+
var outStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite);
61+
byte[] decodeProperties = new byte[5];
62+
int n = inStream.Read(decodeProperties, 0, 5);
63+
Debug.Assert(n == 5);
64+
SevenZip.Compression.LZMA.Decoder decoder = new();
65+
decoder.SetDecoderProperties(decodeProperties);
66+
long outSize = 0;
67+
for (int i = 0; i < 8; i++)
68+
{
69+
int v = inStream.ReadByte();
70+
if (v < 0)
71+
{
72+
Console.WriteLine("read outSize error.");
73+
return null;
74+
}
75+
76+
outSize |= (long)(byte)v << (8 * i);
77+
}
78+
79+
long compressedSize = inStream.Length - inStream.Position;
80+
decoder.Code(inStream, outStream, compressedSize, outSize, null);
81+
inStream.Close();
82+
return outStream;
83+
}
84+
}

PCL.Neo.Core/Helpers/FileHelper.cs renamed to PCL.Neo.Core/Models/DownloadService.cs

Lines changed: 11 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System.Diagnostics;
2-
using System.Runtime.InteropServices;
3-
using System.Runtime.Versioning;
4-
5-
namespace PCL.Neo.Core.Helpers;
1+
namespace PCL.Neo.Core.Models;
62

73
/// <summary>
8-
/// 一些文件操作和下载请求之类的
4+
/// 下载任务管理器
5+
/// 之所以不static是因为后续可能需要接入进度显示和下载统计
96
/// </summary>
10-
public static class FileHelper
7+
public class DownloadService
118
{
12-
public static readonly HttpClient HttpClient = new();
9+
/// <summary>
10+
/// 一个static的HttpClient,以便在任何地方调用
11+
/// </summary>
12+
public static HttpClient HttpClient { get; } = new();
1313

1414
/// <summary>
1515
/// 从某个 URL 下载并保存文件
@@ -21,7 +21,7 @@ public static class FileHelper
2121
/// <param name="maxRetries">最大重试次数</param>
2222
/// <param name="cancellationToken">用于取消</param>
2323
/// <returns>向外传递的文件流</returns>
24-
public static async Task<FileStream?> DownloadFileAsync(
24+
public async Task<FileStream?> DownloadFileAsync(
2525
Uri uri, string localFilePath, string? sha1 = null, bool passStreamDown = false, int maxRetries = 3,
2626
CancellationToken cancellationToken = default)
2727
{
@@ -40,6 +40,7 @@ public static class FileHelper
4040
try
4141
{
4242
await response.Content.CopyToAsync(fileStream, cancellationToken);
43+
response.EnsureSuccessStatusCode();
4344
if (!string.IsNullOrEmpty(sha1))
4445
{
4546
fileStream.Position = 0;
@@ -73,82 +74,10 @@ public static class FileHelper
7374
}
7475
}
7576

76-
/// <summary>
77-
/// 校验文件流与SHA-1是否匹配
78-
/// </summary>
79-
/// <param name="fileStream">文件流</param>
80-
/// <param name="sha1">SHA-1</param>
81-
/// <returns>是否匹配</returns>
82-
private static async Task<bool> CheckSha1(this FileStream fileStream, string sha1)
83-
{
84-
using var sha1Provider = System.Security.Cryptography.SHA1.Create();
85-
fileStream.Position = 0; // 重置文件流位置
86-
var computedHash = await sha1Provider.ComputeHashAsync(fileStream);
87-
var computedHashString = Convert.ToHexStringLower(computedHash);
88-
return string.Equals(computedHashString, sha1, StringComparison.OrdinalIgnoreCase);
89-
}
90-
91-
/// <summary>
92-
/// 在 Unix 系统中给予可执行文件运行权限
93-
/// </summary>
94-
/// <param name="path">文件路径</param>
95-
[SupportedOSPlatform(nameof(OSPlatform.OSX))]
96-
[SupportedOSPlatform(nameof(OSPlatform.Linux))]
97-
public static void SetFileExecutableUnix(this string path)
98-
{
99-
if (Const.Os is Const.RunningOs.Windows) return;
100-
try
101-
{
102-
var currentMode = File.GetUnixFileMode(path);
103-
var newMode = currentMode | UnixFileMode.UserExecute | UnixFileMode.GroupExecute |
104-
UnixFileMode.OtherExecute;
105-
File.SetUnixFileMode(path, newMode);
106-
}
107-
catch (Exception e)
108-
{
109-
Console.WriteLine($"无法设置可执行权限:{e.Message}");
110-
throw;
111-
}
112-
}
113-
114-
/// <summary>
115-
/// 从某个地方抄来的很像 C 语言风格的解压 LZMA 压缩算法的函数
116-
/// </summary>
117-
/// <param name="inStream">被压缩的文件流</param>
118-
/// <param name="outputFile">输出文件路径</param>
119-
/// <returns>解压后的文件流</returns>
120-
private static FileStream? DecompressLZMA(this FileStream inStream, string outputFile)
121-
{
122-
inStream.Position = 0;
123-
var outStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite);
124-
byte[] decodeProperties = new byte[5];
125-
int n = inStream.Read(decodeProperties, 0, 5);
126-
Debug.Assert(n == 5);
127-
SevenZip.Compression.LZMA.Decoder decoder = new();
128-
decoder.SetDecoderProperties(decodeProperties);
129-
long outSize = 0;
130-
for (int i = 0; i < 8; i++)
131-
{
132-
int v = inStream.ReadByte();
133-
if (v < 0)
134-
{
135-
Console.WriteLine("read outSize error.");
136-
return null;
137-
}
138-
139-
outSize |= (long)(byte)v << (8 * i);
140-
}
141-
142-
long compressedSize = inStream.Length - inStream.Position;
143-
decoder.Code(inStream, outStream, compressedSize, outSize, null);
144-
inStream.Close();
145-
return outStream;
146-
}
147-
14877
/// <summary>
14978
/// 整合函数:下载并解压,然后删去原压缩文件
15079
/// </summary>
151-
public static async Task DownloadAndDeCompressFileAsync(Uri uri, string localFilePath, string sha1Raw,
80+
public async Task DownloadAndDeCompressFileAsync(Uri uri, string localFilePath, string sha1Raw,
15281
string sha1Lzma, CancellationToken cancellationToken = default)
15382
{
15483
var stream = await DownloadFileAsync(uri, localFilePath + ".lzma", sha1Lzma, true,

0 commit comments

Comments
 (0)