-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMValueConst.Locked.cs
204 lines (192 loc) · 8 KB
/
MValueConst.Locked.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using AltV.Net.Data;
using AltV.Net.Elements.Args;
using AltV.Net.Elements.Entities;
using AltV.Net.Native;
using AltV.Net.Shared.Elements.Entities;
namespace AltV.Net.Async
{
public static class MValueConstLocked
{
public static void CreateLocked(ISharedBaseObject baseObject, out MValueConst mValue)
{
Alt.CoreImpl.CreateMValueBaseObject(out mValue, baseObject);
}
public static void CreateFromObjectLocked(object obj, out MValueConst mValue)
{
if (obj == null)
{
mValue = MValueConst.Nil;
return;
}
int i;
string[] dictKeys;
MValueConst[] dictValues;
MValueWriter2 writer;
switch (obj)
{
case ISharedBaseObject baseObject:
CreateLocked(baseObject, out mValue);
return;
case bool value:
Alt.CoreImpl.CreateMValueBool(out mValue, value);
return;
case int value:
Alt.CoreImpl.CreateMValueInt(out mValue, value);
return;
case uint value:
Alt.CoreImpl.CreateMValueUInt(out mValue, value);
return;
case long value:
Alt.CoreImpl.CreateMValueInt(out mValue, value);
return;
case ulong value:
Alt.CoreImpl.CreateMValueUInt(out mValue, value);
return;
case double value:
Alt.CoreImpl.CreateMValueDouble(out mValue, value);
return;
case float value:
Alt.CoreImpl.CreateMValueDouble(out mValue, value);
return;
case string value:
Alt.CoreImpl.CreateMValueString(out mValue, value);
return;
case MValueConst value:
mValue = value;
return;
case MValueConst[] value:
Alt.CoreImpl.CreateMValueList(out mValue, value, (ulong) value.Length);
return;
case Invoker value:
Alt.CoreImpl.CreateMValueFunction(out mValue, value.NativePointer);
return;
case MValueFunctionCallback value:
Alt.CoreImpl.CreateMValueFunction(out mValue, Alt.CoreImpl.Resource.CSharpResourceImpl.CreateInvoker(value));
return;
case Net.Function function:
Alt.CoreImpl.CreateMValueFunction(out mValue,
Alt.CoreImpl.Resource.CSharpResourceImpl.CreateInvoker(function.Call));
return;
case byte[] byteArray:
Alt.CoreImpl.CreateMValueByteArray(out mValue, byteArray);
break;
case IDictionary dictionary:
dictKeys = new string[dictionary.Count];
dictValues = new MValueConst[dictionary.Count];
i = 0;
foreach (var key in dictionary.Keys)
{
if (key is string stringKey)
{
dictKeys[i++] = stringKey;
}
else
{
mValue = MValueConst.Nil;
return;
}
}
i = 0;
foreach (var value in dictionary.Values)
{
Alt.CoreImpl.CreateMValue(out var elementMValue, value);
dictValues[i++] = elementMValue;
}
Alt.CoreImpl.CreateMValueDict(out mValue, dictKeys, dictValues, (ulong) dictionary.Count);
for (int j = 0, dictLength = dictionary.Count; j < dictLength; j++)
{
dictValues[j].Dispose();
}
return;
case ICollection collection:
var length = (ulong) collection.Count;
var listValues = new MValueConst[length];
i = 0;
foreach (var value in collection)
{
Alt.CoreImpl.CreateMValue(out var elementMValue, value);
listValues[i++] = elementMValue;
}
Alt.CoreImpl.CreateMValueList(out mValue, listValues, length);
for (ulong j = 0; j < length; j++)
{
listValues[j].Dispose();
}
return;
case IDictionary<string, object> dictionary:
dictKeys = new string[dictionary.Count];
dictValues = new MValueConst[dictionary.Count];
i = 0;
foreach (var key in dictionary.Keys)
{
dictKeys[i++] = key;
}
i = 0;
foreach (var value in dictionary.Values)
{
Alt.CoreImpl.CreateMValue(out var elementMValue, value);
dictValues[i++] = elementMValue;
}
Alt.CoreImpl.CreateMValueDict(out mValue, dictKeys, dictValues, (ulong) dictionary.Count);
for (int j = 0, dictLength = dictValues.Length; j < dictLength; j++)
{
dictValues[j].Dispose();
}
return;
case IWritable writable:
writer = Alt.CreateMValueWriter();
writable.OnWrite(writer);
writer.ToMValue(out mValue);
return;
case IMValueConvertible convertible:
writer = Alt.CreateMValueWriter();
convertible.GetAdapter().ToMValue(obj, writer);
writer.ToMValue(out mValue);
return;
case Position position:
Alt.CoreImpl.CreateMValueVector3(out mValue, position);
return;
case Rotation rotation:
Alt.CoreImpl.CreateMValueVector3(out mValue, rotation);
return;
case Rgba rgba:
Alt.CoreImpl.CreateMValueRgba(out mValue, rgba);
return;
case short value:
Alt.CoreImpl.CreateMValueInt(out mValue, value);
return;
case ushort value:
Alt.CoreImpl.CreateMValueUInt(out mValue, value);
return;
case Vector3 position:
Alt.CoreImpl.CreateMValueVector3(out mValue, position);
return;
case Vector2 value:
Alt.CoreImpl.CreateMValueVector2(out mValue, value);
return;
default:
var type = obj?.GetType();
if (type != null && Alt.CoreImpl.IsMValueConvertible(obj.GetType()))
{
Alt.ToMValue(obj, type, out mValue);
return;
}
Alt.Log("can't convert type:" + type);
mValue = MValueConst.Nil;
return;
}
}
internal static void CreateFromObjectsLocked(object[] objects, MValueConst[] mValues)
{
var length = objects.Length;
for (var i = 0; i < length; i++)
{
CreateFromObjectLocked(objects[i], out var mValueElement);
mValues[i] = mValueElement;
}
}
}
}