-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUtil.cs
executable file
·350 lines (288 loc) · 11 KB
/
Util.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Globalization;
using EVE.ISXEVE.Extensions;
using LavishScriptAPI;
using LavishScriptAPI.Interfaces;
namespace EVE.ISXEVE
{
/// <summary>
/// CallbackDelegate used in trace logging.
/// </summary>
/// <param name="method"></param>
/// <param name="args"></param>
public delegate void CallbackDelegate(string method, string args);
/// <summary>
/// Class used for trace logging.
/// </summary>
public class Tracing
{
/// <summary>
/// Callback to use for logging.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")]
public static CallbackDelegate Callback;
/// <summary>
/// Add a callback delegate for trace logging.
/// </summary>
/// <param name="callbackDelegate"></param>
public static void AddCallback(CallbackDelegate callbackDelegate)
{
Callback = callbackDelegate;
}
/// <summary>
/// Clear the callback delegate.
/// </summary>
public static void RemoveCallback()
{
Callback = null;
}
/// <summary>
/// Use the callback delegate to send a log message.
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public static void SendCallback(string message, params object[] args)
{
if (Callback == null) return;
var argString = new StringBuilder();
if (args.Length > 0)
{
argString.Append(args[0].ToString());
}
for (int idx = 1; idx < args.Length; idx++)
{
argString.Append(String.Format(CultureInfo.InvariantCulture, ", {0}", args[idx]));
}
Callback(message, argString.ToString());
}
}
public static class Util
{
private static readonly Dictionary<Type, Type> _implementingTypesByInterfaceType = new Dictionary<Type, Type>();
private static readonly Dictionary<Type, ConstructorInfo> _constructorInfoByType = new Dictionary<Type, ConstructorInfo>();
private static T[] PrefixArray<T>(T first, T[] rest)
{
var newArray = new T[rest.Length + 1];
newArray[0] = first;
for (var i = 0; i < rest.Length; i++)
newArray[i + 1] = rest[i];
return newArray;
}
private static Type GetImplementingTypeForInterfaceType(Type interfaceType)
{
if (!_implementingTypesByInterfaceType.ContainsKey(interfaceType))
{
var implementingType = interfaceType.Assembly.GetTypes()
.Where(t => t.IsClass)
.FirstOrDefault(interfaceType.IsAssignableFrom);
if (implementingType == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not find implementing type for interface type {0}.", interfaceType.Name));
}
_implementingTypesByInterfaceType.Add(interfaceType, implementingType);
}
return _implementingTypesByInterfaceType[interfaceType];
}
private static ConstructorInfo GetConstructorInfoForType(Type type)
{
if (!_constructorInfoByType.ContainsKey(type))
{
ConstructorInfo constructorInfo;
if (type.IsInterface)
{
var implementingType = GetImplementingTypeForInterfaceType(type);
constructorInfo = implementingType.GetConstructor(new[] { typeof(LavishScriptObject) });
}
else
{
constructorInfo = type.GetConstructor(new[] { typeof(LavishScriptObject) });
}
if (constructorInfo == null)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not find a constructor for type {0}.", type.Name));
_constructorInfoByType.Add(type, constructorInfo);
}
return _constructorInfoByType[type];
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1820:TestForEmptyStringsUsingStringLength")]
private static List<T> IndexToLavishScriptObjectList<T>(LavishScriptObject index, string lsTypeName)
{
//string methodName = "IndexToLSOList";
//Tracing.SendCallback(methodName, LSTypeName);
//Tracing.SendCallback(methodName, "getmember Used");
var list = new List<T>();
var count = index.GetMember<int>("Used");
if (count == 0)
{
return list;
}
//Tracing.SendCallback(methodName, "get constructor info");
var constructorInfo = GetConstructorInfoForType(typeof(T));
//Tracing.SendCallback(methodName, "loop add items");
for (var i = 1; i <= count; i++)
{
var objectLso = index.GetIndex(i.ToString(CultureInfo.CurrentCulture));
if (LavishScriptObject.IsNullOrInvalid(objectLso))
{
Tracing.SendCallback(String.Format(CultureInfo.InvariantCulture, "Error: Index contains invalid LSO. NewObject will fail; aborting."));
return list;
}
var objectId = objectLso.GetString("ID");
if (objectId == null)
{
Tracing.SendCallback(String.Format(CultureInfo.InvariantCulture, "Error: LStype \"{0}\" has no ID member. NewObject will fail; aborting.", lsTypeName));
return list;
}
if (objectId == string.Empty)
{
Tracing.SendCallback(String.Format(CultureInfo.InvariantCulture, "Error: LStype \"{0}\" has an ID member but it is returning an empty string. NewObject will fail; aborting.", lsTypeName));
return list;
}
var lsObject = LavishScript.Objects.NewObject(lsTypeName, objectId);
var item = (T)constructorInfo.Invoke(new object[] { lsObject });
list.Add(item);
}
return list;
}
private static List<T> IndexToStructList<T>(LavishScriptObject index)
{
var list = new List<T>();
var count = index.GetMember<int>("Used");
for (var i = 1; i <= count; i++)
list.Add(index.GetIndex<T>(i.ToString(CultureInfo.CurrentCulture)));
return list;
}
public static List<T> IndexToList<T>(LavishScriptObject index, string lsTypeName)
{
//Tracing.SendCallback("IndextoList", LSTypeName);
//return typeof(T).Is(typeof(ILSObject)) ? IndexToLavishScriptObjectList<T>(index, lsTypeName) : IndexToStructList<T>(index);
return (typeof(ILSObject)).IsAssignableFrom(typeof(T)) ? IndexToLavishScriptObjectList<T>(index, lsTypeName) : IndexToStructList<T>(index);
}
public static T GetIndexMember<T>(LavishScriptObject index, int number)
{
if (typeof(T).IsSubclassOf(typeof(LavishScriptObject)))
return (T)typeof(T).GetConstructor(new[] { typeof(LavishScriptObject) }).Invoke(new object[] { index.GetIndex(number.ToString(CultureInfo.CurrentCulture)) });
return index.GetIndex<T>(number.ToString(CultureInfo.CurrentCulture));
}
/// <summary>
/// Translate an index of a given LavishScript type returned from the given method on the given object to a List of a .NET datatype equivalent.
/// </summary>
/// <param name="obj"></param>
/// <param name="methodName"></param>
/// <param name="lsTypeName"></param>
/// <param name="args"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<T> GetListFromMethod<T>(this ILSObject obj, string methodName, string lsTypeName, params string[] args)
{
//string methodName = "GetListFromMethod";
//Tracing.SendCallback(methodName, MethodName, LSTypeName);
if (obj == null || !obj.IsValid)
return null;
//Tracing.SendCallback(methodName, "Create new LSO of index. Type: ", LSTypeName);
using (var index = LavishScript.Objects.NewObject("index:" + lsTypeName))
{
//Tracing.SendCallback(methodName, "Collapsing Args[]");
string[] allargs;
if (args.Length > 0)
{
allargs = PrefixArray(index.LSReference, args);
}
else
{
allargs = new string[1];
allargs[0] = index.LSReference;
}
//Tracing.SendCallback(methodName, "Execute method, name: ", MethodName);
if (!obj.ExecuteMethod(methodName, allargs))
return null;
//Tracing.SendCallback(methodName, "Get member Used");
using (var used = index.GetMember("Used"))
{
//Tracing.SendCallback(methodName, "LSO.IsNullOrInvalid (used)");
if (LavishScriptObject.IsNullOrInvalid(used))
return null;
}
//Tracing.SendCallback(methodName, "IndexToList");
var list = IndexToList<T>(index, lsTypeName);
//Tracing.SendCallback(methodName, "Returning");
return list;
}
}
internal static T GetFromIndexMethod<T>(ILSObject obj, string methodName, string lsTypeName, int number, params string[] args)
{
// argument is 0-based
number += 1;
if (obj == null || !obj.IsValid || number <= 0)
return default;
using (var index = LavishScript.Objects.NewObject("index:" + lsTypeName))
{
var allargs = PrefixArray(index.LSReference, args);
if (!obj.ExecuteMethod(methodName, allargs))
return default;
using (var used = index.GetMember("Used"))
{
// if it failed or we want one off the end, return
if (LavishScriptObject.IsNullOrInvalid(used) || used.GetValue<int>() < number)
return default;
}
var member = GetIndexMember<T>(index, number);
return member;
}
}
/// <summary>
/// Translate an index of a given LavishScript type returned from the given member on the given object to a List of a .NET datatype equivalent.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="memberName"></param>
/// <param name="lsTypeName"></param>
/// <param name="args"></param>
/// <returns></returns>
public static List<T> GetListFromMember<T>(this ILSObject obj, string memberName, string lsTypeName, params string[] args)
{
//var methodName = "GetListFromMember";
//Tracing.SendCallback(methodName, MemberName, LSTypeName);
if (obj == null || !obj.IsValid)
return null;
//Tracing.SendCallback(methodName, "new index");
using (var index = LavishScript.Objects.NewObject("index:" + lsTypeName))
{
//Tracing.SendCallback(methodName, "arg condensing");
var allargs = PrefixArray(index.LSReference, args);
//Tracing.SendCallback(methodName, "getmember retval");
using (var retval = obj.GetMember(memberName, allargs))
{
if (LavishScriptObject.IsNullOrInvalid(retval))
return null;
}
//Tracing.SendCallback(methodName, "index to list");
var list = IndexToList<T>(index, lsTypeName);
//Tracing.SendCallback(methodName, "invalidate");
//Tracing.SendCallback(methodName, "return");
return list;
}
}
internal static T GetFromIndexMember<T>(ILSObject obj, string memberName, string lsTypeName, int number, params string[] args)
{
// argument is 0-based
number += 1;
if (obj == null || !obj.IsValid)
return default;
using (var index = LavishScript.Objects.NewObject("index:" + lsTypeName))
{
var allargs = PrefixArray(index.LSReference, args);
using (var retval = obj.GetMember(memberName, allargs))
{
if (LavishScriptObject.IsNullOrInvalid(retval) || retval.GetValue<int>() < number)
return default;
}
var member = GetIndexMember<T>(index, number);
return member;
}
}
}
}