Skip to content

Commit 5b07f75

Browse files
committed
feat(本地化): 为Hash模块添加本地化支持
- 添加Localization文件夹及Keys.cs文件定义本地化资源键 - 修改CrcHelper.cs使用LocalizationService获取错误消息 - 添加对GameFrameX.Foundation.Localization的项目引用
1 parent f3dff3f commit 5b07f75

3 files changed

Lines changed: 188 additions & 3 deletions

File tree

GameFrameX.Foundation.Hash/CrcHelper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using GameFrameX.Foundation.Localization.Core;
2+
using GameFrameX.Foundation.Hash.Localization;
3+
14
namespace GameFrameX.Foundation.Hash;
25

36
/// <summary>
@@ -90,7 +93,7 @@ public static int GetCrc32(byte[] bytes, int offset, int length)
9093

9194
if (offset < 0 || length < 0 || offset + length > bytes.Length)
9295
{
93-
throw new ArgumentException("Offset or length is invalid.", nameof(offset));
96+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Validation.InvalidDataLength), nameof(offset));
9497
}
9598

9699
SAlgorithm.HashCore(bytes, offset, length);
@@ -168,7 +171,7 @@ public static void GetCrc32Bytes(int crc32, byte[] bytes, int offset)
168171

169172
if (offset < 0 || offset + 4 > bytes.Length)
170173
{
171-
throw new ArgumentException("Offset or length is invalid.", nameof(offset));
174+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Validation.InvalidDataLength), nameof(offset));
172175
}
173176

174177
bytes[offset] = (byte)((crc32 >> 24) & 0xff);
@@ -201,7 +204,7 @@ internal static int GetCrc32(Stream stream, byte[] code, int length)
201204
var codeLength = code.Length;
202205
if (codeLength <= 0)
203206
{
204-
throw new ArgumentException("Code length is invalid.", nameof(codeLength));
207+
throw new ArgumentException(LocalizationService.GetString(LocalizationKeys.Validation.InvalidDataLength), nameof(codeLength));
205208
}
206209

207210
var bytesLength = (int)stream.Length;

