Skip to content

Commit 3bfc23c

Browse files
committed
[NativeAPI] Almost all the implementations for now
1 parent 168dc5d commit 3bfc23c

46 files changed

Lines changed: 1518 additions & 286 deletions

Some content is hidden

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

Lagrange.Core.NativeAPI/Context.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ public Context(BotContext botContext)
88
{
99
BotContext = botContext;
1010
EventInvoker = new ReverseEventInvoker(BotContext);
11+
SendMessageContext = new SendMessageContext(BotContext);
1112
}
1213
public BotContext BotContext { get; set; }
1314
public ReverseEventInvoker EventInvoker { get; set; }
15+
public SendMessageContext SendMessageContext { get; set; }
1416
}
1517
}

Lagrange.Core.NativeAPI/Lagrange.Core.NativeAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<PublishAot>true</PublishAot>
78
</PropertyGroup>
89

910
<ItemGroup>

Lagrange.Core.NativeAPI/NativeModel/Common/BotConfigStruct.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.InteropServices;
2+
using Lagrange.Core.Common;
23

34
namespace Lagrange.Core.NativeAPI.NativeModel.Context
45
{
@@ -22,5 +23,33 @@ public BotConfigStruct()
2223
public uint HighwayConcurrent { get; set; } = 4;
2324

2425
public bool AutoReLogin { get; set; } = true;
26+
27+
public static implicit operator BotConfig(BotConfigStruct config)
28+
{
29+
return new BotConfig()
30+
{
31+
Protocol = (Protocols)config.Protocol,
32+
AutoReconnect = config.AutoReconnect,
33+
UseIPv6Network = config.UseIPv6Network,
34+
GetOptimumServer = config.GetOptimumServer,
35+
HighwayChunkSize = config.HighwayChunkSize,
36+
HighwayConcurrent = config.HighwayConcurrent,
37+
AutoReLogin = config.AutoReLogin
38+
};
39+
}
40+
41+
public static implicit operator BotConfigStruct(BotConfig config)
42+
{
43+
return new BotConfigStruct()
44+
{
45+
Protocol = (byte)config.Protocol,
46+
AutoReconnect = config.AutoReconnect,
47+
UseIPv6Network = config.UseIPv6Network,
48+
GetOptimumServer = config.GetOptimumServer,
49+
HighwayChunkSize = config.HighwayChunkSize,
50+
HighwayConcurrent = config.HighwayConcurrent,
51+
AutoReLogin = config.AutoReLogin
52+
};
53+
}
2554
}
2655
}
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.Text;
2+
using Lagrange.Core.Common.Entity;
3+
14
namespace Lagrange.Core.NativeAPI.NativeModel.Common
25
{
36
public struct BotFriendCategoryStruct
@@ -6,10 +9,31 @@ public BotFriendCategoryStruct() { }
69

710
public int Id = 0;
811

9-
public byte[] Name = [];
12+
public ByteArrayNative Name = new();
1013

1114
public int Count = 0;
1215

1316
public int SortId = 0;
17+
18+
public static implicit operator BotFriendCategoryStruct(BotFriendCategory category)
19+
{
20+
return new BotFriendCategoryStruct()
21+
{
22+
Id = category.Id,
23+
Name = Encoding.UTF8.GetBytes(category.Name),
24+
Count = category.Count,
25+
SortId = category.SortId
26+
};
27+
}
28+
29+
public static implicit operator BotFriendCategory(BotFriendCategoryStruct category)
30+
{
31+
return new BotFriendCategory(
32+
category.Id,
33+
Encoding.UTF8.GetString(category.Name),
34+
category.Count,
35+
category.SortId
36+
);
37+
}
1438
}
1539
}
Lines changed: 127 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text;
2+
using Lagrange.Core.Common;
13
using Lagrange.Core.NativeAPI.NativeModel.Common;
24

