From d079e86fcb92fabbcab3ba12df92bfa0b64ce340 Mon Sep 17 00:00:00 2001 From: Jesse Date: Thu, 29 May 2025 12:05:41 +0200 Subject: [PATCH] Add OnLazyLoad event so modders can modify LazyLoaded data (#297) * Add OnLazyLoad event so modders can modify LazyLoaded data * Remove whitespace --- .../Utils/Json/LazyLoad.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs b/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs index 162d6d7c..effd9d53 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs @@ -8,6 +8,12 @@ public class LazyLoad(Func deserialize) private Timer? autoCleanerTimeout; + /// + /// can be subscribed to for mods to modify. It is fired right after lazy loading is complete + /// and any modification passed to will stay for the duration of this 's lifecycle + /// + public event EventHandler>? OnLazyLoad; + public T? Value { get @@ -16,6 +22,12 @@ public class LazyLoad(Func deserialize) { _result = deserialize(); _isLoaded = true; + + OnLazyLoadEventArgs args = new(_result); + OnLazyLoad?.Invoke(this, args); + + _result = args.Value; + autoCleanerTimeout = new Timer( _ => { @@ -33,9 +45,10 @@ public class LazyLoad(Func deserialize) autoCleanerTimeout?.Change(_autoCleanerTimeout, Timeout.InfiniteTimeSpan); return _result; } - set - { - _result = value; - } } } + +public class OnLazyLoadEventArgs(T value) : EventArgs +{ + public T Value { get; set; } = value; +}