Пример #1
0
        object ParseLuaTarget_LuaValue(RealStatePtr L, object value, LuaTypes valueType, out Descriptor[] descriptors)
        {
            descriptors = null;

            if (valueType == LuaTypes.LUA_TTABLE)
            {
                LuaTable realValue = (LuaTable)value;
                Type     type      = null;

                string typeStr = null;

                realValue.ForEach <int, string>((index, typeString) =>
                {
                    typeStr = typeString;
                    return(true);
                });

                if (typeStr != null)
                {
                    type = LuaTarget.GetInternalType(typeStr);
                    if (type == typeof(float))
                    {
                        float min = 0f, max = 0f;
                        int   flag = 0;
                        realValue.ForEach <string, float>((name, v) =>
                        {
                            if (name == LuaTarget.NameMin)
                            {
                                min = v;
                                flag++;
                            }
                            else if (name == LuaTarget.NameMax)
                            {
                                max = v;
                                flag++;
                            }
                        });

                        if (flag == 2)
                        {
                            descriptors = new Descriptor[] { new MinMaxDescriptor(min, max) };
                        }
                    }
                    return(type != null ? type : value.GetType());
                }
            }
            else if (valueType == LuaTypes.LUA_TNUMBER)
            {
                return(System.Convert.ToSingle(value));
            }

            return(value);
        }
Пример #2
0
        List <string> ParseLuaBehaviour_Begin(RealStatePtr L, string filename, ref int originTop, ref LuaTable argsTable)
        {
            originTop = LuaAPI.lua_gettop(L);
            List <string> keylist = null;

            try
            {
                byte[]   chunk = System.IO.File.ReadAllBytes(filename);
                object[] rets  = Utils.Lua.DoString(chunk, filename);

                if (rets.Length == 0)
                {
                    throw new Exception("lua file " + filename + " need return table type");
                }

                LuaTable table = rets[0] as LuaTable;

                if (table == null)
                {
                    throw new Exception("lua file " + filename + " convert to table type failed");
                }

                argsTable = table.Get <string, LuaTable>(LuaTarget.ArgsName);

                if (argsTable == null)
                {
                    throw new Exception("lua file " + filename + " field " + LuaTarget.ArgsName + " should be table type");
                }

                keylist = new List <string>();

                argsTable.ForEach <string, object>((key, value, valueType) =>
                {
                    if (valueType == LuaTypes.LUA_TTABLE || valueType == LuaTypes.LUA_TNUMBER || valueType == LuaTypes.LUA_TSTRING ||
                        valueType == LuaTypes.LUA_TBOOLEAN || valueType == LuaTypes.LUA_TUSERDATA || valueType == LuaTypes.LUA_TLIGHTUSERDATA)
                    {
                        keylist.Add(key);
                    }
                });
            }
            catch (Exception e)
            {
                LuaAPI.lua_settop(L, originTop);
                throw e;
            }

            return(keylist);
        }
Пример #3
0
    public static string ToString(this XLua.LuaTable self, string fmt = null, int indent = 0, bool strfun = false)
    {
        var indents0 = "";
        var indents  = "";

        if (fmt != null)
        {
            indents0 = "\n" + new String(' ', 4 * indent);
            indents  = "\n" + new String(' ', 4 * (indent + 1));
        }

        StringBuilder sb = new StringBuilder(20480);

        sb.Append(indents0 + "{");
        self.ForEach <int, object>((k, v) =>
        {
            if (v is XLua.LuaTable)
            {
                var t = (v as XLua.LuaTable);
                sb.Append(indents + "[" + k.ToString() + "] = ");
                sb.Append(t.ToString(fmt, 1 + indent));
                sb.Append(",");
            }
            else
            {
                if ((v is bool) || (v is long) || (v is double))
                {
                    sb.Append(indents + "[" + k + "] = " + (v != null ? v.ToString() + "," : "nil,"));
                }
                // else if (v is LuaFunction && strfun)
                // {
                //     sb.Append(indents + "[\"" + k + "\"] = \"" + v.ToString() + "\",");
                // }
                else if (v is string) //|| (v is Component))
                {
                    sb.Append(indents + "[\"" + k + "\"] = \"" + v.ToString() + "\",");
                }
            }
        });
        self.ForEach <string, object>((k, v) =>
        {
            if (v is XLua.LuaTable)
            {
                var t = (v as XLua.LuaTable);
                sb.Append(indents + "[\"" + k.ToString() + "\"] = ");
                sb.Append(t.ToString(fmt, 1 + indent));
                sb.Append(",");
            }
            else
            {
                if ((v is bool) || (v is long) || (v is double))
                {
                    sb.Append(indents + "[\"" + k + "\"] = " + (v != null ? v.ToString() + "," : "nil,"));
                }
                else //if (v is string || (v is Component))
                {
                    sb.Append(indents + "[\"" + k + "\"] = \"" + v.ToString() + "\",");
                }
            }
        });

        sb.Append(indents0 + "}");
        return(sb.ToString());
    }
