2 using System.Collections.Generic;
4 using System.Reflection;
5 using MoonSharp.Interpreter;
6 using MoonSharp.Interpreter.Interop;
20 throw new ScriptRuntimeException($
"tried to register a type that doesn't exist: {typeName}.");
23 return UserData.RegisterType(type);
35 return UserData.GetDescriptorForType(type,
true) !=
null;
38 public static void UnregisterType(
string typeName,
bool deleteHistory =
false)
44 throw new ScriptRuntimeException($
"tried to unregister a type that doesn't exist: {typeName}.");
47 UserData.UnregisterType(type, deleteHistory);
49 public static IUserDataDescriptor
RegisterGenericType(
string typeName, params
string[] typeNameArguements)
52 Type[] typeArguements = typeNameArguements.Select(x =>
GetType(x)).ToArray();
53 Type genericType = type.MakeGenericType(typeArguements);
54 return UserData.RegisterType(genericType);
60 Type[] typeArguements = typeNameArguements.Select(x =>
GetType(x)).ToArray();
61 Type genericType = type.MakeGenericType(typeArguements);
62 UserData.UnregisterType(genericType);
67 if (obj ==
null) {
throw new ScriptRuntimeException(
"userdata is nil"); }
68 Type targetType =
GetType(typeName);
69 if (targetType ==
null) {
throw new ScriptRuntimeException(
"target type not found"); }
71 Type type = obj is Type ? (Type)obj : obj.GetType();
72 return targetType.IsAssignableFrom(type);
75 public static string TypeOf(
object obj)
77 if (obj ==
null) {
throw new ScriptRuntimeException(
"userdata is nil"); }
79 return obj.GetType().FullName;
88 throw new ScriptRuntimeException($
"tried to create a static userdata of a type that doesn't exist: {typeName}.");
91 MethodInfo method = typeof(UserData).GetMethod(nameof(UserData.CreateStatic), 1,
new Type[0]);
92 MethodInfo
generic = method.MakeGenericMethod(type);
93 return generic.Invoke(
null,
null);
102 throw new ScriptRuntimeException($
"tried to create an enum table with a type that doesn't exist:: {typeName}.");
105 Dictionary<string, object> result =
new Dictionary<string, object>();
107 foreach (var value
in Enum.GetValues(type))
109 string name = Enum.GetName(type, value);
111 result[name] = value;
117 private static FieldInfo FindFieldRecursively(Type type,
string fieldName)
119 var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
121 if (field ==
null && type.BaseType !=
null)
123 return FindFieldRecursively(type.BaseType, fieldName);
133 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to make {fieldName} accessible.");
136 var descriptor = (StandardUserDataDescriptor)IUUD;
137 FieldInfo field = FindFieldRecursively(IUUD.Type, fieldName);
141 throw new ScriptRuntimeException($
"tried to make field '{fieldName}' accessible, but the field doesn't exist.");
144 descriptor.RemoveMember(fieldName);
145 descriptor.AddMember(fieldName,
new FieldMemberDescriptor(field, InteropAccessMode.Default));
148 private static MethodInfo FindMethodRecursively(Type type,
string methodName, Type[] types =
null)
154 method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
158 method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, types);
161 if (method ==
null && type.BaseType !=
null)
163 return FindMethodRecursively(type.BaseType, methodName, types);
169 public static void MakeMethodAccessible(IUserDataDescriptor IUUD,
string methodName,
string[] parameters =
null)
173 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to make {methodName} accessible.");
176 Type[] parameterTypes =
null;
179 if (parameters !=
null)
181 parameterTypes =
new Type[parameters.Length];
183 for (
int i = 0; i < parameters.Length; i++)
188 throw new ScriptRuntimeException($
"invalid parameter type '{parameters[i]}'");
190 parameterTypes[i] = type;
194 var descriptor = (StandardUserDataDescriptor)IUUD;
200 method = FindMethodRecursively(IUUD.Type, methodName, parameterTypes);
202 catch (AmbiguousMatchException ex)
204 throw new ScriptRuntimeException(
"ambiguous method signature.");
209 throw new ScriptRuntimeException($
"tried to make method '{methodName}' accessible, but the method doesn't exist.");
212 descriptor.AddMember(methodName,
new MethodMemberDescriptor(method, InteropAccessMode.Default));
215 private static PropertyInfo FindPropertyRecursively(Type type,
string propertyName)
217 var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
219 if (property ==
null && type.BaseType !=
null)
221 return FindPropertyRecursively(type.BaseType, propertyName);
231 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to make {propertyName} accessible.");
234 var descriptor = (StandardUserDataDescriptor)IUUD;
235 PropertyInfo property = FindPropertyRecursively(IUUD.Type, propertyName);
237 if (property ==
null)
239 throw new ScriptRuntimeException($
"tried to make property '{propertyName}' accessible, but the property doesn't exist.");
242 descriptor.RemoveMember(propertyName);
243 descriptor.AddMember(propertyName,
new PropertyMemberDescriptor(property, InteropAccessMode.Default, property.GetGetMethod(
true), property.GetSetMethod(
true)));
246 public static void AddMethod(IUserDataDescriptor IUUD,
string methodName,
object function)
250 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to add method {methodName}.");
253 var descriptor = (StandardUserDataDescriptor)IUUD;
255 descriptor.RemoveMember(methodName);
256 descriptor.AddMember(methodName,
new ObjectCallbackMemberDescriptor(methodName, (
object arg1, ScriptExecutionContext arg2, CallbackArguments arg3) =>
264 public static void AddField(IUserDataDescriptor IUUD,
string fieldName, DynValue value)
268 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to add field {fieldName}.");
271 var descriptor = (StandardUserDataDescriptor)IUUD;
272 descriptor.RemoveMember(fieldName);
273 descriptor.AddMember(fieldName,
new DynValueMemberDescriptor(fieldName, value));
276 public static void RemoveMember(IUserDataDescriptor IUUD,
string memberName)
280 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to remove the member {memberName}.");
283 var descriptor = (StandardUserDataDescriptor)IUUD;
284 descriptor.RemoveMember(memberName);
287 public static bool HasMember(
object obj,
string memberName)
289 if (obj ==
null) {
throw new ScriptRuntimeException(
"object is nil"); }
296 else if(obj is IUserDataDescriptor descriptor)
298 type = descriptor.Type;
300 if (((StandardUserDataDescriptor)descriptor).HasMember(memberName))
307 type = obj.GetType();
310 if (type.GetMember(memberName).Length == 0)
327 return UserData.Create(scriptObject.ToObject(desiredTypeDescriptor.Type), desiredTypeDescriptor);
346 IUserDataDescriptor descriptor = UserData.GetDescriptorForType(desiredType,
true);
347 descriptor ??=
new StandardUserDataDescriptor(desiredType, InteropAccessMode.Default);
static Type GetType(string typeName, bool throwOnError=false, bool ignoreCase=false)
DynValue CallLuaFunction(object function, params object[] args)
static void UnregisterType(string typeName, bool deleteHistory=false)
static bool IsRegistered(string typeName)
static void AddField(IUserDataDescriptor IUUD, string fieldName, DynValue value)
static void UnregisterGenericType(string typeName, params string[] typeNameArguements)
static void AddMethod(IUserDataDescriptor IUUD, string methodName, object function)
static Type GetType(string typeName)
static object CreateEnumTable(string typeName)
static object CreateStatic(string typeName)
static bool IsTargetType(object obj, string typeName)
static void MakePropertyAccessible(IUserDataDescriptor IUUD, string propertyName)
static DynValue CreateUserDataFromDescriptor(DynValue scriptObject, IUserDataDescriptor desiredTypeDescriptor)
See CreateUserDataFromType.
static void MakeFieldAccessible(IUserDataDescriptor IUUD, string fieldName)
static IUserDataDescriptor RegisterType(string typeName)
static DynValue CreateUserDataFromType(DynValue scriptObject, Type desiredType)
Converts a Lua value to a CLR object of a desired type and wraps it in a userdata....
static bool HasMember(object obj, string memberName)
static string TypeOf(object obj)
static void MakeMethodAccessible(IUserDataDescriptor IUUD, string methodName, string[] parameters=null)
static void RemoveMember(IUserDataDescriptor IUUD, string memberName)
static IUserDataDescriptor RegisterGenericType(string typeName, params string[] typeNameArguements)