From 9c5cfab87031251fd4cb891ce3ae974a93364733 Mon Sep 17 00:00:00 2001 From: sven-n Date: Sat, 7 Sep 2024 14:32:46 +0200 Subject: [PATCH] Improved initializing of custom plugin configurations --- .../Initialization/DataInitializationBase.cs | 67 +++++++------------ 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/src/Persistence/Initialization/DataInitializationBase.cs b/src/Persistence/Initialization/DataInitializationBase.cs index ee28f8751..c6123f546 100644 --- a/src/Persistence/Initialization/DataInitializationBase.cs +++ b/src/Persistence/Initialization/DataInitializationBase.cs @@ -5,13 +5,11 @@ namespace MUnique.OpenMU.Persistence.Initialization; using System.ComponentModel.Design; +using System.Text.Json.Serialization; using Microsoft.Extensions.Logging; using MUnique.OpenMU.DataModel.Configuration; using MUnique.OpenMU.GameLogic; -using MUnique.OpenMU.GameLogic.MuHelper; using MUnique.OpenMU.GameLogic.PlayerActions.ItemConsumeActions; -using MUnique.OpenMU.GameLogic.PlugIns.InvasionEvents; -using MUnique.OpenMU.GameLogic.PlugIns.PeriodicTasks; using MUnique.OpenMU.GameLogic.Resets; using MUnique.OpenMU.GameServer.MessageHandler; using MUnique.OpenMU.Network; @@ -138,46 +136,9 @@ public async Task CreateInitialDataAsync(byte numberOfGameServers, bool createTe plugInConfiguration.IsActive = true; this.GameConfiguration.PlugInConfigurations.Add(plugInConfiguration); - // Resets are disabled by default. - if (plugInType == typeof(ResetFeaturePlugIn)) - { - plugInConfiguration.IsActive = false; - plugInConfiguration.SetConfiguration(new ResetConfiguration(), referenceHandler); - } - - if (plugInType == typeof(ChaosCastleStartPlugIn)) + if (plugInType.GetInterfaces().Contains(typeof(ISupportDefaultCustomConfiguration))) { - plugInConfiguration.SetConfiguration(ChaosCastleStartConfiguration.Default, referenceHandler); - } - - if (plugInType == typeof(DevilSquareStartPlugIn)) - { - plugInConfiguration.SetConfiguration(DevilSquareStartConfiguration.Default, referenceHandler); - } - - if (plugInType == typeof(BloodCastleStartPlugIn)) - { - plugInConfiguration.SetConfiguration(BloodCastleStartConfiguration.Default, referenceHandler); - } - - if (plugInType == typeof(GoldenInvasionPlugIn)) - { - plugInConfiguration.SetConfiguration(PeriodicInvasionConfiguration.DefaultGoldenInvasion, referenceHandler); - } - - if (plugInType == typeof(RedDragonInvasionPlugIn)) - { - plugInConfiguration.SetConfiguration(PeriodicInvasionConfiguration.DefaultRedDragonInvasion, referenceHandler); - } - - if (plugInType == typeof(HappyHourPlugIn)) - { - plugInConfiguration.SetConfiguration(HappyHourConfiguration.Default, referenceHandler); - } - - if (plugInType == typeof(MuHelperFeaturePlugIn)) - { - plugInConfiguration.SetConfiguration(new MuHelperConfiguration(), referenceHandler); + this.CreateDefaultPlugInConfiguration(plugInType, plugInConfiguration, referenceHandler); } if (plugInType == typeof(BlessJewelConsumeHandlerPlugIn)) @@ -191,7 +152,7 @@ public async Task CreateInitialDataAsync(byte numberOfGameServers, bool createTe ResetToLevel0WhenFailMinLevel = 0, }; - if (this.GameConfiguration.Items.FirstOrDefault(item => item.Group == 13 && item.Number == 37) is { } fenrir) + if (this.GameConfiguration.Items.FirstOrDefault(item => item is { Group: 13, Number: 37 }) is { } fenrir) { config.RepairTargetItems.Add(fenrir); } @@ -206,6 +167,12 @@ public async Task CreateInitialDataAsync(byte numberOfGameServers, bool createTe { plugInConfiguration.IsActive = false; } + + // Resets are disabled by default. + if (plugInType == typeof(ResetFeaturePlugIn)) + { + plugInConfiguration.IsActive = false; + } }); this.AddAllUpdateEntries(plugInManager); @@ -218,6 +185,20 @@ public async Task CreateInitialDataAsync(byte numberOfGameServers, bool createTe /// protected abstract void CreateGameClientDefinition(); + private void CreateDefaultPlugInConfiguration(Type plugInType, PlugInConfiguration plugInConfiguration, ReferenceHandler referenceHandler) + { + try + { + var plugin = (ISupportDefaultCustomConfiguration)Activator.CreateInstance(plugInType)!; + var defaultConfig = plugin.CreateDefaultConfig(); + plugInConfiguration.SetConfiguration(defaultConfig, referenceHandler); + } + catch (Exception ex) + { + this._loggerFactory.CreateLogger(this.GetType()).LogWarning(ex, "Could not create custom default configuration for plugin type {plugInType}", plugInType); + } + } + private void AddAllUpdateEntries(PlugInManager plugInManager) { var updates = plugInManager.GetStrategyProvider()