Ejemplo n.º 1
0
    private int CallLuaFuncInternal(IntPtr l, int refid, string funcname, int oldtop)
    {
#if UNITY_EDITOR
        if (!UnityEngine.Application.isPlaying)
        {
            return(lua.LUA_ERRERR);
        }
#endif
        l.checkstack(2);
        var pcnt = l.gettop() - oldtop;
        l.getref(refid);          // args(*pcnt), this
        l.GetField(-1, funcname); // args(*pcnt), this, func
        if (l.isfunction(-1) || l.istable(-1) || l.IsUserData(-1))
        {
            l.insert(oldtop + 1);                                      // func, args(*pcnt), this
            l.insert(oldtop + 2);                                      // func, this, args(*pcnt)
            l.pushcfunction(LuaHub.LuaFuncOnError);                    // func, this, args(*pcnt), err
            l.insert(oldtop + 1);                                      // err, func, this, args(*pcnt)
            var code = l.pcall(pcnt + 1, lua.LUA_MULTRET, oldtop + 1); // err, rv(*x)
            l.remove(oldtop + 1);                                      // rv(*x)
            if (code != 0)
            {
                DynamicHelper.LogError(l.GetLua(-1));
            }
            return(code);
        }
        return(lua.LUA_ERRERR);
    }
Ejemplo n.º 2
0
 public static object[] PushArgsAndCall(this IntPtr l, params object[] args)
 {
     if (l != IntPtr.Zero)
     {
         var      oldtop = l.gettop() - 1;
         var      code   = PushArgsAndCallRaw(l, args);
         var      newtop = l.gettop();
         object[] rv     = null;
         if (code == 0 && newtop >= oldtop)
         {
             rv = ObjectPool.GetReturnValueFromPool(newtop - oldtop);
             for (int i = 0; i < (newtop - oldtop); ++i)
             {
                 rv[i] = l.GetLua(i + oldtop + 1);
             }
         }
         if (code != 0)
         {
             DynamicHelper.LogError(l.GetLua(-1));
         }
         if (newtop >= oldtop)
         {
             l.pop(newtop - oldtop);
         }
         return(rv);
     }
     return(null);
 }
Ejemplo n.º 3
0
        public static int ClrFuncApkLoader(IntPtr l)
        {
            string mname = l.GetString(1);

            if (!string.IsNullOrEmpty(mname))
            {
                string           location;
                System.IO.Stream stream = null;
                GCHandle?        handle = null;
                try
                {
                    stream = CapsLuaFileManager.GetLuaStream(mname, out location);
                    if (stream != null)
                    {
                        if (_LuaStreamReader == null)
                        {
                            _LuaStreamReader = new CapsLuaFileManager.LuaStreamReader(null);
                        }
                        _LuaStreamReader.Reuse(stream, PlatDependant.CopyStreamBuffer);
                        handle   = GCHandle.Alloc(_LuaStreamReader);
                        location = string.Format("{0}<{1}>", mname, location);
                        if (l.load(CapsLuaFileManager.LuaStreamReader.ReaderDel, (IntPtr)handle.Value, location) == 0)
                        {
                            return(1);
                        }
                        else
                        {
                            DynamicHelper.LogError(l.GetLua(-1));
                            l.pop(1);
                            return(0);
                        }
                    }
                }
                catch (Exception e)
                {
                    l.LogError(e);
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                    if (handle != null)
                    {
                        handle.Value.Free();
                    }
                }
            }
            return(0);
        }
Ejemplo n.º 4
0
        private static int CallInternalSingleReturn(IntPtr l, int oldtop)
        {
            int pcnt = l.gettop() - oldtop;         // func, args(*pcnt)

            l.pushcfunction(LuaHub.LuaFuncOnError); // func, args(*pcnt), err
            l.insert(oldtop);                       // err, func, args(*pcnt)
            var code = l.pcall(pcnt, 1, oldtop);    // err, rv

            l.remove(oldtop);                       // rv
            if (code != 0)
            {
                DynamicHelper.LogError(l.GetLua(-1));
            }
            return(code);
        }
Ejemplo n.º 5
0
    public static BaseLua BindBehav(IntPtr l, CapsUnityLuaBehav behav, int index)
    {
        ExpandExFields(l, behav, index);
        using (var lr = new LuaStateRecover(l))
        {
            if (string.IsNullOrEmpty(behav.InitLuaPath))
            {
                l.pushvalue(index);
                var refid = l.refer();
                return(new BaseLua(l, refid));
            }

            int  oldtop      = lr.Top;
            bool luaFileDone = false;
            l.pushcfunction(LuaHub.LuaFuncOnError);
            l.GetGlobal("require");
            l.PushString(behav.InitLuaPath);
            if (l.pcall(1, 1, -3) == 0)
            {
                if (l.gettop() > oldtop + 1 && l.istable(oldtop + 2))
                {
                    luaFileDone = true;
                }
                else
                {
                    DynamicHelper.LogError("Failed to init script by require " + behav.InitLuaPath + ". (Not a table.) Now Init it by file.");
                }
            }
            else
            {
                DynamicHelper.LogError(l.GetLua(-1));
                DynamicHelper.LogInfo("Failed to init script by require " + behav.InitLuaPath + ". Now Init it by file.");
            }
            if (!luaFileDone)
            {
                DynamicHelper.LogInfo("Init it by file. - Disabled");
                //string path = behav.InitLuaPath;
                //if (path.EndsWith(".lua"))
                //{
                //    path = path.Substring(0, path.Length - 4);
                //}
                //path = path.Replace('.', '/');
                //path = path.Replace('\\', '/');
                //if (!path.StartsWith("spt/"))
                //{
                //    path = "spt/" + path;
                //}
                //path += ".lua";
                //path = ResManager.UpdatePath + "/" + path;

                //l.settop(oldtop);
                //if (l.dofile(path) == 0)
                //{
                //    if (l.gettop() > oldtop && l.istable(oldtop + 1))
                //    {
                //        luaFileDone = true;
                //    }
                //    else
                //    {
                //        DynamicHelper.LogInfo("Failed to load script " + path + ". (Not a table.)");
                //    }
                //}
                //else
                //{
                //    DynamicHelper.LogInfo(l.GetLua(-1).UnwrapDynamic());
                //    DynamicHelper.LogInfo("Failed to load script " + path);
                //}
            }
            if (luaFileDone)
            {
                l.GetField(oldtop + 2, "___bind_ex_to_self");
                if (!l.isnoneornil(-1))
                {
                    bool bindex;
                    l.GetLua(-1, out bindex);
                    if (bindex)
                    {
                        ExpandExFieldsToSelf(l, behav, oldtop);
                    }
                }
                l.pop(1);

                l.GetField(oldtop + 2, "attach");
                if (l.isfunction(-1))
                {
                    l.pushvalue(oldtop);
                    if (l.pcall(1, 0, oldtop + 1) == 0)
                    {
                    }
                    else
                    {
                        DynamicHelper.LogError(l.GetLua(-1));
                    }
                    l.pushvalue(oldtop);
                    var refid = l.refer();
                    return(new BaseLua(l, refid));
                }
            }
        }
        {
            l.pushvalue(index);
            var refid = l.refer();
            return(new BaseLua(l, refid));
        }
    }