Exemple #1
0
    private void Awake()
    {
        luaEnv = new XLua.LuaEnv();

        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("mono", this);
        foreach (var injection in injections)
        {
            scriptEnv.Set(injection.name, injection.value);
        }

        luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("fixedupdate", out luaFixedUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Exemple #2
0
        /// <summary>
        /// 创建LuaTable
        /// </summary>
        /// <param name="tableName">做key</param>
        /// <returns></returns>
        public LuaTable CreateTable(string tableName)
        {
            if (_luaTableMap.ContainsKey(tableName))
            {
                Error("CreateTable():the table has existed :{0}", tableName);
                return(null);
            }
            LuaTable luaTable = _luaEnv.NewTable();

            AddTable(tableName, luaTable);
            return(luaTable);
        }
Exemple #3
0
        public static LuaTable Create(LuaEnv luaEnv, string globalName)
        {
            LuaTable table = luaEnv.NewTable();

            luaEnv.Global.Set(globalName, table);
            return(table);
        }
Exemple #4
0
    /// <summary>
    /// 初始化控制器
    /// </summary>
    private void init()
    {
        mStatus = EnumConfig.SystemControllerStatusEnum.INIT;
        mLuaenv = new XLua.LuaEnv();
        mLuaenv.DoString("CS.UnityEngine.Debug.Log('Lua Env create successful.')");

        mScriptEnv = mLuaenv.NewTable();

        Utils.BindMetaTable(mLuaenv, mScriptEnv);

        mScriptEnv.Set("self", this);

        foreach (var injection in injections)
        {
            mScriptEnv.Set(injection.name, injection.value);
        }
        mLuaenv.DoString(mLuaScript.text, "LuaTestScript", mScriptEnv);

        Action awake = mScriptEnv.Get <Action>("awake");

        if (awake != null)
        {
            awake();
        }

        mScriptEnv.Get("onStart", out mLuaStart);
        mScriptEnv.Get("update", out mLuaUpdate);
        mScriptEnv.Get("onDestroy", out mLuaOnDestroy);
    }
Exemple #5
0
        public MessageLua(XLua.LuaEnv luaEnv, UInt32 clsId, String clsName, UInt32 signId, String type, List <Attr> attrs)
        {
            _luaEnv   = luaEnv;
            _luaTable = _luaEnv.NewTable();
            _type     = type;
            _luaTable.Set(s_sSelf, this);

            if (_type.Equals(s_sEnum))
            {
                foreach (var v in attrs)
                {
                    _luaTable.Set(v.name, Int32.Parse(v.defVal));
                }
            }
            else
            {
                _clsId   = clsId;
                _clsName = clsName;
                _signId  = signId;
                _attrs   = attrs;

                _luaTable.Set("ClsName", _clsName);
                _luaTable.Set("ClsId", _clsId);
                _luaTable.Set("SignId", _signId);

                _luaTable.Set <String, DelegateCreate>("Create", this.Create);
            }
        }
Exemple #6
0
        protected override void SingletonAwake()
        {
            _luaEnv = new LuaEnv();
            AddLoaders();

            _luaTableMap = new Dictionary <string, LuaTable>();
            tableMap     = _luaEnv.NewTable();
            _luaEnv.Global.Set("tablemap", tableMap);
            _initialized = true;
        }
Exemple #7
0
        public static LuaTable CreateFromDictionary(LuaEnv luaEnv, IDictionary <string, object> objDic)
        {
            LuaTable table = luaEnv.NewTable();

            if (objDic != null && objDic.Count > 0)
            {
                foreach (var kvp in objDic)
                {
                    table.Set(kvp.Key, kvp.Value);
                }
            }
            return(table);
        }
Exemple #8
0
        public static LuaTable CreateFromList(LuaEnv luaEnv, IList <object> objList)
        {
            LuaTable table = luaEnv.NewTable();

            if (objList != null && objList.Count > 0)
            {
                for (int i = 0; i < objList.Count; ++i)
                {
                    table.Set(i + 1, objList[i]);
                }
            }
            return(table);
        }
    public static void BindMetaTable(XLua.LuaEnv env, XLua.LuaTable table)
    {
        if (env == null)
        {
            return;
        }
        if (table == null)
        {
            return;
        }
        LuaTable t = env.NewTable();

        t.Set("__index", env.Global);
        table.SetMetaTable(t);
        t.Dispose();
    }
Exemple #10
0
        public static void SetTable <T>(String k, T v)
        {
            var t     = s_luaEnv.Global;
            var names = new List <String>(k.Split('.'));
            var name  = names.Last();

            names.RemoveAt(names.Count - 1);
            foreach (var s in names)
            {
                var tt = t.Get <String, XLua.LuaTable>(s);
                if (tt == null)
                {
                    tt = s_luaEnv.NewTable();
                    t.Set <String, XLua.LuaTable>(s, tt);
                }

                t = tt;
            }

            t.Set(name, v);
        }
Exemple #11
0
        static int _m_NewTable(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                XLua.LuaEnv gen_to_be_invoked = (XLua.LuaEnv)translator.FastGetCSObj(L, 1);



                {
                    XLua.LuaTable gen_ret = gen_to_be_invoked.NewTable(  );
                    translator.Push(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Exemple #12
0
 public static LuaTable Create(LuaEnv luaEnv)
 {
     return(luaEnv.NewTable());
 }
Exemple #13
0
        public void LoadCfg()
        {
            if (m_isCfgLoad == true)
            {
                return;
            }

            FileStream ifs = null;

            try
            {
                _keys.Clear();
                _items.Clear();


                var cfgPath = App.WorkPath + _cfgPath;

                if (!File.Exists(cfgPath))
                {
                    UnityEngine.Debug.LogError(String.Format("{0} no exists", cfgPath));
                    return;
                }

                ifs = new FileStream(cfgPath, FileMode.Open, FileAccess.Read);

                UInt32 cfgCodeVersion = 0;
                UInt32 cfgDataVersion = 0;

                var binaryReader = new BinaryReader(ifs);
                cfgCodeVersion = binaryReader.ReadUInt32();
                cfgDataVersion = binaryReader.ReadUInt32();

                if (this.SignId != cfgCodeVersion)
                {
                    return;
                }

                UInt32 cfgCount = binaryReader.ReadUInt32();


                for (var i = 0; i < cfgCount; i++)
                {
                    _keys.Add(ReadValue(binaryReader, _index));
                }

                for (var i = 0; i < cfgCount; i++)
                {
                    var luaTable = _luaEnv.NewTable();
                    this.Decode(luaTable, binaryReader);
                    _items.Add(luaTable);
                }

                m_isCfgLoad = true;
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
            }
            finally
            {
                if (ifs != null)
                {
                    ifs.Close();
                }
            }
        }
Exemple #14
0
        public CfgLua(XLua.LuaEnv luaEnv, UInt32 clsId, String clsName, UInt32 signId, List <Attr> attrs, String index = "", String cfgPath = "")
        {
            _luaEnv = luaEnv;
            _index  = index;

            _cfgPath  = cfgPath;
            _luaTable = _luaEnv.NewTable();
            _luaTable.Set(_sSelf, this);

            _clsId   = clsId;
            _clsName = clsName;
            _signId  = signId;
            _attrs   = attrs;

            _luaTable.Set("ClsName", _clsName);
            _luaTable.Set("ClsId", _clsId);
            _luaTable.Set("SignId", _signId);

            _keys  = new List <object>();
            _items = new List <LuaTable>();

            _luaTable.Set <String, List <object> >("Keys", this.Keys);
            _luaTable.Set <String, List <LuaTable> >("Items", this.Items);
            _luaTable.Set <String, DelegateGetItem>("GetItem", this.GetItem);

            if (index.Equals(_sString))
            {
                _keyTypeConvert = new KeyTypeConvert <String>();
            }
            else if (index.Equals(_sInt32))
            {
                _keyTypeConvert = new KeyTypeConvert <Int32>();
            }
            else if (index.Equals(_sUint32))
            {
                _keyTypeConvert = new KeyTypeConvert <UInt32>();
            }
            else if (index.Equals(_sUint8))
            {
                _keyTypeConvert = new KeyTypeConvert <UInt8>();
            }
            else if (index.Equals(_sUint16))
            {
                _keyTypeConvert = new KeyTypeConvert <UInt16>();
            }
            else if (index.Equals(_sUint64))
            {
                _keyTypeConvert = new KeyTypeConvert <UInt64>();
            }
            else if (index.Equals(_sInt8))
            {
                _keyTypeConvert = new KeyTypeConvert <Int8>();
            }
            else if (index.Equals(_sInt16))
            {
                _keyTypeConvert = new KeyTypeConvert <Int16>();
            }

            else if (index.Equals(_sInt64))
            {
                _keyTypeConvert = new KeyTypeConvert <Int64>();
            }
            else if (index.Equals(_sDouble))
            {
                _keyTypeConvert = new KeyTypeConvert <Double>();
            }
            else if (index.Equals(_sFloat))
            {
                _keyTypeConvert = new KeyTypeConvert <Float>();
            }
            else if (index.Equals(_sBool))
            {
                _keyTypeConvert = new KeyTypeConvert <Bool>();
            }
        }
Exemple #15
0
        public XLua.LuaTable Create()
        {
            var t = _luaEnv.NewTable();

            t.Set("ClsName", _clsName);
            t.Set("ClsId", _clsId);
            t.Set("SignId", _signId);
            t.Set(s_sSelf, this);

            object objDefVal = null;

            foreach (var v in _attrs)
            {
                if (v.objDefVal == null)
                {
                    try
                    {
                        if (v.type.Equals(s_sString))
                        {
                            v.objDefVal = v.defVal;
                        }
                        else if (v.type.Equals(s_sInt32))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = Int32.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sUint32))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = UInt32.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sEnum))
                        {
                            v.objDefVal = false;
                            v.objDefVal = Int32.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sUint8))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = Byte.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sUint16))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = UInt16.Parse(v.defVal);
                        }

                        else if (v.type.Equals(s_sUint64))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = UInt64.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sInt8))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = SByte.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sInt16))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = Int16.Parse(v.defVal);
                        }

                        else if (v.type.Equals(s_sInt64))
                        {
                            v.objDefVal = 0;
                            v.objDefVal = Int64.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sDouble))
                        {
                            v.objDefVal = 0.0;
                            v.objDefVal = Double.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sFloat))
                        {
                            v.objDefVal = 0.0;
                            v.objDefVal = Single.Parse(v.defVal);
                        }
                        else if (v.type.Equals(s_sBool))
                        {
                            v.objDefVal = false;
                            v.objDefVal = Boolean.Parse(v.defVal);
                        }
                        else
                        {
                            var        m    = _luaEnv.Global.GetInPath <XLua.LuaTable>(v.type);
                            MessageLua self = m.Get <String, MessageLua>(s_sSelf);
                            objDefVal = self.Create();
                        }
                    }
                    catch (Exception e)
                    {
                        UnityEngine.Debug.LogException(e);
                    }
                }

                if (v.objDefVal != null)
                {
                    t.Set(v.name, v.objDefVal);
                }
                else if (objDefVal != null)
                {
                    t.Set(v.name, objDefVal);
                }
            }
            return(t);
        }