Beispiel #1
0
        void Start()
        {
            if (scriptType == ScriptType.Lua)
            {
                FileInfo   file   = new FileInfo(scriptPath);
                FileStream stream = file.OpenRead();
                byte[]     text   = new byte[file.Length];
                stream.Read(text, 0, (int)file.Length);

                LuaState lua = new LuaState(); lua.Start();
                lua.DoString(System.Text.Encoding.Default.GetString(text));

                string   tableName = Path.GetFileNameWithoutExtension(scriptPath);
                LuaTable luaTable  = lua.GetTable(tableName);
                if (luaTable != null)
                {
                    LuaDictTable dict = luaTable.ToDictTable();
                    var          iter = dict.GetEnumerator();
                    while (iter.MoveNext())
                    {
                        if (iter.Current.Value is LuaFunction)
                        {
                            _functions.Add(iter.Current.Key as string);
                        }
                        else if ((iter.Current.Value as string) != null)
                        {
                            _properties.Add(iter.Current.Key as string);
                        }
                    }
                }
            }
            else
            {
            }
        }
    protected object castMapData(LuaTable table, Type destType)
    {
        Type         keyType   = destType.GetGenericArguments()[0];
        Type         valueType = destType.GetGenericArguments()[1];
        LuaDictTable dictTable = table.ToDictTable();

        //IDictionary ret = System.Activator.CreateInstance(typeof(Dictionary<object,object>)) as IDictionary;
        IDictionary ret = System.Activator.CreateInstance(destType) as IDictionary;
        object      value;

        /*IEnumerator<DictionaryEntry> tableIter = dictTable.GetEnumerator();
         * while (tableIter.MoveNext())
         * {
         *  if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
         *  {
         *      LuaTable valueTable = tableIter.Current.Value as LuaTable;
         *      value = castMapData(valueTable, valueType);
         *  }
         *  else
         *  {
         *      value = castTypeData(tableIter.Current.Value, valueType);
         *  }
         *  ret.Add(castTypeData(tableIter.Current.Key, keyType), value);
         * }*/

        IDictionaryEnumerator tableIter = dictTable.ToHashtable().GetEnumerator();

        while (tableIter.MoveNext())
        {
            if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Dictionary <,>))
            {
                LuaTable valueTable = tableIter.Value as LuaTable;
                value = castMapData(valueTable, valueType);
            }
            else if (valueType.IsArray)
            {
                LuaTable valueTable = tableIter.Value as LuaTable;
                value = castArrayData(valueTable, valueType);
            }
            else if (valueType == typeof(FixVector2))
            {
                LuaTable valueTable = tableIter.Value as LuaTable;
                value = castFixVector2Data(valueTable);
            }
            else if (valueType == typeof(FixVector3))
            {
                LuaTable valueTable = tableIter.Value as LuaTable;
                value = castFixVector3Data(valueTable);
            }
            else
            {
                value = castTypeData(tableIter.Value, valueType);
            }
            //key只是基本类型 目前不扩展
            ret.Add(castTypeData(tableIter.Key, keyType), value);
            //ret[castTypeData(tableIter.Key, keyType)] = value;
        }
        return(ret);
    }
