Пример #1
0
        private void parse_lua_array(LuaTable table, JArray array)
        {
            var keys = table.GetKeys <long>().ToArray();
            Dictionary <object, Type> __type_map__ = null;

            table.Get <string, Dictionary <object, Type> >("__type_map__", out __type_map__);
            foreach (var key in keys)
            {
                var val = table[key];
                if (val == null)
                {
                    continue;
                }

                if (val.GetType() == typeof(LuaTable))
                {
                    JContainer c = null;
                    parse_lua((LuaTable)val, ref c);
                    array.Add(c);
                }
                else
                {
                    if (__type_map__ != null && __type_map__.TryGetValue(key, out Type tp))
                    {
                        array.Add(JToken.FromObject(convert(val, tp) ?? val));
                    }
                    else
                    {
                        array.Add(JToken.FromObject(val));
                    }
                }
            }
        }
        /// <summary>
        /// 发送HttpGet请求(Lua层使用)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="header"></param>
        /// <param name="args"></param>
        /// <param name="callback"></param>
        public void GetHttpRequest(string url, LuaTable header, LuaTable args, Action <byte[]> callback)
        {
            var headerKeys = header.GetKeys();
            var e          = headerKeys.GetEnumerator();

            HttpHeads.Clear();
            while (e.MoveNext())
            {
                var key   = e.Current.ToString();
                var value = header.Get <string>(key);
                HttpHeads.Add(key, value);
            }

            var argKeys = args.GetKeys();
            var e2      = argKeys.GetEnumerator();

            HttpArgs.Clear();
            while (e2.MoveNext())
            {
                var key   = e2.Current.ToString();
                var value = args.Get <string>(key);
                HttpArgs.Add(key, value);
            }
            HttpAgent.Get(url, HttpArgs, HttpHeads, callback);
        }
Пример #3
0
        protected override void Collect()
        {
            if (vmData != null)
            {
                IEnumerable <string> keys = vmData.GetKeys <string>();
                foreach (string k in keys)
                {
                    LuaTable data = vmData.Get <LuaTable>(k);
                    this.AddData(k, XLuaData.GenerateDataWithLuaTable(data));
                }
            }

            if (vmCommand != null)
            {
                IEnumerable <string> keys = vmCommand.GetKeys <string>();
                foreach (string k in keys)
                {
                    LuaTable command = vmCommand.Get <LuaTable>(k);
                    this.AddCommand(k, XLuaCommand.GenerateCommandWithLuaTable(vmTable, command));
                }
            }

            if (this.BindRoot == null)
            {
                this.BindRoot = this.gameObject;
            }
            this.BindRoot.GetComponentsInChildren <AbstractDataBinder>(true, allDataBinders);
            this.BindRoot.GetComponentsInChildren <AbstractCommandBinder>(true, allCommandBinders);
        }
Пример #4
0
        public static ArrayList luaTableKeys2List(LuaTable table)
        {
            ArrayList list = new ArrayList();

            foreach (object key in table.GetKeys <object>())
            {
                list.Add(key);
            }
            return(list);
        }
Пример #5
0
    /// <summary>
    /// 通过xlua提供的luatable获得lua的表,但是性能降低
    /// </summary>
    void GetLuaTableByLuaTable()
    {
        LuaTable luaTable = luaEnv.Global.Get <LuaTable>("person");

        //输出表中的内容
        print(luaTable.Get <string>("name"));
        print(luaTable.Get <int>("age"));
        //获得表中的所有键
        foreach (var i in luaTable.GetKeys())
        {
            print(i);
        }
    }
        /// <summary>
        /// 发送HttpPost请求(Lua层使用)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="header"></param>
        /// <param name="postData"></param>
        /// <param name="callback"></param>
        public void PostHttpRequest(string url, LuaTable header, byte[] postData, Action <byte[]> callback)
        {
            var headerKeys = header.GetKeys();
            var e          = headerKeys.GetEnumerator();

            HttpHeads.Clear();
            while (e.MoveNext())
            {
                var key   = e.Current.ToString();
                var value = header.Get <string>(key);
                HttpHeads.Add(key, value);
            }
            HttpAgent.Post(url, postData, HttpHeads, callback);
        }
