Skip to content

Commit

Permalink
No longer save plugin state on shutdown. Use callbacks of edit contro…
Browse files Browse the repository at this point in the history
…ller instead. Fixes issue #3.

The state gets written to the pin/channel on
- last `IComponentHandler.endEdit` call
- `IComponentHandler.restartComponent` call with `kParamValuesChanged`
- `IComponentHandler2.setDirty` with `true`
  • Loading branch information
azeno committed Nov 15, 2024
1 parent 7b5fe98 commit e3aea7f
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/EffectHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal partial class EffectHost : FactoryBasedVLNode, IVLNode, IComponentHandl

private readonly object processingLock = new();
private bool isDisposed;
private int editCount;

private readonly NodeContext nodeContext;
private readonly ILogger logger;
Expand Down Expand Up @@ -184,11 +185,6 @@ public void Dispose()
HideEditor();
audioOutput.Dispose();

var state = PluginState.From(plugProvider.ClassInfo.ID, component, controller);
var statePin = (StatePin)Inputs[0];
var channel = statePin.Value;
SaveToChannelOrPin(channel, StateInputPinName, state);

processor.SetProcessing_IgnoreNotImplementedException(false);
component.setActive(false);

Expand All @@ -197,6 +193,14 @@ public void Dispose()
}
}

private void SavePluginState()
{
var state = PluginState.From(plugProvider.ClassInfo.ID, component, controller);
var statePin = (StatePin)Inputs[0];
var channel = statePin.Value;
SaveToChannelOrPin(channel, StateInputPinName, state);
}

private static bool Acknowledge<T>(ref T current, T value)
{
if (!EqualityComparer<T>.Default.Equals(current, value))
Expand Down Expand Up @@ -535,11 +539,17 @@ private void SetParameter(uint id, double normalizedValue)
void IComponentHandler.beginEdit(uint id)
{
logger.LogTrace("Begin edit for parameter {id}", id);

editCount++;
}

void IComponentHandler.endEdit(uint id)
{
logger.LogTrace("End edit for parameter {id}", id);

editCount--;
if (editCount == 0)
SavePluginState();
}

void IComponentHandler.performEdit(uint id, double valueNormalized)
Expand All @@ -554,11 +564,17 @@ void IComponentHandler.performEdit(uint id, double valueNormalized)
void IComponentHandler.restartComponent(RestartFlags flags)
{
logger.LogTrace("Restarting component with flags {flags}", flags);

if (flags.HasFlag(RestartFlags.kParamValuesChanged))
SavePluginState();
}

void IComponentHandler2.setDirty(bool state)
{
logger.LogTrace("Setting dirty state to {state}", state);

if (true)
SavePluginState();
}

void IComponentHandler2.requestOpenEditor(string name)
Expand All @@ -578,6 +594,10 @@ void IComponentHandler2.finishGroupEdit()

private void SaveToChannelOrPin<T>(IChannel<T>? channel, string pinName, T value)
{
// Only write changes to the channel. Avoids document marked as dirty on open.
if (channel != null && Equals(channel.Value, value))
return;

if (channel is null || channel.IsSystemGenerated())
SaveToPin(pinName, value);
else
Expand Down

0 comments on commit e3aea7f

Please sign in to comment.