Beispiel #1
0
        private static string[] FindAndLoadScriptingAssemblies(string gameDataPath)
        {
            var gameDataSettings = GameDataSettings.Load(gameDataPath);
            // enumerate loaded assemblies by name and fullname
            var loadedAssemblies = (
                from assembly in AppDomain.CurrentDomain.GetAssemblies()
                where assembly is AssemblyBuilder == false && string.IsNullOrEmpty(assembly.Location) == false
                let assemblyName = assembly.GetName(false)
                                   from name in new[] { assemblyName.Name, assemblyName.FullName }
                select new { name, assembly.Location }
                ).ToLookup(a => a.name, a => a.Location);
            // get scripting assemblies from gamedata settings and add all internal scripting assemblies with prefix "Assembly-"
            var scriptingAssemblies = (gameDataSettings.ScriptingAssemblies ?? Enumerable.Empty <string>())
                                      .Union(loadedAssemblies.Where(k => k.Key.StartsWith("Assembly-", StringComparison.Ordinal)).Select(k => k.Key).Distinct());

            var foundScriptingAssemblies = new HashSet <string>();

            foreach (var assemblyName in scriptingAssemblies)
            {
                var path = assemblyName;
                // found as path
                if (File.Exists(path))
                {
                    foundScriptingAssemblies.Add(Path.GetFullPath(path));
                    continue;
                }
                path = loadedAssemblies[assemblyName].FirstOrDefault();
                if (path != null && File.Exists(path))
                {
                    foundScriptingAssemblies.Add(Path.GetFullPath(path));
                    continue;
                }

                try
                {
                    var assembly = AppDomain.CurrentDomain.Load(assemblyName);
                    if (File.Exists(assembly.Location))
                    {
                        foundScriptingAssemblies.Add(Path.GetFullPath(assembly.Location));
                    }
                    continue;
                }
                catch (Exception loadError)
                {
                    if (Settings.Current.Verbose)
                    {
                        Debug.LogWarning("Failed to load assembly with name '" + assemblyName + "':\r\n" + loadError);
                    }
                }

                Debug.LogWarning("Failed to find scripting assembly with name '" + assemblyName + "'.");
            }

            return(foundScriptingAssemblies.ToArray());
        }