Пример #4
0
        public void ShowInternalItem_Playing(string path, string key, object value, bool showKey = true)
        {
            if (value == null)
            {
                return;
            }

            var  type = value.GetType();
            bool endSingleVertical = false;

            if (showKey)
            {
                if (type.IsValueType || type == typeof(string) || type.IsSubclassOf(typeof(UnityEngine.Object)) || type == typeof(UnityEngine.Object))
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.PrefixLabel(GetPrefixKey(key));

                    endSingleVertical = true;
                }
            }

            if (type.IsValueType)
            {
                if (type == typeof(double))
                {
                    EditorGUILayout.DoubleField((double)value);
                }
                else if (type == typeof(bool))
                {
                    EditorGUILayout.Toggle((bool)value);
                }
                else if (type == typeof(System.Int32))
                {
                    EditorGUILayout.IntField(System.Convert.ToInt32(value));
                }
                else if (type == typeof(System.Int64))
                {
                    EditorGUILayout.IntField(System.Convert.ToInt32(value));
                }
                else if (type == typeof(System.Double))
                {
                    EditorGUILayout.DoubleField(System.Convert.ToDouble(value));
                }
                else if (type == typeof(System.Single))
                {
                    EditorGUILayout.FloatField(System.Convert.ToSingle(value));
                }
                else if (type == typeof(Vector2))
                {
                    EditorGUILayout.Vector2Field(string.Empty, (Vector2)value);
                }
                else if (type == typeof(Vector3))
                {
                    EditorGUILayout.Vector3Field(string.Empty, (Vector3)value);
                }
                else if (type == typeof(Vector4))
                {
                    EditorGUILayout.Vector4Field(string.Empty, (Vector4)value);
                }
                else if (type == typeof(Color))
                {
                    EditorGUILayout.ColorField((Color)value);
                }
                else if (type == typeof(Rect))
                {
                    EditorGUILayout.RectField((Rect)value);
                }
                else if (type == typeof(Bounds))
                {
                    EditorGUILayout.BoundsField((Bounds)value);
                }
                else if (type.IsSubclassOf(typeof(System.Enum)))
                {
                    EditorGUILayout.EnumPopup(value as System.Enum);
                }
            }
            else
            {
                if (type == typeof(string))
                {
                    EditorGUILayout.TextField(value as string);
                }
                else if (type.IsSubclassOf(typeof(UnityEngine.Object)) || type == typeof(UnityEngine.Object))
                {
                    EditorGUILayout.ObjectField(value as UnityEngine.Object, type, true);
                }
                else if (type.IsArray)
                {
                    bool show = false;

                    string fullpath = path + "." + key;

                    EditorGUILayout.BeginVertical("Box");
                    EditorGUI.indentLevel++;

                    foldout.TryGetValue(fullpath, out show);
                    show = EditorGUILayout.Foldout(show, GetPrefixKey(key));
                    foldout[fullpath] = show;

                    if (show)
                    {
                        var array  = value as System.Array;
                        int length = array.Length;
                        for (int i = 0; i < length; i++)
                        {
                            ShowInternalItem_Playing(fullpath, i.ToString(), array.GetValue(i), false);
                        }
                    }

                    EditorGUI.indentLevel--;
                    EditorGUILayout.EndVertical();
                }
                else if (type == typeof(LuaTable))
                {
                    bool   show     = false;
                    string fullpath = null;
                    if (string.IsNullOrEmpty(key))
                    {
                        show     = true;
                        fullpath = key;
                    }
                    else
                    {
                        fullpath = path + "." + key;

                        EditorGUILayout.BeginVertical("Box");


                        if (!foldout.TryGetValue(fullpath, out show) && string.IsNullOrEmpty(path))
                        {
                            show = true;
                        }

                        show = EditorGUILayout.Foldout(show, GetPrefixKey(key));
                        foldout[fullpath] = show;
                    }

                    if (show)
                    {
                        EditorGUI.indentLevel++;

                        LuaTable realValue = value as LuaTable;
                        realValue.ForEach <string, object>((k, v) =>
                        {
                            ShowInternalItem_Playing(fullpath, k, v);
                        });

                        EditorGUI.indentLevel--;
                    }

                    if (!string.IsNullOrEmpty(key))
                    {
                        EditorGUILayout.EndVertical();
                    }
                }
            }

            if (endSingleVertical)
            {
                EditorGUILayout.EndHorizontal();
            }
        }