35
namespace Lagrange.Core.NativeAPI.NativeModel.Context
@@ -6,54 +8,138 @@ public struct BotKeystoreStruct
68
{
79
public BotKeystoreStruct() { }
810

9-
public long Uin { get; set; } = 0;
11+
public long Uin = 0;
1012

11-
public byte[] Uid { get; set; } = [];
13+
public ByteArrayNative Uid = new();
1214

13-
public byte[] Guid { get; set; } = [];
15+
public ByteArrayNative Guid = new();
1416

15-
public byte[] AndroidId { get; set; } = [];
17+
public ByteArrayNative AndroidId = new();
1618

17-
public byte[] Qimei { get; set; } = [];
19+
public ByteArrayNative Qimei = new();
1820

19-
public byte[] DeviceName { get; set; } = [];
21+
public ByteArrayNative DeviceName = new();
2022

2123
// WLoginSigs
2224

23-
public byte[] A2 { get; set; } = [];
24-
25-
public byte[] A2Key { get; set; } = new byte[16];
26-
27-
public byte[] D2 { get; set; } = [];
28-
29-
public byte[] D2Key { get; set; } = new byte[16];
30-
31-
public byte[] A1 { get; set; } = [];
32-
33-
public byte[] A1Key { get; set; } = new byte[16];
34-
35-
public byte[] NoPicSig { get; set; } = [];
36-
37-
public byte[] TgtgtKey { get; set; } = [];
38-
39-
public byte[] Ksid { get; set; } = [];
40-
41-
public byte[] SuperKey { get; set; } = [];
42-
43-
public byte[] StKey { get; set; } = [];
44-
45-
public byte[] StWeb { get; set; } = [];
46-
47-
public byte[] St { get; set; } = [];
48-
49-
public byte[] WtSessionTicket { get; set; } = [];
50-
51-
public byte[] WtSessionTicketKey { get; set; } = [];
52-
53-
public byte[] RandomKey { get; set; } = new byte[16];
54-
55-
public byte[] SKey { get; set; } = [];
56-
57-
public KeyValuePairNative<byte[], byte[]>[] PsKey { get; set; } = [];
25+
public ByteArrayNative A2 = new();
26+
27+
public ByteArrayNative A2Key = new byte[16];
28+
29+
public ByteArrayNative D2 = new();
30+
31+
public ByteArrayNative D2Key = new byte[16];
32+
33+
public ByteArrayNative A1 = new();
34+
35+
public ByteArrayNative A1Key = new byte[16];
36+
37+
public ByteArrayNative NoPicSig = new();
38+
39+
public ByteArrayNative TgtgtKey = new();
40+
41+
public ByteArrayNative Ksid = new();
42+
43+
public ByteArrayNative SuperKey = new();
44+
45+
public ByteArrayNative StKey = new();
46+
47+
public ByteArrayNative StWeb = new();
48+
49+
public ByteArrayNative St = new();
50+
51+
public ByteArrayNative WtSessionTicket = new();
52+
53+
public ByteArrayNative WtSessionTicketKey = new();
54+
55+
public ByteArrayNative RandomKey = new byte[16];
56+
57+
public ByteArrayNative SKey = new();
58+
59+
public KeyValuePairNative<ByteArrayNative, ByteArrayNative>[] PsKey = [];
60+
61+
public static implicit operator BotKeystore(BotKeystoreStruct keystore)
62+
{
63+
var psKey = new Dictionary<string, string>();
64+
foreach (var kvp in keystore.PsKey)
65+
{
66+
psKey[Encoding.UTF8.GetString(kvp.Key)] = Encoding.UTF8.GetString(kvp.Value);
67+
}
68+
69+
return new BotKeystore()
70+
{
71+
Uin = keystore.Uin,
72+
Uid = Encoding.UTF8.GetString(keystore.Uid),
73+
Guid = keystore.Guid,
74+
AndroidId = Encoding.UTF8.GetString(keystore.AndroidId),
75+
Qimei = Encoding.UTF8.GetString(keystore.Qimei),
76+
DeviceName = Encoding.UTF8.GetString(keystore.DeviceName),
77+
WLoginSigs = new WLoginSigs()
78+
{
79+
A2 = keystore.A2,
80+
A2Key = keystore.A2Key,
81+
D2 = keystore.D2,
82+
D2Key = keystore.D2Key,
83+
A1 = keystore.A1,
84+
A1Key = keystore.A1Key,
85+
NoPicSig = keystore.NoPicSig,
86+
TgtgtKey = keystore.TgtgtKey,
87+
Ksid = keystore.Ksid,
88+
SuperKey = keystore.SuperKey,
89+
StKey = keystore.StKey,
90+
StWeb = keystore.StWeb,
91+
St = keystore.St,
92+
WtSessionTicket = keystore.WtSessionTicket,
93+
WtSessionTicketKey = keystore.WtSessionTicketKey,
94+
RandomKey = keystore.RandomKey,
95+
SKey = keystore.SKey,
96+
PsKey = psKey
97+
}
98+
};
99+
}
100+
101+
public static implicit operator BotKeystoreStruct(BotKeystore keystore)
102+
{
103+
var bytePsKey = new KeyValuePairNative<ByteArrayNative, ByteArrayNative>[
104+
keystore.WLoginSigs.PsKey.Count
105+
];
106+
int i = 0;
107+
foreach (var kvp in keystore.WLoginSigs.PsKey)
108+
{
109+
bytePsKey[i++] = new KeyValuePairNative<ByteArrayNative, ByteArrayNative>()
110+
{
111+
Key = Encoding.UTF8.GetBytes(kvp.Key),
112+
Value = Encoding.UTF8.GetBytes(kvp.Value)
113+
};
114+
}
115+
116+
return new BotKeystoreStruct()
117+
{
118+
Uin = keystore.Uin,
119+
Uid = Encoding.UTF8.GetBytes(keystore.Uid),
120+
Guid = keystore.Guid,
121+
AndroidId = Encoding.UTF8.GetBytes(keystore.AndroidId),
122+
Qimei = Encoding.UTF8.GetBytes(keystore.Qimei),
123+
DeviceName = Encoding.UTF8.GetBytes(keystore.DeviceName),
124+
A2 = keystore.WLoginSigs.A2,
125+
A2Key = keystore.WLoginSigs.A2Key,
126+
D2 = keystore.WLoginSigs.D2,
127+
D2Key = keystore.WLoginSigs.D2Key,
128+
A1 = keystore.WLoginSigs.A1,
129+
A1Key = keystore.WLoginSigs.A1Key,
130+
NoPicSig = keystore.WLoginSigs.NoPicSig,
131+
TgtgtKey = keystore.WLoginSigs.TgtgtKey,
132+
Ksid = keystore.WLoginSigs.Ksid,
133+
SuperKey = keystore.WLoginSigs.SuperKey,
134+
StKey = keystore.WLoginSigs.StKey,
135+
StWeb = keystore.WLoginSigs.StWeb,
136+
St = keystore.WLoginSigs.St,
137+
WtSessionTicket = keystore.WLoginSigs.WtSessionTicket,
138+
WtSessionTicketKey = keystore.WLoginSigs.WtSessionTicketKey,
139+
RandomKey = keystore.WLoginSigs.RandomKey,
140+
SKey = keystore.WLoginSigs.SKey,
141+
PsKey = bytePsKey
142+
};
143+
}
58144
}
59145
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace Lagrange.Core.NativeAPI.NativeModel.Common
4+
{
5+
[StructLayout(LayoutKind.Sequential)]
6+
public struct ByteArrayNative
7+
{
8+
public int Length;
9+
public IntPtr Data;
10+
11+
public static implicit operator ByteArrayNative(byte[] bytes)
12+
{
13+
IntPtr ptr = Marshal.AllocHGlobal(bytes.Length);
14+
Marshal.Copy(bytes, 0, ptr, bytes.Length);
15+
16+
return new ByteArrayNative { Length = bytes.Length, Data = ptr };
17+
}
18+
19+
public static implicit operator byte[](ByteArrayNative byteArray)
20+
{
21+
if (byteArray.Data == IntPtr.Zero)
22+
{
23+
return [];
24+
}
25+
byte[] bytes = new byte[byteArray.Length];
26+
Marshal.Copy(byteArray.Data, bytes, 0, byteArray.Length);
27+
28+
Marshal.FreeHGlobal(byteArray.Data);
29+
30+
return bytes;
31+
}
32+
33+
public byte[] ToByteArrayWithoutFree()
34+
{
35+
byte[] bytes = new byte[Length];
36+
Marshal.Copy(Data, bytes, 0, Length);
37+
return bytes;
38+
}
39+
40+
public bool IsEmpty()
41+
{
42+
return Length == 0 || Data == IntPtr.Zero;
43+
}
44+
}
45+
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
using System.Text;
2+
using Lagrange.Core.Events.EventArgs;
3+
using Lagrange.Core.NativeAPI.NativeModel.Common;
4+
15
namespace Lagrange.Core.NativeAPI.NativeModel.Event
26
{
37
public struct BotCaptchaEventStruct : IEventStruct
48
{
59
public BotCaptchaEventStruct() { }
10+
11+
public ByteArrayNative CaptchaUrl = new();
12+
13+
public static implicit operator BotCaptchaEventStruct(BotCaptchaEvent e)
14+
{
15+
return new BotCaptchaEventStruct()
16+
{
17+
CaptchaUrl = Encoding.UTF8.GetBytes(e.CaptchaUrl)
18+
};
19+
}
620

7-
public byte[] CaptchaUrl = [];
21+
public static implicit operator BotCaptchaEvent(BotCaptchaEventStruct e)
22+
{
23+
return new BotCaptchaEvent(Encoding.UTF8.GetString(e.CaptchaUrl));
24+
}
825
}
9-
}
26+
}

0 commit comments

Comments
 (0)