GameFrameX.Foundation.Hash/GameFrameX.Foundation.Hash.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
3434
<OutputPath>..\bin\app</OutputPath>
3535
</PropertyGroup>
36+
<ItemGroup>
37+
<Folder Include="Localization\Messages\" />
38+
</ItemGroup>
39+
<ItemGroup>
40+
<ProjectReference Include="..\GameFrameX.Foundation.Localization\GameFrameX.Foundation.Localization.csproj" />
41+
</ItemGroup>
3642
<ItemGroup>
3743
<None Include="..\logo.png">
3844
<Pack>True</Pack>
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// GameFrameX 组织下的以及组织衍生的项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2+
//
3+
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE 文件。
4+
//
5+
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
6+
7+
using System;
8+
9+
namespace GameFrameX.Foundation.Hash.Localization;
10+
11+
/// <summary>
12+
/// Hash 模块本地化资源键常量定义
13+
/// </summary>
14+
/// <remarks>
15+
/// 这个类定义了 Hash 模块中所有可本地化字符串的键常量。
16+
/// 使用常量可以避免字符串硬编码,提高代码的可维护性和类型安全性。
17+
/// </remarks>
18+
/// <example>
19+
/// <code>
20+
/// // 在代码中使用本地化键常量
21+
/// throw new ArgumentException(
22+
/// nameof(data),
23+
/// LocalizationService.GetString(LocalizationKeys.Exceptions.DataCannotBeNullOrEmpty));
24+
/// </code>
25+
/// </example>
26+
public static class LocalizationKeys
27+
{
28+
/// <summary>
29+
/// 异常消息资源键
30+
/// </summary>
31+
public static class Exceptions
32+
{
33+
/// <summary>
34+
/// 数据不能为空的错误消息
35+
/// </summary>
36+
/// <remarks>
37+
/// 键名: Hash.Exceptions.DataCannotBeNullOrEmpty
38+
/// 用途: 当需要非空数据但传入了空数据时使用
39+
/// </remarks>
40+
public const string DataCannotBeNullOrEmpty = "Hash.Exceptions.DataCannotBeNullOrEmpty";
41+
42+
/// <summary>
43+
/// 哈希计算失败的错误消息
44+
/// </summary>
45+
/// <remarks>
46+
/// 键名: Hash.Exceptions.HashComputationFailed
47+
/// 用途: 当哈希算法计算过程中发生错误时使用
48+
/// 参数: {0} - 算法名称, {1} - 错误信息
49+
/// </remarks>
50+
public const string HashComputationFailed = "Hash.Exceptions.HashComputationFailed";
51+
52+
/// <summary>
53+
/// 不支持的哈希算法的错误消息
54+
/// </summary>
55+
/// <remarks>
56+
/// 键名: Hash.Exceptions.UnsupportedHashAlgorithm
57+
/// 用途: 当使用不支持的哈希算法时使用
58+
/// 参数: {0} - 算法名称
59+
/// </remarks>
60+
public const string UnsupportedHashAlgorithm = "Hash.Exceptions.UnsupportedHashAlgorithm";
61+
}
62+
63+
/// <summary>
64+
/// 日志消息资源键
65+
/// </summary>
66+
public static class Logs
67+
{
68+
/// <summary>
69+
/// 哈希计算完成的日志消息
70+
/// </summary>
71+
/// <remarks>
72+
/// 键名: Hash.Logs.HashComputationCompleted
73+
/// 用途: 记录哈希计算操作完成
74+
/// 参数: {0} - 算法名称, {1} - 数据长度, {2} - 哈希值
75+
/// </remarks>
76+
public const string HashComputationCompleted = "Hash.Logs.HashComputationCompleted";
77+
78+
/// <summary>
79+
/// CRC计算完成的日志消息
80+
/// </summary>
81+
/// <remarks>
82+
/// 键名: Hash.Logs.CrcComputationCompleted
83+
/// 用途: 记录CRC计算操作完成
84+
/// 参数: {0} - CRC类型, {1} - 数据长度, {2} - CRC值
85+
/// </remarks>
86+
public const string CrcComputationCompleted = "Hash.Logs.CrcComputationCompleted";
87+
}
88+
89+
/// <summary>
90+
/// 状态消息资源键
91+
/// </summary>
92+
public static class Status
93+
{
94+
/// <summary>
95+
/// 哈希算法初始化成功的状态消息
96+
/// </summary>
97+
/// <remarks>
98+
/// 键名: Hash.Status.HashAlgorithmInitialized
99+
/// 用途: 表示哈希算法初始化完成
100+
/// 参数: {0} - 算法名称
101+
/// </remarks>
102+
public const string HashAlgorithmInitialized = "Hash.Status.HashAlgorithmInitialized";
103+
104+
/// <summary>
105+
/// CRC校验成功的状态消息
106+
/// </summary>
107+
/// <remarks>
108+
/// 键名: Hash.Status.CrcValidationSuccessful
109+
/// 用途: 表示CRC校验通过
110+
/// 参数: {0} - CRC类型, {1} - 期望值, {2} - 实际值
111+
/// </remarks>
112+
public const string CrcValidationSuccessful = "Hash.Status.CrcValidationSuccessful";
113+
}
114+
115+
/// <summary>
116+
/// 性能消息资源键
117+
/// </summary>
118+
public static class Performance
119+
{
120+
/// <summary>
121+
/// 哈希计算性能统计
122+
/// </summary>
123+
/// <remarks>
124+
/// 键名: Hash.Performance.HashComputationTime
125+
/// 用途: 记录哈希计算的性能统计
126+
/// 参数: {0} - 算法名称, {1} - 数据长度, {2} - 执行时间(毫秒)
127+
/// </remarks>
128+
public const string HashComputationTime = "Hash.Performance.HashComputationTime";
129+
130+
/// <summary>
131+
/// 哈希吞吐量统计
132+
/// </summary>
133+
/// <remarks>
134+
/// 键名: Hash.Performance.HashThroughput
135+
/// 用途: 记录哈希计算的吞吐量
136+
/// 参数: {0} - 算法名称, {1} - 数据量(MB), {2} - 处理时间(秒)
137+
/// </remarks>
138+
public const string HashThroughput = "Hash.Performance.HashThroughput";
139+
}
140+
141+
/// <summary>
142+
/// 验证消息资源键
143+
/// </summary>
144+
public static class Validation
145+
{
146+
/// <summary>
147+
/// 数据长度验证失败的错误消息
148+
/// </summary>
149+
/// <remarks>
150+
/// 键名: Hash.Validation.InvalidDataLength
151+
/// 用途: 当数据长度不符合要求时使用
152+
/// 参数: {0} - 实际长度, {1} - 期望长度范围
153+
/// </remarks>
154+
public const string InvalidDataLength = "Hash.Validation.InvalidDataLength";
155+
156+
/// <summary>
157+
/// 哈希值验证失败的消息
158+
/// </summary>
159+
/// <remarks>
160+
/// 键名: Hash.Validation.HashValueMismatch
161+
/// 用途: 当计算出的哈希值与期望值不匹配时使用
162+
/// 参数: {0} - 期望值, {1} - 实际值
163+
/// </remarks>
164+
public const string HashValueMismatch = "Hash.Validation.HashValueMismatch";
165+
166+
/// <summary>
167+
/// CRC校验失败的消息
168+
/// </summary>
169+
/// <remarks>
170+
/// 键名: Hash.Validation.CrcCheckFailed
171+
/// 用途: 当CRC校验失败时使用
172+
/// 参数: {0} - CRC类型, {1} - 期望值, {2} - 实际值
173+
/// </remarks>
174+
public const string CrcCheckFailed = "Hash.Validation.CrcCheckFailed";
175+
}
176+
}

0 commit comments

Comments
 (0)