Exemple #1
0
        public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx)
        {
            if (Application.isPlaying)
            {
                var path       = ctx.assetPath.Substring(21, ctx.assetPath.Length - 21 - 4);
                var moduleName = path.Replace('/', '.');
                lock ( modifiedModules ) {
                    modifiedModules.Add(moduleName);
                }
            }

            ExecLuaCheck(ctx.assetPath);
            var text  = File.ReadAllText(ctx.assetPath);
            var asset = new TextAsset(text);

            using (var reader = new StringReader(asset.text)) {
                var line = reader.ReadLine();
                while (line != null)
                {
                    if (line.StartsWith("---@class"))
                    {
                        var statements = line.Split(' ');
                        var className  = statements[1];
                        LuaClassEditorFactory.ReloadDescriptor(className);
                        break;
                    }

                    line = reader.ReadLine();
                }
            }

            ctx.AddObjectToAsset("main obj", asset);
            ctx.SetMainObject(asset);
        }
Exemple #2
0
        private static void ExecLuaCheck(string luaPath)
        {
            luaPath = luaPath.Replace('\\', '/');
            var index       = luaPath.IndexOf("Lua/", StringComparison.InvariantCulture);
            var projectPath = luaPath.Substring(index + 4, luaPath.Length - index - 4 - 4);

            LuaClassEditorFactory.ReloadDescriptor(projectPath);
            var setting = LuaCheckSetting.GetOrCreateSettings();

            if (setting.Active && !string.IsNullOrEmpty(setting.LuaCheckExecPath) && File.Exists(setting.LuaCheckExecPath))
            {
                var proc = new Process {
                    StartInfo = new ProcessStartInfo {
                        FileName               = setting.LuaCheckExecPath,
                        Arguments              = $"{luaPath} --no-global --max-line-length {setting.MaxLineLength}",
                        UseShellExecute        = false,
                        RedirectStandardOutput = true,
                        CreateNoWindow         = true
                    }
                };
                proc.Start();
                var builder = new StringBuilder(1024);
                while (!proc.StandardOutput.EndOfStream)
                {
                    var line = proc.StandardOutput.ReadLine();
                    builder.AppendLine(line);
                }

                Debug.Log(builder.ToString());
            }
        }
        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);
        }
Exemple #4
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();
        }