Skip to content

Commit

Permalink
feat: add Owner field to IExtensionContext (#480)
Browse files Browse the repository at this point in the history
When set, errors that occur while activating or deactivating the context will
be properly attributed to the owning plugin.

Closes: #463
  • Loading branch information
bdunderscore authored Nov 28, 2024
1 parent 79e8d5f commit 2dcda1b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#473] Added `BuildContext.SetEnableUVDistributionRecalculation` to allow opting out from the automatic call to
`Mesh.RecalculateUVDistributionMetrics` on generated meshes.
- [#478] Added `ProfilerScope` API
- [#480] Added `IExtensionContext.Owner` API. Setting this property will allow errors to be correctly attributed to the
plugin that contains an extension context.

### Fixed

Expand Down
16 changes: 16 additions & 0 deletions Editor/API/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,18 @@ public void DeactivateExtensionContext(Type t)
{
var ctx = _activeExtensions[t];
Profiler.BeginSample("NDMF Deactivate: " + t);

using var scope = ErrorReport.WithContext(ctx);
try
{
ctx.OnDeactivate(this);
}
catch (Exception e)
{
// ensure we report the exception while the error report context is set
ErrorReport.ReportException(e);
return;
}
finally
{
Profiler.EndSample();
Expand Down Expand Up @@ -405,10 +413,18 @@ public IExtensionContext ActivateExtensionContext(Type ty)
if (!_activeExtensions.ContainsKey(ty))
{
Profiler.BeginSample("NDMF Activate: " + ty);

using var scope = ErrorReport.WithContext(ctx);
try
{
ctx.OnActivate(this);
}
catch (Exception e)
{
// ensure we report the exception while the error report context is set
ErrorReport.ReportException(e);
return null;
}
finally
{
Profiler.EndSample();
Expand Down
10 changes: 9 additions & 1 deletion Editor/API/IExtensionContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;

Expand All @@ -20,6 +22,12 @@ public interface IExtensionContext
/// </summary>
/// <param name="context"></param>
void OnDeactivate(BuildContext context);

/// <summary>
/// Return the plugin owning this extension context. Implementing this API is optional for backwards
/// compatibility, but is encouraged as it will provide better error messaging.
/// </summary>
PluginBase? Owner => null;
}

internal static class ExtensionContextUtil
Expand Down
9 changes: 9 additions & 0 deletions Editor/ErrorReporting/ErrorReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ internal IDisposable WithContext(PluginBase thePlugin)
CurrentContext.Plugin = thePlugin;
return scope;
}

internal IDisposable WithContext(IExtensionContext theExtension)
{
var scope = new RestoreContextScope(this);

if (theExtension.Owner != null) CurrentContext.Plugin = theExtension.Owner;

return scope;
}

internal IDisposable WithContextPassName(string name)
{
Expand Down

0 comments on commit 2dcda1b

Please sign in to comment.