Skip to content

Commit a78fb9d

Browse files
committed
[NativeAPI] Removed ManagedArray
1 parent 1846b0a commit a78fb9d

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public BotKeystoreStruct() { }
5555

5656
public ByteArrayNative SKey = new();
5757

58-
public ByteArrayKVPNative[] PsKey = [];
58+
public ByteArrayDictNative PsKey = new();
5959

6060
public static implicit operator BotKeystore(BotKeystoreStruct keystore)
6161
{
6262
var psKey = new Dictionary<string, string>();
63-
foreach (var kvp in keystore.PsKey)
63+
foreach (var kvp in (ByteArrayKVPNative[])keystore.PsKey)
6464
{
6565
psKey[Encoding.UTF8.GetString(kvp.Key)] = Encoding.UTF8.GetString(kvp.Value);
6666
}
@@ -141,7 +141,7 @@ public static implicit operator BotKeystoreStruct(BotKeystore keystore)
141141
public BotKeystore ToKeystoreWithoutFree()
142142
{
143143
var psKey = new Dictionary<string, string>();
144-
foreach (var kvp in PsKey)
144+
foreach (var kvp in (ByteArrayKVPNative[])PsKey)
145145
{
146146
psKey[Encoding.UTF8.GetString(kvp.Key)] = Encoding.UTF8.GetString(kvp.Value);
147147
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace Lagrange.Core.NativeAPI.NativeModel.Common;
4+
5+
public struct ByteArrayDictNative
6+
{
7+
public int Length;
8+
public IntPtr Data;
9+
10+
public static implicit operator ByteArrayDictNative(ByteArrayKVPNative[] dict)
11+
{
12+
int size = Marshal.SizeOf<ByteArrayKVPNative>();
13+
IntPtr ptr = Marshal.AllocHGlobal(size * dict.Length);
14+
for (int i = 0; i < dict.Length; i++)
15+
{
16+
Marshal.StructureToPtr(dict[i], ptr + i * size, false);
17+
}
18+
19+
return new ByteArrayDictNative { Length = dict.Length, Data = ptr };
20+
}
21+
22+
public static implicit operator ByteArrayKVPNative[](ByteArrayDictNative dict)
23+
{
24+
if (dict.Data == IntPtr.Zero || dict.Length == 0)
25+
{
26+
return [];
27+
}
28+
29+
ByteArrayKVPNative[] result = new ByteArrayKVPNative[dict.Length];
30+
int size = Marshal.SizeOf<ByteArrayKVPNative>();
31+
for (int i = 0; i < dict.Length; i++)
32+
{
33+
result[i] = Marshal.PtrToStructure<ByteArrayKVPNative>(dict.Data + i * size);
34+
}
35+
36+
Marshal.FreeHGlobal(dict.Data);
37+
38+
return result;
39+
}
40+
}

0 commit comments

Comments
 (0)