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

feat: add Owner field to IExtensionContext #480

Merged
merged 1 commit into from
Nov 28, 2024
Merged
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
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