Beispiel #3
0
    void TestToLua2()
    {
        string filename = "TestToLuaC2";

        m_state.DoFile(filename);

        //这里获取不到
        GameLogger.Log("Get Lua Local Value : " + m_state["local_num"]);

        //可以读取到全局的变量
        GameLogger.Log("Get Lua Value : " + m_state["global_num"]);
        m_state["global_num"] = 10;
        GameLogger.Log("Set Lua Value : " + m_state["global_num"]);

        //调用lua方法
        LuaFunction luaFunc = m_state.GetFunction("Count");

        luaFunc.Call();
        GameLogger.Log("Get Lua Value : " + m_state["global_num"]);
        //也可以直接
        m_state.Call("Count", false);
        GameLogger.Log("Get Lua Value : " + m_state["global_num"]);

        //方法传入参数
        LuaFunction valueFunc = m_state.GetFunction("InputValue");

        valueFunc.BeginPCall();
        valueFunc.Push("-- 这是CSharp中的参数----");
        valueFunc.PCall();
        valueFunc.EndPCall();

        valueFunc.Call("--这是CSharp中直接调用传入参数-----");

        // Get Table
        LuaTable table = m_state.GetTable("mytable");

        table.Call("tableFunc");

        LuaFunction tableFunc = table.GetLuaFunction("tableFunc");

        GameLogger.Log("Lua Table Function");
        tableFunc.Call();
        //这里访问的时table的变量,不是local或者全局变量
        GameLogger.Log("Get Table Value local_num : " + table["local_num"]);
        GameLogger.Log("Get Table Value global_num : " + table["global_num"]);
        GameLogger.Log("Get Table Value table_num : " + table["table_num"]);
        for (int i = 0; i < table.Length; ++i)
        {
            GameLogger.Log("table " + i.ToString() + " : " + table[i + 1]);
        }

        LuaDictTable dicTable = table.ToDictTable();

        foreach (var item in dicTable)
        {
            GameLogger.LogFormat("dicTable {0} -- {1}", item.Key, item.Value);
        }
    }
Beispiel #4
0
    public static Dictionary <object, object> toDict(LuaTable table)
    {
        Dictionary <object, object> dict = new Dictionary <object, object>();
        LuaDictTable dictTable           = table.ToDictTable();

        foreach (var item in dictTable)
        {
            dict.Add(item.Key, item.Value);
        }
        return(dict);
    }
Beispiel #5
0
    private void InitConfig <T>(LuaTable luaTable) where T : IConfigItem, new()
    {
        string typeName = typeof(T).Name;

        if (_allConfigDict.ContainsKey(typeName))
        {
            return;
        }

        try
        {
            bool hasRecord            = false;
            Dictionary <int, T> dict  = new Dictionary <int, T>();
            LuaDictTable        table = luaTable.ToDictTable();
            luaTable.Dispose();
            var iter2 = table.GetEnumerator();
            while (iter2 != null)
            {
                var      one     = iter2.Current.Key;
                LuaTable content = iter2.Current.Value as LuaTable;
                if (content != null)
                {
                    T configItem = new T();
                    configItem.SetKey(int.Parse(one.ToString()));
                    configItem.CreateByLuaTable(content);
                    if (dict.ContainsKey(configItem.GetKey()))
                    {
                        UnityEngine.Debug.LogError(string.Format("[{0}][{1}]配置表key重复:{2}", typeof(T), one, configItem.GetKey()));
                    }
                    else
                    {
                        hasRecord = true;
                        dict.Add(configItem.GetKey(), configItem);
                    }
                }

                //临时解决读表结束不退出循环
                if ((one == null) && hasRecord)
                {
                    break;
                }
                else
                {
                    iter2.MoveNext();
                }
            }
            _allConfigDict.Add(typeName, dict);
            table.Dispose();
        }
        catch (Exception e)
        {
            UnityEngine.Debug.LogError(e);
        }
    }
Beispiel #6
0
    /// <summary>
    /// 遍历lua表
    /// <param name="table">需要遍历的表</param>
    /// <param name="ac">对表数据的操作委托</param>
    /// </summary>
    public static void dealTable(LuaTable table, Action <object, object> ac)
    {
        LuaDictTable dict = table.ToDictTable();

        table.Dispose();
        var iter2 = dict.GetEnumerator();

        while (iter2.MoveNext())
        {
            ac(iter2.Current.Key, iter2.Current.Value);
        }

        iter2.Dispose();
        dict.Dispose();
    }
Beispiel #7
0
    void Start()
    {
#if UNITY_5 || UNITY_2017
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        new LuaResLoader();
        lua = new LuaState();
        lua.Start();
        DelegateFactory.Init();
        lua.DoString(script);

        //Get the function object
        luaFunc = lua.GetFunction("test.luaFunc");

        if (luaFunc != null)
        {
            int num = luaFunc.Invoke <int, int>(123456);
            Debugger.Log("generic call return: {0}", num);

            num = CallFunc();
            Debugger.Log("expansion call return: {0}", num);

            Func <int, int> Func = luaFunc.ToDelegate <Func <int, int> >();
            num = Func(123456);
            Debugger.Log("Delegate call return: {0}", num);

            num = lua.Invoke <int, int>("test.luaFunc", 123456, true);
            Debugger.Log("luastate call return: {0}", num);
        }

        LuaTable luaTable = lua.GetTable("test");
        luaTable.Call <int>("luaFunc", 1000);
        LuaDictTable dict  = luaTable.ToDictTable();
        var          iter2 = dict.GetEnumerator();

        while (iter2.MoveNext())
        {
            Debugger.Log("map item, k,v is {0}:{1}", iter2.Current.Key, iter2.Current.Value);
        }

        iter2.Dispose();
        dict.Dispose();
        luaTable.Dispose();

        lua.CheckTop();
    }