Beispiel #2
0
        public override void OnInspectorGUI()
        {
            var gameDataAsset = (Object)this.target;
            var gameDataPath  = FileAndPathUtils.MakeProjectRelative(AssetDatabase.GetAssetPath(gameDataAsset));

            var assetPath = FileAndPathUtils.MakeProjectRelative(AssetDatabase.GetAssetPath(Selection.activeObject));

            if (GameDataTracker.IsGameDataFile(assetPath) == false)
            {
                this.DrawDefaultInspector();
                return;
            }

            if (this.lastAsset != gameDataAsset || this.gameDataSettings == null)
            {
                this.gameDataSettings = GameDataSettings.Load(gameDataPath);
                this.gameDataSettings.ScriptingAssemblies = this.gameDataSettings.ScriptingAssemblies ?? new string[0];
                this.lastAsset                = gameDataAsset;
                this.newScriptingAssembly     = null;
                this.newScriptingAssemblyName = null;
            }
            GUI.enabled = true;
            GUILayout.Label(Path.GetFileName(gameDataPath), EditorStyles.boldLabel);
            this.gameDataSettings.Generator = (int)(GameDataSettings.CodeGenerator)EditorGUILayout.EnumPopup(Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_GENERATOR, (GameDataSettings.CodeGenerator) this.gameDataSettings.Generator);
            if (this.gameDataSettings.Generator != (int)GameDataSettings.CodeGenerator.None)
            {
                this.codeGenerationFold = EditorGUILayout.Foldout(this.codeGenerationFold, Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_GENERATION_LABEL);
                if (this.codeGenerationFold)
                {
                    this.gameDataSettings.AutoGeneration = EditorGUILayout.Toggle(Resources.UI_UNITYPLUGIN_INSPECTOR_AUTO_GENERATION,
                                                                                  this.gameDataSettings.AutoGeneration);
                    var codeAsset  = !string.IsNullOrEmpty(this.gameDataSettings.CodeGenerationPath) && File.Exists(this.gameDataSettings.CodeGenerationPath) ? AssetDatabase.LoadAssetAtPath <TextAsset>(this.gameDataSettings.CodeGenerationPath) : null;
                    var assetAsset = !string.IsNullOrEmpty(this.gameDataSettings.AssetGenerationPath) && File.Exists(this.gameDataSettings.AssetGenerationPath) ? AssetDatabase.LoadAssetAtPath <ScriptableObject>(this.gameDataSettings.AssetGenerationPath) : null;

                    if (codeAsset != null)
                    {
                        this.gameDataSettings.CodeGenerationPath = AssetDatabase.GetAssetPath(EditorGUILayout.ObjectField(Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_GENERATION_PATH, codeAsset, typeof(TextAsset), false));
                    }
                    else
                    {
                        this.gameDataSettings.CodeGenerationPath = EditorGUILayout.TextField(Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_GENERATION_PATH,
                                                                                             this.gameDataSettings.CodeGenerationPath);
                    }

                    if (this.gameDataSettings.Generator == (int)GameDataSettings.CodeGenerator.CSharpCodeAndAsset)
                    {
                        if (assetAsset != null)
                        {
                            this.gameDataSettings.AssetGenerationPath = AssetDatabase.GetAssetPath(EditorGUILayout.ObjectField(Resources.UI_UNITYPLUGIN_INSPECTOR_ASSET_GENERATION_PATH, assetAsset, typeof(ScriptableObject), false));
                        }
                        else
                        {
                            this.gameDataSettings.AssetGenerationPath = EditorGUILayout.TextField(Resources.UI_UNITYPLUGIN_INSPECTOR_ASSET_GENERATION_PATH, this.gameDataSettings.AssetGenerationPath);
                        }
                    }

                    if ((this.gameDataSettings.Generator == (int)GameDataSettings.CodeGenerator.CSharp ||
                         this.gameDataSettings.Generator == (int)GameDataSettings.CodeGenerator.CSharpCodeAndAsset) &&
                        Path.GetExtension(this.gameDataSettings.CodeGenerationPath) != ".cs")
                    {
                        this.gameDataSettings.CodeGenerationPath = Path.ChangeExtension(this.gameDataSettings.CodeGenerationPath, ".cs");
                    }
                    if (this.gameDataSettings.Generator == (int)GameDataSettings.CodeGenerator.CSharpCodeAndAsset &&
                        Path.GetExtension(this.gameDataSettings.AssetGenerationPath) != ".asset")
                    {
                        this.gameDataSettings.AssetGenerationPath = Path.ChangeExtension(this.gameDataSettings.AssetGenerationPath, ".asset");
                    }

                    this.gameDataSettings.Namespace = EditorGUILayout.TextField(Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_NAMESPACE,
                                                                                this.gameDataSettings.Namespace);
                    this.gameDataSettings.GameDataClassName = EditorGUILayout.TextField(Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_API_CLASS_NAME,
                                                                                        this.gameDataSettings.GameDataClassName);
                    this.gameDataSettings.DocumentClassName = EditorGUILayout.TextField(Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_DOCUMENT_CLASS_NAME,
                                                                                        this.gameDataSettings.DocumentClassName);
                    this.gameDataSettings.LineEnding = (int)(GameDataSettings.LineEndings)EditorGUILayout.EnumPopup(
                        Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_LINE_ENDINGS, (GameDataSettings.LineEndings) this.gameDataSettings.LineEnding);
                    this.gameDataSettings.Indentation = (int)(GameDataSettings.Indentations)EditorGUILayout.EnumPopup(
                        Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_INDENTATION, (GameDataSettings.Indentations) this.gameDataSettings.Indentation);
                    this.gameDataSettings.Options = (int)(CodeGenerationOptions)EditorGUILayout.EnumMaskField(
                        Resources.UI_UNITYPLUGIN_INSPECTOR_CODE_OPTIONS, (CodeGenerationOptions)this.gameDataSettings.Options);
                }
            }

            this.scriptingAssembliesFold = EditorGUILayout.Foldout(this.scriptingAssembliesFold, Resources.UI_UNITYPLUGIN_INSPECTOR_SCRIPTING_ASSEMBLIES_LABEL);
            if (this.scriptingAssembliesFold)
            {
                for (var i = 0; i < this.gameDataSettings.ScriptingAssemblies.Length; i++)
                {
                    var watchedAssetPath = this.gameDataSettings.ScriptingAssemblies[i];
                    var assetExists      = !string.IsNullOrEmpty(watchedAssetPath) && (File.Exists(watchedAssetPath) || Directory.Exists(watchedAssetPath));
                    var watchedAsset     = assetExists ? AssetDatabase.LoadMainAssetAtPath(watchedAssetPath) : null;
                    if (watchedAsset != null)
                    {
                        this.gameDataSettings.ScriptingAssemblies[i] = AssetDatabase.GetAssetPath(EditorGUILayout.ObjectField(Resources.UI_UNITYPLUGIN_INSPECTOR_ASSET_LABEL, watchedAsset, typeof(Object), false));
                    }
                    else
                    {
                        this.gameDataSettings.ScriptingAssemblies[i] = EditorGUILayout.TextField(Resources.UI_UNITYPLUGIN_INSPECTOR_NAME_LABEL, watchedAssetPath);
                    }
                }
                EditorGUILayout.Space();
                this.newScriptingAssembly = EditorGUILayout.ObjectField("<" + Resources.UI_UNITYPLUGIN_INSPECTOR_ADD_ASSET_BUTTON + ">", this.newScriptingAssembly, typeof(Object), false);
                if (Event.current.type == EventType.repaint && this.newScriptingAssembly != null)
                {
                    var assemblies = new HashSet <string>(this.gameDataSettings.ScriptingAssemblies);
                    assemblies.Remove("");
                    assemblies.Add(AssetDatabase.GetAssetPath(this.newScriptingAssembly));
                    this.gameDataSettings.ScriptingAssemblies = assemblies.ToArray();
                    this.newScriptingAssembly = null;
                    GUI.changed = true;
                }
                EditorGUILayout.BeginHorizontal();
                this.newScriptingAssemblyName = EditorGUILayout.TextField("<" + Resources.UI_UNITYPLUGIN_INSPECTOR_ADD_NAME_BUTTON + ">", this.newScriptingAssemblyName);
                if (GUILayout.Button(Resources.UI_UNITYPLUGIN_INSPECTOR_ADD_BUTTON, EditorStyles.toolbarButton, GUILayout.Width(70), GUILayout.Height(18)))
                {
                    var assemblies = new HashSet <string>(this.gameDataSettings.ScriptingAssemblies);
                    assemblies.Remove("");
                    assemblies.Add(this.newScriptingAssemblyName);
                    this.gameDataSettings.ScriptingAssemblies = assemblies.ToArray();
                    this.newScriptingAssemblyName             = null;
                    GUI.changed = true;
                    this.Repaint();
                }
                GUILayout.Space(5);
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.Space();
            GUILayout.Label(Resources.UI_UNITYPLUGIN_INSPECTOR_ACTIONS_GROUP, EditorStyles.boldLabel);

            if (EditorApplication.isCompiling)
            {
                EditorGUILayout.HelpBox(Resources.UI_UNITYPLUGIN_COMPILING_WARNING, MessageType.Warning);
            }
            else if (CoroutineScheduler.IsRunning)
            {
                EditorGUILayout.HelpBox(Resources.UI_UNITYPLUGIN_COROUTINE_IS_RUNNIG_WARNING, MessageType.Warning);
            }

            EditorGUILayout.BeginHorizontal();
            GUI.enabled = !CoroutineScheduler.IsRunning && !EditorApplication.isCompiling;
            if (GUILayout.Button(Resources.UI_UNITYPLUGIN_INSPECTOR_EDIT_BUTTON))
            {
                AssetDatabase.OpenAsset(gameDataAsset);
                this.Repaint();
            }
            if (this.gameDataSettings.Generator != (int)GameDataSettings.CodeGenerator.None && string.IsNullOrEmpty(this.gameDataSettings.CodeGenerationPath) == false)
            {
                if (GUILayout.Button(Resources.UI_UNITYPLUGIN_INSPECTOR_RUN_GENERATOR_BUTTON))
                {
                    CoroutineScheduler.Schedule(Menu.GenerateCodeAndAssetsAsync(gameDataPath, ProgressUtils.ReportToLog(Resources.UI_UNITYPLUGIN_INSPECTOR_GENERATION_PREFIX + " ")), "generation::" + gameDataPath)
                    .ContinueWith(_ => this.Repaint());
                }
            }
            GUI.enabled = !CoroutineScheduler.IsRunning && !EditorApplication.isCompiling;
            if (GUILayout.Button(Resources.UI_UNITYPLUGIN_INSPECTOR_VALIDATE_BUTTON))
            {
                CoroutineScheduler.Schedule(Menu.ValidateAsync(gameDataPath, ProgressUtils.ReportToLog(Resources.UI_UNITYPLUGIN_INSPECTOR_VALIDATION_PREFIX + " ")), "validation::" + gameDataPath)
                .ContinueWith(_ => this.Repaint());
            }
            if (GUILayout.Button(Resources.UI_UNITYPLUGIN_INSPECTOR_BACKUP_BUTTON))
            {
                this.Backup(gameDataPath);
            }
            if (GUILayout.Button(Resources.UI_UNITYPLUGIN_INSPECTOR_RESTORE_BUTTON))
            {
                this.Restore(gameDataPath);
            }
            EditorGUILayout.EndHorizontal();
            GUI.enabled = true;

            if (GUI.changed)
            {
                this.gameDataSettings.Save(gameDataPath);
            }
        }