public static int enumFromInt(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); Type t = translator.typeOf(luaState, 1); if (t == null || !t.IsEnum) { return(translator.pushError(luaState, "not an enum")); } object res = null; LuaTypes lt = LuaDLL.lua_type(luaState, 2); if (lt == LuaTypes.LUA_TNUMBER) { int ival = (int)LuaDLL.lua_tonumber(luaState, 2); res = Enum.ToObject(t, ival); } else if (lt == LuaTypes.LUA_TSTRING) { string sflags = LuaDLL.lua_tostring(luaState, 2); string err = null; try { res = Enum.Parse(t, sflags); } catch (ArgumentException e) { err = e.Message; } if (err != null) { return(translator.pushError(luaState, err)); } } else { return(translator.pushError(luaState, "second argument must be a integer or a string")); } translator.pushObject(luaState, res, "luaNet_metatable"); return(1); }
public static int ctype(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); Type t = translator.typeOf(luaState, 1); if (t == null) { return translator.pushError(luaState, "not a CLR class"); } translator.pushObject(luaState, t, "luaNet_metatable"); return 1; }
public static int enumFromInt(IntPtr luaState) { ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState); Type type = objectTranslator.typeOf(luaState, 1); if (type == null || !type.IsEnum) { return(objectTranslator.pushError(luaState, "not an enum")); } object o = null; LuaTypes luaTypes = LuaDLL.lua_type(luaState, 2); if (luaTypes == LuaTypes.LUA_TNUMBER) { int value = (int)LuaDLL.lua_tonumber(luaState, 2); o = Enum.ToObject(type, value); } else { if (luaTypes != LuaTypes.LUA_TSTRING) { return(objectTranslator.pushError(luaState, "second argument must be a integer or a string")); } string value2 = LuaDLL.lua_tostring(luaState, 2); string text = null; try { o = Enum.Parse(type, value2); } catch (ArgumentException ex) { text = ex.Message; } if (text != null) { return(objectTranslator.pushError(luaState, text)); } } objectTranslator.pushObject(luaState, o, "luaNet_metatable"); return(1); }
public static int ctype(IntPtr luaState) { ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState); Type type = objectTranslator.typeOf(luaState, 1); if (type == null) { return(objectTranslator.pushError(luaState, "not a CLR class")); } objectTranslator.pushObject(luaState, type, "luaNet_metatable"); return(1); }
public static int getMethod(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); object obj = translator.getRawNetObject(luaState, 1); if (obj == null) { translator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object index = translator.getObject(luaState, 2); //Type indexType = index.GetType(); //* not used string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name. // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object, // ie: xmlelement['item'] <- item is a property of xmlelement try { if (methodName != null && translator.metaFunctions.isMemberPresent(objType, methodName)) { return(translator.metaFunctions.getMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase)); } } catch { } bool failed = true; // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { int intIndex = (int)((double)index); Array aa = obj as Array; if (intIndex >= aa.Length) { return(translator.pushError(luaState, "array index out of bounds: " + intIndex + " " + aa.Length)); } object val = aa.GetValue(intIndex); translator.push(luaState, val); failed = false; } else { // Try to use get_Item to index into this .net object //MethodInfo getter = objType.GetMethod("get_Item"); // issue here is that there may be multiple indexers.. MethodInfo[] methods = objType.GetMethods(); foreach (MethodInfo mInfo in methods) { if (mInfo.Name == "get_Item") { //check if the signature matches the input if (mInfo.GetParameters().Length == 1) { MethodInfo getter = mInfo; ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null; if (actualParms == null || actualParms.Length != 1) { return(translator.pushError(luaState, "method not found (or no indexer): " + index)); } else { // Get the index in a form acceptable to the getter index = translator.getAsType(luaState, 2, actualParms[0].ParameterType); // Just call the indexer - if out of bounds an exception will happen try { object result = getter.Invoke(obj, new object[] { index }); translator.push(luaState, result); failed = false; } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { return(translator.pushError(luaState, "key '" + index + "' not found ")); } else { return(translator.pushError(luaState, "exception indexing '" + index + "' " + e.Message)); } } } } } } } if (failed) { return(translator.pushError(luaState, "cannot find " + index)); } LuaDLL.lua_pushboolean(luaState, false); return(2); }
public static int getMethod(IntPtr luaState) { ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState); object rawNetObject = objectTranslator.getRawNetObject(luaState, 1); if (rawNetObject == null) { objectTranslator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object obj = objectTranslator.getObject(luaState, 2); string text = obj as string; Type type = rawNetObject.GetType(); try { if (text != null && objectTranslator.metaFunctions.isMemberPresent(type, text)) { int result = objectTranslator.metaFunctions.getMember(luaState, type, rawNetObject, text, BindingFlags.IgnoreCase | BindingFlags.Instance); return(result); } } catch { } bool flag = true; if (type.IsArray && obj is double) { int num = (int)((double)obj); Array array = rawNetObject as Array; if (num >= array.Length) { return(objectTranslator.pushError(luaState, string.Concat(new object[] { "array index out of bounds: ", num, " ", array.Length }))); } object value = array.GetValue(num); objectTranslator.push(luaState, value); flag = false; } else { MethodInfo[] methods = type.GetMethods(); MethodInfo[] array2 = methods; for (int i = 0; i < array2.Length; i++) { MethodInfo methodInfo = array2[i]; if (methodInfo.Name == "get_Item" && methodInfo.GetParameters().Length == 1) { MethodInfo methodInfo2 = methodInfo; ParameterInfo[] array3 = (methodInfo2 == null) ? null : methodInfo2.GetParameters(); if (array3 == null || array3.Length != 1) { return(objectTranslator.pushError(luaState, "method not found (or no indexer): " + obj)); } obj = objectTranslator.getAsType(luaState, 2, array3[0].ParameterType); try { object o = methodInfo2.Invoke(rawNetObject, new object[] { obj }); objectTranslator.push(luaState, o); flag = false; } catch (TargetInvocationException ex) { int result; if (ex.InnerException is KeyNotFoundException) { result = objectTranslator.pushError(luaState, "key '" + obj + "' not found "); return(result); } result = objectTranslator.pushError(luaState, string.Concat(new object[] { "exception indexing '", obj, "' ", ex.Message })); return(result); } } } } if (flag) { return(objectTranslator.pushError(luaState, "cannot find " + obj)); } LuaDLL.lua_pushboolean(luaState, false); return(2); }