Skip to content

Commit dd38a61

Browse files
committed
rebuild: use baseaccount to instead of direct property in userinfo class
1 parent a3a593e commit dd38a61

File tree

19 files changed

+533
-313
lines changed

19 files changed

+533
-313
lines changed

PCL.Neo.Core/PCL.Neo.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
1011
<PackageReference Include="SevenZip" Version="19.0.0" />
1112
<PackageReference Include="System.Reactive" Version="6.0.1" />
1213
<PackageReference Include="System.Reactive.Linq" Version="6.0.1" />

PCL.Neo.Core/Service/Accounts/OAuthService/AuthCodeMode.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static async Task<MsaAccount> LogIn()
4141

4242
private static string GetAuthCode()
4343
{
44-
var url = OAuthData.FormUrlReqData.AuthCodeData;
44+
var url = OAuthData.FormUrlReqData.GetAuthCodeData();
4545
var redirectServer = new RedirectServer.RedirectServer(5050); // todo: set prot in app configureation
4646
var authCode = new AuthCode();
4747
redirectServer.Subscribe(authCode);
@@ -53,12 +53,14 @@ private static string GetAuthCode()
5353
}
5454

5555
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(OAuthData.FormUrlReqData))]
56-
public static async ValueTask<OAuthData.ResponseData.AccessTokenResponce> GetAuthToken(string authCode)
56+
public static async ValueTask<OAuthData.ResponseData.AccessTokenResponse> GetAuthToken(string authCode)
5757
{
58-
var authTokenData = OAuthData.FormUrlReqData.AuthTokenData;
59-
authTokenData["authCode"] = authCode;
58+
var authTokenData = new Dictionary<string, string>(OAuthData.FormUrlReqData.AuthTokenData)
59+
{
60+
["authCode"] = authCode
61+
};
6062

61-
return await Net.SendHttpRequestAsync<OAuthData.ResponseData.AccessTokenResponce>(
63+
return await Net.SendHttpRequestAsync<OAuthData.ResponseData.AccessTokenResponse>(
6264
HttpMethod.Post,
6365
OAuthData.RequestUrls.TokenUri,
6466
new FormUrlEncodedContent(authTokenData));

PCL.Neo.Core/Service/Accounts/Storage/BaseAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract record BaseAccount(string UserType)
1515
{
1616
// Properties common to all account types
1717
public required string Uuid { get; set; } // Made set for flexibility, could be init
18-
public required string UserName { get; init; }
18+
public required string UserName { get; set; }
1919
public string UserType { get; } = UserType; // This is crucial for discrimination
2020
public string UserProperties { get; init; } = string.Empty;
2121
public required List<Skin> Skins { get; init; } = [];

PCL.Neo.Core/Utils/Uuid.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace PCL.Neo.Core.Utils;
4+
5+
public static class Uuid
6+
{
7+
private const string DefaultUuid = "00000000-0000-0000-0000-000000000000";
8+
9+
public static string GenerateOfflineUuid(string username)
10+
{
11+
if (string.IsNullOrEmpty(username) || !IsValidUsername(username))
12+
return DefaultUuid;
13+
var guid = new Guid(MurmurHash3.Hash(username));
14+
return guid.ToString();
15+
}
16+
17+
public static bool IsValidUsername(string username)
18+
{
19+
return !string.IsNullOrEmpty(username) &&
20+
username.Length >= 3 &&
21+
username.Length <= 16 &&
22+
Regex.IsMatch(username, "^[a-zA-Z0-9_]+$");
23+
}
24+
25+
// MurmurHash3算法实现
26+
private static class MurmurHash3
27+
{
28+
public static byte[] Hash(string str)
29+
{
30+
var bytes = System.Text.Encoding.UTF8.GetBytes(str);
31+
const uint seed = 144;
32+
const uint c1 = 0xcc9e2d51;
33+
const uint c2 = 0x1b873593;
34+
uint h1 = seed;
35+
uint k1;
36+
int len = bytes.Length;
37+
int i = 0;
38+
for (; i + 4 <= len; i += 4)
39+
{
40+
k1 = (uint)((bytes[i] & 0xFF) |
41+
((bytes[i + 1] & 0xFF) << 8) |
42+
((bytes[i + 2] & 0xFF) << 16) |
43+
((bytes[i + 3] & 0xFF) << 24));
44+
k1 *= c1;
45+
k1 = RotateLeft(k1, 15);
46+
k1 *= c2;
47+
h1 ^= k1;
48+
h1 = RotateLeft(h1, 13);
49+
h1 = h1 * 5 + 0xe6546b64;
50+
}
51+
k1 = 0;
52+
switch (len & 3)
53+
{
54+
case 3: k1 ^= (uint)(bytes[i + 2] & 0xFF) << 16; goto case 2;
55+
case 2: k1 ^= (uint)(bytes[i + 1] & 0xFF) << 8; goto case 1;
56+
case 1:
57+
k1 ^= (uint)(bytes[i] & 0xFF);
58+
k1 *= c1;
59+
k1 = RotateLeft(k1, 15);
60+
k1 *= c2;
61+
h1 ^= k1;
62+
break;
63+
}
64+
h1 ^= (uint)len;
65+
h1 = Fmix(h1);
66+
byte[] result = new byte[16];
67+
BitConverter.GetBytes(h1).CopyTo(result, 0);
68+
BitConverter.GetBytes(h1 ^ seed).CopyTo(result, 4);
69+
BitConverter.GetBytes(seed ^ (h1 >> 16)).CopyTo(result, 8);
70+
BitConverter.GetBytes(seed ^ (h1 << 8)).CopyTo(result, 12);
71+
return result;
72+
}
73+
private static uint RotateLeft(uint x, int r) => (x << r) | (x >> (32 - r));
74+
private static uint Fmix(uint h)
75+
{
76+
h ^= h >> 16;
77+
h *= 0x85ebca6b;
78+
h ^= h >> 13;
79+
h *= 0xc2b2ae35;
80+
h ^= h >> 16;
81+
return h;
82+
}
83+
}
84+
}

PCL.Neo.Tests/BitConvertorTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using NUnit.Framework.Legacy;
8+
9+
namespace PCL.Neo.Tests
10+
{
11+
[TestFixture]
12+
public class BitConvertorTest
13+
{
14+
[Test]
15+
public void ConvertorTest()
16+
{
17+
using var md5 = System.Security.Cryptography.MD5.Create();
18+
var inputBytes = System.Text.Encoding.UTF8.GetBytes("OfflinePlayer:whiteact346");
19+
var hashBytes = md5.ComputeHash(inputBytes);
20+
21+
// 设置UUID版本 (版本3 = MD5)
22+
hashBytes[6] = (byte)((hashBytes[6] & 0x0F) | 0x30);
23+
// 设置UUID变体
24+
hashBytes[8] = (byte)((hashBytes[8] & 0x3F) | 0x80);
25+
26+
// 转换为UUID字符串格式
27+
var fir = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
28+
var sec = Convert.ToHexStringLower(hashBytes);
29+
30+
Console.Write(fir);
31+
32+
ClassicAssert.AreEqual(fir, sec);
33+
}
34+
}
35+
}

PCL.Neo.Tests/Core/Models/FileHelper/FileTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using PCL.Neo.Core.Models;
22
using PCL.Neo.Core.Models.Minecraft.Java;
33
using PCL.Neo.Core.Models.Minecraft.Mod;
4+
using System;
5+
using System.IO;
6+
using System.Threading.Tasks;
47

58
namespace PCL.Neo.Tests.Core.Models.FileHelper;
69

PCL.Neo.Tests/Core/Models/Minecraft/AssetIndexFileTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using PCL.Neo.Core.Models.Minecraft;
2+
using System.IO;
23

34
namespace PCL.Neo.Tests.Core.Models.Minecraft;
45

PCL.Neo.Tests/Core/Models/Minecraft/JavaTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using PCL.Neo.Core.Models;
22
using PCL.Neo.Core.Models.Minecraft.Java;
3+
using System;
4+
using System.Threading.Tasks;
35

46
namespace PCL.Neo.Tests.Core.Models.Minecraft
57
{

PCL.Neo.Tests/Core/Models/Minecraft/MetadataFileTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using PCL.Neo.Core.Models.Minecraft;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
25
using System.Text.Json.Nodes;
36

47
namespace PCL.Neo.Tests.Core.Models.Minecraft;

PCL.Neo.Tests/Core/Models/Minecraft/VersionManifestFileTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using PCL.Neo.Core.Models.Minecraft;
2+
using System.IO;
23

34
namespace PCL.Neo.Tests.Core.Models.Minecraft;
45

0 commit comments

Comments
 (0)