on update and on load order

This commit is contained in:
Alex
2025-01-11 18:30:38 +00:00
parent 18c901c2af
commit d5958dff31
14 changed files with 50 additions and 38 deletions
@@ -8,15 +8,15 @@ using Core.Utils;
namespace Core.Callbacks;
[Injectable(TypePriority = OnUpdateOrder.DialogCallbacks)]
public class DialogCallbacks : OnUpdate
[Injectable(InjectableTypeOverride = typeof(OnUpdate), TypePriority = OnUpdateOrder.DialogCallbacks)]
public class DialogueCallbacks : OnUpdate
{
protected HashUtil _hashUtil;
protected TimeUtil _timeUtil;
protected HttpResponseUtil _httpResponseUtil;
protected DialogueController _dialogueController;
public DialogCallbacks
public DialogueCallbacks
(
HashUtil hashUtil,
TimeUtil timeUtil,
+1 -1
View File
@@ -10,7 +10,7 @@ using Core.Utils;
namespace Core.Callbacks;
[Injectable(TypePriority = OnLoadOrder.GameCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.GameCallbacks)]
public class GameCallbacks : OnLoad
{
protected HttpResponseUtil _httpResponseUtil;
+1 -1
View File
@@ -4,7 +4,7 @@ using Core.DI;
namespace Core.Callbacks;
[Injectable(TypePriority = OnLoadOrder.HandbookCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.HandbookCallbacks)]
public class HandbookCallbacks : OnLoad
{
protected HandBookController _handBookController;
+1 -2
View File
@@ -10,12 +10,11 @@ using Core.Servers;
namespace Core.Callbacks;
[Injectable]
[Injectable(InjectableTypeOverride = typeof(OnUpdate), TypePriority = OnUpdateOrder.HideoutCallbacks)]
public class HideoutCallbacks : OnUpdate
{
protected HideoutController _hideoutController;
protected ConfigServer _configServer;
protected HideoutConfig _hideoutConfig;
public HideoutCallbacks
+1 -1
View File
@@ -5,7 +5,7 @@ using Core.Servers;
namespace Core.Callbacks;
[Injectable(InjectionType.Singleton, typePriority: OnLoadOrder.HttpCallbacks)]
[Injectable(InjectionType.Singleton, InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.HttpCallbacks)]
public class HttpCallbacks : OnLoad
{
private readonly HttpServer _httpServer;
+1 -1
View File
@@ -13,7 +13,7 @@ using Core.Utils;
namespace Core.Callbacks;
[Injectable]
[Injectable(InjectableTypeOverride = typeof(OnUpdate), TypePriority = OnUpdateOrder.InsuranceCallbacks)]
public class InsuranceCallbacks : OnUpdate
{
protected InsuranceController _insuranceController;
+1 -1
View File
@@ -9,7 +9,7 @@ using ILogger = Core.Models.Utils.ILogger;
namespace Core.Callbacks;
[Injectable(TypePriority = OnLoadOrder.ModCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.ModCallbacks)]
public class ModCallbacks : OnLoad
{
protected ILogger _logger;
+1 -1
View File
@@ -4,7 +4,7 @@ using Core.DI;
namespace Core.Callbacks;
[Injectable(TypePriority = OnLoadOrder.PresetCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.PresetCallbacks)]
public class PresetCallbacks : OnLoad
{
protected PresetController _presetController;
+2 -2
View File
@@ -2,7 +2,6 @@
using Core.Controllers;
using Core.DI;
using Core.Models.Eft.Common;
using Core.Models.Eft.HttpResponse;
using Core.Models.Eft.ItemEvent;
using Core.Models.Eft.Ragfair;
using Core.Models.Enums;
@@ -13,7 +12,8 @@ using Core.Utils;
namespace Core.Callbacks;
[Injectable(TypePriority = OnLoadOrder.RagfairCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.RagfairCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnUpdate), TypePriority = OnUpdateOrder.RagfairCallbacks)]
public class RagfairCallbacks : OnLoad, OnUpdate
{
protected HttpResponseUtil _httpResponseUtil;
+2 -1
View File
@@ -7,7 +7,8 @@ using Core.Services;
namespace Core.Callbacks;
[Injectable(TypePriority = OnLoadOrder.SaveCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.SaveCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnUpdate), TypePriority = OnUpdateOrder.SaveCallbacks)]
public class SaveCallbacks : OnLoad, OnUpdate
{
protected SaveServer _saveServer;
+2 -1
View File
@@ -11,7 +11,8 @@ using Core.Utils;
namespace Core.Callbacks;
[Injectable(TypePriority = OnLoadOrder.TraderCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.TraderCallbacks)]
[Injectable(InjectableTypeOverride = typeof(OnUpdate), TypePriority = OnUpdateOrder.TraderCallbacks)]
public class TraderCallbacks : OnLoad, OnUpdate
{
protected HttpResponseUtil _httpResponseUtil;
+4 -4
View File
@@ -3,9 +3,9 @@ namespace Core.DI;
public static class OnUpdateOrder
{
public const int DialogCallbacks = 0;
public const int PostDBModLoader = 1;
public const int HandbookCallbacks = 2;
public const int HttpCallbacks = 3;
public const int PresetCallbacks = 4;
public const int HideoutCallbacks = 1;
public const int TraderCallbacks = 2;
public const int RagfairCallbacks = 3;
public const int InsuranceCallbacks = 4;
public const int SaveCallbacks = 5;
}
+1 -1
View File
@@ -10,7 +10,7 @@ using ILogger = Core.Models.Utils.ILogger;
namespace Core.Utils;
[Injectable(InjectionType.Singleton, TypePriority = OnLoadOrder.Database)]
[Injectable(InjectionType.Singleton, InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.Database)]
public class DatabaseImporter : OnLoad
{
private object hashedFile;
+29 -18
View File
@@ -43,38 +43,42 @@ public static class Program
private static void RegisterComponents(IServiceCollection builderServices, IEnumerable<Type> types)
{
var groupedTypes = types.Select(t =>
var groupedTypes = types.SelectMany(t =>
{
var attribute = (Injectable)Attribute.GetCustomAttribute(t, typeof(Injectable))!;
var attributes = (Injectable[]) Attribute.GetCustomAttributes(t, typeof(Injectable))!;
var registerableType = t;
// if we have a type override this takes priority
if (attribute.InjectableTypeOverride != null)
var registerableComponents = new List<RegisterableType>();
foreach (var attribute in attributes)
{
registerableType = attribute.InjectableTypeOverride;
// if we have a type override this takes priority
if (attribute.InjectableTypeOverride != null)
{
registerableType = attribute.InjectableTypeOverride;
}
// if this class only has 1 interface we register it on that interface
else if (registerableType.GetInterfaces().Length == 1)
{
registerableType = registerableType.GetInterfaces()[0];
}
registerableComponents.Add(new(registerableType, t, attribute));
}
// if this class only has 1 interface we register it on that interface
else if (registerableType.GetInterfaces().Length == 1)
{
registerableType = registerableType.GetInterfaces()[0];
}
return (registerableInterface: registerableType, typeToRegister: t, injectableAttribute: attribute);
}).GroupBy(t => t.registerableInterface.FullName);
return registerableComponents;
}).GroupBy(t => t.RegisterableInterface.FullName);
// We get all injectable services to register them on our services
foreach (var groupedInjectables in groupedTypes)
{
foreach (var valueTuple in groupedInjectables.OrderBy(t => t.injectableAttribute.TypePriority))
foreach (var valueTuple in groupedInjectables.OrderBy(t => t.InjectableAttribute.TypePriority))
{
switch (valueTuple.injectableAttribute.InjectionType)
switch (valueTuple.InjectableAttribute.InjectionType)
{
case InjectionType.Singleton:
builderServices.AddSingleton(valueTuple.registerableInterface, valueTuple.typeToRegister);
builderServices.AddSingleton(valueTuple.RegisterableInterface, valueTuple.TypeToRegister);
break;
case InjectionType.Transient:
builderServices.AddTransient(valueTuple.registerableInterface, valueTuple.typeToRegister);
builderServices.AddTransient(valueTuple.RegisterableInterface, valueTuple.TypeToRegister);
break;
case InjectionType.Scoped:
builderServices.AddScoped(valueTuple.registerableInterface, valueTuple.typeToRegister);
builderServices.AddScoped(valueTuple.RegisterableInterface, valueTuple.TypeToRegister);
break;
default:
throw new ArgumentOutOfRangeException();
@@ -89,4 +93,11 @@ public static class Program
RegisterComponents(builderServices, typeof(App).Assembly.GetTypes()
.Where(type => Attribute.IsDefined(type, typeof(Injectable))));
}
class RegisterableType(Type registerableInterface, Type typeToRegister, Injectable injectableAttribute)
{
public Type RegisterableInterface { get; } = registerableInterface;
public Type TypeToRegister { get; } = typeToRegister;
public Injectable InjectableAttribute { get; } = injectableAttribute;
}
}