Beispiel #8
0
    // 从脚本更新最新状态
    private void UpdateStatus()
    {
        object[] ret = CallFunction("GetStatus");
        if (ret == null)
        {
            return;
        }

        LuaTable     tbl  = (LuaTable)ret[0];
        LuaDictTable dict = new LuaDictTable(tbl);

        foreach (var item in dict)
        {
            // lua中value都为double
            ProcessStatus((StatusFlag)item.Key, (bool)item.Value);
        }
    }
Beispiel #9
0
    public static LuaTable TestLuaCallByLuaDicTable(LuaTable param)
    {
        LuaDictTable dicTable = param.ToDictTable();

        foreach (var iter in dicTable)
        {
            Debug.Log("key " + iter.Key + " value " + iter.Value);
        }

        int      reference = SingletonMgr.GetLuaState().ToLuaRef();
        LuaTable ret       = new LuaTable(reference, SingletonMgr.GetLuaState());

        //ret();
        //Dictionary<string, string> ret = new Dictionary<string, string>();
        //ret.Add("csharpKey", "csharpValue");
        ret["csharpKey"] = "csharpValue";
        return(ret);
    }
Beispiel #10
0
    //public void PrintTable(string name)
    //{
    //    LuaTable table = GetTable(name);
    //    LuaDictTable dict = table.ToDictTable();
    //    table.Dispose();
    //    var iter2 = dict.GetEnumerator();

    //    while (iter2.MoveNext())
    //    {
    //        Debugger.Log("map item, k,v is {0}:{1}", iter2.Current.Key, iter2.Current.Value);
    //    }

    //    iter2.Dispose();
    //    dict.Dispose();
    //}

    void TestBattleConfig()
    {
        LuaTable table  = LuaManager.GetInstance().luaState.GetTable("config_battle_config");
        LuaTable table2 = table[1] as LuaTable;
        //Debug.LogError(table2["id"]);
        //Debug.LogError(table2["diff_name"]);

        LuaDictTable dict = table.ToDictTable();

        foreach (var kv in dict)
        {
            Debug.LogError(kv.Key);
            LuaTable     table3 = kv.Value as LuaTable;
            LuaDictTable dict2  = table3.ToDictTable();
            foreach (var kv2 in dict2)
            {
                Debug.LogError(kv2.Key + " " + kv2.Value);
            }
        }
    }
Beispiel #11
0
    // 从脚本获取加成数据
    private void UpdateProperty()
    {
        object[] ret = CallFunction("GetProperty");
        if (ret == null)
        {
            return;
        }

        _cacheValues.Clear();

        LuaTable     tbl  = (LuaTable)ret[0];
        LuaDictTable dict = new LuaDictTable(tbl);

        foreach (var item in dict)
        {
            // lua 中 value 都为 double
            AffixCode code = (AffixCode)Enum.Parse(typeof(AffixCode), (string)item.Key);
            _cacheValues[code] = (int)(double)item.Value;
        }
    }
Beispiel #12
0
    void TestAutoGen()
    {
        LuaTable tsheet = LuaManager.GetInstance().luaState.GetTable("config_sheet1");

        LuaDictTable dsheet = tsheet.ToDictTable();

        foreach (var kv in dsheet)
        {
            //Debug.LogError("dsheet  " + kv.Key);

            LuaTable      tfield3 = (kv.Value as LuaTable)["field3"] as LuaTable;
            LuaArrayTable afield3 = tfield3.ToArrayTable();
            //afield3.ForEach((obj) => { Debug.LogError("obj " + System.Convert.ToInt32(obj)); });
            for (int i = 1; i <= afield3.Length; i++)
            {
                Debug.LogError("dsheet  " + kv.Key + " obj " + System.Convert.ToInt32(afield3[i]));
            }

            ArrayData ad = new ArrayData(tfield3);
            Debug.LogError("ad " + ad.GetString(0));
        }
    }
