File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed
Lagrange.Core.NativeAPI/NativeModel/Common Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -55,12 +55,12 @@ public BotKeystoreStruct() { }
55
55
56
56
public ByteArrayNative SKey = new ( ) ;
57
57
58
- public ByteArrayKVPNative [ ] PsKey = [ ] ;
58
+ public ByteArrayDictNative PsKey = new ( ) ;
59
59
60
60
public static implicit operator BotKeystore ( BotKeystoreStruct keystore )
61
61
{
62
62
var psKey = new Dictionary < string , string > ( ) ;
63
- foreach ( var kvp in keystore . PsKey )
63
+ foreach ( var kvp in ( ByteArrayKVPNative [ ] ) keystore . PsKey )
64
64
{
65
65
psKey [ Encoding . UTF8 . GetString ( kvp . Key ) ] = Encoding . UTF8 . GetString ( kvp . Value ) ;
66
66
}
@@ -141,7 +141,7 @@ public static implicit operator BotKeystoreStruct(BotKeystore keystore)
141
141
public BotKeystore ToKeystoreWithoutFree ( )
142
142
{
143
143
var psKey = new Dictionary < string , string > ( ) ;
144
- foreach ( var kvp in PsKey )
144
+ foreach ( var kvp in ( ByteArrayKVPNative [ ] ) PsKey )
145
145
{
146
146
psKey [ Encoding . UTF8 . GetString ( kvp . Key ) ] = Encoding . UTF8 . GetString ( kvp . Value ) ;
147
147
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments