Example #1
0
        public void Dispose(bool destoryService = true)
        {
            if (L == null)
            {
                return;
            }

            if (_DispatchEvent != null)
            {
                _DispatchEvent("UNITY_OnApplicationQuit");
            }

            _Sync = null;
            _RegisterEventListener     = null;
            _RemoveEventListener       = null;
            _DispatchEvent             = null;
            _StartLuaCoroutineDelegate = null;

#if UNITY_EDITOR
            LL = L;
#else
            Debug.LogFormat("GC");
            L.GC();
            System.GC.Collect();

            L.Dispose();
            L = null;
#endif
        }
Example #2
0
        public void Dispose(bool destoryService = true)
        {
            if (L == null)
            {
                return;
            }

            if (_DispatchEvent != null)
            {
                _DispatchEvent("UNITY_OnApplicationQuit");
            }

            _RegisterEventListener     = null;
            _RemoveEventListener       = null;
            _DispatchEvent             = null;;
            _StartLuaCoroutineDelegate = null;

            //if (destoryService)
            //{
            //    IService[] services = gameObject.GetComponents<IService>();
            //    for (int i = 0; i < services.Length; i++)
            //    {
            //        DestroyImmediate(services[i] as MonoBehaviour);
            //    }
            //}

            L.GC();

            System.GC.Collect();

            L.Dispose();
            L = null;
        }
Example #3
0
 private void Update()
 {
     if (Time.time - LuaManager.lastGCTime > GCInterval)
     {
         luaEnv.GC();
         LuaManager.lastGCTime = Time.time;
     }
 }
Example #4
0
 private void Tick()
 {
     if (_luaEnv != null && Time.frameCount % 100 == 0)
     {
         _luaEnv.GC();
         _luaEnv.FullGc();
     }
 }
Example #5
0
        public Dictionary <string, RuleInfo> GetRules(string address, long height, DbSnapshot dbSnapshot)
        {
            Dictionary <string, RuleInfo> rules = new Dictionary <string, RuleInfo>();

            try
            {
                LuaEnv       luaenv       = GetLuaEnv(address);
                LuaVMCall    luaVMCall    = new LuaVMCall();
                LuaVMScript  luaVMScript  = null;
                LuaVMContext LuaVMContext = null;

                s_dbSnapshot = dbSnapshot;
                LuaVMContext Storages = dbSnapshot?.Storages.Get(address);
                // rapidjson
                luaenv.DoString(initScript);
                luaVMScript  = dbSnapshot.Contracts.Get(address);
                LuaVMContext = dbSnapshot.Storages.Get(address);
                luaenv.DoString(luaVMScript.script, address);
                luaenv.DoString($"Storages = rapidjson.decode('{LuaVMContext.jsonData.ToStr()}')\n");
                luaVMCall.fnName = "update";
                luaVMCall.args   = new FieldParam[0];

                object[]    args   = luaVMCall.args.Select(a => a.GetValue()).ToArray();
                LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName);

                luaenv.DoString($"curHeight    =  {height}\n");
                luaFun.Call(args);

                // rapidjson
                luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                dbSnapshot.Storages.Add(address, LuaVMContext);

                JToken   jdStorages = JToken.Parse(LuaVMContext.jsonData.ToStr());
                JToken[] jdRule     = jdStorages["Rules"].ToArray();
                for (int ii = 0; ii < jdRule.Length; ii++)
                {
                    RuleInfo rule = new RuleInfo();
                    rule.Address = jdRule[ii]["Address"].ToString();
                    rule.Start   = long.Parse(jdRule[ii]["Start"].ToString());
                    rule.End     = long.Parse(jdRule[ii]["End"].ToString());
                    rule.LBH     = long.Parse(jdRule[ii]["LBH"].ToString());
                    rules.Remove(rule.Address);
                    rules.Add(rule.Address, rule);
                }
                luaenv.GC();
            }
            catch (Exception e)
            {
                Log.Debug(e.ToString());
                Log.Info("GetRules Error!");
            }
            return(rules);
        }
Example #6
0
 protected override void ShutdownProcess()
 {
     base.ShutdownProcess();
     CallMethod("StartUp", "Shutdown");
     if (LuaEnv != null)
     {
         LuaEnv.Global.Dispose();
         LuaEnv.Tick();
         LuaEnv.GC();
         LuaEnv = new LuaEnv();
     }
 }
Example #7
0
        public void GC()
        {
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();

            if (m_luaEnv != null)
            {
                m_luaEnv.GC();
            }

            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
        }
