135 lines
4.3 KiB
C#
135 lines
4.3 KiB
C#
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using YachtDice.Modifiers.Core;
|
|
using YachtDice.Modifiers.Definition;
|
|
using YachtDice.Modifiers.Runtime;
|
|
|
|
namespace YachtDice.Modifiers.Pipeline
|
|
{
|
|
public class ModifierPipeline
|
|
{
|
|
private readonly ModifierRegistry _registry;
|
|
private readonly List<EffectEntry> _effectBuffer = new();
|
|
private bool _isExecuting;
|
|
private readonly Queue<(TriggerType trigger, ModifierContext context)> _deferredQueue = new();
|
|
|
|
private const int MaxRecursionDepth = 1;
|
|
private int _currentDepth;
|
|
|
|
public bool TracingEnabled { get; set; }
|
|
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
|
= true;
|
|
#endif
|
|
|
|
public ModifierPipeline(ModifierRegistry registry)
|
|
{
|
|
this._registry = registry;
|
|
}
|
|
|
|
public async UniTask<ModifierContext> Execute(TriggerType trigger, ModifierContext context)
|
|
{
|
|
if (_isExecuting)
|
|
{
|
|
if (_currentDepth >= MaxRecursionDepth)
|
|
{
|
|
Debug.LogWarning($"[ModifierPipeline] Max recursion depth reached for trigger {trigger}. Dropping.");
|
|
return context;
|
|
}
|
|
|
|
_currentDepth++;
|
|
}
|
|
|
|
_isExecuting = true;
|
|
context.Trigger = trigger;
|
|
|
|
PipelineTrace trace = null;
|
|
if (TracingEnabled)
|
|
{
|
|
trace = new PipelineTrace { Trigger = trigger };
|
|
context.DebugLog ??= new List<string>();
|
|
}
|
|
|
|
_effectBuffer.Clear();
|
|
|
|
// Snapshot active modifiers to avoid modification during iteration
|
|
var activeSnapshot = _registry.Active;
|
|
|
|
// Gather eligible effects
|
|
foreach (var inst in activeSnapshot)
|
|
{
|
|
var behaviors = inst.Definition.Behaviors;
|
|
|
|
foreach (var behavior in behaviors)
|
|
{
|
|
if (behavior == null) continue;
|
|
if (behavior.Trigger != trigger) continue;
|
|
|
|
// Evaluate conditions with tracing
|
|
bool passed = true;
|
|
string failedCondition = null;
|
|
|
|
var conditions = behavior.Conditions;
|
|
for (int c = 0; c < conditions.Count; c++)
|
|
{
|
|
if (conditions[c] == null) continue;
|
|
if (!conditions[c].Evaluate(context, inst))
|
|
{
|
|
passed = false;
|
|
failedCondition = conditions[c].name;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (trace != null)
|
|
trace.AddConditionResult(inst.Definition.Id, behavior.name, passed, failedCondition);
|
|
|
|
if (!passed) continue;
|
|
|
|
// Collect effects
|
|
var effects = behavior.Effects;
|
|
for (int e = 0; e < effects.Count; e++)
|
|
{
|
|
var effect = effects[e];
|
|
if (effect == null) continue;
|
|
if (context.IsPreview && effect.Phase == ModifierPhase.SideEffect) continue;
|
|
|
|
_effectBuffer.Add(new EffectEntry
|
|
{
|
|
Effect = effect,
|
|
Instance = inst,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sort by Phase -> Priority -> Id
|
|
_effectBuffer.Sort(ModifierComparer.Default);
|
|
|
|
// Execute sequentially
|
|
for (int i = 0; i < _effectBuffer.Count; i++)
|
|
{
|
|
var entry = _effectBuffer[i];
|
|
await entry.Effect.Apply(context, entry.Instance);
|
|
|
|
if (trace != null)
|
|
trace.AddEffectApplied(entry.Instance.Definition.Id, entry.Effect.name, entry.Effect.Phase);
|
|
}
|
|
|
|
_registry.RemoveExpired();
|
|
|
|
if (trace != null)
|
|
{
|
|
string traceStr = trace.ToString();
|
|
context.DebugLog.Add(traceStr);
|
|
// Debug.Log(traceStr);
|
|
}
|
|
|
|
_isExecuting = false;
|
|
_currentDepth = 0;
|
|
|
|
return context;
|
|
}
|
|
}
|
|
}
|