Beispiel #13
0
    // Start is called before the first frame update
    void Start()
    {
        LuaManager.GetInstance().Init();
        LuaManager.GetInstance().LuaState.Require("Main");

        //学习如何调用Lua中的List 和 Dic

        //List
        //toLua中的 C#得到Lua中的表 只有一个套路 通过LuaTable获取
        LuaTable luaTable = LuaManager.GetInstance().LuaState.GetTable("testList");

        //1,2.对应的是位置 不是Key
        Debug.Log("luTable:" + luaTable[1]);
        Debug.Log("luTable:" + luaTable[2]);
        Debug.Log("luTable:" + luaTable[3]);
        Debug.Log("luTable:" + luaTable[4]);
        Debug.Log("luTable:" + luaTable[5]);

        //遍历
        //先转成数组
        object[] objs = luaTable.ToArray();
        foreach (var i in objs)
        {
            Debug.Log("遍历打印:" + i);
        }
        //是引用拷贝
        luaTable[1] = 999;
        LuaTable luaTable2 = LuaManager.GetInstance().LuaState.GetTable("testList");

        Debug.Log("测试引用:" + luaTable2[1]);



        LuaTable luaTable1 = LuaManager.GetInstance().LuaState.GetTable("testList2");

        //1,2.对应的是位置 不是Key
        Debug.Log("luaTable1:" + luaTable1[1]);
        Debug.Log("luaTable1:" + luaTable1[2]);
        Debug.Log("luaTable1:" + luaTable1[3]);
        Debug.Log("luaTable1:" + luaTable1[4]);
        Debug.Log("luaTable1:" + luaTable1[5]);

        objs = luaTable1.ToArray();
        foreach (var i in objs)
        {
            Debug.Log("遍历打印:" + i);
        }

        //Dictionary相关
        LuaTable dic = LuaManager.GetInstance().LuaState.GetTable("testDic");

        Debug.Log(dic["1"]);
        Debug.Log(dic["2"]);
        Debug.Log(dic["3"]);
        Debug.Log(dic["4"]);

        LuaDictTable <string, int> luaDic = dic.ToDictTable <string, int>();

        Debug.Log("luaDic:" + luaDic["1"]);
        Debug.Log("luaDic:" + luaDic["2"]);
        Debug.Log("luaDic:" + luaDic["3"]);
        Debug.Log("luaDic:" + luaDic["4"]);

        //dictionary也是引用拷贝
        dic["1"] = 8527;
        LuaTable luaTable3 = LuaManager.GetInstance().LuaState.GetTable("testDic");

        Debug.Log("luaTable3引用拷贝测试:" + dic["1"]);

        //如果想通过中括号得到值 只支持string和int 其它类型的键值无法获取
        LuaTable dic2 = LuaManager.GetInstance().LuaState.GetTable("testDic2");
        //Debug.Log(dic2[true]);
        LuaDictTable <object, object> luaDic2 = dic2.ToDictTable <object, object>();

        Debug.Log("luaDic2:" + luaDic2["1"]);
        Debug.Log("luaDic2:" + luaDic2[true]);
        Debug.Log("luaDic2:" + luaDic2["4"]);
        Debug.Log("luaDic2:" + luaDic2[3]);

        //dic使用迭代器遍历
        IEnumerator <LuaDictEntry <object, object> > dicIE = luaDic2.GetEnumerator();

        while (dicIE.MoveNext())
        {
            Debug.Log(dicIE.Current.Key + "_" + dicIE.Current.Value);
        }

        List <int> tes = null;

        List <int> .Enumerator tefd = tes.GetEnumerator();
    }
 public static LuaTable GetLuaTable(this LuaDictTable luaDictTable, string key)
 {
     return(luaDictTable[key] as LuaTable);
 }