Example #8
0
        public bool Execute(DbSnapshot dbSnapshot, BlockSub transfer, long height, out object[] result)
        {
            LuaVMCall    luaVMCall    = new LuaVMCall();
            LuaVMScript  luaVMScript  = null;
            LuaVMContext LuaVMContext = null;

            result = null;
            try
            {
                string address = GetContractAddress(transfer);
                LuaEnv luaenv  = GetLuaEnv(address);

                //LuaEnv Global
                s_consAddress = address;
                s_transfer    = transfer;
                s_dbSnapshot  = dbSnapshot;

                luaenv.DoString(initScript);

                if (string.IsNullOrEmpty(transfer.addressOut))
                {
                    // 已存在
                    if (dbSnapshot.Contracts.Get(address) != null)
                    {
                        return(false);
                    }

                    luaVMScript = new LuaVMScript()
                    {
                        script = FileHelper.GetFileData($"./Data/Contract/{transfer.depend}.lua").ToByteArray()
                    };
                    LuaVMContext = new LuaVMContext()
                    {
                        jsonData = "{}".ToByteArray()
                    };
                    luaVMCall = LuaVMCall.Decode(transfer.data);
                    dbSnapshot.Contracts.Add(address, luaVMScript);
                    luaenv.DoString(luaVMScript.script);
                }
                else
                {
                    luaVMScript  = dbSnapshot.Contracts.Get(address);
                    LuaVMContext = dbSnapshot.Storages.Get(address);
                    luaVMCall    = LuaVMCall.Decode(transfer.data);
                    luaenv.DoString(luaVMScript.script, transfer.addressOut);
                    luaenv.DoString($"Storages = rapidjson.decode('{LuaVMContext.jsonData.ToStr()}')\n");
                }

                object[]    args   = luaVMCall.args.Select(a => a.GetValue()).ToArray();
                LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName);

                luaenv.Global.Set("curHeight", height);
                luaenv.Global.Set("sender", transfer.addressIn);
                result = luaFun?.Call(args);

                // rapidjson
                luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                dbSnapshot.Storages.Add(address, LuaVMContext);
                luaenv.GC();

                s_consAddress = "";
                s_dbSnapshot  = null;
                return(true);
            }
            catch (Exception e)
            {
                Log.Error(e);
                Log.Info($"LuaVMEnv.Execute Error, transfer.hash: {transfer.hash} , contract: {transfer.addressOut} func:{luaVMCall.fnName}");
            }
            s_consAddress = "";
            s_dbSnapshot  = null;
            return(false);
        }
Example #9
0
 public void GC()
 {
     luaEnv.GC();
 }
Example #10
0
 // Update is called once per frame
 void Update()
 {
     luaenv.GC();
 }
Example #11
0
 public void Update()
 {
     _luaenv.GC();
 }
Example #12
0
        public bool Execute(DbSnapshot dbSnapshot, BlockSub transfer, long height, out object[] result)
        {
            lock (this)
            {
                using (LuaVMDB luaVMDB = new LuaVMDB(dbSnapshot))
                {
                    result = null;
                    LuaVMCall    luaVMCall    = new LuaVMCall();
                    LuaVMScript  luaVMScript  = null;
                    LuaVMContext LuaVMContext = null;
                    try
                    {
                        string consAddress = GetContractAddress(transfer);
                        LuaEnv luaenv      = GetLuaEnv(consAddress, "Execute");

                        //LuaEnv Global
                        LuaVMStack.Reset(transfer, luaVMDB, consAddress, transfer.addressIn);

                        luaenv.DoString(initScript);

                        if (string.IsNullOrEmpty(transfer.addressOut))
                        {
                            // 已存在
                            if (luaVMDB.Contracts.Get(consAddress) != null)
                            {
                                return(false);
                            }

                            luaVMScript = new LuaVMScript()
                            {
                                script = FileHelper.GetFileData($"./Data/Contract/{transfer.depend}.lua").ToByteArray(), tablName = transfer.depend
                            };
                            LuaVMContext = new LuaVMContext()
                            {
                                jsonData = "{}".ToByteArray()
                            };
                            luaVMCall = LuaVMCall.Decode(transfer.data);
                            luaVMDB.Contracts.Add(consAddress, luaVMScript);
                            luaenv.DoString(luaVMScript.script);
                        }
                        else
                        {
                            luaVMScript  = LuaVMScript.Get(luaVMDB, consAddress);
                            LuaVMContext = luaVMDB.Storages.Get(consAddress);
                            luaVMCall    = LuaVMCall.Decode(transfer.data);
                            luaenv.DoString(luaVMScript.script, transfer.addressOut);
                            luaenv.DoString($"Storages = rapidjson.decode('{LuaVMContext.jsonData.ToStr()}')\n");
                        }

                        object[]    args   = luaVMCall.args.Select(a => a.GetValue()).ToArray();
                        LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName);

                        luaenv.Global.Set("curHeight", height);
                        luaenv.Global.Set("sender", transfer.addressIn);
                        luaenv.Global.Set("addressThis", consAddress);
                        if (luaFun != null)
                        {
                            result = luaFun.Call(args);

                            // rapidjson
                            luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                            LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                            luaVMDB.Storages.Add(consAddress, LuaVMContext);
                            luaVMDB.Commit();
                            luaenv.GC();

                            return(true);
                        }
                    }
                    catch (Exception e)
                    {
#if !RELEASE
                        Log.Error(e);
                        Log.Info($"LuaVMEnv.Execute Error, transfer.hash: {transfer.hash} , contract: {transfer.addressOut} func:{luaVMCall.fnName}");
#endif
                        var array = e.Message.Split("\n");
                        if (array != null && array.Length > 0)
                        {
                            array[0] = array[0].Replace("c# exception:XLua.LuaException: c# exception:System.Exception: ", "");
                            LuaVMStack.Add2TransferTemp(array[0]);
                        }
                    }
                    finally
                    {
                        LuaVMStack.Reset(null, null, null, null);
                    }
                    return(false);
                }
            }
        }
Example #13
0
 public void GC()
 {
     m_LuaEnv.GC();
 }
Example #14
0
 public void GC()
 {
     luaenv.GC();
 }