Refactored pre spt mod loader to user IOnLoad, moved Watermark to IOnLoad as well (#313)

Co-authored-by: Alex <alex@dm-me-for-questions.com>
This commit is contained in:
clodanSPT
2025-05-30 11:03:08 +01:00
committed by GitHub
parent 1aa9bc3f2f
commit ee51e1fcab
27 changed files with 127 additions and 166 deletions
@@ -23,11 +23,6 @@ public class DialogueCallbacks(
return true;
}
public string GetRoute()
{
return "spt-dialogue";
}
/// <summary>
/// Handle client/friend/list
/// </summary>
@@ -24,11 +24,6 @@ public class GameCallbacks(
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-game";
}
/// <summary>
/// Handle client/game/version/validate
/// </summary>
@@ -12,9 +12,4 @@ public class HandbookCallbacks(HandBookController _handBookController) : IOnLoad
_handBookController.Load();
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-handbook";
}
}
@@ -28,11 +28,6 @@ public class HideoutCallbacks(
return false;
}
public string GetRoute()
{
return "spt-hideout";
}
/// <summary>
/// Handle HideoutUpgrade event
/// </summary>
@@ -14,11 +14,6 @@ public class HttpCallbacks(HttpServer _httpServer) : IOnLoad
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-http";
}
public string GetImage()
{
return "";
@@ -33,11 +33,6 @@ public class InsuranceCallbacks(
return false;
}
public string GetRoute()
{
return "spt-insurance";
}
/// <summary>
/// Handle client/insurance/items/list/cost
/// </summary>
@@ -12,9 +12,4 @@ public class PresetCallbacks(PresetController _presetController) : IOnLoad
_presetController.Initialize();
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-presets";
}
}
@@ -30,11 +30,6 @@ public class RagfairCallbacks(
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-ragfair";
}
public bool OnUpdate(long timeSinceLastRun)
{
if (timeSinceLastRun > _ragfairConfig.RunIntervalSeconds)
@@ -22,11 +22,6 @@ public class SaveCallbacks(
_saveServer.Load();
}
public string GetRoute()
{
return "spt-save";
}
public bool OnUpdate(long timeSinceLastRun)
{
if (timeSinceLastRun > _coreConfig.ProfileSaveIntervalInSeconds)
@@ -23,11 +23,6 @@ public class TraderCallbacks(
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-traders";
}
public bool OnUpdate(long _)
{
return _traderController.Update();
@@ -3,5 +3,4 @@ namespace SPTarkov.Server.Core.DI;
public interface IOnLoad
{
Task OnLoad();
string GetRoute();
}
@@ -3,5 +3,4 @@ namespace SPTarkov.Server.Core.DI;
public interface IOnUpdate
{
bool OnUpdate(long timeSinceLastRun);
string GetRoute();
}
@@ -2,15 +2,17 @@ namespace SPTarkov.Server.Core.DI;
public static class OnLoadOrder
{
public const int Database = 0;
public const int GameCallbacks = 100;
public const int PostDBModLoader = 200;
public const int TraderRegistration = 300;
public const int HandbookCallbacks = 400;
public const int HttpCallbacks = 500;
public const int SaveCallbacks = 600;
public const int TraderCallbacks = 700;
public const int PresetCallbacks = 800;
public const int RagfairCallbacks = 900;
public const int PostSptModLoader = 1000;
public const int Watermark = 0;
public const int PreSptModLoader = 1000;
public const int Database = 2000;
public const int GameCallbacks = 3000;
public const int PostDBModLoader = 4000;
public const int TraderRegistration = 5000;
public const int HandbookCallbacks = 6000;
public const int HttpCallbacks = 7000;
public const int SaveCallbacks = 8000;
public const int TraderCallbacks = 9000;
public const int PresetCallbacks = 10000;
public const int RagfairCallbacks = 11000;
public const int PostSptModLoader = 12000;
}
@@ -2,12 +2,7 @@ namespace SPTarkov.Server.Core.DI;
public static class OnUpdateOrder
{
public const int PreSptUpdate = 0;
public const int DialogueCallbacks = 1;
public const int HideoutCallbacks = 100;
public const int TraderCallbacks = 200;
public const int RagfairCallbacks = 300;
public const int InsuranceCallbacks = 400;
public const int SaveCallbacks = 500;
public const int PostSptUpdate = 9999;
public const int DialogueCallbacks = 1000;
public const int HideoutCallbacks = 2000;
public const int InsuranceCallbacks = 3000;
}
@@ -49,11 +49,6 @@ public class BotGeneratorHelper(
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-botGeneratorHelper";
}
/// <summary>
/// Adds properties to an item
/// e.g. Repairable / HasHinge / Foldable / MaxDurability
@@ -6,6 +6,7 @@ using SPTarkov.Server.Core.Utils;
namespace SPTarkov.Server.Core.Loaders;
[Obsolete("This mod loader is obsolete and will be removed in 4.1.0. See documentation in IPostDBLoadModAsync for more information.")]
[Injectable(TypePriority = OnLoadOrder.PostDBModLoader)]
public class PostDBModLoader(
ISptLogger<PostDBModLoader> _logger,
@@ -25,9 +26,4 @@ public class PostDBModLoader(
_logger.Info("Finished loading PostDBMods...");
}
}
public string GetRoute()
{
return "spt-post-db-mods";
}
}
@@ -6,6 +6,7 @@ using SPTarkov.Server.Core.Utils;
namespace SPTarkov.Server.Core.Loaders;
[Obsolete("This mod loader is obsolete and will be removed in 4.1.0. See documentation in IPostSptLoadModAsync for more information.")]
[Injectable(TypePriority = OnLoadOrder.PostSptModLoader)]
public class PostSptModLoader(
ISptLogger<PostSptModLoader> _logger,
@@ -25,9 +26,4 @@ public class PostSptModLoader(
_logger.Info("Finished loading PostSptMods...");
}
}
public string GetRoute()
{
return "spt-post-spt-mods";
}
}
@@ -0,0 +1,30 @@
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Models.External;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Utils;
namespace SPTarkov.Server.Core.Loaders;
[Obsolete(
"This mod loader is obsolete and will be removed in 4.1.0. See documentation in IPreSptLoadModAsync for more information.")]
[Injectable(InjectionType.Singleton, TypePriority = OnLoadOrder.PreSptModLoader)]
public class PreSptModLoader(
ISptLogger<PreSptModLoader> _logger,
IEnumerable<IPreSptLoadModAsync> _preSptLoadMods
) : IOnLoad
{
public async Task OnLoad()
{
if (ProgramStatics.MODS())
{
_logger.Info("Loading PreSptMods...");
foreach (var postSptLoadMod in _preSptLoadMods)
{
await postSptLoadMod.PreSptLoadAsync();
}
_logger.Info("Finished loading PreSptMods...");
}
}
}
@@ -1,5 +1,20 @@
namespace SPTarkov.Server.Core.Models.External;
/// <summary>
/// This interface used to be used in TS to load mods after the database finished loading.
/// This class is now deprecated and should not be used, see code example below for replacement.
/// </summary>
/// <code>
/// [Injectable(TypePriority = OnLoadOrder.Database + 1)]
/// public class MyMod : IOnLoad
/// {
/// // ... implementation
/// }
/// </code>
/// <remarks>
/// <b>DEPRECATED, see code example above for replacement!</b>
/// </remarks>
[Obsolete("This interface is obsolete and will be removed in 4.1.0, please use IOnLoad instead with the desired Injectable(TypePriority). See class documentation for examples.")]
public interface IPostDBLoadModAsync
{
Task PostDBLoadAsync();
@@ -1,5 +1,20 @@
namespace SPTarkov.Server.Core.Models.External;
/// <summary>
/// This interface used to be used in TS to load mods after SPT finished loading.
/// This class is now deprecated and should not be used, see code example below for replacement.
/// </summary>
/// <code>
/// [Injectable(TypePriority = OnLoadOrder.RagfairCallbacks + 1)]
/// public class MyMod : IOnLoad
/// {
/// // ... implementation
/// }
/// </code>
/// <remarks>
/// <b>DEPRECATED, see code example above for replacement!</b>
/// </remarks>
[Obsolete("This interface is obsolete and will be removed in 4.1.0, please use IOnLoad instead with the desired Injectable(TypePriority). See class documentation for examples.")]
public interface IPostSptLoadModAsync
{
Task PostSptLoadAsync();
@@ -1,5 +1,20 @@
namespace SPTarkov.Server.Core.Models.External;
/// <summary>
/// This interface used to be used in TS to load mods before SPT components loading.
/// This class is now deprecated and should not be used, see code example below for replacement.
/// </summary>
/// <code>
/// [Injectable(TypePriority = OnLoadOrder.Watermark + 1)]
/// public class MyMod : IOnLoad
/// {
/// // ... implementation
/// }
/// </code>
/// <remarks>
/// <b>DEPRECATED, see code example above for replacement!</b>
/// </remarks>
[Obsolete("This interface is obsolete and will be removed in 4.1.0, please use IOnLoad instead with the desired Injectable(TypePriority). See class documentation for examples.")]
public interface IPreSptLoadModAsync
{
Task PreSptLoadAsync();
@@ -67,11 +67,6 @@ public class TraderStore : IOnLoad
return Task.CompletedTask;
}
public string GetRoute()
{
return "spt-trader-registration";
}
/// <summary>
/// Returns a trader by given ID.
/// </summary>
+26 -49
View File
@@ -10,52 +10,23 @@ using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel;
namespace SPTarkov.Server.Core.Utils;
[Injectable(InjectionType.Singleton)]
public class App
public class App(
ISptLogger<App> _logger,
TimeUtil _timeUtil,
RandomUtil _randomUtil,
LocalisationService _localisationService,
ConfigServer _configServer,
EncodingUtil _encodingUtil,
HttpServer _httpServer,
DatabaseService _databaseService,
IEnumerable<IOnLoad> _onLoadComponents,
IEnumerable<IOnUpdate> _onUpdateComponents,
HttpServerHelper _httpServerHelper
)
{
protected readonly RandomUtil _randomUtil;
protected ConfigServer _configServer;
protected CoreConfig _coreConfig;
protected DatabaseService _databaseService;
protected EncodingUtil _encodingUtil;
protected HttpServer _httpServer;
protected HttpServerHelper _httpServerHelper;
protected LocalisationService _localisationService;
protected ISptLogger<App> _logger;
protected IEnumerable<IOnLoad> _onLoad;
protected IEnumerable<IOnUpdate> _onUpdate;
protected CoreConfig _coreConfig = _configServer.GetConfig<CoreConfig>();
protected Dictionary<string, long> _onUpdateLastRun = new();
protected Timer _timer;
protected TimeUtil _timeUtil;
public App(
ISptLogger<App> logger,
TimeUtil timeUtil,
RandomUtil randomUtil,
LocalisationService localisationService,
ConfigServer configServer,
EncodingUtil encodingUtil,
HttpServer httpServer,
DatabaseService databaseService,
IEnumerable<IOnLoad> onLoadComponents,
IEnumerable<IOnUpdate> onUpdateComponents,
HttpServerHelper httpServerHelper
)
{
_logger = logger;
_timeUtil = timeUtil;
_randomUtil = randomUtil;
_localisationService = localisationService;
_configServer = configServer;
_encodingUtil = encodingUtil;
_httpServer = httpServer;
_httpServerHelper = httpServerHelper;
_databaseService = databaseService;
_onLoad = onLoadComponents;
_onUpdate = onUpdateComponents;
_coreConfig = configServer.GetConfig<CoreConfig>();
}
public async Task InitializeAsync()
{
@@ -90,12 +61,12 @@ public class App
}
}
foreach (var onLoad in _onLoad)
foreach (var onLoad in _onLoadComponents)
{
await onLoad.OnLoad();
}
_timer = new Timer(_ => Update(_onUpdate), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(5000));
_timer = new Timer(_ => Update(_onUpdateComponents), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(5000));
}
public async Task StartAsync()
@@ -133,8 +104,14 @@ public class App
foreach (var updateable in onUpdateComponents)
{
var updateableName = updateable.GetType().FullName;
if (string.IsNullOrEmpty(updateableName))
{
updateableName = $"{updateable.GetType().Namespace}.{updateable.GetType().Name}";
}
var success = false;
if (!_onUpdateLastRun.TryGetValue(updateable.GetRoute(), out var lastRunTimeTimestamp))
if (!_onUpdateLastRun.TryGetValue(updateableName, out var lastRunTimeTimestamp))
{
lastRunTimeTimestamp = 0;
}
@@ -152,7 +129,7 @@ public class App
if (success)
{
_onUpdateLastRun[updateable.GetRoute()] = _timeUtil.GetTimeStamp();
_onUpdateLastRun[updateableName] = _timeUtil.GetTimeStamp();
}
else
{
@@ -163,7 +140,7 @@ public class App
{
if (_logger.IsLogEnabled(LogLevel.Debug))
{
_logger.Debug(_localisationService.GetText("route_onupdate_no_response", updateable.GetRoute()));
_logger.Debug(_localisationService.GetText("route_onupdate_no_response", updateableName));
}
}
}
@@ -178,7 +155,7 @@ public class App
protected void LogUpdateException(Exception err, IOnUpdate updateable)
{
_logger.Error(_localisationService.GetText("scheduled_event_failed_to_run", updateable.GetRoute()));
_logger.Error(_localisationService.GetText("scheduled_event_failed_to_run", updateable.GetType().FullName));
_logger.Error(err.ToString());
}
}
@@ -90,11 +90,6 @@ public class DatabaseImporter : IOnLoad
CreateRouteMapping(imageFilePath, "files");
}
public string GetRoute()
{
return "spt-database";
}
/**
* Get path to spt data
* @returns path to data
@@ -1,4 +1,5 @@
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Models.Logging;
using SPTarkov.Server.Core.Models.Spt.Config;
using SPTarkov.Server.Core.Models.Utils;
@@ -61,8 +62,8 @@ public class WatermarkLocale
}
}
[Injectable]
public class Watermark
[Injectable(TypePriority = OnLoadOrder.Watermark)]
public class Watermark : IOnLoad
{
protected ConfigServer _configServer;
protected LocalisationService _localisationService;
@@ -87,7 +88,7 @@ public class Watermark
sptConfig = _configServer.GetConfig<CoreConfig>();
}
public virtual void Initialize()
public Task OnLoad()
{
var description = _watermarkLocale.GetDescription();
var warning = _watermarkLocale.GetWarning();
@@ -121,6 +122,8 @@ public class Watermark
SetTitle();
Draw();
return Task.CompletedTask;
}
/// <summary>