forked from FabianTerhorst/coreclr-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedNativeResource.cs
168 lines (149 loc) · 5.11 KB
/
SharedNativeResource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Runtime.InteropServices;
using AltV.Net.Elements.Args;
using AltV.Net.Shared.Utils;
namespace AltV.Net.Shared
{
public abstract class SharedNativeResource : ISharedNativeResource
{
protected readonly ISharedCore core;
public IntPtr NativePointer { get; }
public abstract ISharedCSharpResourceImpl CSharpResourceImpl { get; }
public IntPtr ResourceImplPtr
{
get
{
unsafe
{
return core.Library.Shared.Resource_GetImpl(NativePointer);
}
}
}
private string? name;
public string Name
{
get
{
if (name != null) return name;
unsafe
{
var size = 0;
name = core.PtrToStringUtf8AndFree(core.Library.Shared.Resource_GetName(NativePointer, &size), size);
return name;
}
}
}
private string? type;
public string Type
{
get
{
if (type != null) return type;
unsafe
{
var size = 0;
type = core.PtrToStringUtf8AndFree(core.Library.Shared.Resource_GetType(NativePointer, &size), size);
return type;
}
}
}
public bool IsStarted
{
get
{
unsafe
{
return core.Library.Shared.Resource_IsStarted(NativePointer) == 1;
}
}
}
private string[] dependencies;
public string[] Dependencies
{
get
{
unsafe
{
if (dependencies != null) return dependencies;
var size = core.Library.Shared.Resource_GetDependenciesSize(NativePointer);
var pointers = new IntPtr[size];
core.Library.Shared.Resource_GetDependencies(NativePointer, pointers, size);
var strings = new string[size];
for (var i = 0; i < size; i++)
{
strings[i] = Marshal.PtrToStringUTF8(pointers[i]);
}
dependencies = strings;
return strings;
}
}
}
private string[] dependants;
public string[] Dependants
{
get
{
unsafe
{
if (dependants != null) return dependants;
var size = core.Library.Shared.Resource_GetDependantsSize(NativePointer);
var pointers = new IntPtr[size];
core.Library.Shared.Resource_GetDependants(NativePointer, pointers, size);
var strings = new string[size];
for (var i = 0; i < size; i++)
{
strings[i] = Marshal.PtrToStringUTF8(pointers[i]);
}
dependants = strings;
return strings;
}
}
}
internal SharedNativeResource(ISharedCore core, IntPtr nativePointer)
{
this.core = core;
NativePointer = nativePointer;
}
public void SetExport(string key, object value)
{
unsafe
{
var stringPtr = MemoryUtils.StringToHGlobalUtf8(key);
core.CreateMValue(out var mValue, value);
core.Library.Shared.Resource_SetExport(core.NativePointer, NativePointer, stringPtr, mValue.nativePointer);
Marshal.FreeHGlobal(stringPtr);
mValue.Dispose();
}
}
public void SetExport(string key, in MValueConst value)
{
unsafe
{
var stringPtr = MemoryUtils.StringToHGlobalUtf8(key);
core.Library.Shared.Resource_SetExport(core.NativePointer, NativePointer, stringPtr, value.nativePointer);
Marshal.FreeHGlobal(stringPtr);
}
}
public object GetExport(string key)
{
unsafe
{
var ptr = MemoryUtils.StringToHGlobalUtf8(key);
var mValue = new MValueConst(core, core.Library.Shared.Resource_GetExport(NativePointer, ptr));
var obj = mValue.ToObject();
mValue.Dispose();
Marshal.FreeHGlobal(ptr);
return obj;
}
}
public bool GetExport(string key, out MValueConst mValue)
{
unsafe
{
var ptr = MemoryUtils.StringToHGlobalUtf8(key);
mValue = new MValueConst(core, core.Library.Shared.Resource_GetExport(NativePointer, ptr));
Marshal.FreeHGlobal(ptr);
return mValue.type != MValueConst.Type.Nil;
}
}
}
}