Skip to content

Commit

Permalink
feat: preview framework (#244)
Browse files Browse the repository at this point in the history
* feat: preview framework

Import the preview framework from NDMF-Core
(bdunderscore/ndmf-core@7af1614)

* feat: NDMF build pipeline integration for preview system

* chore: improve API docs

* chore: remove some spammy debug logs

* chore: adjustments to public API scope

* chore: CHANGELOG updates

* fix: invalidate all watchers when the scene changes

* chore: suppress spurious warnings

* chore: add 0Harmony.dll for standalone configurations

* fix: scene header shows as "deleted object" in some cases
  • Loading branch information
bdunderscore authored Jun 3, 2024
1 parent d48dd39 commit 64e0b51
Show file tree
Hide file tree
Showing 114 changed files with 7,587 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- [#244] - Added a framework that can be used to override the rendering of an object without modifying the object itself
- [#244] - Added a framework for observing scene object changes and reacting to them.
- [#244] - Added `SelfDestructComponent` (useful for hidden preview-only components)

### Fixed

Expand Down
21 changes: 21 additions & 0 deletions Dependencies~/0Harmony.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Andreas Pardeike

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions Dependencies~/0Harmony.LICENSE.txt.meta

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

Binary file added Dependencies~/0Harmony.dll
Binary file not shown.
33 changes: 33 additions & 0 deletions Dependencies~/0Harmony.dll.meta

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

14 changes: 14 additions & 0 deletions Editor/API/Fluent/Sequence/Sequence.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#region

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using nadena.dev.ndmf.model;
using nadena.dev.ndmf.preview;

#endregion

Expand Down Expand Up @@ -50,6 +52,18 @@ internal DeclaringPass(SolverPass pass, SolverContext solverContext, BuildPhase
_seq = seq;
}

public DeclaringPass PreviewingWith(IRenderFilter filter)
{
if (_pass.RenderFilter != null)
{
throw new InvalidOperationException("Render filter already set");
}

_pass.RenderFilter = filter;

return this;
}

/// <summary>
/// Declares that the pass you just declared must run before a particular other plugin.
///
Expand Down
2 changes: 2 additions & 0 deletions Editor/API/Model/SolverPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Immutable;
using nadena.dev.ndmf.preview;

#endregion

Expand All @@ -22,6 +23,7 @@ internal class SolverPass

internal IImmutableSet<Type> RequiredExtensions { get; set; }
internal IImmutableSet<string> CompatibleExtensions { get; set; }
internal IRenderFilter RenderFilter { get; set; }

internal bool IsExtensionCompatible(Type ty)
{
Expand Down
10 changes: 9 additions & 1 deletion Editor/API/Solver/PluginResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Immutable;
using System.Linq;
using nadena.dev.ndmf.model;
using UnityEngine;
using nadena.dev.ndmf.preview;

#endregion

Expand Down Expand Up @@ -49,6 +49,7 @@ internal ConcretePass(IPluginInternal plugin, IPass pass, ImmutableList<Type> de

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

public PluginResolver() : this(
Expand All @@ -70,6 +71,8 @@ public PluginResolver(IEnumerable<Type> plugins) : this(

public PluginResolver(IEnumerable<IPluginInternal> pluginTemplates)
{
PreviewSession = new PreviewSession();

var solverContext = new SolverContext();

foreach (var plugin in pluginTemplates)
Expand Down Expand Up @@ -195,6 +198,11 @@ ImmutableList<ConcretePass> ToConcretePasses(BuildPhase phase, IEnumerable<Solve

concrete.Add(new ConcretePass(pass.Plugin, pass.Pass, toDeactivate.ToImmutableList(),
toActivate.ToImmutableList()));

if (pass.RenderFilter != null)
{
PreviewSession.AddMutator(new SequencePoint(), pass.RenderFilter);
}
}

if (activeExtensions.Count > 0)
Expand Down
3 changes: 3 additions & 0 deletions Editor/ChangeStream.meta

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

204 changes: 204 additions & 0 deletions Editor/ChangeStream/ChangeStreamMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#region

using System;
using System.Diagnostics;
using UnityEditor;
using UnityEngine.Profiling;
using Debug = UnityEngine.Debug;

#endregion

namespace nadena.dev.ndmf.rq.unity.editor
{
internal class ChangeStreamMonitor
{
[InitializeOnLoadMethod]
static void Init()
{
ObjectChangeEvents.changesPublished += OnChange;
}

private static void OnChange(ref ObjectChangeEventStream stream)
{
Profiler.BeginSample("ChangeStreamMonitor.OnChange");

int length = stream.length;
for (int i = 0; i < length; i++)
{
try
{
HandleEvent(stream, i);
}
catch (Exception e)
{
Debug.LogError($"Error handling event {i}: {e}");
}
}

Profiler.EndSample();
}

private static void HandleEvent(ObjectChangeEventStream stream, int i)
{
switch (stream.GetEventType(i))
{
case ObjectChangeKind.None: break;

case ObjectChangeKind.ChangeScene:
{
ObjectWatcher.Instance.Hierarchy.InvalidateAll();

break;
}

case ObjectChangeKind.CreateGameObjectHierarchy:
{
stream.GetCreateGameObjectHierarchyEvent(i, out var data);

ObjectWatcher.Instance.Hierarchy.FireGameObjectCreate(data.instanceId);
break;
}

case ObjectChangeKind.ChangeGameObjectStructureHierarchy:
{
stream.GetChangeGameObjectStructureHierarchyEvent(i, out var data);

OnChangeGameObjectStructureHierarchy(data);

break;
}

case ObjectChangeKind.ChangeGameObjectStructure: // add/remove components
{
stream.GetChangeGameObjectStructureEvent(i, out var data);
OnChangeGameObjectStructure(data);

break;
}

case ObjectChangeKind.ChangeGameObjectParent:
{
stream.GetChangeGameObjectParentEvent(i, out var data);
OnChangeGameObjectParent(data);

break;
}

case ObjectChangeKind.ChangeGameObjectOrComponentProperties:
{
stream.GetChangeGameObjectOrComponentPropertiesEvent(i, out var data);
OnChangeGameObjectOrComponentProperties(data);

break;
}

case ObjectChangeKind.DestroyGameObjectHierarchy:
{
stream.GetDestroyGameObjectHierarchyEvent(i, out var data);
OnDestroyGameObjectHierarchy(data);

break;
}

case ObjectChangeKind.CreateAssetObject: break;
case ObjectChangeKind.DestroyAssetObject:
{
stream.GetDestroyAssetObjectEvent(i, out var data);
OnDestroyAssetObject(data);

break;
}

case ObjectChangeKind.ChangeAssetObjectProperties:
{
stream.GetChangeAssetObjectPropertiesEvent(i, out var data);
OnChangeAssetObjectProperties(data);

break;
}

case ObjectChangeKind.UpdatePrefabInstances:
{
stream.GetUpdatePrefabInstancesEvent(i, out var data);
OnUpdatePrefabInstances(data);

break;
}

case ObjectChangeKind.ChangeChildrenOrder:
{
stream.GetChangeChildrenOrderEvent(i, out var data);
OnChangeChildrenOrder(data);

break;
}
}
}

private static void OnChangeChildrenOrder(ChangeChildrenOrderEventArgs data)
{
var instanceId = data.instanceId;

ObjectWatcher.Instance.Hierarchy.FireReorderNotification(instanceId);
}

private static void OnUpdatePrefabInstances(UpdatePrefabInstancesEventArgs data)
{
foreach (var iid in data.instanceIds)
{
ObjectWatcher.Instance.Hierarchy.InvalidateTree(iid);
}
}

private static void OnChangeAssetObjectProperties(ChangeAssetObjectPropertiesEventArgs data)
{
var instanceId = data.instanceId;

ObjectWatcher.Instance.Hierarchy.FireObjectChangeNotification(instanceId);
}

private static void OnDestroyAssetObject(DestroyAssetObjectEventArgs data)
{
var instanceId = data.instanceId;

ObjectWatcher.Instance.Hierarchy.InvalidateTree(instanceId);
}

private static void OnDestroyGameObjectHierarchy(DestroyGameObjectHierarchyEventArgs data)
{
var instanceId = data.instanceId;

ObjectWatcher.Instance.Hierarchy.InvalidateTree(instanceId);
}

private static void OnChangeGameObjectOrComponentProperties(ChangeGameObjectOrComponentPropertiesEventArgs data)
{
var instanceId = data.instanceId;

ObjectWatcher.Instance.Hierarchy.FireObjectChangeNotification(instanceId);
}

private static void OnChangeGameObjectParent(ChangeGameObjectParentEventArgs data)
{
var instanceId = data.instanceId;
var priorParentId = data.previousParentInstanceId;
var newParentId = data.newParentInstanceId;

ObjectWatcher.Instance.Hierarchy.FireReparentNotification(instanceId);
}

private static void OnChangeGameObjectStructure(ChangeGameObjectStructureEventArgs data)
{
var instanceId = data.instanceId;

ObjectWatcher.Instance.Hierarchy.FireStructureChangeEvent(instanceId);
}

private static void OnChangeGameObjectStructureHierarchy(ChangeGameObjectStructureHierarchyEventArgs data)
{
var instanceId = data.instanceId;

ObjectWatcher.Instance.Hierarchy.InvalidateTree(instanceId);
}
}
}
3 changes: 3 additions & 0 deletions Editor/ChangeStream/ChangeStreamMonitor.cs.meta

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

Loading

0 comments on commit 64e0b51

Please sign in to comment.