Пример #7
0
        private void parse_lua_object(LuaTable table, JObject obj)
        {
            var keys = new List <object>();

            foreach (var k in table.GetKeys())
            {
                keys.Add(k);
            }

            Dictionary <object, Type> __type_map__ = null;

            table.Get <string, Dictionary <object, Type> >("__type_map__", out __type_map__);
            foreach (var item in keys)
            {
                string jsonKey = item.ToString();
                if (is_internal_key(jsonKey))
                {
                    continue;
                }

                var val = table[item];
                if (val == null)
                {
                    obj.Add(jsonKey, null);
                }
                else
                {
                    if (val.GetType() == typeof(LuaTable))
                    {
                        JContainer nobj = null;
                        parse_lua((LuaTable)val, ref nobj);
                        obj.Add(jsonKey, nobj);
                    }
                    else
                    {
                        if (__type_map__ != null && __type_map__.TryGetValue(jsonKey, out Type tp))
                        {
                            obj.Add(jsonKey, JToken.FromObject(convert(val, tp) ?? val));
                        }
                        else
                        {
                            obj.Add(jsonKey, JToken.FromObject(val));
                        }
                    }
                }
            }
        }
Пример #8
0
        public static void LuaTableToString(LuaTable map, StringBuilder outstr, int spacecount = 0)
        {
            //			IEnumerable<object> list = map.GetKeys<object> ();
            //			IEnumerator<object> e = list.GetEnumerator ();
            StringBuilder space = new StringBuilder();

            for (int i = 0; i < spacecount; i++)
            {
                space.Append(" ");
            }
            outstr.Append("\n" + space.ToString()).Append("{");
            foreach (object key in map.GetKeys <object>())
            {
                //				object key = cell.Key;
                object val = map.Get <object>(key);
                if (val == null)
                {
                    continue;
                }
                outstr.Append(space.ToString()).Append(key).Append("(").Append(key.GetType().ToString()).Append(")").Append("=");
                if (val is Hashtable)
                {
                    MapToString((Hashtable)val, outstr, spacecount++);
                }
                else if (val is ArrayList)
                {
                    ArrayListToString((ArrayList)val, outstr, spacecount++);
                }
                else if (val is LuaTable)
                {
                    LuaTableToString((LuaTable)val, outstr, spacecount++);
                }
                else if (val is byte[])
                {
                    outstr.Append(NumEx.bio2Int((byte[])val)).Append("(").Append(val.GetType().ToString()).Append(")").Append("\n");
                }
                else
                {
                    outstr.Append(val).Append("(").Append(val.GetType().ToString()).Append(")").Append("\n");
                }
            }
            outstr.Append("}\n");
            //Debug.Log(outstr.ToString());
        }
