From 8f55b8642dfd2ca13b44a9fa14fbc831b2bb738c Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 28 Aug 2025 20:26:56 +0100 Subject: [PATCH] Added before and after events for router actions --- Libraries/SPTarkov.Server.Core/DI/Router.cs | 77 +++++++++++++++++-- .../CustomizationItemEventRouter.cs | 2 +- .../ItemEvents/HealthItemEventRouter.cs | 2 +- .../ItemEvents/HideoutItemEventRouter.cs | 2 +- .../ItemEvents/InsuranceItemEventRouter.cs | 2 +- .../ItemEvents/InventoryItemEventRouter.cs | 2 +- .../Routers/ItemEvents/NoteItemEventRouter.cs | 2 +- .../ItemEvents/QuestItemEventRouter.cs | 2 +- .../ItemEvents/RagfairItemEventRouter.cs | 2 +- .../ItemEvents/RepairItemEventRouter.cs | 2 +- .../ItemEvents/TradeItemEventRouter.cs | 2 +- .../ItemEvents/WishlistItemEventRouter.cs | 2 +- .../Routers/SaveLoad/HealthSaveLoadRouter.cs | 2 +- .../Routers/SaveLoad/InraidSaveLoadRouter.cs | 2 +- .../SaveLoad/InsuranceSaveLoadRouter.cs | 2 +- .../Routers/SaveLoad/ProfileSaveLoadRouter.cs | 2 +- 16 files changed, 87 insertions(+), 20 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/DI/Router.cs b/Libraries/SPTarkov.Server.Core/DI/Router.cs index cbdd02f1..2f31a3c4 100644 --- a/Libraries/SPTarkov.Server.Core/DI/Router.cs +++ b/Libraries/SPTarkov.Server.Core/DI/Router.cs @@ -8,8 +8,18 @@ using SPTarkov.Server.Core.Utils; namespace SPTarkov.Server.Core.DI; +public interface IOnBeforeEventRequestData; +public interface IOnAfterEventRequestData; + +public record StaticDynamicOnBeforeEventRequestData(string Url, IRequestData RequestData, MongoId SessionId, string Output) + : IOnBeforeEventRequestData; +public record StaticDynamicOnAfterEventRequestData(string Url, IRequestData RequestData, MongoId SessionId, string Output, object Result) + : IOnAfterEventRequestData; public abstract class Router { + public event EventHandler? OnBeforeAction; + public event EventHandler? OnAfterAction; + protected IEnumerable handledRoutes = []; public virtual string GetTopLevelRoute() @@ -29,6 +39,15 @@ public abstract class Router return handledRoutes; } + protected void TriggerOnBeforeAction(IOnBeforeEventRequestData requestData) + { + OnBeforeAction?.Invoke(this, requestData); + } + protected void TriggerOnAfterAction(IOnAfterEventRequestData requestData) + { + OnAfterAction?.Invoke(this, requestData); + } + public bool CanHandle(string url, bool partialMatch = false) { if (partialMatch) @@ -52,7 +71,11 @@ public abstract class StaticRouter(JsonUtil jsonUtil, IEnumerable r info = (IRequestData?)jsonUtil.Deserialize(body, type); } - return await action.action(url, info ?? new EmptyRequestData(), sessionId, output); + info ??= new EmptyRequestData(); + TriggerOnBeforeAction(new StaticDynamicOnBeforeEventRequestData(url, info, sessionId, output)); + var result = await action.action(url, info, sessionId, output); + TriggerOnAfterAction(new StaticDynamicOnAfterEventRequestData(url, info, sessionId, output, result)); + return result; } protected override IEnumerable GetHandledRoutes() @@ -63,7 +86,7 @@ public abstract class StaticRouter(JsonUtil jsonUtil, IEnumerable r public abstract class DynamicRouter(JsonUtil jsonUtil, IEnumerable routes) : Router { - public async ValueTask HandleDynamic(string url, string? body, MongoId sessionID, string output) + public async ValueTask HandleDynamic(string url, string? body, MongoId sessionId, string output) { var action = routes.First(r => url.Contains(r.url)); var type = action.bodyType; @@ -73,7 +96,11 @@ public abstract class DynamicRouter(JsonUtil jsonUtil, IEnumerable info = (IRequestData?)jsonUtil.Deserialize(body, type); } - return await action.action(url, info ?? new EmptyRequestData(), sessionID, output); + info ??= new EmptyRequestData(); + TriggerOnBeforeAction(new StaticDynamicOnBeforeEventRequestData(url, info, sessionId, output)); + var result = await action.action(url, info, sessionId, output); + TriggerOnAfterAction(new StaticDynamicOnAfterEventRequestData(url, info, sessionId, output, result)); + return result; } protected override IEnumerable GetHandledRoutes() @@ -82,11 +109,40 @@ public abstract class DynamicRouter(JsonUtil jsonUtil, IEnumerable } } +public record ItemRouterOnBeforeEventRequestData( + string Url, + PmcData PmcData, + BaseInteractionRequestData Body, + MongoId SessionId, + ItemEventRouterResponse Output) : IOnBeforeEventRequestData; +public record ItemRouterOnAfterEventRequestData( + string Url, + PmcData PmcData, + BaseInteractionRequestData Body, + MongoId SessionId, + ItemEventRouterResponse Output, + ValueTask Result) : IOnAfterEventRequestData; + +public record OnAfterEventRequestData(string Url, T RequestData, MongoId SessionId, R Output, object Result) : IOnAfterEventRequestData; // The name of this class should be ItemEventRouter, but that name is taken, // So instead I added the definition public abstract class ItemEventRouterDefinition : Router { - public abstract ValueTask HandleItemEvent( + public ValueTask HandleItemEvent( + string url, + PmcData pmcData, + BaseInteractionRequestData body, + MongoId sessionID, + ItemEventRouterResponse output + ) + { + TriggerOnBeforeAction(new ItemRouterOnBeforeEventRequestData(url, pmcData, body, sessionID, output)); + var result = HandleItemEventInternal(url, pmcData, body, sessionID, output); + TriggerOnAfterAction(new ItemRouterOnAfterEventRequestData(url, pmcData, body, sessionID, output, result)); + return result; + } + + protected abstract ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, @@ -95,9 +151,20 @@ public abstract class ItemEventRouterDefinition : Router ); } +public record SaveLoadOnBeforeEventRequestData(SptProfile Profile) : IOnBeforeEventRequestData; +public record SaveLoadRouterOnAfterEventRequestData(SptProfile Profile) : IOnAfterEventRequestData; + public abstract class SaveLoadRouter : Router { - public abstract SptProfile HandleLoad(SptProfile profile); + public SptProfile HandleLoad(SptProfile profile) + { + TriggerOnBeforeAction(new SaveLoadOnBeforeEventRequestData(profile)); + var result = HandleLoadInternal(profile); + TriggerOnAfterAction(new SaveLoadRouterOnAfterEventRequestData(profile)); + return result; + } + + protected abstract SptProfile HandleLoadInternal(SptProfile profile); } public record HandledRoute(string route, bool dynamic); diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/CustomizationItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/CustomizationItemEventRouter.cs index c206fa0a..d44f4e33 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/CustomizationItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/CustomizationItemEventRouter.cs @@ -20,7 +20,7 @@ public class CustomizationItemEventRouter(ISptLogger HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HealthItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HealthItemEventRouter.cs index b3c359c6..e1cc707e 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HealthItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HealthItemEventRouter.cs @@ -23,7 +23,7 @@ public class HealthItemEventRouter(HealthCallbacks healthCallbacks) : ItemEventR ]; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HideoutItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HideoutItemEventRouter.cs index df77fd92..6ae7681c 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HideoutItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/HideoutItemEventRouter.cs @@ -36,7 +36,7 @@ public class HideoutItemEventRouter(HideoutCallbacks hideoutCallbacks) : ItemEve ]; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InsuranceItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InsuranceItemEventRouter.cs index 853e8f2e..30b941c9 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InsuranceItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InsuranceItemEventRouter.cs @@ -18,7 +18,7 @@ public class InsuranceItemEventRouter(InsuranceCallbacks insuranceCallbacks) : I return [new(ItemEventActions.INSURE, false)]; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InventoryItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InventoryItemEventRouter.cs index 1b3fa704..592082eb 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InventoryItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/InventoryItemEventRouter.cs @@ -46,7 +46,7 @@ public class InventoryItemEventRouter(InventoryCallbacks inventoryCallbacks, Hid }; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/NoteItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/NoteItemEventRouter.cs index 10f113c0..44a9c236 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/NoteItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/NoteItemEventRouter.cs @@ -18,7 +18,7 @@ public class NoteItemEventRouter(NoteCallbacks noteCallbacks) : ItemEventRouterD return [new(ItemEventActions.ADD_NOTE, false), new(ItemEventActions.EDIT_NOTE, false), new(ItemEventActions.DELETE_NOTE, false)]; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/QuestItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/QuestItemEventRouter.cs index a724a709..0e497cd7 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/QuestItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/QuestItemEventRouter.cs @@ -24,7 +24,7 @@ public class QuestItemEventRouter(QuestCallbacks questCallbacks) : ItemEventRout ]; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RagfairItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RagfairItemEventRouter.cs index 38231343..2071de60 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RagfairItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RagfairItemEventRouter.cs @@ -23,7 +23,7 @@ public class RagfairItemEventRouter(RagfairCallbacks ragfairCallbacks) : ItemEve }; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RepairItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RepairItemEventRouter.cs index ac1fed52..0cc0b2e3 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RepairItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/RepairItemEventRouter.cs @@ -18,7 +18,7 @@ public class RepairItemEventRouter(RepairCallbacks repairCallbacks) : ItemEventR return new List { new(ItemEventActions.REPAIR, false), new(ItemEventActions.TRADER_REPAIR, false) }; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/TradeItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/TradeItemEventRouter.cs index 4e61ad85..9c1cf30e 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/TradeItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/TradeItemEventRouter.cs @@ -23,7 +23,7 @@ public class TradeItemEventRouter(TradeCallbacks tradeCallbacks) : ItemEventRout ]; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/WishlistItemEventRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/WishlistItemEventRouter.cs index 2eac6257..a32bd71a 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/WishlistItemEventRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ItemEvents/WishlistItemEventRouter.cs @@ -23,7 +23,7 @@ public class WishlistItemEventRouter(WishlistCallbacks wishlistCallbacks) : Item }; } - public override ValueTask HandleItemEvent( + protected override ValueTask HandleItemEventInternal( string url, PmcData pmcData, BaseInteractionRequestData body, diff --git a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/HealthSaveLoadRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/HealthSaveLoadRouter.cs index 672cfa67..488cd245 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/HealthSaveLoadRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/HealthSaveLoadRouter.cs @@ -12,7 +12,7 @@ public class HealthSaveLoadRouter : SaveLoadRouter return [new HandledRoute("spt-health", false)]; } - public override SptProfile HandleLoad(SptProfile profile) + protected override SptProfile HandleLoadInternal(SptProfile profile) { return profile; } diff --git a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InraidSaveLoadRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InraidSaveLoadRouter.cs index 4fdfda39..9d105c3c 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InraidSaveLoadRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InraidSaveLoadRouter.cs @@ -12,7 +12,7 @@ public class InraidSaveLoadRouter : SaveLoadRouter return [new HandledRoute("spt-inraid", false)]; } - public override SptProfile HandleLoad(SptProfile profile) + protected override SptProfile HandleLoadInternal(SptProfile profile) { profile.InraidData ??= new Inraid { Location = "none", Character = "none" }; diff --git a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs index 28fcca63..c02080cf 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs @@ -12,7 +12,7 @@ public class InsuranceSaveLoadRouter : SaveLoadRouter return [new HandledRoute("spt-insurance", false)]; } - public override SptProfile HandleLoad(SptProfile profile) + protected override SptProfile HandleLoadInternal(SptProfile profile) { profile.InsuranceList ??= []; diff --git a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs index a6a4b9ce..d54c9fd5 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs @@ -13,7 +13,7 @@ public class ProfileSaveLoadRouter : SaveLoadRouter return [new HandledRoute("spt-profile", false)]; } - public override SptProfile HandleLoad(SptProfile profile) + protected override SptProfile HandleLoadInternal(SptProfile profile) { profile.CharacterData ??= new Characters { PmcData = new PmcData(), ScavData = new PmcData() };