Skip to content

Commit a7cc885

Browse files
authored
Merge pull request #1 from Crequency/dev=main
[Pull Request] 一些小优化, 新的版本规则
2 parents 953e259 + 5bc101d commit a7cc885

9 files changed

Lines changed: 132 additions & 97 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Ignore for Rider files
2+
.idea/
3+
14
## Ignore Visual Studio temporary files, build results, and
25
## files generated by popular Visual Studio add-ons.
36
##

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ add_subdirectory ("Math")
1212
add_subdirectory ("Encode")
1313

1414
add_subdirectory ("Test")
15+
16+
add_definitions ("cpp.hint")

Common.Algorithm.Interop/Common.Algorithm.Interop.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<AssemblyVersion>$(Version)</AssemblyVersion>
2222
<FileVersion>$(Version)</FileVersion>
23-
<Version>1.1.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2005-07-16"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))</Version>
23+
<Version>1.2.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2005-06-06"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))</Version>
2424

2525
</PropertyGroup>
2626

Common.Algorithm.Interop/Environment.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,47 @@
22

33
#pragma warning disable SYSLIB0014 // 类型或成员已过时
44

5-
namespace Algorithm.Interop
5+
namespace Common.Algorithm.Interop
66
{
7-
public class Environment
7+
public static class Environment
88
{
99

1010
/// <summary>
1111
/// 核心文件路径
1212
/// </summary>
13-
public const string dll_path = "./Core/";
13+
public const string DllPath = "./Core/";
1414

1515
/// <summary>
1616
/// 云端 DLL 存储路径
1717
/// </summary>
18-
public const string cloudUrl = "https://source.catrol.cn/lib/Algorithm/Core/";
18+
private static string _cloudUrl = "https://source.catrol.cn/lib/Algorithm/Core/";
1919

2020
/// <summary>
2121
/// 库版本
2222
/// </summary>
23-
public const string version = "v1.0";
23+
private static string _version = "v1.0";
2424

2525
/// <summary>
2626
/// 核心文件文件名
2727
/// </summary>
28-
private readonly static List<string> CoreFiles = new()
28+
private static readonly List<string> CoreFiles = new()
2929
{
3030
"Math.dll",
3131
"Hash.dll"
3232
};
3333

34+
/// <summary>
35+
/// 更新云端存储链接
36+
/// </summary>
37+
/// <param name="url"></param>
38+
public static void UpdateCloudStorageUrl(string url) => _cloudUrl = url;
39+
40+
/// <summary>
41+
/// 更新云端存储版本
42+
/// </summary>
43+
/// <param name="ver"></param>
44+
public static void UpdateCloudStorageVersion(string ver) => _version = ver;
45+
3446
/// <summary>
3547
/// 检查环境是否就绪, 核心文件是否存在
3648
/// </summary>
@@ -39,7 +51,7 @@ public static bool CheckEnvironment()
3951
{
4052
foreach (string fn in CoreFiles)
4153
{
42-
if (!File.Exists(Path.GetFullPath($"{dll_path}{fn}")))
54+
if (!File.Exists(Path.GetFullPath($"{DllPath}{fn}")))
4355
return false;
4456
}
4557
return true;
@@ -51,19 +63,19 @@ public static bool CheckEnvironment()
5163
/// </summary>
5264
public static void InstallEnvironment(InstallMethod im = InstallMethod.WebClient)
5365
{
54-
if(!Directory.Exists(dll_path))
55-
Directory.CreateDirectory(dll_path);
66+
if (!Directory.Exists(DllPath))
67+
Directory.CreateDirectory(DllPath);
5668
switch (im)
5769
{
5870
case InstallMethod.WebClient:
5971
WebClient wc = new();
6072
foreach (string fn in CoreFiles)
6173
{
62-
if (!File.Exists($"{dll_path}{fn}"))
74+
if (!File.Exists($"{DllPath}{fn}"))
6375
{
6476
wc.DownloadFile(
65-
new Uri($"{cloudUrl}{version}/{fn}", UriKind.Absolute),
66-
Path.GetFullPath($"{dll_path}{fn}"));
77+
new Uri($"{_cloudUrl}{_version}/{fn}", UriKind.Absolute),
78+
Path.GetFullPath($"{DllPath}{fn}"));
6779
}
6880
}
6981
wc.Dispose();
@@ -77,19 +89,19 @@ public static void InstallEnvironment(InstallMethod im = InstallMethod.WebClient
7789
/// </summary>
7890
public static async Task InstallEnvironmentAsync(InstallMethodAsync im = InstallMethodAsync.WebClientAsync)
7991
{
80-
if (!Directory.Exists(dll_path))
81-
Directory.CreateDirectory(dll_path);
92+
if (!Directory.Exists(DllPath))
93+
Directory.CreateDirectory(DllPath);
8294
switch (im)
8395
{
8496
case InstallMethodAsync.WebClientAsync:
8597
var wc = new WebClient();
8698
foreach (string fn in CoreFiles)
8799
{
88-
if (!File.Exists($"{dll_path}{fn}"))
100+
if (!File.Exists($"{DllPath}{fn}"))
89101
{
90102
await wc.DownloadFileTaskAsync(
91-
new Uri($"{cloudUrl}{version}/{fn}", UriKind.Absolute),
92-
Path.GetFullPath($"{dll_path}{fn}"));
103+
new Uri($"{_cloudUrl}{_version}/{fn}", UriKind.Absolute),
104+
Path.GetFullPath($"{DllPath}{fn}"));
93105
}
94106
}
95107
wc?.Dispose();

Common.Algorithm.Interop/Exceptions/BasicException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Algorithm.Interop.Exceptions
1+
namespace Common.Algorithm.Interop.Exceptions
22
{
33
public class BasicException : Exception
44
{

Common.Algorithm.Interop/Exceptions/HashException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Algorithm.Interop.Exceptions
1+
namespace Common.Algorithm.Interop.Exceptions
22
{
33
public class HashException : BasicException
44
{

Common.Algorithm.Interop/Hash.cs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,83 @@
1-
using Algorithm.Interop.Exceptions;
1+
using Common.Algorithm.Interop.Exceptions;
22
using System.Runtime.InteropServices;
33
using System.Text;
44

5-
namespace Algorithm.Interop
5+
namespace Common.Algorithm.Interop
66
{
77
/// <summary>
88
/// 文件哈希类
99
/// </summary>
10-
public class Hash
10+
public static class Hash
1111
{
1212
/// <summary>
1313
/// DLL 路径
1414
/// </summary>
15-
private const string dll_path = Environment.dll_path;
15+
private const string DllPath = Environment.DllPath;
1616

1717
#region 扩展库函数导入 [DLLImport Hash.dll]
1818

1919
/// <summary>
2020
/// 调用扩展库进行 Hash
2121
/// </summary>
2222
/// <param name="src">元数据</param>
23+
/// <param name="output">输出</param>
24+
/// <param name="length">长度</param>
2325
/// <returns>哈希后的数据</returns>
24-
[DllImport($"{dll_path}Hash.dll", EntryPoint = "hash_str")]
26+
[DllImport($"{DllPath}Hash.dll", EntryPoint = "hash_str")]
2527
private static extern void hash_str(byte[] src, byte[] output, int length);
2628

2729
/// <summary>
2830
/// 调用扩展库进行 Hash 压缩
2931
/// </summary>
3032
/// <param name="src">哈希数据</param>
33+
/// <param name="output">输出</param>
3134
/// <returns>哈希压缩后的数据</returns>
32-
[DllImport($"{dll_path}Hash.dll", EntryPoint = "hash_compress_128_str")]
35+
[DllImport($"{DllPath}Hash.dll", EntryPoint = "hash_compress_128_str")]
3336
private static extern void hash_compress_128_str(byte[] src, byte[] output);
3437

3538
/// <summary>
3639
/// 调用扩展库进行 Hash 压缩
3740
/// </summary>
3841
/// <param name="src">哈希数据</param>
42+
/// <param name="output">输出</param>
3943
/// <returns>哈希压缩后的数据</returns>
40-
[DllImport($"{dll_path}Hash.dll", EntryPoint = "hash_compress_64_str")]
44+
[DllImport($"{DllPath}Hash.dll", EntryPoint = "hash_compress_64_str")]
4145
private static extern void hash_compress_64_str(byte[] src, byte[] output);
4246

4347
/// <summary>
4448
/// 调用扩展库进行 Hash 压缩
4549
/// </summary>
4650
/// <param name="src">哈希数据</param>
51+
/// <param name="output">输出</param>
4752
/// <returns>哈希压缩后的数据</returns>
48-
[DllImport($"{dll_path}Hash.dll", EntryPoint = "hash_compress_32_str")]
53+
[DllImport($"{DllPath}Hash.dll", EntryPoint = "hash_compress_32_str")]
4954
private static extern void hash_compress_32_str(byte[] src, byte[] output);
5055

5156
/// <summary>
5257
/// 调用扩展库进行 Hash 压缩
5358
/// </summary>
5459
/// <param name="src">哈希数据</param>
60+
/// <param name="output">输出</param>
5561
/// <returns>哈希压缩后的数据</returns>
56-
[DllImport($"{dll_path}Hash.dll", EntryPoint = "hash_compress_16_str")]
62+
[DllImport($"{DllPath}Hash.dll", EntryPoint = "hash_compress_16_str")]
5763
private static extern void hash_compress_16_str(byte[] src, byte[] output);
5864

5965
/// <summary>
6066
/// 调用扩展库进行 Hash 压缩
6167
/// </summary>
6268
/// <param name="src">哈希数据</param>
69+
/// <param name="output">输出</param>
6370
/// <returns>哈希压缩后的数据</returns>
64-
[DllImport($"{dll_path}Hash.dll", EntryPoint = "hash_compress_8_str")]
71+
[DllImport($"{DllPath}Hash.dll", EntryPoint = "hash_compress_8_str")]
6572
private static extern void hash_compress_8_str(byte[] src, byte[] output);
6673

6774
/// <summary>
6875
/// 调用扩展库进行 Hash 压缩
6976
/// </summary>
7077
/// <param name="src">哈希数据</param>
78+
/// <param name="output">输出</param>
7179
/// <returns>哈希压缩后的数据</returns>
72-
[DllImport($"{dll_path}Hash.dll", EntryPoint = "hash_compress_4_str")]
80+
[DllImport($"{DllPath}Hash.dll", EntryPoint = "hash_compress_4_str")]
7381
private static extern void hash_compress_4_str(byte[] src, byte[] output);
7482

7583
#endregion
@@ -80,34 +88,33 @@ public class Hash
8088
/// 统一调用哈希压缩
8189
/// </summary>
8290
/// <param name="mid">源压缩中间哈希值</param>
83-
/// <param name="rst">哈希压缩值储存地址</param>
8491
/// <param name="clv">哈希压缩级别</param>
8592
private static byte[] Compress(ref byte[] mid, CompressLevel clv)
8693
{
8794
switch (clv)
8895
{
8996
case CompressLevel.x128:
90-
byte[] ans128 = new byte[128];
97+
var ans128 = new byte[128];
9198
hash_compress_128_str(mid, ans128);
9299
return ans128;
93100
case CompressLevel.x64:
94-
byte[] ans64 = new byte[64];
101+
var ans64 = new byte[64];
95102
hash_compress_64_str(mid, ans64);
96103
return ans64;
97104
case CompressLevel.x32:
98-
byte[] ans32 = new byte[32];
105+
var ans32 = new byte[32];
99106
hash_compress_32_str(mid, ans32);
100107
return ans32;
101108
case CompressLevel.x16:
102-
byte[] ans16 = new byte[16];
109+
var ans16 = new byte[16];
103110
hash_compress_16_str(mid, ans16);
104111
return ans16;
105112
case CompressLevel.x8:
106-
byte[] ans8 = new byte[8];
113+
var ans8 = new byte[8];
107114
hash_compress_8_str(mid, ans8);
108115
return ans8;
109116
case CompressLevel.x4:
110-
byte[] ans4 = new byte[4];
117+
var ans4 = new byte[4];
111118
hash_compress_4_str(mid, ans4);
112119
return ans4;
113120
default: return Array.Empty<byte>();
@@ -139,14 +146,15 @@ public static CompressLevel HashCompressLevelParse(string compressed,
139146
/// 进行字符串哈希
140147
/// </summary>
141148
/// <param name="str">字符串</param>
149+
/// <param name="clv">压缩级别</param>
142150
/// <returns>哈希后的Byte数组</returns>
143151
public static byte[] FromString(string str,
144152
CompressLevel clv = CompressLevel.x64)
145153
{
146-
byte[] array = Encoding.UTF8.GetBytes(str); // 源字符串转 byte[]
147-
byte[] mid = new byte[2048]; // 存储哈希值
154+
var array = Encoding.UTF8.GetBytes(str); // 源字符串转 byte[]
155+
var mid = new byte[2048]; // 存储哈希值
148156
hash_str(array, mid, array.Length); // 哈希运算
149-
byte[] rst = Compress(ref mid, clv); // 哈希压缩运算
157+
var rst = Compress(ref mid, clv); // 哈希压缩运算
150158
return rst; // 返回哈希压缩值
151159
}
152160

@@ -155,15 +163,16 @@ public static byte[] FromString(string str,
155163
/// </summary>
156164
/// <param name="str">字符串</param>
157165
/// <param name="rmLink">是否移除连字符</param>
166+
/// <param name="clv">压缩级别</param>
158167
/// <returns>十六进制哈希字符串</returns>
159168
public static string FromString2Hex(string str, bool rmLink = false,
160169
CompressLevel clv = CompressLevel.x64)
161170
{
162-
byte[] array = Encoding.UTF8.GetBytes(str); // 源字符串转 byte[]
163-
byte[] mid = new byte[2048]; // 存储哈希值
171+
var array = Encoding.UTF8.GetBytes(str); // 源字符串转 byte[]
172+
var mid = new byte[2048]; // 存储哈希值
164173
hash_str(array, mid, array.Length); // 哈希运算
165-
byte[] rst = Compress(ref mid, clv); // 哈希压缩运算
166-
string ans = BitConverter.ToString(rst); // 哈希压缩运算转十六进制字符串
174+
var rst = Compress(ref mid, clv); // 哈希压缩运算
175+
var ans = BitConverter.ToString(rst); // 哈希压缩运算转十六进制字符串
167176
return rmLink ? ans.Replace("-", "") : ans; // 返回字符串, 据参数删除连字符
168177
}
169178

@@ -215,7 +224,9 @@ public enum HashMode
215224
/// </summary>
216225
public enum CompressLevel
217226
{
227+
// ReSharper disable InconsistentNaming
218228
x128 = 5, x64 = 4, x32 = 3, x16 = 2, x8 = 1, x4 = 0
229+
// ReSharper restore InconsistentNaming
219230
}
220231

221232
#endregion

0 commit comments

Comments
 (0)