Пример #5
0
        void OnInspectorGUI_Playing()
        {
            var currTarget = target as LuaTarget;

            if (currTarget == null || !currTarget.Valid)
            {
                EditorGUILayout.HelpBox("lua not initialized", MessageType.Warning);
                return;
            }

            EditorGUI.BeginDisabledGroup(false);

            int originTop = LuaAPI.lua_gettop(Utils.LuaL);

            LuaAPI.lua_getref(Utils.LuaL, currTarget.LuaReference);
            object value = Utils.ObjTranslator.GetObject(Utils.LuaL, -1, typeof(object));

            ShowInternalItem_Playing(string.Empty, string.Empty, value);

            LuaAPI.lua_settop(Utils.LuaL, originTop);

            EditorGUI.EndDisabledGroup();

            originTop = LuaAPI.lua_gettop(Utils.LuaL);

            object   evtVal       = null;
            LuaTypes evtValueType = LuaTypes.LUA_TNIL;

            currTarget.Table.Get <string, object>(LuaTarget.NameEvents, out evtVal, out evtValueType);
            if (evtValueType == LuaTypes.LUA_TTABLE)
            {
                var events = new List <string>();
                EditorGUILayout.BeginVertical("Box");

                LuaTable evtTable = evtVal as LuaTable;

                evtTable.ForEach <string, object>((key, val, type) =>
                {
                    if (type != LuaTypes.LUA_TFUNCTION)
                    {
                        return;
                    }

                    events.Add(key);
                });

                if (content != null)
                {
                    var eventsFieldIndex = GetFieldSourceIndex(content, LuaTarget.ArgsName);
                    var orderMap         = (eventsFieldIndex.Key == -1 || eventsFieldIndex.Value == -1) ? null : GetOrderedKey(events, content.Substring(eventsFieldIndex.Key, eventsFieldIndex.Value - eventsFieldIndex.Key - 1));

                    events = events.OrderBy(key => { int index; orderMap.TryGetValue(key, out index); return(index); }).ToList();
                }

                int methodNum = 0;
                EditorGUILayout.BeginHorizontal();
                bool invoked = false;

                foreach (var key in events)
                {
                    if (GUILayout.Button(key) && !invoked)
                    {
                        invoked = true;
                        LuaFunction func = null;

                        evtTable.Get <string, LuaFunction>(key, out func);
                        if (func != null)
                        {
                            func.Call(evtTable, editorMethodArgument);
                        }
                    }

                    methodNum++;
                    if (methodNum % maxMethodButtonPerRow == 0)
                    {
                        methodNum = 0;
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.BeginHorizontal();
                    }
                }

                if (methodNum % maxMethodButtonPerRow != 0)
                {
                    EditorGUILayout.LabelField(string.Empty);
                }

                EditorGUILayout.EndHorizontal();

                editorMethodArgument = EditorGUILayout.TextField("Method Argument", editorMethodArgument);
                EditorGUILayout.EndVertical();
            }

            LuaAPI.lua_settop(Utils.LuaL, originTop);

            if (GUILayout.Button("Reload"))
            {
                currTarget.Reload();
                content = null;
            }
        }