Пример #9
0
        private bool is_array(LuaTable table)
        {
            var keys = table.GetKeys();

            foreach (var key in keys)
            {
                if (is_internal_key(key as string))
                {
                    continue;
                }

                if (!typeof(long).IsAssignableFrom(key.GetType()))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #10
0
        public static object ConvertToObject(LuaTable target)
        {
            var resullt = new System.Collections.Generic.Dictionary <object, object>();

            foreach (var VARIABLE in target.GetKeys())
            {
                var value = target.Get <object, object>(VARIABLE);


                var t = value.GetType();
                if (t == typeof(XLua.LuaTable))
                {
                    resullt[VARIABLE] = ConvertToObject(value as LuaTable);
                }
                else
                {
                    resullt[VARIABLE] = value;
                }
            }

            return(resullt);
        }
Пример #11
0
        public static object ConvertSendToObject(LuaTable target)
        {
            var result = new System.Collections.Generic.List <object>();

            foreach (var VARIABLE in target.GetKeys())
            {
                var value = target.Get <object, object>(VARIABLE);


                var t = value.GetType();
                if (t == typeof(XLua.LuaTable))
                {
                    result.Add(ConvertSendToObject(value as LuaTable));
                }
                else
                {
                    result.Add(value);
                }
            }

            return(result.ToArray());
        }
Пример #12
0
    void Awake()
    {
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

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

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

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

        Debug.Log(scriptEnv.Get <string, GameObject>("0"));
        Debug.Log("====================================");
        foreach (var item in scriptEnv.GetKeys())
        {
            Debug.Log(scriptEnv.Get <string, object>((string)item));
        }
        Debug.Log("====================================");


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

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

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Пример #13
0
        public void ParseResultLuaTable(LuaTable luaTable, int depth)
        {
            if (depth >= GSUnityLuaShellConst.LuaTableDepth)
            {
                return;
            }

            foreach (var key in luaTable.GetKeys())
            {
                var value = luaTable[key];
                if (value == null)
                {
                    treeView.AddChild($"{key}    nil", false, depth);
                    continue;
                }
                treeView.AddChild($"{key}    {value.ToString()}", false, depth);
                if (value is LuaTable)
                {
                    ParseResultLuaTable(value as LuaTable, depth + 1);
                }
            }

            for (int i = 1; i <= luaTable.Length; i++)
            {
                object value = luaTable[i];
                if (value == null)
                {
                    break;
                }
                treeView.AddChild($"{value.ToString()}", false, depth);
                if (value is LuaTable)
                {
                    ParseResultLuaTable(value as LuaTable, depth + 1);
                }
            }
        }
Пример #14
0
        private void PropertyDropDown(BindingBase.BindingInfo info)
        {
            var type = Target.Context.DataType;

            var labels = new System.Collections.Generic.List <string>();
            var names  = new System.Collections.Generic.List <string>();

            if (!Target.Context.DataType.IsAssignableFrom(typeof(GameSystem.LuaObservableBehaviour)))
            {
                var members = new MemberInfo[0];

                // filter
                switch (info.Filters)
                {
                case BindingBase.BindingFilter.Commands:
                    members = EditorMembersHelper.GetMethods(type);
                    break;

                case BindingBase.BindingFilter.Properties:
                    members = EditorMembersHelper.GetProperties(type);
                    break;
                }

                // filter
                if (info.FilterTypes != null)
                {
                    members = members.Where(o => info.FilterTypes.Any(t => ValidType(t, o.GetParamaterType()))).ToArray();
                }

                labels = members.Select(o => string.Format("{0} : {1}", o.Name, o.GetParamaterType())).ToList();
                names  = members.Select(o => o.Name).ToList();
            }
            else
            {
                var    luaObervableBehaviour = Target.Context.DataInstance as GameSystem.LuaObservableBehaviour;
                string luaScript             = luaObervableBehaviour.GetLuaScript();
                if (!string.IsNullOrEmpty(luaScript))
                {
                    luaEnv.DoString(luaScript, GetType().Name, luaTable);
                    var keys = luaTable.GetKeys <string>().OrderBy(c => c);
                    foreach (var k in keys)
                    {
                        if (k == "set" || k == "get")
                        {
                            continue;
                        }

                        object value;
                        if (info.FilterTypes != null)
                        {
                            foreach (var t in info.FilterTypes)
                            {
                                if (luaTable.TryGet(k, t, out value))
                                {
                                    labels.Add(string.Format("{0} : {1}", k, value.GetType()));
                                    names.Add(k);
                                }
                            }
                        }
                        else
                        {
                            if (luaTable.TryGet(k, out value))
                            {
                                Type t = value.GetType();
                                if (info.Filters == BindingBase.BindingFilter.Commands && t == typeof(LuaFunction) ||
                                    info.Filters == BindingBase.BindingFilter.Properties && t != typeof(LuaFunction))
                                {
                                    labels.Add(string.Format("{0} : {1}", k, value.GetType()));
                                    names.Add(k);
                                }
                            }
                        }
                    }
                    luaEnv.Tick();
                }
            }

            if (labels.Count == 0)
            {
                EditorGUILayout.LabelField(string.Format("{0}->{1} has no valid members.", info.BindingName, type.Name));
                return;
            }

            labels.Insert(0, "Null");
            names.Insert(0, "");

            var index = names.FindIndex(c => c == info.MemberName);
            var i     = EditorGUILayout.Popup(info.BindingName, index, labels.ToArray());

            if (i < 0)
            {
                return;
            }
            if (i != index || info.MemberName != names[i])
            {
                info.MemberName = names[i];
                EditorUtility.SetDirty(target);
            }
        }