Exemplo n.º 1
0
        public static int loadAssembly(IntPtr luaState)
        {
            ObjectTranslator translator = ObjectTranslator.FromState(luaState);

            try
            {
                string assemblyName = LuaDLL.lua_tostring(luaState, 1);

                Assembly assembly = null;

                //assembly = Assembly.GetExecutingAssembly();

                try
                {
                    assembly = Assembly.Load(assemblyName);
                }
                catch (BadImageFormatException)
                {
                    // The assemblyName was invalid.  It is most likely a path.
                }

                if (assembly == null)
                {
                    assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyName));
                }

                if (assembly != null && !translator.assemblies.Contains(assembly))
                {
                    translator.assemblies.Add(assembly);
                }
            }
            catch (Exception e)
            {
                translator.throwError(luaState, e);
            }

            return(0);
        }
Exemplo n.º 2
0
        public static int unregisterTable(IntPtr luaState)
        {
            ObjectTranslator translator = ObjectTranslator.FromState(luaState);

            try
            {
                if (LuaDLL.lua_getmetatable(luaState, 1) != 0)
                {
                    LuaDLL.lua_pushstring(luaState, "__index");
                    LuaDLL.lua_gettable(luaState, -2);
                    object obj = translator.getRawNetObject(luaState, -1);
                    if (obj == null)
                    {
                        translator.throwError(luaState, "unregister_table: arg is not valid table");
                    }
                    FieldInfo luaTableField = obj.GetType().GetField("__luaInterface_luaTable");
                    if (luaTableField == null)
                    {
                        translator.throwError(luaState, "unregister_table: arg is not valid table");
                    }
                    luaTableField.SetValue(obj, null);
                    LuaDLL.lua_pushnil(luaState);
                    LuaDLL.lua_setmetatable(luaState, 1);
                    LuaDLL.lua_pushstring(luaState, "base");
                    LuaDLL.lua_pushnil(luaState);
                    LuaDLL.lua_settable(luaState, 1);
                }
                else
                {
                    translator.throwError(luaState, "unregister_table: arg is not valid table");
                }
            }
            catch (Exception e)
            {
                translator.throwError(luaState, e.Message);
            }
            return(0);
        }
Exemplo n.º 3
0
        public static int getClassMethod(IntPtr luaState)
        {
            ObjectTranslator translator = ObjectTranslator.FromState(luaState);
            IReflect         klass;
            object           obj = translator.getRawNetObject(luaState, 1);

            if (obj == null || !(obj is IReflect))
            {
                translator.throwError(luaState, "trying to index an invalid type reference");
                LuaDLL.lua_pushnil(luaState);
                return(1);
            }
            else
            {
                klass = (IReflect)obj;
            }
            if (LuaDLL.lua_isnumber(luaState, 2))
            {
                int size = (int)LuaDLL.lua_tonumber(luaState, 2);
                translator.push(luaState, Array.CreateInstance(klass.UnderlyingSystemType, size));
                return(1);
            }
            else
            {
                string methodName = LuaDLL.lua_tostring(luaState, 2);
                if (methodName == null)
                {
                    LuaDLL.lua_pushnil(luaState);
                    return(1);
                }                 //CP: Ignore case
                else
                {
                    return(translator.metaFunctions.getMember(luaState, klass, null, methodName, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.IgnoreCase));
                }
            }
        }
Exemplo n.º 4
0
        public static int setFieldOrProperty(IntPtr luaState)
        {
            ObjectTranslator translator = ObjectTranslator.FromState(luaState);
            object           target     = translator.getRawNetObject(luaState, 1);

            if (target == null)
            {
                translator.throwError(luaState, "trying to index and invalid object reference");
                return(0);
            }
            Type type = target.GetType();

            // First try to look up the parameter as a property name
            string detailMessage;
            bool   didMember = translator.metaFunctions.trySetMember(luaState, type, target, BindingFlags.Instance | BindingFlags.IgnoreCase, out detailMessage);

            if (didMember)
            {
                return(0);                      // Must have found the property name
            }
            // We didn't find a property name, now see if we can use a [] style this accessor to set array contents
            try
            {
                if (type.IsArray && LuaDLL.lua_isnumber(luaState, 2))
                {
                    int index = (int)LuaDLL.lua_tonumber(luaState, 2);

                    Array  arr = (Array)target;
                    object val = translator.getAsType(luaState, 3, arr.GetType().GetElementType());
                    arr.SetValue(val, index);
                }
                else
                {
                    // Try to see if we have a this[] accessor
                    MethodInfo setter = type.GetMethod("set_Item");
                    if (setter != null)
                    {
                        ParameterInfo[] args      = setter.GetParameters();
                        Type            valueType = args[1].ParameterType;

                        // The new val ue the user specified
                        object val = translator.getAsType(luaState, 3, valueType);

                        Type   indexType = args[0].ParameterType;
                        object index     = translator.getAsType(luaState, 2, indexType);

                        object[] methodArgs = new object[2];

                        // Just call the indexer - if out of bounds an exception will happen
                        methodArgs[0] = index;
                        methodArgs[1] = val;

                        setter.Invoke(target, methodArgs);
                    }
                    else
                    {
                        translator.throwError(luaState, detailMessage);                         // Pass the original message from trySetMember because it is probably best
                    }
                }
            }
            catch (SEHException)
            {
                // If we are seeing a C++ exception - this must actually be for Lua's private use.  Let it handle it
                throw;
            }
            catch (Exception e)
            {
                translator.metaFunctions.ThrowError(luaState, e);
            }
            return(0);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 public LuaTable(int reference, IntPtr L)
 {
     _Reference   = reference;
     translator   = ObjectTranslator.FromState(L);
     _Interpreter = translator.interpreter;
 }
Exemplo n.º 7
0
        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);
        }