1+ using System . Collections ;
2+ using System . Collections . Generic ;
3+ using AltV . Net . Elements . Args ;
4+ using AltV . Net . Elements . Entities ;
5+ using AltV . Net . Native ;
6+
7+ namespace AltV . Net . Async
8+ {
9+ public static class MValueLocked
10+ {
11+ public static MValue CreateLocked ( IPlayer player )
12+ {
13+ lock ( player )
14+ {
15+ if ( ! player . Exists )
16+ {
17+ return MValue . Nil ;
18+ }
19+ }
20+
21+ var mValue = MValue . Nil ;
22+ AltNative . MValueCreate . MValue_CreatePlayer ( player . NativePointer , ref mValue ) ;
23+ return mValue ;
24+ }
25+
26+ public static MValue CreateLocked ( IVehicle vehicle )
27+ {
28+ lock ( vehicle )
29+ {
30+ if ( ! vehicle . Exists )
31+ {
32+ return MValue . Nil ;
33+ }
34+ }
35+
36+ var mValue = MValue . Nil ;
37+ AltNative . MValueCreate . MValue_CreateVehicle ( vehicle . NativePointer , ref mValue ) ;
38+ return mValue ;
39+ }
40+
41+ public static MValue CreateLocked ( IBlip blip )
42+ {
43+ lock ( blip )
44+ {
45+ if ( ! blip . Exists )
46+ {
47+ return MValue . Nil ;
48+ }
49+ }
50+
51+ var mValue = MValue . Nil ;
52+ AltNative . MValueCreate . MValue_CreateBlip ( blip . NativePointer , ref mValue ) ;
53+ return mValue ;
54+ }
55+
56+ public static MValue CreateLocked ( ICheckpoint checkpoint )
57+ {
58+ lock ( checkpoint )
59+ {
60+ if ( ! checkpoint . Exists )
61+ {
62+ return MValue . Nil ;
63+ }
64+ }
65+
66+ var mValue = MValue . Nil ;
67+ AltNative . MValueCreate . MValue_CreateCheckpoint ( checkpoint . NativePointer , ref mValue ) ;
68+ return mValue ;
69+ }
70+
71+ public static MValue CreateFromObjectLocked ( object obj )
72+ {
73+ if ( obj == null )
74+ {
75+ return MValue . Nil ;
76+ }
77+
78+ int i ;
79+
80+ string [ ] dictKeys ;
81+ MValue [ ] dictValues ;
82+ MValueWriter writer ;
83+
84+ switch ( obj )
85+ {
86+ case IPlayer player :
87+ return CreateLocked ( player ) ;
88+ case IVehicle vehicle :
89+ return CreateLocked ( vehicle ) ;
90+ case IBlip blip :
91+ return CreateLocked ( blip ) ;
92+ case ICheckpoint checkpoint :
93+ return CreateLocked ( checkpoint ) ;
94+ case bool value :
95+ return MValue . Create ( value ) ;
96+ case int value :
97+ return MValue . Create ( value ) ;
98+ case uint value :
99+ return MValue . Create ( value ) ;
100+ case long value :
101+ return MValue . Create ( value ) ;
102+ case ulong value :
103+ return MValue . Create ( value ) ;
104+ case double value :
105+ return MValue . Create ( value ) ;
106+ case float value :
107+ return MValue . Create ( value ) ;
108+ case string value :
109+ return MValue . Create ( value ) ;
110+ case MValue value :
111+ return value ;
112+ case MValue [ ] value :
113+ return MValue . Create ( value ) ;
114+ case Invoker value :
115+ return MValue . Create ( value ) ;
116+ case MValue . Function value :
117+ return MValue . Create ( value ) ;
118+ case Function function :
119+ return MValue . Create ( function . call ) ;
120+ case IDictionary dictionary :
121+ dictKeys = new string [ dictionary . Count ] ;
122+ dictValues = new MValue [ dictionary . Count ] ;
123+ i = 0 ;
124+ foreach ( var key in dictionary . Keys )
125+ {
126+ if ( key is string stringKey )
127+ {
128+ dictKeys [ i ++ ] = stringKey ;
129+ }
130+ else
131+ {
132+ return MValue . Nil ;
133+ }
134+ }
135+
136+ i = 0 ;
137+ foreach ( var value in dictionary . Values )
138+ {
139+ dictValues [ i ++ ] = CreateFromObjectLocked ( value ) ;
140+ }
141+
142+ return MValue . Create ( dictValues , dictKeys ) ;
143+ case ICollection collection :
144+ var length = collection . Count ;
145+ var listValues = new MValue [ length ] ;
146+ i = 0 ;
147+ foreach ( var value in collection )
148+ {
149+ listValues [ i ++ ] = CreateFromObjectLocked ( value ) ;
150+ }
151+
152+ return MValue . Create ( listValues ) ;
153+ case IDictionary < string , object > dictionary :
154+ dictKeys = new string [ dictionary . Count ] ;
155+ dictValues = new MValue [ dictionary . Count ] ;
156+ i = 0 ;
157+ foreach ( var key in dictionary . Keys )
158+ {
159+ dictKeys [ i ++ ] = key ;
160+ }
161+
162+ i = 0 ;
163+ foreach ( var value in dictionary . Values )
164+ {
165+ dictValues [ i ++ ] = CreateFromObjectLocked ( value ) ;
166+ }
167+
168+ return MValue . Create ( dictValues , dictKeys ) ;
169+ case IWritable writable :
170+ writer = new MValueWriter ( ) ;
171+ writable . OnWrite ( writer ) ;
172+ return writer . ToMValue ( ) ;
173+ case IMValueConvertible convertible :
174+ writer = new MValueWriter ( ) ;
175+ convertible . GetAdapter ( ) . ToMValue ( obj , writer ) ;
176+ return writer . ToMValue ( ) ;
177+ default :
178+ Alt . Log ( "can't convert type:" + obj . GetType ( ) ) ;
179+ return MValue . Nil ;
180+ }
181+ }
182+
183+ internal static MValue [ ] CreateFromObjectsLocked ( object [ ] objects )
184+ {
185+ var length = objects . Length ;
186+ var mValues = new MValue [ length ] ;
187+ for ( var i = 0 ; i < length ; i ++ )
188+ {
189+ mValues [ i ] = CreateFromObjectLocked ( objects [ i ] ) ;
190+ }
191+
192+ return mValues ;
193+ }
194+ }
195+ }
0 commit comments