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);
32 throw new ScriptRuntimeException($
"tried to register a type that doesn't exist: {typeName}.");
35 UserData.RegisterExtensionType(type);
47 return UserData.GetDescriptorForType(type,
true) !=
null;
50 public static void UnregisterType(
string typeName,
bool deleteHistory =
false)
56 throw new ScriptRuntimeException($
"tried to unregister a type that doesn't exist: {typeName}.");
59 UserData.UnregisterType(type, deleteHistory);
61 public static IUserDataDescriptor
RegisterGenericType(
string typeName, params
string[] typeNameArguements)
64 Type[] typeArguements = typeNameArguements.Select(x =>
GetType(x)).ToArray();
65 Type genericType = type.MakeGenericType(typeArguements);
66 return UserData.RegisterType(genericType);
72 Type[] typeArguements = typeNameArguements.Select(x =>
GetType(x)).ToArray();
73 Type genericType = type.MakeGenericType(typeArguements);
74 UserData.UnregisterType(genericType);
79 if (obj ==
null) {
throw new ScriptRuntimeException(
"userdata is nil"); }
80 Type targetType =
GetType(typeName);
81 if (targetType ==
null) {
throw new ScriptRuntimeException(
"target type not found"); }
83 Type type = obj is Type ? (Type)obj : obj.GetType();
84 return targetType.IsAssignableFrom(type);
87 public static string TypeOf(
object obj)
89 if (obj ==
null) {
throw new ScriptRuntimeException(
"userdata is nil"); }
91 return obj.GetType().FullName;
100 throw new ScriptRuntimeException($
"tried to create a static userdata of a type that doesn't exist: {typeName}.");
103 MethodInfo method = typeof(UserData).GetMethod(nameof(UserData.CreateStatic), 1,
new Type[0]);
104 MethodInfo
generic = method.MakeGenericMethod(type);
105 return generic.Invoke(
null,
null);
114 throw new ScriptRuntimeException($
"tried to create an enum table with a type that doesn't exist:: {typeName}.");
117 Dictionary<string, object> result =
new Dictionary<string, object>();
119 foreach (var value
in Enum.GetValues(type))
121 string name = Enum.GetName(type, value);
123 result[name] = value;
129 private static FieldInfo FindFieldRecursively(Type type,
string fieldName)
131 var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
133 if (field ==
null && type.BaseType !=
null)
135 return FindFieldRecursively(type.BaseType, fieldName);
145 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to make {fieldName} accessible.");
148 var descriptor = (StandardUserDataDescriptor)IUUD;
149 FieldInfo field = FindFieldRecursively(IUUD.Type, fieldName);
153 throw new ScriptRuntimeException($
"tried to make field '{fieldName}' accessible, but the field doesn't exist.");
156 descriptor.RemoveMember(fieldName);
157 descriptor.AddMember(fieldName,
new FieldMemberDescriptor(field, InteropAccessMode.Default));
160 private static MethodInfo FindMethodRecursively(Type type,
string methodName, Type[] types =
null)
166 method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
170 method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, types);
173 if (method ==
null && type.BaseType !=
null)
175 return FindMethodRecursively(type.BaseType, methodName, types);
181 public static void MakeMethodAccessible(IUserDataDescriptor IUUD,
string methodName,
string[] parameters =
null)
185 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to make {methodName} accessible.");
188 Type[] parameterTypes =
null;
191 if (parameters !=
null)
193 parameterTypes =
new Type[parameters.Length];
195 for (
int i = 0; i < parameters.Length; i++)
200 throw new ScriptRuntimeException($
"invalid parameter type '{parameters[i]}'");
202 parameterTypes[i] = type;
206 var descriptor = (StandardUserDataDescriptor)IUUD;
212 method = FindMethodRecursively(IUUD.Type, methodName, parameterTypes);
214 catch (AmbiguousMatchException ex)
216 throw new ScriptRuntimeException(
"ambiguous method signature.");
221 throw new ScriptRuntimeException($
"tried to make method '{methodName}' accessible, but the method doesn't exist.");
224 descriptor.AddMember(methodName,
new MethodMemberDescriptor(method, InteropAccessMode.Default));
227 private static PropertyInfo FindPropertyRecursively(Type type,
string propertyName)
229 var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
231 if (property ==
null && type.BaseType !=
null)
233 return FindPropertyRecursively(type.BaseType, propertyName);
243 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to make {propertyName} accessible.");
246 var descriptor = (StandardUserDataDescriptor)IUUD;
247 PropertyInfo property = FindPropertyRecursively(IUUD.Type, propertyName);
249 if (property ==
null)
251 throw new ScriptRuntimeException($
"tried to make property '{propertyName}' accessible, but the property doesn't exist.");
254 descriptor.RemoveMember(propertyName);
255 descriptor.AddMember(propertyName,
new PropertyMemberDescriptor(property, InteropAccessMode.Default, property.GetGetMethod(
true), property.GetSetMethod(
true)));
258 public static void AddMethod(IUserDataDescriptor IUUD,
string methodName,
object function)
262 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to add method {methodName}.");
265 var descriptor = (StandardUserDataDescriptor)IUUD;
267 descriptor.RemoveMember(methodName);
268 descriptor.AddMember(methodName,
new ObjectCallbackMemberDescriptor(methodName, (
object arg1, ScriptExecutionContext arg2, CallbackArguments arg3) =>
276 public static void AddField(IUserDataDescriptor IUUD,
string fieldName, DynValue value)
280 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to add field {fieldName}.");
283 var descriptor = (StandardUserDataDescriptor)IUUD;
284 descriptor.RemoveMember(fieldName);
285 descriptor.AddMember(fieldName,
new DynValueMemberDescriptor(fieldName, value));
288 public static void RemoveMember(IUserDataDescriptor IUUD,
string memberName)
292 throw new ScriptRuntimeException($
"tried to use a UserDataDescriptor that is null to remove the member {memberName}.");
295 var descriptor = (StandardUserDataDescriptor)IUUD;
296 descriptor.RemoveMember(memberName);
299 public static bool HasMember(
object obj,
string memberName)
301 if (obj ==
null) {
throw new ScriptRuntimeException(
"object is nil"); }
308 else if(obj is IUserDataDescriptor descriptor)
310 type = descriptor.Type;
312 if (((StandardUserDataDescriptor)descriptor).HasMember(memberName))
319 type = obj.GetType();
322 if (type.GetMember(memberName).Length == 0)
339 return UserData.Create(scriptObject.ToObject(desiredTypeDescriptor.Type), desiredTypeDescriptor);
358 IUserDataDescriptor descriptor = UserData.GetDescriptorForType(desiredType,
true);
359 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 void RegisterExtensionType(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)