Example #1
0
        public static LuaClassDescriptor GetDescriptor(string className)
        {
            className = className.Replace('.', '/');
            if (descriptors.TryGetValue(className, out var descriptor))
            {
                return(descriptor);
            }

            var text = LoadLuaTextFile("Lua/" + className);

            if (text == null)
            {
                return(null);
            }

            if (!text.Contains("---@class"))
            {
                return(null);
            }

            using (var reader = new StringReader(text)) {
                descriptor = new LuaClassDescriptor(reader);
                descriptors.Add(className, descriptor);
                return(descriptor);
            }
        }
        private void OnEnable()
        {
            binding = target as LuaBinding;
            var luaPathProp = serializedObject.FindProperty("LuaFile");

            if (string.IsNullOrEmpty(luaPathProp.stringValue) || !File.Exists(Application.dataPath + "/Resources/Lua/" + luaPathProp.stringValue + ".lua"))
            {
                return;
            }

            descriptor = LuaClassEditorFactory.GetDescriptorWithFilePath(luaPathProp.stringValue);
        }
Example #3
0
        public void Reload(TextReader reader)
        {
            Fields.Clear();
            Methods.Clear();

            while (true)
            {
                var line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }

                if (line == string.Empty)
                {
                    continue;
                }

                string[] statements;
                if (line.StartsWith("---@class"))
                {
                    statements = line.Split(' ');
                    ClassName  = statements[1];
                    if (statements.Length == 4)
                    {
                        var baseClassName = statements[3];
                        baseClass = LuaClassEditorFactory.GetDescriptor(baseClassName);
                    }
                }
                else if (line.StartsWith("---@field"))
                {
                    string declareField;
                    var    comment = string.Empty;
                    var    index   = line.LastIndexOf("@", StringComparison.Ordinal);
                    if (index >= 4)
                    {
                        declareField = line.Substring(0, index);
                        comment      = line.Substring(index + 1);
                    }
                    else
                    {
                        declareField = line;
                    }

                    statements = declareField.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (statements.Length < 3)
                    {
                        Debug.LogWarning($"Can not recognized field line : {line}");
                        continue;
                    }

                    if (statements.Length >= 4 && statements[1] != "public")
                    {
                        continue;
                    }

                    Fields.Add(new LuaClassField {
                        FieldName = statements.Length == 3 ? statements[1] : statements[2],
                        FieldType = statements.Length == 3 ? statements[2] : statements[3],
                        Comment   = comment
                    });
                }
                else
                {
                    const string methodStart = @"function M:";
                    var          match       = line.StartsWith(methodStart);
                    if (!match)
                    {
                        continue;
                    }
                    var methodName = line.Substring(methodStart.Length, line.IndexOf('(') - methodStart.Length);
                    if (!methodName.StartsWith("_") && methodName[0] == char.ToUpper(methodName[0]))
                    {
                        if (methodName.StartsWith("DEBUG"))
                        {
                            DebugMethods.Add(methodName);
                        }
                        else
                        {
                            Methods.Add(methodName);
                        }
                    }
                }
            }

            if (baseClass != null)
            {
                foreach (var baseMethodName in baseClass.Methods.Where(baseMethodName => !Methods.Contains(baseMethodName)))
                {
                    Methods.Add(baseMethodName);
                }

                Fields.AddRange(baseClass.Fields);
            }
        }
        public override void OnInspectorGUI()
        {
            if (binding.LuaData == null)
            {
                binding.LuaData = new LuaBindingDataBase[0];
            }

            var luaPathProp = serializedObject.FindProperty("LuaFile");

            if (string.IsNullOrEmpty(luaPathProp.stringValue))
            {
                EditorGUILayout.PropertyField(luaPathProp);
                EditorGUILayout.HelpBox("需要设置Lua文件!", MessageType.Error);
                serializedObject.ApplyModifiedProperties();
                return;
            }

            if (descriptor == null)
            {
                EditorGUILayout.PropertyField(luaPathProp);
                EditorGUILayout.HelpBox("需要设置Lua文件!", MessageType.Error);
                descriptor = LuaClassEditorFactory.GetDescriptorWithFilePath(luaPathProp.stringValue);
                serializedObject.ApplyModifiedProperties();
                return;
            }

            isUsedBinding.Clear();
            foreach (var field in descriptor.Fields.Where(field => !field.FieldName.StartsWith("_")))
            {
                if (field.FieldType.Contains("[]"))
                {
                    CheckBinding <LuaBindingUOArrayData>(field);
                }
                else if (field.FieldType.StartsWith("CS."))
                {
                    var typeName = field.FieldType.Substring(3);
                    var type     = String2TypeCache.GetType(typeName);
                    if (type == null)
                    {
                        EditorGUILayout.HelpBox($"Can not find type : {typeName}", MessageType.Error);
                    }
                    else
                    {
                        if (field.FieldType == "CS.Extend.Asset.AssetReference")
                        {
                            var match = CheckBinding <LuaBindingAssetReferenceData>(field);
                            if (!string.IsNullOrEmpty(field.Comment) && field.Comment.StartsWith("CS."))
                            {
                                match.AssetType = String2TypeCache.GetType(field.Comment.Substring(3));
                            }
                        }
                        else
                        {
                            CheckBinding <LuaBindingUOData>(field);
                        }
                    }
                }
                else
                {
                    var index = Array.IndexOf(basicTypes, field.FieldType);
                    if (index >= 0)
                    {
                        var typ = basicBindingTypes[index];
                        CheckBinding(field, typ);
                    }
                    else
                    {
                        CheckBinding <LuaBindingUOData>(field);
                    }
                }
            }

            for (var i = 0; i < binding.LuaData.Length;)
            {
                var bindingData = binding.LuaData[i];
                if (isUsedBinding.Contains(bindingData))
                {
                    i++;
                }
                else
                {
                    ArrayUtility.RemoveAt(ref binding.LuaData, i);
                }
            }

            serializedObject.UpdateIfRequiredOrScript();
            var luaDataProp = serializedObject.FindProperty("LuaData");

            for (var i = 0; i < binding.LuaData.Length; i++)
            {
                var arrElem     = binding.LuaData[i];
                var elementProp = luaDataProp.GetArrayElementAtIndex(i);
                var dataProp    = elementProp.FindPropertyRelative("Data");
                arrElem.OnPropertyDrawer(dataProp);
            }

            if (Application.isPlaying)
            {
                foreach (var methodName in descriptor.DebugMethods)
                {
                    if (GUILayout.Button(methodName) && binding.LuaInstance != null)
                    {
                        var func = binding.LuaInstance.Get <Action <LuaTable> >(methodName);
                        func(binding.LuaInstance);
                    }
                }
            }

            serializedObject.ApplyModifiedProperties();
            base.OnInspectorGUI();
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("重新加载Lua文件"))
            {
                if (descriptor == null)
                {
                    return;
                }
                descriptor = LuaClassEditorFactory.ReloadDescriptor(descriptor.ClassName.Replace('.', '/'));
            }

            if (GUILayout.Button("在编辑器中打开"))
            {
                string idePath = EditorPrefs.GetString("kScriptsDefaultApp_h2657262712");
                var    luaPath = $"{Application.dataPath}/../Lua/{luaPathProp.stringValue.Replace('.', '/')}.lua";
                if (idePath.Contains("Rider"))
                {
                    Process.Start($"\"{idePath}\"", $"--line 0 {luaPath}");
                }
                else if (idePath.Contains("Code"))
                {
                    string binPath = idePath.Replace("Code.exe", "bin/code");
                    Process.Start($"\"{binPath}\"", $"-r -g \"{luaPath}:0\"");
                }
            }

            GUILayout.EndHorizontal();
        }