using System;
using System.Collections.Generic;
using MonoMod.Cil;
namespace CalamityMod.ILEditing;
///
/// Simple helper object to capture multiple IL edits to apply in a single
/// pass to avoid additional overhead.
///
///
/// Since multiple edits are applied at once, a failure from one edit will
/// cause every edit to fail. Use with caution.
///
internal sealed class ManipulatorBatch : IDisposable
{
public required Action ManipulatorProvider { get; init; }
private readonly List manipulators = [];
///
/// Adds a manipulator to be applied when this object is disposed.
///
public void Add(ILContext.Manipulator manipulator)
{
manipulators.Add(manipulator);
}
///
/// Applies the queued manipulators.
///
public void Dispose()
{
var theManipulators = manipulators;
ManipulatorProvider?.Invoke(
il =>
{
foreach (var manipulator in theManipulators)
{
manipulator?.Invoke(il);
}
}
);
}
///
/// Initializes a batch from
///
///
///
public static ManipulatorBatch From(Action manipulatorProvider)
{
ArgumentNullException.ThrowIfNull(manipulatorProvider);
return new ManipulatorBatch { ManipulatorProvider = manipulatorProvider };
}
}