Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporarily disable some NDMF plugin #532

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- [#532] Window to temporarily disable some NDMF plugins

### Fixed

Expand Down
51 changes: 38 additions & 13 deletions Editor/API/Solver/PluginResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using JetBrains.Annotations;
using nadena.dev.ndmf.model;
using nadena.dev.ndmf.preview;
using nadena.dev.ndmf.preview.UI;
Expand Down Expand Up @@ -51,30 +52,52 @@ internal ConcretePass(IPluginInternal plugin, IPass pass, ImmutableList<Type> de
}
}

/// <summary>
/// The class manages if each plugin is temporarily disabled.
/// </summary>
internal static class TemporalPluginDisable
{
// parameter: plugin id, new state
public static event Action<string, bool> OnPluginDisableChanged;

private const string SessionStateKey = "nadena.dev.ndmf.plugin-temporally-disabled.";

public static bool IsPluginDisabled(string pluginId) => UnityEditor.SessionState.GetBool(SessionStateKey + pluginId, false);
public static void SetPluginDisabled(string pluginId, bool state)
{
UnityEditor.SessionState.SetBool(SessionStateKey + pluginId, state);
OnPluginDisableChanged?.Invoke(pluginId, state);
}
}

internal class PluginResolver
{
internal ImmutableList<(BuildPhase, IList<ConcretePass>)> Passes { get; }

private readonly List<SolverPass> _allPasses = new();

public PluginResolver() : this(
AppDomain.CurrentDomain.GetAssemblies().SelectMany(
assembly => assembly.GetCustomAttributes(typeof(ExportsPlugin), false))
.Select(export => ((ExportsPlugin) export).PluginType)
.ToImmutableSortedSet(new TypeComparer())
.Prepend(typeof(InternalPasses))
)

public static IEnumerable<Type> FindPassTypes() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(
assembly => assembly.GetCustomAttributes(typeof(ExportsPlugin), false))
.Select(export => ((ExportsPlugin)export).PluginType)
.ToImmutableSortedSet(new TypeComparer())
.Prepend(typeof(InternalPasses));

[CanBeNull] private static IPluginInternal InstantiatePlugin(Type pluginType) =>
pluginType.GetConstructor(Type.EmptyTypes)?.Invoke(Array.Empty<object>()) as IPluginInternal;

[ItemCanBeNull]
public static IEnumerable<IPluginInternal> FindAllPlugins() => FindPassTypes().Select(InstantiatePlugin);

public PluginResolver(bool includeDisabled = false) : this(FindPassTypes(), includeDisabled)
{
}

public PluginResolver(IEnumerable<Type> plugins) : this(
plugins.Select(plugin =>
plugin.GetConstructor(new Type[0]).Invoke(new object[0]) as IPluginInternal)
)
public PluginResolver(IEnumerable<Type> plugins, bool includeDisabled = false)
: this(plugins.Select(InstantiatePlugin), includeDisabled)
{
}

public PluginResolver(IEnumerable<IPluginInternal> pluginTemplates)
public PluginResolver(IEnumerable<IPluginInternal> pluginTemplates, bool includeDisabled = false)
{
var solverContext = new SolverContext();

Expand Down Expand Up @@ -159,6 +182,7 @@ public PluginResolver(IEnumerable<IPluginInternal> pluginTemplates)
#endif

var sorted = TopoSort.DoSort(passes, constraints);
if (!includeDisabled) sorted.RemoveAll(pass => TemporalPluginDisable.IsPluginDisabled(pass.Plugin.QualifiedName));
_allPasses.AddRange(sorted);

var concrete = ToConcretePasses(phase, sorted);
Expand Down Expand Up @@ -270,6 +294,7 @@ internal PreviewSession PreviewSession
foreach (var pass in _allPasses)
{
if (!PreviewPrefs.instance.IsPreviewPluginEnabled(pass.Plugin.QualifiedName)) continue;
if (TemporalPluginDisable.IsPluginDisabled(pass.Plugin.QualifiedName)) continue;

foreach (var filter in pass.RenderFilters)
session.AddMutator(new SequencePoint(), filter);
Expand Down
2 changes: 1 addition & 1 deletion Editor/PreviewSystem/NDMFPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static void Init()
{
Menu.SetChecked(Menus.ENABLE_PREVIEW_MENU_NAME, EnablePreviewsUI);

var resolver = new PluginResolver();
var resolver = new PluginResolver(includeDisabled: true);
_globalPreviewSession = resolver.PreviewSession;
PreviewSession.Current = resolver.PreviewSession;

Expand Down
5 changes: 5 additions & 0 deletions Editor/PreviewSystem/UI/PreviewPrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ private void OnValidate()
_nodeStates = _savedNodeStates.ToDictionary(kv => kv.key, kv => kv.value);
}

private void OnEnable()
{
TemporalPluginDisable.OnPluginDisableChanged += (_, _) => OnPreviewConfigChanged?.Invoke();
}

public bool GetNodeState(string qualifiedName, bool defaultValue)
{
if (_nodeStates == null) OnValidate();
Expand Down
25 changes: 20 additions & 5 deletions Editor/PreviewSystem/UI/PreviewPrefsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class PreviewPrefsUI : EditorWindow
{
internal static bool DISABLE_WINDOW = false;

private ImmutableList<(BuildPhase, IList<ConcretePass>)> _passes = new PluginResolver().Passes;
private readonly List<TreeViewItemData<ItemData>> _treeViewData = new();
private TreeView _treeView;

Expand All @@ -37,6 +36,14 @@ public static void ShowPreviewConfigWindow()
private void OnEnable()
{
BuildTreeViewData();
TemporalPluginDisable.OnPluginDisableChanged += OnPluginDisableChanged;
}

private void OnPluginDisableChanged(string arg1, bool arg2) => _treeView.Rebuild();

private void OnDisable()
{
TemporalPluginDisable.OnPluginDisableChanged -= OnPluginDisableChanged;
}

private class InternalNode
Expand Down Expand Up @@ -79,7 +86,14 @@ private InternalNode CreatePluginNode(PluginBase plugin)
{
return new InternalNode
{
DisplayName = () => plugin.DisplayName,
DisplayName = () =>
{
var name = "";
if (TemporalPluginDisable.IsPluginDisabled(plugin.QualifiedName))
name += "(Disabled) ";
name += plugin.DisplayName;
return name;
},
QualifiedName = plugin.QualifiedName,
SetEnabled = b => { PreviewPrefs.instance.SetPreviewPluginEnabled(plugin.QualifiedName, b); },
IsEnabled = _ => PreviewPrefs.instance.IsPreviewPluginEnabled(plugin.QualifiedName)
Expand All @@ -89,7 +103,7 @@ private InternalNode CreatePluginNode(PluginBase plugin)
private void BuildTreeViewData()
{
var nodesByPlugin =
new PluginResolver().Passes
new PluginResolver(includeDisabled: true).Passes
.SelectMany(kv => kv.Item2)
.Where(pass => pass.HasPreviews)
.OrderBy(pass => pass.Description)
Expand All @@ -103,12 +117,13 @@ private void BuildTreeViewData()
var id = 0;
foreach (var (plugin, nodes) in nodesByPlugin)
{
var pluginNode = CreatePluginNode((PluginBase)plugin);
var pluginItemData = new ItemData
{
IsPass = false,
QualifiedName = plugin.QualifiedName,
DisplayName = () => plugin.DisplayName,
Node = CreatePluginNode((PluginBase)plugin)
DisplayName = pluginNode.DisplayName,
Node = pluginNode
};

var items = new List<TreeViewItemData<ItemData>>();
Expand Down
56 changes: 56 additions & 0 deletions Editor/UI/DisableWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#region

using System;
using System.Collections.Generic;
using System.Linq;
using nadena.dev.ndmf.reporting;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

#endregion

namespace nadena.dev.ndmf.ui
{
internal class DisableWindow : EditorWindow
{
[MenuItem("Tools/NDM Framework/Debug Tools/Temporally Disable Plugins", false, 100)]
public static void ShowWindow()
{
GetWindow<DisableWindow>("Temporally Disable Plugins");
}

private List<IPluginInternal> _plugins = null!;

private void OnEnable()
{
_plugins = PluginResolver.FindAllPlugins().ToList();
TemporalPluginDisable.OnPluginDisableChanged += OnDisableChanged;
}

private void OnDisable()
{
TemporalPluginDisable.OnPluginDisableChanged -= OnDisableChanged;
}

private void OnDisableChanged(string _, bool _1) => Repaint();

void OnGUI()
{
EditorGUILayout.HelpBox(
"This window allows you to temporally disable plugins for the current session.\n" +
"Configuration you made will be rolled back after restarting the Unity.",
MessageType.None);

foreach (var plugin in _plugins)
{
var disabled = TemporalPluginDisable.IsPluginDisabled(plugin.QualifiedName);
var newDisabled = EditorGUILayout.ToggleLeft($"{plugin.DisplayName} ({plugin.QualifiedName})", disabled);
if (newDisabled != disabled)
{
TemporalPluginDisable.SetPluginDisabled(plugin.QualifiedName, newDisabled);
}
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/UI/DisableWindow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 68 additions & 13 deletions Editor/UI/SolverWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region

using System;
using System.Collections.Generic;
using System.Linq;
using nadena.dev.ndmf.reporting;
Expand Down Expand Up @@ -29,6 +30,7 @@ private void OnEnable()

private void OnDisable()
{
_solverUI?.Dispose();
BuildEvent.OnBuildEvent -= OnBuildEvent;
}

Expand All @@ -52,11 +54,14 @@ void OnGUI()
internal class SolverUIItem : TreeViewItem
{
public double? ExecutionTimeMS;
public bool IsDisabled;
public bool IsPlugin;
}

internal class SolverUI : TreeView
internal class SolverUI : TreeView, IDisposable
{
private static PluginResolver Resolver = new PluginResolver();
private static PluginResolver Resolver = new PluginResolver(includeDisabled: true);
private Dictionary<string, List<SolverUIItem>> _pluginItems = new();

public SolverUI() : this(new TreeViewState())
{
Expand All @@ -65,6 +70,30 @@ public SolverUI() : this(new TreeViewState())
public SolverUI(TreeViewState state) : base(state)
{
Reload();

TemporalPluginDisable.OnPluginDisableChanged += OnPluginDisableChanged;
}

private void OnPluginDisableChanged(string pluginId, bool disabled)
{
if (_pluginItems.TryGetValue(pluginId, out var items))
{
foreach (var item in items)
{
item.IsDisabled = disabled;
foreach (var childItem in item.children.OfType<SolverUIItem>())
{
childItem.IsDisabled = disabled;
}
}
}

Repaint();
}

public void Dispose()
{
TemporalPluginDisable.OnPluginDisableChanged -= OnPluginDisableChanged;
}

BuildEvent.PassExecuted NextPassExecuted(IEnumerator<BuildEvent> events)
Expand All @@ -81,6 +110,7 @@ protected override TreeViewItem BuildRoot()
{
var root = new SolverUIItem() {id = 0, depth = -1, displayName = "Avatar Build"};
var allItems = new List<SolverUIItem>();
_pluginItems.Clear();

int id = 1;

Expand All @@ -96,19 +126,35 @@ protected override TreeViewItem BuildRoot()
allItems.Add(new SolverUIItem() {id = id++, depth = 1, displayName = phase.ToString()});
var phaseItem = allItems[allItems.Count - 1];

var isDisabled = false;

foreach (var pass in passes)
{
if (pass.InstantiatedPass.IsPhantom) continue;

var plugin = pass.Plugin;
if (plugin != priorPlugin)
{
allItems.Add(new SolverUIItem() {id = id++, depth = 2, displayName = $"{plugin.DisplayName} ({plugin.QualifiedName})"});
isDisabled = TemporalPluginDisable.IsPluginDisabled(plugin.QualifiedName);
pluginItem = new SolverUIItem()
{
id = id++,
depth = 2,
displayName = $"{plugin.DisplayName} ({plugin.QualifiedName})",
IsDisabled = isDisabled,
IsPlugin = true,
};
allItems.Add(pluginItem);
priorPlugin = plugin;
pluginItem = allItems[allItems.Count - 1];

if (!_pluginItems.TryGetValue(plugin.QualifiedName, out var pluginItems)) _pluginItems.Add(plugin.QualifiedName, pluginItems = new List<SolverUIItem>());
pluginItems.Add(pluginItem);
}

allItems.Add(new SolverUIItem() {id = id++, depth = 3, displayName = pass.Description});
allItems.Add(new SolverUIItem() {id = id++, depth = 3, displayName = pass.Description, IsDisabled = isDisabled});

if (isDisabled) continue;

BuildEvent.PassExecuted passEvent;

do
Expand Down Expand Up @@ -159,17 +205,26 @@ protected override TreeViewItem BuildRoot()
}
}

foreach (var pass in allItems)
{
if (pass.ExecutionTimeMS.HasValue)
{
pass.displayName = $"({pass.ExecutionTimeMS:F}ms) {pass.displayName}";
}
}

SetupParentsAndChildrenFromDepths(root, allItems.Select(i => (TreeViewItem) i).ToList());

return root;
}

protected override void RowGUI(RowGUIArgs args)
{
if (args.item is not SolverUIItem pass)
{
base.RowGUI(args);
return;
}

EditorGUI.BeginDisabledGroup(pass.IsDisabled);
args.label = "";
if (pass.ExecutionTimeMS is {} executionTimeMS) args.label += $"({executionTimeMS:F}ms) ";
if (pass.IsDisabled && pass.IsPlugin) args.label += "(Disabled) ";
args.label += pass.displayName;
base.RowGUI(args);
EditorGUI.EndDisabledGroup();
}
}
}