From 01263eb937e8bb9dbd6e362ccd68690b7d1d1e8b Mon Sep 17 00:00:00 2001 From: KYPremco Date: Mon, 19 Sep 2022 19:41:48 +0200 Subject: [PATCH 1/6] Improved error message --- Core/TimberApi/AssetSystem/AssetLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/TimberApi/AssetSystem/AssetLoader.cs b/Core/TimberApi/AssetSystem/AssetLoader.cs index 8f7ecbf2..ccc4c6a4 100644 --- a/Core/TimberApi/AssetSystem/AssetLoader.cs +++ b/Core/TimberApi/AssetSystem/AssetLoader.cs @@ -42,7 +42,7 @@ public T Load(string prefix, string pathToFile, string name) where T : Object } T asset = assetFolder.GetAssetBundleAtPath(pathToFile).Load(name) ?? - throw new InvalidOperationException($"Failed to load asset at {prefix}/{pathToFile}/{name}. Asset name does not exists inside bundle"); + throw new InvalidOperationException($"Failed to load asset ({prefix}/{pathToFile}/{name}). Asset name does not exists inside bundle"); if (asset is not GameObject gameObject) { From f5d42fc8650214435f818d0e7e50575b207aee77 Mon Sep 17 00:00:00 2001 From: KYPremco Date: Mon, 19 Sep 2022 22:14:18 +0200 Subject: [PATCH 2/6] Swapped out SingletonSystem, Seeder -> PreLoadable --- .../SingletonSystem/ISingletonRepository.cs | 12 --- .../ITimberApiLoadableSingleton.cs | 10 ++ .../ITimberApiPostLoadableSingleton.cs | 10 ++ .../ITimberApiPreLoadableSingleton.cs | 10 ++ .../SingletonSystem/SingletonListener.cs | 94 ------------------- .../SingletonSystem/SingletonRepository.cs | 23 ----- .../SingletonSystem/SingletonRunner.cs | 32 ------- .../Singletons/ITimberApiLoadableSingleton.cs | 8 -- .../ITimberApiPostLoadableSingleton.cs | 8 -- .../Singletons/ITimberApiSeeder.cs | 8 -- .../TimberApiSingletonAttribute.cs | 11 --- ...{BootstrapPatch.cs => BootstrapPatcher.cs} | 7 +- .../TimberApiBootstrapSystemConfigurator.cs | 13 +-- .../SingletonSystem/SingletonSystemPatch.cs | 43 +++++++++ .../AssetShaderSystem/AssetShaderFixer.cs | 11 ++- ...=> AssetRepositoryPreLoadableSingleton.cs} | 8 +- .../TimberApi/AssetSystem/AssetSceneLoader.cs | 2 +- .../AssetSystem/AssetSystemConfigurator.cs | 2 +- ...s => ConfigurationPreLoadableSingleton.cs} | 8 +- .../ConfiguratorInstaller.cs | 9 -- .../ConfiguratorSystemConfigurator.cs | 2 +- .../LocalizationSystem/LocalizationPatcher.cs | 2 +- .../AssetSystemConfiguratorPatcher.cs | 2 +- Core/TimberApi/SceneSystem/SceneListener.cs | 2 +- .../Golems/GolemFactionPatcher.cs | 2 +- .../SpecificationPatcher.cs | 2 +- ...ficationRepositoryPreLoadableSingleton.cs} | 8 +- .../SpecificationSystemConfigurator.cs | 2 +- 28 files changed, 111 insertions(+), 240 deletions(-) delete mode 100644 Core/TimberApi.Common/SingletonSystem/ISingletonRepository.cs create mode 100644 Core/TimberApi.Common/SingletonSystem/ITimberApiLoadableSingleton.cs create mode 100644 Core/TimberApi.Common/SingletonSystem/ITimberApiPostLoadableSingleton.cs create mode 100644 Core/TimberApi.Common/SingletonSystem/ITimberApiPreLoadableSingleton.cs delete mode 100644 Core/TimberApi.Common/SingletonSystem/SingletonListener.cs delete mode 100644 Core/TimberApi.Common/SingletonSystem/SingletonRepository.cs delete mode 100644 Core/TimberApi.Common/SingletonSystem/SingletonRunner.cs delete mode 100644 Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiLoadableSingleton.cs delete mode 100644 Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiPostLoadableSingleton.cs delete mode 100644 Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiSeeder.cs delete mode 100644 Core/TimberApi.Common/SingletonSystem/TimberApiSingletonAttribute.cs rename Core/TimberApi.Core/BootstrapSystem/{BootstrapPatch.cs => BootstrapPatcher.cs} (80%) create mode 100644 Core/TimberApi.Core/SingletonSystem/SingletonSystemPatch.cs rename Core/TimberApi/AssetSystem/{AssetRepositorySeeder.cs => AssetRepositoryPreLoadableSingleton.cs} (77%) rename Core/TimberApi/ConfiguratorSystem/{ConfigurationSeeder.cs => ConfigurationPreLoadableSingleton.cs} (93%) rename Core/TimberApi/SpecificationSystem/{SpecificationRepositorySeeder.cs => SpecificationRepositoryPreLoadableSingleton.cs} (80%) diff --git a/Core/TimberApi.Common/SingletonSystem/ISingletonRepository.cs b/Core/TimberApi.Common/SingletonSystem/ISingletonRepository.cs deleted file mode 100644 index c8c273ce..00000000 --- a/Core/TimberApi.Common/SingletonSystem/ISingletonRepository.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; - -namespace TimberApi.Common.SingletonSystem -{ - /// - /// Replicate from: Timberborn SingletonSystem - /// - public interface ISingletonRepository - { - IEnumerable GetSingletons(); - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/ITimberApiLoadableSingleton.cs b/Core/TimberApi.Common/SingletonSystem/ITimberApiLoadableSingleton.cs new file mode 100644 index 00000000..4f98a654 --- /dev/null +++ b/Core/TimberApi.Common/SingletonSystem/ITimberApiLoadableSingleton.cs @@ -0,0 +1,10 @@ +using Timberborn.SingletonSystem; + +namespace TimberApi.Common.SingletonSystem +{ + [Singleton] + public interface ITimberApiLoadableSingleton + { + void Load(); + } +} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/ITimberApiPostLoadableSingleton.cs b/Core/TimberApi.Common/SingletonSystem/ITimberApiPostLoadableSingleton.cs new file mode 100644 index 00000000..0f10392f --- /dev/null +++ b/Core/TimberApi.Common/SingletonSystem/ITimberApiPostLoadableSingleton.cs @@ -0,0 +1,10 @@ +using Timberborn.SingletonSystem; + +namespace TimberApi.Common.SingletonSystem +{ + [Singleton] + public interface ITimberApiPostLoadableSingleton + { + void PostLoad(); + } +} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/ITimberApiPreLoadableSingleton.cs b/Core/TimberApi.Common/SingletonSystem/ITimberApiPreLoadableSingleton.cs new file mode 100644 index 00000000..0749ce4a --- /dev/null +++ b/Core/TimberApi.Common/SingletonSystem/ITimberApiPreLoadableSingleton.cs @@ -0,0 +1,10 @@ +using Timberborn.SingletonSystem; + +namespace TimberApi.Common.SingletonSystem +{ + [Singleton] + public interface ITimberApiPreLoadableSingleton + { + void PreLoad(); + } +} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/SingletonListener.cs b/Core/TimberApi.Common/SingletonSystem/SingletonListener.cs deleted file mode 100644 index 4182d1be..00000000 --- a/Core/TimberApi.Common/SingletonSystem/SingletonListener.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using System.Reflection; -using Bindito.Core; - -namespace TimberApi.Common.SingletonSystem -{ - /// - /// Replicate from: Timberborn SingletonSystem - /// - public class SingletonListener : IInjectionListener, IProvisionListener - { - private readonly List _injectedSingletons = new(); - private readonly List _providedSingletons = new(); - private readonly Dictionary _typeCache = new(); - private ImmutableArray _allSingletons; - private bool _collected; - - void IInjectionListener.Listen(object injectee) - { - if (!ObjectIsSingleton(injectee)) - { - return; - } - - ThrowIfCollected(injectee); - _injectedSingletons.Add(injectee); - } - - void IProvisionListener.Listen(object injectee) - { - if (!ObjectIsSingleton(injectee)) - { - return; - } - - ThrowIfCollected(injectee); - _providedSingletons.Add(injectee); - } - - public IEnumerable Collect() - { - if (!_collected) - { - _allSingletons = _providedSingletons.Concat(_injectedSingletons).Distinct().ToImmutableArray(); - _collected = true; - } - - return _allSingletons; - } - - private bool ObjectIsSingleton(object target) - { - Type type = target.GetType(); - bool flag1; - if (_typeCache.TryGetValue(type, out flag1)) - { - return flag1; - } - - bool flag2 = TypeIsSingleton(type); - _typeCache.Add(type, flag2); - return flag2; - } - - private static bool TypeIsSingleton(Type type) - { - if (type.GetCustomAttribute(typeof(TimberApiSingletonAttribute), true) != null) - { - return true; - } - - foreach (MemberInfo element in type.GetInterfaces()) - { - if (element.GetCustomAttribute(typeof(TimberApiSingletonAttribute), true) != null) - { - return true; - } - } - - return false; - } - - private void ThrowIfCollected(object injectee) - { - if (_collected) - { - throw new InvalidOperationException(string.Format("Attempting to add an instance of {0} after collecting.", injectee.GetType())); - } - } - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/SingletonRepository.cs b/Core/TimberApi.Common/SingletonSystem/SingletonRepository.cs deleted file mode 100644 index 1bed4ec8..00000000 --- a/Core/TimberApi.Common/SingletonSystem/SingletonRepository.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace TimberApi.Common.SingletonSystem -{ - /// - /// Replicate from: Timberborn SingletonSystem - /// - public class SingletonRepository : ISingletonRepository - { - private readonly SingletonListener _singletonListener; - - public SingletonRepository(SingletonListener singletonListener) - { - _singletonListener = singletonListener; - } - - public IEnumerable GetSingletons() - { - return _singletonListener.Collect().OfType(); - } - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/SingletonRunner.cs b/Core/TimberApi.Common/SingletonSystem/SingletonRunner.cs deleted file mode 100644 index 48918f58..00000000 --- a/Core/TimberApi.Common/SingletonSystem/SingletonRunner.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using TimberApi.Common.SingletonSystem.Singletons; - -namespace TimberApi.Common.SingletonSystem -{ - public class SingletonRunner - { - private readonly ISingletonRepository _singletonRepository; - - public SingletonRunner(ISingletonRepository singletonRepository) - { - _singletonRepository = singletonRepository; - Run(); - } - - private void Run() - { - SingletonActivator(_singletonRepository.GetSingletons(), singleton => singleton.Run()); - SingletonActivator(_singletonRepository.GetSingletons(), singleton => singleton.Load()); - SingletonActivator(_singletonRepository.GetSingletons(), singleton => singleton.PostLoad()); - } - - private static void SingletonActivator(IEnumerable singletons, Action action) where T : class - { - foreach (T singleton in singletons) - { - action(singleton); - } - } - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiLoadableSingleton.cs b/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiLoadableSingleton.cs deleted file mode 100644 index e31dcd32..00000000 --- a/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiLoadableSingleton.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace TimberApi.Common.SingletonSystem.Singletons -{ - [TimberApiSingleton] - public interface ITimberApiLoadableSingleton - { - void Load(); - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiPostLoadableSingleton.cs b/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiPostLoadableSingleton.cs deleted file mode 100644 index 02624d6e..00000000 --- a/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiPostLoadableSingleton.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace TimberApi.Common.SingletonSystem.Singletons -{ - [TimberApiSingleton] - public interface ITimberApiPostLoadableSingleton - { - void PostLoad(); - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiSeeder.cs b/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiSeeder.cs deleted file mode 100644 index ec88a730..00000000 --- a/Core/TimberApi.Common/SingletonSystem/Singletons/ITimberApiSeeder.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace TimberApi.Common.SingletonSystem.Singletons -{ - [TimberApiSingleton] - public interface ITimberApiSeeder - { - void Run(); - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/SingletonSystem/TimberApiSingletonAttribute.cs b/Core/TimberApi.Common/SingletonSystem/TimberApiSingletonAttribute.cs deleted file mode 100644 index a2f0ce52..00000000 --- a/Core/TimberApi.Common/SingletonSystem/TimberApiSingletonAttribute.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using JetBrains.Annotations; - -namespace TimberApi.Common.SingletonSystem -{ - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] - [MeansImplicitUse] - public class TimberApiSingletonAttribute : Attribute - { - } -} \ No newline at end of file diff --git a/Core/TimberApi.Core/BootstrapSystem/BootstrapPatch.cs b/Core/TimberApi.Core/BootstrapSystem/BootstrapPatcher.cs similarity index 80% rename from Core/TimberApi.Core/BootstrapSystem/BootstrapPatch.cs rename to Core/TimberApi.Core/BootstrapSystem/BootstrapPatcher.cs index 2dbefa67..5371592e 100644 --- a/Core/TimberApi.Core/BootstrapSystem/BootstrapPatch.cs +++ b/Core/TimberApi.Core/BootstrapSystem/BootstrapPatcher.cs @@ -7,15 +7,16 @@ namespace TimberApi.Core.BootstrapSystem { - internal static class BootstrapPatch + internal static class BootstrapPatcher { - public static void Apply() + public static void Patch() { try { var harmony = new Harmony("TimberApi.bootstrapper"); - harmony.Patch(AccessTools.Method(typeof(BootstrapperConfigurator), "Configure"), new HarmonyMethod(AccessTools.Method(typeof(BootstrapPatch), nameof(BootstrapperConfiguratorPatch)))); + harmony.Patch(AccessTools.Method(typeof(BootstrapperConfigurator), "Configure"), + new HarmonyMethod(AccessTools.Method(typeof(BootstrapPatcher), nameof(BootstrapperConfiguratorPatch)))); } catch (Exception e) { diff --git a/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs b/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs index c5d58ac8..7e34a50b 100644 --- a/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs +++ b/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs @@ -3,11 +3,11 @@ using Bindito.Core; using Bindito.Unity; using TimberApi.Common; -using TimberApi.Common.SingletonSystem; using TimberApi.Core.ConfigSystem; using TimberApi.Core.ConsoleSystem; using TimberApi.Core.LoggingSystem; using TimberApi.Core.ModLoaderSystem; +using TimberApi.Core.SingletonSystem; namespace TimberApi.Core.BootstrapSystem { @@ -21,7 +21,8 @@ private void Awake() { Instance = this; AddPrefabConfigurators(); - BootstrapPatch.Apply(); + BootstrapPatcher.Patch(); + SingletonSystemPatcher.Patch(); } catch (Exception e) { @@ -42,15 +43,7 @@ public override void Configure(IContainerDefinition containerDefinition) containerDefinition.Bind().AsSingleton(); // TimberAPI Initialization - containerDefinition.Bind().To().AsSingleton(); - var instance = new SingletonListener(); - containerDefinition.Bind().ToInstance(instance); - containerDefinition.AddInjectionListener(instance); - containerDefinition.AddProvisionListener(instance); - containerDefinition.Install(new BootstrapConfigurator()); - - containerDefinition.Bind().AsSingleton(); } private void AddPrefabConfigurators() diff --git a/Core/TimberApi.Core/SingletonSystem/SingletonSystemPatch.cs b/Core/TimberApi.Core/SingletonSystem/SingletonSystemPatch.cs new file mode 100644 index 00000000..fe05a6b6 --- /dev/null +++ b/Core/TimberApi.Core/SingletonSystem/SingletonSystemPatch.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.IO; +using HarmonyLib; +using TimberApi.Common; +using TimberApi.Common.SingletonSystem; +using Timberborn.SingletonSystem; + +namespace TimberApi.Core.SingletonSystem +{ + public static class SingletonSystemPatcher + { + public static void Patch() + { + try + { + var harmony = new Harmony("TimberApi.singletonSystem"); + harmony.Patch(AccessTools.TypeByName("Timberborn.SingletonSystem.SingletonLifecycleService").GetMethod("LoadAll"), + new HarmonyMethod(AccessTools.Method(typeof(SingletonSystemPatcher), nameof(SingletonLifecycleServicePrefix)))); + } + catch (Exception e) + { + File.WriteAllText(Path.Combine(Paths.Logs, $"HarmonyException-{DateTime.Now:yyyy-MM-dd-HH\\hmm\\mss\\s}.log"), e.ToString()); + throw; + } + } + + public static void SingletonLifecycleServicePrefix(ISingletonRepository ____singletonRepository) + { + SingletonActivator(____singletonRepository.GetSingletons(), singleton => singleton.PreLoad()); + SingletonActivator(____singletonRepository.GetSingletons(), singleton => singleton.Load()); + SingletonActivator(____singletonRepository.GetSingletons(), singleton => singleton.PostLoad()); + } + + private static void SingletonActivator(IEnumerable singletons, Action action) where T : class + { + foreach (T singleton in singletons) + { + action(singleton); + } + } + } +} \ No newline at end of file diff --git a/Core/TimberApi/AssetShaderSystem/AssetShaderFixer.cs b/Core/TimberApi/AssetShaderSystem/AssetShaderFixer.cs index 0fb9b151..849faca0 100644 --- a/Core/TimberApi/AssetShaderSystem/AssetShaderFixer.cs +++ b/Core/TimberApi/AssetShaderSystem/AssetShaderFixer.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using TimberApi.Common.SingletonSystem; using UnityEngine; namespace TimberApi.AssetShaderSystem { - internal class AssetShaderFixer + internal class AssetShaderFixer : ITimberApiPreLoadableSingleton { private readonly ImmutableArray _shaderFixAppliers; @@ -30,6 +31,14 @@ private Shader Shader } } + public void PreLoad() + { + foreach (IShaderFixApplier shaderFixApplier in _shaderFixAppliers) + { + shaderFixApplier.LoadShader(); + } + } + public void FixShaders(GameObject gameObject) { var meshRenderer = gameObject.GetComponent(); diff --git a/Core/TimberApi/AssetSystem/AssetRepositorySeeder.cs b/Core/TimberApi/AssetSystem/AssetRepositoryPreLoadableSingleton.cs similarity index 77% rename from Core/TimberApi/AssetSystem/AssetRepositorySeeder.cs rename to Core/TimberApi/AssetSystem/AssetRepositoryPreLoadableSingleton.cs index 0d3b1e1e..4a832786 100644 --- a/Core/TimberApi/AssetSystem/AssetRepositorySeeder.cs +++ b/Core/TimberApi/AssetSystem/AssetRepositoryPreLoadableSingleton.cs @@ -1,24 +1,24 @@ using System.Linq; using TimberApi.Common; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using TimberApi.ModSystem; using TimberApi.SceneSystem; namespace TimberApi.AssetSystem { - internal class AssetRepositorySeeder : ITimberApiSeeder + internal class AssetRepositoryPreLoadableSingleton : ITimberApiPreLoadableSingleton { private readonly AssetRepository _assetRepository; private readonly IModRepository _modRepository; - public AssetRepositorySeeder(AssetRepository assetRepository, IModRepository modRepository) + public AssetRepositoryPreLoadableSingleton(AssetRepository assetRepository, IModRepository modRepository) { _assetRepository = assetRepository; _modRepository = modRepository; } - public void Run() + public void PreLoad() { SetTimberApiAssets(); SetModAssets(); diff --git a/Core/TimberApi/AssetSystem/AssetSceneLoader.cs b/Core/TimberApi/AssetSystem/AssetSceneLoader.cs index 3d304993..a7e92c34 100644 --- a/Core/TimberApi/AssetSystem/AssetSceneLoader.cs +++ b/Core/TimberApi/AssetSystem/AssetSceneLoader.cs @@ -1,4 +1,4 @@ -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using TimberApi.SceneSystem; namespace TimberApi.AssetSystem diff --git a/Core/TimberApi/AssetSystem/AssetSystemConfigurator.cs b/Core/TimberApi/AssetSystem/AssetSystemConfigurator.cs index 8e2db2f3..9e696717 100644 --- a/Core/TimberApi/AssetSystem/AssetSystemConfigurator.cs +++ b/Core/TimberApi/AssetSystem/AssetSystemConfigurator.cs @@ -20,7 +20,7 @@ public void Configure(IContainerDefinition containerDefinition) { containerDefinition.Bind().AsSingleton(); containerDefinition.Bind().AsSingleton(); - containerDefinition.Bind().AsSingleton(); + containerDefinition.Bind().AsSingleton(); } } } \ No newline at end of file diff --git a/Core/TimberApi/ConfiguratorSystem/ConfigurationSeeder.cs b/Core/TimberApi/ConfiguratorSystem/ConfigurationPreLoadableSingleton.cs similarity index 93% rename from Core/TimberApi/ConfiguratorSystem/ConfigurationSeeder.cs rename to Core/TimberApi/ConfiguratorSystem/ConfigurationPreLoadableSingleton.cs index c519bf28..9ba4d8d3 100644 --- a/Core/TimberApi/ConfiguratorSystem/ConfigurationSeeder.cs +++ b/Core/TimberApi/ConfiguratorSystem/ConfigurationPreLoadableSingleton.cs @@ -5,16 +5,16 @@ using System.Reflection; using Bindito.Core; using TimberApi.Common.Extensions; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using TimberApi.SceneSystem; namespace TimberApi.ConfiguratorSystem { - internal class ConfigurationSeeder : ITimberApiSeeder + internal class ConfigurationPreLoadableSingleton : ITimberApiPreLoadableSingleton { private readonly ConfiguratorRepository _configuratorRepository; - public ConfigurationSeeder(ConfiguratorRepository configuratorRepository) + public ConfigurationPreLoadableSingleton(ConfiguratorRepository configuratorRepository) { _configuratorRepository = configuratorRepository; } @@ -22,7 +22,7 @@ public ConfigurationSeeder(ConfiguratorRepository configuratorRepository) /// /// Searches for configurators to fill repository /// - public void Run() + public void PreLoad() { ImmutableArray validatedConfigurators = ValidateAndCreateConfigurators(AppDomain.CurrentDomain.GetTypesInDomainByAttribute()).ToImmutableArray(); diff --git a/Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs b/Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs index f646d610..03409be4 100644 --- a/Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs +++ b/Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs @@ -1,5 +1,4 @@ using Bindito.Core; -using TimberApi.Common.SingletonSystem; using TimberApi.SceneSystem; namespace TimberApi.ConfiguratorSystem @@ -8,18 +7,10 @@ internal static class ConfiguratorInstaller { public static void Install(IContainerDefinition containerDefinition, SceneEntrypoint sceneEntrypoint) { - containerDefinition.Bind().To().AsSingleton(); - var instance = new SingletonListener(); - containerDefinition.Bind().ToInstance(instance); - containerDefinition.AddInjectionListener(instance); - containerDefinition.AddProvisionListener(instance); - foreach (IConfigurator configurator in ConfiguratorRepository.SceneConfigurators[sceneEntrypoint]) { containerDefinition.Install(configurator); } - - containerDefinition.Bind().AsSingleton(); } } } \ No newline at end of file diff --git a/Core/TimberApi/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs b/Core/TimberApi/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs index 04a3ab42..f961516e 100644 --- a/Core/TimberApi/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs +++ b/Core/TimberApi/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs @@ -7,7 +7,7 @@ internal class ConfiguratorSystemConfigurator : IConfigurator public void Configure(IContainerDefinition containerDefinition) { containerDefinition.Bind().AsSingleton(); - containerDefinition.Bind().AsSingleton(); + containerDefinition.Bind().AsSingleton(); } } } \ No newline at end of file diff --git a/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs b/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs index 6f04f531..1596ec60 100644 --- a/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs +++ b/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using HarmonyLib; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using Timberborn.Common; using UnityEngine; diff --git a/Core/TimberApi/ResourceAssetSystem/AssetSystemConfiguratorPatcher.cs b/Core/TimberApi/ResourceAssetSystem/AssetSystemConfiguratorPatcher.cs index 54d86ea3..11b75d0e 100644 --- a/Core/TimberApi/ResourceAssetSystem/AssetSystemConfiguratorPatcher.cs +++ b/Core/TimberApi/ResourceAssetSystem/AssetSystemConfiguratorPatcher.cs @@ -1,6 +1,6 @@ using Bindito.Core; using HarmonyLib; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using Timberborn.AssetSystem; namespace TimberApi.ResourceAssetSystem diff --git a/Core/TimberApi/SceneSystem/SceneListener.cs b/Core/TimberApi/SceneSystem/SceneListener.cs index 3029bb11..1b0b7ae7 100644 --- a/Core/TimberApi/SceneSystem/SceneListener.cs +++ b/Core/TimberApi/SceneSystem/SceneListener.cs @@ -1,6 +1,6 @@ using Bindito.Core; using HarmonyLib; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using TimberApi.ConfiguratorSystem; using Timberborn.MainMenuScene; using Timberborn.MapEditorScene; diff --git a/Core/TimberApi/SpecificationSystem/CustomSpecifications/Golems/GolemFactionPatcher.cs b/Core/TimberApi/SpecificationSystem/CustomSpecifications/Golems/GolemFactionPatcher.cs index 9a70e682..b908a188 100644 --- a/Core/TimberApi/SpecificationSystem/CustomSpecifications/Golems/GolemFactionPatcher.cs +++ b/Core/TimberApi/SpecificationSystem/CustomSpecifications/Golems/GolemFactionPatcher.cs @@ -1,6 +1,6 @@ using System.Linq; using HarmonyLib; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using TimberApi.DependencyContainerSystem; using Timberborn.EntitySystem; using Timberborn.FactionSystemGame; diff --git a/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs b/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs index a55b7fd6..b49e7cee 100644 --- a/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs +++ b/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Reflection.Emit; using HarmonyLib; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using Timberborn.MainMenuScene; using Timberborn.Persistence; diff --git a/Core/TimberApi/SpecificationSystem/SpecificationRepositorySeeder.cs b/Core/TimberApi/SpecificationSystem/SpecificationRepositoryPreLoadableSingleton.cs similarity index 80% rename from Core/TimberApi/SpecificationSystem/SpecificationRepositorySeeder.cs rename to Core/TimberApi/SpecificationSystem/SpecificationRepositoryPreLoadableSingleton.cs index c428fd06..0f005476 100644 --- a/Core/TimberApi/SpecificationSystem/SpecificationRepositorySeeder.cs +++ b/Core/TimberApi/SpecificationSystem/SpecificationRepositoryPreLoadableSingleton.cs @@ -2,14 +2,14 @@ using System.IO; using System.Linq; using TimberApi.Common.Helpers; -using TimberApi.Common.SingletonSystem.Singletons; +using TimberApi.Common.SingletonSystem; using TimberApi.ModSystem; using TimberApi.SpecificationSystem.SpecificationTypes; using UnityEngine; namespace TimberApi.SpecificationSystem { - internal class SpecificationRepositorySeeder : ITimberApiSeeder + internal class SpecificationRepositoryPreLoadableSingleton : ITimberApiPreLoadableSingleton { private static readonly string TimberbornSpecificationPath = "Specifications"; @@ -19,14 +19,14 @@ internal class SpecificationRepositorySeeder : ITimberApiSeeder private readonly SpecificationRepository _specificationRepository; - public SpecificationRepositorySeeder(IModRepository modRepository, SpecificationRepository specificationRepository, IEnumerable specificationGenerators) + public SpecificationRepositoryPreLoadableSingleton(IModRepository modRepository, SpecificationRepository specificationRepository, IEnumerable specificationGenerators) { _modRepository = modRepository; _specificationRepository = specificationRepository; _specificationGenerators = specificationGenerators; } - public void Run() + public void PreLoad() { List specifications = new(); diff --git a/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs b/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs index e9a6a738..f170b995 100644 --- a/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs +++ b/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs @@ -11,7 +11,7 @@ internal class SpecificationSystemConfigurator : IConfigurator public void Configure(IContainerDefinition containerDefinition) { containerDefinition.Bind().AsSingleton(); - containerDefinition.Bind().AsSingleton(); + containerDefinition.Bind().AsSingleton(); containerDefinition.Bind().To().AsSingleton(); } } From 15214abee9259bb887014b333bd85fb1a2f9700f Mon Sep 17 00:00:00 2001 From: KYPremco Date: Mon, 19 Sep 2022 22:52:06 +0200 Subject: [PATCH 3/6] Added version to loaded message --- Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs b/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs index 906896ec..cd00745e 100644 --- a/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs +++ b/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs @@ -97,7 +97,7 @@ private IEnumerable LoadMods(IMod[] sortedLoadableMods) loadableMod.IsLoaded = true; loadedMods.Add(loadableMod); - _consoleWriter.LogAs(loadableMod.Name, "Loaded!", LogType.Log); + _consoleWriter.LogAs(loadableMod.Name, $"Loaded version {loadableMod.Version}", LogType.Log); } catch (Exception e) { From a29ff34e5fe7eeb66fcb0dbe011ddc131ecfd296 Mon Sep 17 00:00:00 2001 From: KYPremco Date: Tue, 20 Sep 2022 22:39:30 +0200 Subject: [PATCH 4/6] Configurator fix --- .../Extensions/AppDomainExtensions.cs | 27 ------------- .../Extensions/AssemblyExtensions.cs | 5 +++ .../TimberApiBootstrapSystemConfigurator.cs | 7 ++++ .../ConfigurationRepositorySeeder.cs} | 39 +++++++++++++------ .../ConfigurationValidationException.cs | 2 +- .../ConfiguratorInstaller.cs | 24 ++++++++++++ .../ConfiguratorPatcher.cs} | 26 ++++++------- .../ConfiguratorRepository.cs | 23 +++++++++++ .../ConfiguratorSystemConfigurator.cs | 5 ++- Core/TimberApi.Core/TimberApiCoreRunner.cs | 7 +++- .../ConfiguratorInstaller.cs | 16 -------- .../ConfiguratorRepository.cs | 23 ----------- .../LocalizationSystem/LocalizationPatcher.cs | 5 +-- .../LocalizationSystemConfigurator.cs | 12 ------ .../SceneSystem/TimberApiSceneManager.cs | 11 +++++- .../SpecificationPatcher.cs | 5 +-- .../SpecificationSystemConfigurator.cs | 11 ------ .../TimberApiBootstrapConfigurator.cs | 8 ---- .../TimberApi/UiBuilderSystem/UIBoxBuilder.cs | 8 +++- .../UiBuilderSystemConfigurator.cs | 4 +- 20 files changed, 130 insertions(+), 138 deletions(-) delete mode 100644 Core/TimberApi.Common/Extensions/AppDomainExtensions.cs rename Core/{TimberApi/ConfiguratorSystem/ConfigurationPreLoadableSingleton.cs => TimberApi.Core/ConfiguratorSystem/ConfigurationRepositorySeeder.cs} (57%) rename Core/{TimberApi => TimberApi.Core}/ConfiguratorSystem/ConfigurationValidationException.cs (93%) create mode 100644 Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorInstaller.cs rename Core/{TimberApi/SceneSystem/SceneListener.cs => TimberApi.Core/ConfiguratorSystem/ConfiguratorPatcher.cs} (56%) create mode 100644 Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorRepository.cs rename Core/{TimberApi => TimberApi.Core}/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs (57%) delete mode 100644 Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs delete mode 100644 Core/TimberApi/ConfiguratorSystem/ConfiguratorRepository.cs delete mode 100644 Core/TimberApi/LocalizationSystem/LocalizationSystemConfigurator.cs diff --git a/Core/TimberApi.Common/Extensions/AppDomainExtensions.cs b/Core/TimberApi.Common/Extensions/AppDomainExtensions.cs deleted file mode 100644 index 7ab8b2c1..00000000 --- a/Core/TimberApi.Common/Extensions/AppDomainExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; - -namespace TimberApi.Common.Extensions -{ - public static class ReflectionHelper - { - public static IEnumerable GetTypesInDomainByAttribute(this AppDomain domain) where TAttribute : class - { - for (var assemblyIndex = 0; assemblyIndex < domain.GetAssemblies().Length; assemblyIndex++) - { - Assembly assembly = domain.GetAssemblies()[assemblyIndex]; - - foreach (Type type in assembly.GetTypes()) - { - if (!type.IsDefined(typeof(TAttribute))) - { - continue; - } - - yield return type; - } - } - } - } -} \ No newline at end of file diff --git a/Core/TimberApi.Common/Extensions/AssemblyExtensions.cs b/Core/TimberApi.Common/Extensions/AssemblyExtensions.cs index 373b137c..f9dc7b00 100644 --- a/Core/TimberApi.Common/Extensions/AssemblyExtensions.cs +++ b/Core/TimberApi.Common/Extensions/AssemblyExtensions.cs @@ -11,5 +11,10 @@ public static IEnumerable GetTypesWithInterface(this Assembly { return assembly.GetTypes().Where(x => typeof(TInterface).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract); } + + public static IEnumerable GetTypesByAttribute(this Assembly assembly) where TAttribute : class + { + return assembly.GetTypes().Where(type => type.IsDefined(typeof(TAttribute))); + } } } \ No newline at end of file diff --git a/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs b/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs index 7e34a50b..cab4e622 100644 --- a/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs +++ b/Core/TimberApi.Core/BootstrapSystem/TimberApiBootstrapSystemConfigurator.cs @@ -4,10 +4,13 @@ using Bindito.Unity; using TimberApi.Common; using TimberApi.Core.ConfigSystem; +using TimberApi.Core.ConfiguratorSystem; using TimberApi.Core.ConsoleSystem; using TimberApi.Core.LoggingSystem; using TimberApi.Core.ModLoaderSystem; using TimberApi.Core.SingletonSystem; +using TimberApi.LocalizationSystem; +using TimberApi.SpecificationSystem; namespace TimberApi.Core.BootstrapSystem { @@ -22,6 +25,9 @@ private void Awake() Instance = this; AddPrefabConfigurators(); BootstrapPatcher.Patch(); + ConfiguratorPatcher.Patch(); + LocalizationPatcher.Patch(); + SpecificationPatcher.Patch(); SingletonSystemPatcher.Patch(); } catch (Exception e) @@ -38,6 +44,7 @@ public override void Configure(IContainerDefinition containerDefinition) containerDefinition.Install(GetInstanceFromPrefab()); containerDefinition.Install(GetInstanceFromPrefab()); containerDefinition.Install(new ConfigSystemConfigurator()); + containerDefinition.Install(new ConfiguratorSystemConfigurator()); // Start runner containerDefinition.Bind().AsSingleton(); diff --git a/Core/TimberApi/ConfiguratorSystem/ConfigurationPreLoadableSingleton.cs b/Core/TimberApi.Core/ConfiguratorSystem/ConfigurationRepositorySeeder.cs similarity index 57% rename from Core/TimberApi/ConfiguratorSystem/ConfigurationPreLoadableSingleton.cs rename to Core/TimberApi.Core/ConfiguratorSystem/ConfigurationRepositorySeeder.cs index 9ba4d8d3..18b99751 100644 --- a/Core/TimberApi/ConfiguratorSystem/ConfigurationPreLoadableSingleton.cs +++ b/Core/TimberApi.Core/ConfiguratorSystem/ConfigurationRepositorySeeder.cs @@ -5,30 +5,45 @@ using System.Reflection; using Bindito.Core; using TimberApi.Common.Extensions; -using TimberApi.Common.SingletonSystem; +using TimberApi.ConfiguratorSystem; +using TimberApi.ModSystem; using TimberApi.SceneSystem; -namespace TimberApi.ConfiguratorSystem +namespace TimberApi.Core.ConfiguratorSystem { - internal class ConfigurationPreLoadableSingleton : ITimberApiPreLoadableSingleton + internal class ConfigurationRepositorySeeder { private readonly ConfiguratorRepository _configuratorRepository; - public ConfigurationPreLoadableSingleton(ConfiguratorRepository configuratorRepository) + private readonly IModRepository _modRepository; + + public ConfigurationRepositorySeeder(ConfiguratorRepository configuratorRepository, IModRepository modRepository) { _configuratorRepository = configuratorRepository; + _modRepository = modRepository; } /// /// Searches for configurators to fill repository /// - public void PreLoad() + public void Run() + { + AddConfiguratorsFromAssemblyToRepository(typeof(TimberApi).Assembly); + + foreach (IMod mod in _modRepository.GetCodeMods()) + { + AddConfiguratorsFromAssemblyToRepository(mod.LoadedAssembly!); + } + } + + public void AddConfiguratorsFromAssemblyToRepository(Assembly assembly) { - ImmutableArray validatedConfigurators = ValidateAndCreateConfigurators(AppDomain.CurrentDomain.GetTypesInDomainByAttribute()).ToImmutableArray(); + IEnumerable configuratorTypes = assembly.GetTypesByAttribute(); + ImmutableArray configurators = ValidateAndCreateConfigurators(configuratorTypes).ToImmutableArray(); - _configuratorRepository.SetSceneConfigurators(SceneEntrypoint.MainMenu, GetConfiguratorWithSceneFlag(validatedConfigurators, SceneEntrypoint.MainMenu)); - _configuratorRepository.SetSceneConfigurators(SceneEntrypoint.InGame, GetConfiguratorWithSceneFlag(validatedConfigurators, SceneEntrypoint.InGame)); - _configuratorRepository.SetSceneConfigurators(SceneEntrypoint.MapEditor, GetConfiguratorWithSceneFlag(validatedConfigurators, SceneEntrypoint.MapEditor)); + _configuratorRepository.AddRange(SceneEntrypoint.MainMenu, GetConfiguratorWithSceneFlag(configurators, SceneEntrypoint.MainMenu)); + _configuratorRepository.AddRange(SceneEntrypoint.InGame, GetConfiguratorWithSceneFlag(configurators, SceneEntrypoint.InGame)); + _configuratorRepository.AddRange(SceneEntrypoint.MapEditor, GetConfiguratorWithSceneFlag(configurators, SceneEntrypoint.MapEditor)); } /// @@ -38,7 +53,7 @@ public void PreLoad() /// validated configurators /// Filter based if attribute has set flag /// Filtered configurators for specific flag - private IEnumerable GetConfiguratorWithSceneFlag(IEnumerable configurators, SceneEntrypoint sceneFlag) + private static IEnumerable GetConfiguratorWithSceneFlag(IEnumerable configurators, SceneEntrypoint sceneFlag) { return configurators.Where(configurator => configurator.GetType().GetCustomAttribute().SceneConfiguratorEntry.HasFlag(sceneFlag)); } @@ -48,7 +63,7 @@ private IEnumerable GetConfiguratorWithSceneFlag(IEnumerable /// Classes with ConfiguratorAttribute /// Validated IConfigurator instances - private IEnumerable ValidateAndCreateConfigurators(IEnumerable configuratorTypes) + private static IEnumerable ValidateAndCreateConfigurators(IEnumerable configuratorTypes) { return configuratorTypes.Select(type => { @@ -63,7 +78,7 @@ private IEnumerable ValidateAndCreateConfigurators(IEnumerable /// /// Configurator class type - private void ValidateConfiguratorAttributeType(Type configuratorType) + private static void ValidateConfiguratorAttributeType(Type configuratorType) { if (configuratorType.GetInterface(nameof(IConfigurator)) == null) { diff --git a/Core/TimberApi/ConfiguratorSystem/ConfigurationValidationException.cs b/Core/TimberApi.Core/ConfiguratorSystem/ConfigurationValidationException.cs similarity index 93% rename from Core/TimberApi/ConfiguratorSystem/ConfigurationValidationException.cs rename to Core/TimberApi.Core/ConfiguratorSystem/ConfigurationValidationException.cs index b5acadc0..cb96ef2e 100644 --- a/Core/TimberApi/ConfiguratorSystem/ConfigurationValidationException.cs +++ b/Core/TimberApi.Core/ConfiguratorSystem/ConfigurationValidationException.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.Serialization; -namespace TimberApi.ConfiguratorSystem +namespace TimberApi.Core.ConfiguratorSystem { internal class ConfigurationValidationException : Exception { diff --git a/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorInstaller.cs b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorInstaller.cs new file mode 100644 index 00000000..f649b316 --- /dev/null +++ b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorInstaller.cs @@ -0,0 +1,24 @@ +using Bindito.Core; +using TimberApi.SceneSystem; + +namespace TimberApi.Core.ConfiguratorSystem +{ + internal class ConfiguratorInstaller + { + private readonly ConfiguratorRepository _configuratorRepository; + + public ConfiguratorInstaller(ConfiguratorRepository configuratorRepository) + { + _configuratorRepository = configuratorRepository; + TimberApiSceneManager.SceneChanged += OnSceneChanged; + } + + private void OnSceneChanged(SceneEntrypoint previousScene, SceneEntrypoint currentScene, IContainerDefinition currentContainerDefinition) + { + foreach (IConfigurator configurator in _configuratorRepository.SceneConfigurators[currentScene]) + { + currentContainerDefinition.Install(configurator); + } + } + } +} \ No newline at end of file diff --git a/Core/TimberApi/SceneSystem/SceneListener.cs b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorPatcher.cs similarity index 56% rename from Core/TimberApi/SceneSystem/SceneListener.cs rename to Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorPatcher.cs index 1b0b7ae7..c037600d 100644 --- a/Core/TimberApi/SceneSystem/SceneListener.cs +++ b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorPatcher.cs @@ -1,41 +1,39 @@ using Bindito.Core; using HarmonyLib; -using TimberApi.Common.SingletonSystem; -using TimberApi.ConfiguratorSystem; +using TimberApi.SceneSystem; using Timberborn.MainMenuScene; using Timberborn.MapEditorScene; using Timberborn.MasterScene; -namespace TimberApi.SceneSystem +namespace TimberApi.Core.ConfiguratorSystem { - internal class SceneListener : ITimberApiLoadableSingleton + public static class ConfiguratorPatcher { - public void Load() + public static void Patch() { var harmony = new Harmony("TimberApi.SceneListener"); - harmony.Patch(AccessTools.Method(typeof(MasterSceneConfigurator), "Configure"), new HarmonyMethod(AccessTools.Method(typeof(SceneListener), nameof(PatchMasterSceneConfigurator)))); + harmony.Patch(AccessTools.Method(typeof(MasterSceneConfigurator), "Configure"), new HarmonyMethod(AccessTools.Method(typeof(ConfiguratorPatcher), nameof(PatchMasterSceneConfigurator)))); - harmony.Patch(AccessTools.Method(typeof(MainMenuSceneConfigurator), "Configure"), new HarmonyMethod(AccessTools.Method(typeof(SceneListener), nameof(PatchMainMenuSceneConfigurator)))); + harmony.Patch(AccessTools.Method(typeof(MainMenuSceneConfigurator), "Configure"), + new HarmonyMethod(AccessTools.Method(typeof(ConfiguratorPatcher), nameof(PatchMainMenuSceneConfigurator)))); - harmony.Patch(AccessTools.Method(typeof(MapEditorSceneConfigurator), "Configure"), new HarmonyMethod(AccessTools.Method(typeof(SceneListener), nameof(PatchMapEditorSceneConfigurator)))); + harmony.Patch(AccessTools.Method(typeof(MapEditorSceneConfigurator), "Configure"), + new HarmonyMethod(AccessTools.Method(typeof(ConfiguratorPatcher), nameof(PatchMapEditorSceneConfigurator)))); } private static void PatchMasterSceneConfigurator(IContainerDefinition containerDefinition) { - TimberApiSceneManager.ChangeScene(SceneEntrypoint.InGame); - ConfiguratorInstaller.Install(containerDefinition, SceneEntrypoint.InGame); + TimberApiSceneManager.ChangeScene(SceneEntrypoint.InGame, containerDefinition); } private static void PatchMainMenuSceneConfigurator(IContainerDefinition containerDefinition) { - TimberApiSceneManager.ChangeScene(SceneEntrypoint.MainMenu); - ConfiguratorInstaller.Install(containerDefinition, SceneEntrypoint.MainMenu); + TimberApiSceneManager.ChangeScene(SceneEntrypoint.MainMenu, containerDefinition); } private static void PatchMapEditorSceneConfigurator(IContainerDefinition containerDefinition) { - TimberApiSceneManager.ChangeScene(SceneEntrypoint.MapEditor); - ConfiguratorInstaller.Install(containerDefinition, SceneEntrypoint.MapEditor); + TimberApiSceneManager.ChangeScene(SceneEntrypoint.MapEditor, containerDefinition); } } } \ No newline at end of file diff --git a/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorRepository.cs b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorRepository.cs new file mode 100644 index 00000000..46a7807a --- /dev/null +++ b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorRepository.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using Bindito.Core; +using TimberApi.SceneSystem; + +namespace TimberApi.Core.ConfiguratorSystem +{ + internal class ConfiguratorRepository + { + public Dictionary> SceneConfigurators = new(); + + public void AddRange(SceneEntrypoint sceneEntrypoint, IEnumerable configurators) + { + if (SceneConfigurators.ContainsKey(sceneEntrypoint)) + { + SceneConfigurators[sceneEntrypoint].AddRange(configurators); + } + else + { + SceneConfigurators.Add(sceneEntrypoint, new List(configurators)); + } + } + } +} \ No newline at end of file diff --git a/Core/TimberApi/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs similarity index 57% rename from Core/TimberApi/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs rename to Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs index f961516e..b6b0035c 100644 --- a/Core/TimberApi/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs +++ b/Core/TimberApi.Core/ConfiguratorSystem/ConfiguratorSystemConfigurator.cs @@ -1,13 +1,14 @@ using Bindito.Core; -namespace TimberApi.ConfiguratorSystem +namespace TimberApi.Core.ConfiguratorSystem { internal class ConfiguratorSystemConfigurator : IConfigurator { public void Configure(IContainerDefinition containerDefinition) { containerDefinition.Bind().AsSingleton(); - containerDefinition.Bind().AsSingleton(); + containerDefinition.Bind().AsSingleton(); + containerDefinition.Bind().AsSingleton(); } } } \ No newline at end of file diff --git a/Core/TimberApi.Core/TimberApiCoreRunner.cs b/Core/TimberApi.Core/TimberApiCoreRunner.cs index de1d9e73..1b971830 100644 --- a/Core/TimberApi.Core/TimberApiCoreRunner.cs +++ b/Core/TimberApi.Core/TimberApiCoreRunner.cs @@ -2,6 +2,7 @@ using TimberApi.Common; using TimberApi.Common.ConsoleSystem; using TimberApi.ConfigSystem; +using TimberApi.Core.ConfiguratorSystem; using TimberApi.Core.ModLoaderSystem; using UnityEngine; @@ -9,17 +10,20 @@ namespace TimberApi.Core { internal class TimberApiCoreRunner { + private readonly ConfigurationRepositorySeeder _configurationRepositorySeeder; private readonly IInternalConsoleWriter _consoleWriter; private readonly ModLoader _modLoader; private readonly ModRepository _modRepository; - public TimberApiCoreRunner(IInternalConsoleWriter consoleWriter, ModLoader modLoader, ModRepository modRepository, IConfigServiceFactory configServiceFactory) + public TimberApiCoreRunner(IInternalConsoleWriter consoleWriter, ModLoader modLoader, ModRepository modRepository, IConfigServiceFactory configServiceFactory, + ConfigurationRepositorySeeder configurationRepositorySeeder) { _consoleWriter = consoleWriter; _modLoader = modLoader; _modRepository = modRepository; + _configurationRepositorySeeder = configurationRepositorySeeder; TimberApiCore.Configs = configServiceFactory.CreateWithAssemblyConfigs(typeof(TimberApiCoreRunner).Assembly, Paths.TimberApi, "TimberAPI"); Run(); } @@ -30,6 +34,7 @@ public void Run() LogType.Log); _modLoader.Run(); _modRepository.LoadMods(); + _configurationRepositorySeeder.Run(); } } } \ No newline at end of file diff --git a/Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs b/Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs deleted file mode 100644 index 03409be4..00000000 --- a/Core/TimberApi/ConfiguratorSystem/ConfiguratorInstaller.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Bindito.Core; -using TimberApi.SceneSystem; - -namespace TimberApi.ConfiguratorSystem -{ - internal static class ConfiguratorInstaller - { - public static void Install(IContainerDefinition containerDefinition, SceneEntrypoint sceneEntrypoint) - { - foreach (IConfigurator configurator in ConfiguratorRepository.SceneConfigurators[sceneEntrypoint]) - { - containerDefinition.Install(configurator); - } - } - } -} \ No newline at end of file diff --git a/Core/TimberApi/ConfiguratorSystem/ConfiguratorRepository.cs b/Core/TimberApi/ConfiguratorSystem/ConfiguratorRepository.cs deleted file mode 100644 index 0dce913f..00000000 --- a/Core/TimberApi/ConfiguratorSystem/ConfiguratorRepository.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using Bindito.Core; -using TimberApi.SceneSystem; - -namespace TimberApi.ConfiguratorSystem -{ - internal class ConfiguratorRepository - { - public static Dictionary> SceneConfigurators = new(); - - public void SetSceneConfigurators(SceneEntrypoint sceneEntrypoint, IEnumerable configurators) - { - if (SceneConfigurators.ContainsKey(sceneEntrypoint)) - { - throw new Exception($"{sceneEntrypoint} already loaded"); - } - - SceneConfigurators.Add(sceneEntrypoint, configurators.ToImmutableArray()); - } - } -} \ No newline at end of file diff --git a/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs b/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs index 1596ec60..16501a4e 100644 --- a/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs +++ b/Core/TimberApi/LocalizationSystem/LocalizationPatcher.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using HarmonyLib; -using TimberApi.Common.SingletonSystem; using Timberborn.Common; using UnityEngine; @@ -9,9 +8,9 @@ namespace TimberApi.LocalizationSystem { - internal class LocalizationPatcher : ITimberApiLoadableSingleton + internal static class LocalizationPatcher { - public void Load() + public static void Patch() { var harmony = new Harmony("TimberApi.Localization"); diff --git a/Core/TimberApi/LocalizationSystem/LocalizationSystemConfigurator.cs b/Core/TimberApi/LocalizationSystem/LocalizationSystemConfigurator.cs deleted file mode 100644 index bc2f2db1..00000000 --- a/Core/TimberApi/LocalizationSystem/LocalizationSystemConfigurator.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Bindito.Core; - -namespace TimberApi.LocalizationSystem -{ - public class LocalizationSystemConfigurator : IConfigurator - { - public void Configure(IContainerDefinition containerDefinition) - { - containerDefinition.Bind().AsSingleton(); - } - } -} \ No newline at end of file diff --git a/Core/TimberApi/SceneSystem/TimberApiSceneManager.cs b/Core/TimberApi/SceneSystem/TimberApiSceneManager.cs index acbc47c5..94d0a218 100644 --- a/Core/TimberApi/SceneSystem/TimberApiSceneManager.cs +++ b/Core/TimberApi/SceneSystem/TimberApiSceneManager.cs @@ -1,15 +1,22 @@ -namespace TimberApi.SceneSystem +using Bindito.Core; + +namespace TimberApi.SceneSystem { internal static class TimberApiSceneManager { + public delegate void SceneChangedCallback(SceneEntrypoint previousScene, SceneEntrypoint currentScene, IContainerDefinition currentContainerDefinition); + public static SceneEntrypoint PreviousScene { get; private set; } public static SceneEntrypoint CurrentScene { get; private set; } - public static void ChangeScene(SceneEntrypoint sceneEntrypoint) + public static event SceneChangedCallback SceneChanged = delegate { }; + + public static void ChangeScene(SceneEntrypoint sceneEntrypoint, IContainerDefinition currentContainerDefinition) { PreviousScene = CurrentScene; CurrentScene = sceneEntrypoint; + SceneChanged(PreviousScene, CurrentScene, currentContainerDefinition); } } } \ No newline at end of file diff --git a/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs b/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs index b49e7cee..0d1fbb57 100644 --- a/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs +++ b/Core/TimberApi/SpecificationSystem/SpecificationPatcher.cs @@ -1,15 +1,14 @@ using System.Collections.Generic; using System.Reflection.Emit; using HarmonyLib; -using TimberApi.Common.SingletonSystem; using Timberborn.MainMenuScene; using Timberborn.Persistence; namespace TimberApi.SpecificationSystem { - internal class SpecificationPatcher : ITimberApiLoadableSingleton + internal static class SpecificationPatcher { - public void Load() + public static void Patch() { var harmony = new Harmony("TimberApi.specification"); diff --git a/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs b/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs index f170b995..5e5f177e 100644 --- a/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs +++ b/Core/TimberApi/SpecificationSystem/SpecificationSystemConfigurator.cs @@ -15,15 +15,4 @@ public void Configure(IContainerDefinition containerDefinition) containerDefinition.Bind().To().AsSingleton(); } } - - /// - /// registered in global container, `ISpecificationService` called before seeder was loaded - /// - internal class SpecificationSystemGlobalConfigurator : IConfigurator - { - public void Configure(IContainerDefinition containerDefinition) - { - containerDefinition.Bind().AsSingleton(); - } - } } \ No newline at end of file diff --git a/Core/TimberApi/TimberApiBootstrapConfigurator.cs b/Core/TimberApi/TimberApiBootstrapConfigurator.cs index bdc378a8..81058107 100644 --- a/Core/TimberApi/TimberApiBootstrapConfigurator.cs +++ b/Core/TimberApi/TimberApiBootstrapConfigurator.cs @@ -1,10 +1,6 @@ using Bindito.Core; using TimberApi.AssetSystem; -using TimberApi.ConfiguratorSystem; -using TimberApi.LocalizationSystem; using TimberApi.ResourceAssetSystem; -using TimberApi.SceneSystem; -using TimberApi.SpecificationSystem; using TimberApi.SpecificationSystem.CustomSpecifications.Golems; namespace TimberApi @@ -14,13 +10,9 @@ internal class BootstrapConfigurator : IConfigurator public void Configure(IContainerDefinition containerDefinition) { containerDefinition.Bind().AsSingleton(); - containerDefinition.Bind().AsSingleton(); - containerDefinition.Install(new ConfiguratorSystemConfigurator()); containerDefinition.Install(new AssetSystemGlobalConfigurator()); - containerDefinition.Install(new SpecificationSystemGlobalConfigurator()); containerDefinition.Install(new GolemFactionPatchConfigurator()); containerDefinition.Install(new ResourceAssetSystemConfigurator()); - containerDefinition.Install(new LocalizationSystemConfigurator()); } } } \ No newline at end of file diff --git a/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs b/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs index 01526c2f..3254600e 100644 --- a/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs +++ b/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs @@ -2,6 +2,7 @@ using TimberApi.UiBuilderSystem.ElementSystem; using TimberApi.UiBuilderSystem.PresetSystem; using Timberborn.AssetSystem; +using Timberborn.SingletonSystem; using UnityEngine; using UnityEngine.UIElements; using static UnityEngine.UIElements.Length.Unit; @@ -10,7 +11,7 @@ namespace TimberApi.UiBuilderSystem { - public class UIBoxBuilder + public class UIBoxBuilder : ILoadableSingleton { private readonly VisualElementBuilder _box; @@ -30,6 +31,11 @@ public UIBoxBuilder(ComponentBuilder componentBuilder, IResourceAssetLoader reso _scrollView = componentBuilder.CreateVisualElement(); } + public void Load() + { + TimberApi.ConsoleWriter.Log("AAAAAAAAAAAAAAAAAAAAAAAA"); + } + public UIBoxBuilder SetBoxInCenter() { _centerWrapper.SetJustifyContent(Justify.Center).SetAlignItems(Align.Center).SetFlexDirection(FlexDirection.Row).SetFlexWrap(Wrap.Wrap).SetStyle(style => style.flexGrow = 1); diff --git a/Core/TimberApi/UiBuilderSystem/UiBuilderSystemConfigurator.cs b/Core/TimberApi/UiBuilderSystem/UiBuilderSystemConfigurator.cs index 58c8a604..8e7ea8c9 100644 --- a/Core/TimberApi/UiBuilderSystem/UiBuilderSystemConfigurator.cs +++ b/Core/TimberApi/UiBuilderSystem/UiBuilderSystemConfigurator.cs @@ -10,8 +10,8 @@ public class UiBuilderSystemConfigurator : IConfigurator { public void Configure(IContainerDefinition containerDefinition) { - containerDefinition.Bind().To().AsTransient(); - containerDefinition.Bind().To().AsSingleton(); + containerDefinition.Bind().AsTransient(); + containerDefinition.Bind().AsSingleton(); containerDefinition.Bind().AsSingleton(); containerDefinition.Bind().AsSingleton(); } From 78c04cd69fb6f7e6d3fe29736149d812d6f452f2 Mon Sep 17 00:00:00 2001 From: KYPremco Date: Wed, 21 Sep 2022 22:47:45 +0200 Subject: [PATCH 5/6] Fixed IModEntrypoint crash when none existing --- Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs b/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs index cd00745e..72d464bf 100644 --- a/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs +++ b/Core/TimberApi.Core/ModLoaderSystem/ModLoader.cs @@ -187,7 +187,7 @@ private void InitializeEntrypointInAssembly(Assembly assembly, IMod mod) try { IModEntrypoint? modEntrypoint = assembly.GetTypes().Where(type => type.GetInterfaces().Contains(typeof(IModEntrypoint)) && type.GetConstructor(Type.EmptyTypes) != null) - .Select(type => Activator.CreateInstance(type) as IModEntrypoint).Single(); + .Select(type => Activator.CreateInstance(type) as IModEntrypoint).SingleOrDefault(); if (modEntrypoint == null) { From 739ab6ce3964aaf73d7acfc7e327044e252953ad Mon Sep 17 00:00:00 2001 From: KYPremco Date: Wed, 21 Sep 2022 22:50:59 +0200 Subject: [PATCH 6/6] Removed weird console log --- Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs b/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs index 3254600e..01526c2f 100644 --- a/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs +++ b/Core/TimberApi/UiBuilderSystem/UIBoxBuilder.cs @@ -2,7 +2,6 @@ using TimberApi.UiBuilderSystem.ElementSystem; using TimberApi.UiBuilderSystem.PresetSystem; using Timberborn.AssetSystem; -using Timberborn.SingletonSystem; using UnityEngine; using UnityEngine.UIElements; using static UnityEngine.UIElements.Length.Unit; @@ -11,7 +10,7 @@ namespace TimberApi.UiBuilderSystem { - public class UIBoxBuilder : ILoadableSingleton + public class UIBoxBuilder { private readonly VisualElementBuilder _box; @@ -31,11 +30,6 @@ public UIBoxBuilder(ComponentBuilder componentBuilder, IResourceAssetLoader reso _scrollView = componentBuilder.CreateVisualElement(); } - public void Load() - { - TimberApi.ConsoleWriter.Log("AAAAAAAAAAAAAAAAAAAAAAAA"); - } - public UIBoxBuilder SetBoxInCenter() { _centerWrapper.SetJustifyContent(Justify.Center).SetAlignItems(Align.Center).SetFlexDirection(FlexDirection.Row).SetFlexWrap(Wrap.Wrap).SetStyle(style => style.flexGrow = 1);