Ejemplo n.º 1
0
        internal static int dofile(IntPtr L)
        {
            int n = LuaDLL.lua_gettop(L);

            loader(L);
            if (!LuaDLL.lua_toboolean(L, -2))
            {
                return(2);
            }
            else
            {
                if (LuaDLL.lua_isnil(L, -1))
                {
                    string fileName = LuaDLL.lua_tostring(L, 1);
                    return(LuaObject.error(L, "Can't find {0}", fileName));
                }
                int k = LuaDLL.lua_gettop(L);
                LuaDLL.lua_call(L, 0, LuaDLL.LUA_MULTRET);
                k = LuaDLL.lua_gettop(L);
                return(k - n);
            }
        }
Ejemplo n.º 2
0
        public static int DeleteAll(IntPtr l)
        {
            if (mapSnTimer == null)
            {
                return(0);
            }
            try
            {
                foreach (var t in mapSnTimer)
                {
                    innerDel(t.Value, false);
                }
                mapSnTimer.Clear();

                pushValue(l, true);
                return(1);
            }
            catch (Exception e)
            {
                return(LuaObject.error(l, e));
            }
        }
Ejemplo n.º 3
0
        internal static int loader(IntPtr L)
        {
            string fileName = LuaDLL.lua_tostring(L, 1);

            byte[] bytes = loadFile(fileName);
            if (bytes != null)
            {
                if (LuaDLL.luaL_loadbuffer(L, bytes, bytes.Length, "@" + fileName) == 0)
                {
                    LuaObject.pushValue(L, true);
                    LuaDLL.lua_insert(L, -2);
                    return(2);
                }
                else
                {
                    string errstr = LuaDLL.lua_tostring(L, -1);
                    return(LuaObject.error(L, errstr));
                }
            }
            LuaObject.pushValue(L, true);
            LuaDLL.lua_pushnil(L);
            return(2);
        }
Ejemplo n.º 4
0
        public static int Add(IntPtr l)
        {
            try{
                int top = LuaDLL.lua_gettop(l);
                if (top == 2)
                {
                    int delay;
                    checkType(l, 1, out delay);
                    LuaDelegate ld;
                    checkType(l, 2, out ld);
                    Action <int> ua;
                    if (ld.d != null)
                    {
                        ua = (Action <int>)ld.d;
                    }
                    else
                    {
                        IntPtr ml = LuaState.get(l).L;
                        ua = (int id) =>
                        {
                            int error = pushTry(ml);
                            pushValue(ml, id);
                            ld.pcall(1, error);
                            LuaDLL.lua_settop(ml, error - 1);
                        };
                    }
                    ld.d = ua;
                    pushValue(l, true);
                    pushValue(l, add(l, delay, ua));
                    return(2);
                }
                else if (top == 3)
                {
                    int delay, cycle;
                    checkType(l, 1, out delay);
                    checkType(l, 2, out cycle);
                    LuaDelegate ld;
                    checkType(l, 3, out ld);
                    Func <int, bool> ua;

                    if (ld.d != null)
                    {
                        ua = (Func <int, bool>)ld.d;
                    }
                    else
                    {
                        IntPtr ml = LuaState.get(l).L;
                        ua = (int id) =>
                        {
                            int error = pushTry(ml);
                            pushValue(ml, id);
                            ld.pcall(1, error);
                            bool ret = LuaDLL.lua_toboolean(ml, -1);
                            LuaDLL.lua_settop(ml, error - 1);
                            return(ret);
                        };
                    }
                    ld.d = ua;
                    pushValue(l, true);
                    pushValue(l, add(l, delay, cycle, ua));
                    return(2);
                }
                return(LuaObject.error(l, "Argument error"));
            }catch (Exception e)
            {
                return(LuaObject.error(l, e));
            }
        }
Ejemplo n.º 5
0
        public static int UserdataToLuaTab(IntPtr l)
        {
            try
            {
                if (LuaDLL.lua_type(l, 1) == LuaTypes.LUA_TUSERDATA)
                {
                    object o  = LuaObject.checkObj(l, 1);
                    Type   ot = o.GetType();

                    if (ot.IsArray)
                    {
                        pushValue(l, true);
                        LuaDLL.lua_newtable(l); //...,true, tab

                        Array tmp = o as Array;

                        for (int i = 0; i < tmp.Length; ++i)
                        {
                            LuaDLL.lua_pushinteger(l, i + 1);      //...,true, tab, index
                            LuaObject.pushVar(l, tmp.GetValue(i)); //...,true, tab, index, value
                            LuaDLL.lua_rawset(l, -3);              //..., true, tab
                        }

                        return(2);
                    }

                    bool isList = ot.IsGenericType && ot.GetGenericTypeDefinition() == typeof(List <>);

                    if (isList)
                    {
                        pushValue(l, true);
                        LuaDLL.lua_newtable(l); //...,true, tab

                        IList tmp = o as IList;

                        for (int i = 0; i < tmp.Count; ++i)
                        {
                            LuaDLL.lua_pushinteger(l, i + 1); //...,true, tab, index
                            LuaObject.pushVar(l, tmp[i]);     //...,true, tab, index, value
                            LuaDLL.lua_rawset(l, -3);         //..., true, tab
                        }

                        return(2);
                    }

                    bool isDict = ot.IsGenericType && ot.GetGenericTypeDefinition() == typeof(Dictionary <,>);

                    if (isDict)
                    {
                        pushValue(l, true);
                        LuaDLL.lua_newtable(l); //...,true, tab
                        IDictionary dict = o as IDictionary;

                        foreach (object key in dict.Keys)
                        {
                            object value = dict[key];
                            LuaObject.pushVar(l, key);   //...,true, tab, index
                            LuaObject.pushVar(l, value); //...,true, tab, index, value
                            LuaDLL.lua_rawset(l, -3);    //..., true, tab
                        }

                        return(2);
                    }
                }

                pushValue(l, false);
                LuaDLL.lua_pushnil(l);
                return(2);
            }
            catch (Exception e)
            {
                return(LuaObject.error(l, e));
            }
        }