From 3a7a56d398ee15b1fe483401f6c21fab5b9351c5 Mon Sep 17 00:00:00 2001 From: KYPremco Date: Fri, 15 Nov 2024 19:40:55 +0100 Subject: [PATCH 01/18] Converted simple tool factories to 1 GenericToolFactory --- .../ToolSystem/IUnselectableTool.cs | 4 +- .../BeaverGeneratorToolConfigurator.cs | 3 +- .../BeaverGeneratorToolFactory.cs | 14 ------ ...ool.cs => BeaverGeneratorToolGenerator.cs} | 8 +-- .../BotGeneratorToolConfigurator.cs | 1 - .../BotGenerator/BotGeneratorToolFactory.cs | 14 ------ .../BotGenerator/BotGeneratorToolGenerator.cs | 6 ++- .../Tools/Cursor/CursorToolConfigurator.cs | 1 - .../Tools/Cursor/CursorToolFactory.cs | 16 ------ .../Tools/Cursor/CursorToolGenerator.cs | 6 ++- .../Tools/Empty/EmptyToolConfigurator.cs | 12 ----- .../Tools/Empty/EmptyToolFactory.cs | 13 ----- .../GenericTool/GenericToolConfigurator.cs | 14 ++++++ .../Tools/GenericTool/GenericToolFactory.cs | 49 +++++++++++++++++++ .../GenericTool/GenericToolToolInformation.cs | 6 +++ .../Tools/SettingBox/SettingBoxTool.cs | 13 +---- .../SettingBox/SettingBoxToolConfigurator.cs | 2 +- .../Tools/SettingBox/SettingBoxToolFactory.cs | 14 ------ .../SettingBox/SettingBoxToolGenerator.cs | 5 +- .../TreeCuttingAreaSelectionToolFactory.cs | 17 ------- .../TreeCuttingAreaToolConfigurator.cs | 2 - .../TreeCuttingAreaToolGenerator.cs | 11 +++-- .../TreeCuttingAreaUnselectionToolFactory.cs | 17 ------- .../WaterGeneratorToolConfigurator.cs | 1 - .../WaterGeneratorToolFactory.cs | 15 ------ .../WaterGeneratorToolGenerator.cs | 6 ++- 26 files changed, 101 insertions(+), 169 deletions(-) delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolFactory.cs rename Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/{BeaverGeneratorTool.cs => BeaverGeneratorToolGenerator.cs} (76%) delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolFactory.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolFactory.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolConfigurator.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolFactory.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolConfigurator.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolFactory.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolToolInformation.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolFactory.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaSelectionToolFactory.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaUnselectionToolFactory.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolFactory.cs diff --git a/Src/TimberApi.Tools/ToolSystem/IUnselectableTool.cs b/Src/TimberApi.Tools/ToolSystem/IUnselectableTool.cs index 64eacfbd..e6b1a310 100644 --- a/Src/TimberApi.Tools/ToolSystem/IUnselectableTool.cs +++ b/Src/TimberApi.Tools/ToolSystem/IUnselectableTool.cs @@ -1,5 +1,3 @@ namespace TimberApi.Tools.ToolSystem; -public interface IUnselectableTool -{ -} \ No newline at end of file +public interface IUnselectableTool; \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolConfigurator.cs index dc7b095b..797c1f8e 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolConfigurator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolConfigurator.cs @@ -8,7 +8,6 @@ internal class BeaverGeneratorToolConfigurator : IConfigurator { public void Configure(IContainerDefinition containerDefinition) { - containerDefinition.MultiBind().To().AsSingleton(); - containerDefinition.MultiBind().To().AsSingleton(); + containerDefinition.MultiBind().To().AsSingleton(); } } \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolFactory.cs deleted file mode 100644 index 0e5f7baa..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolFactory.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Timberborn.ToolSystem; - -namespace TimberApi.Tools.ToolSystem.Tools.BeaverGenerator; - -internal class BeaverGeneratorToolFactory(Timberborn.BeaversUI.BeaverGeneratorTool beaverGeneratorTool) - : IToolFactory -{ - public string Id => "BeaverGeneratorTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - return beaverGeneratorTool; - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorTool.cs b/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolGenerator.cs similarity index 76% rename from Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorTool.cs rename to Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolGenerator.cs index 2772a456..1f59d257 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorTool.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/BeaverGenerator/BeaverGeneratorToolGenerator.cs @@ -1,17 +1,18 @@ using System.Collections.Generic; using Newtonsoft.Json; using TimberApi.SpecificationSystem; +using Timberborn.BeaversUI; namespace TimberApi.Tools.ToolSystem.Tools.BeaverGenerator; -internal class BeaverGeneratorTool : ISpecificationGenerator +internal class BeaverGeneratorToolGenerator : ISpecificationGenerator { public IEnumerable Generate() { var json = JsonConvert.SerializeObject(new { Id = "BeaverGenerator", - Type = "BeaverGeneratorTool", + Type = "GenericTool", Layout = "GrouplessRed", Order = 60, NameLocKey = "Cursor", @@ -21,7 +22,8 @@ public IEnumerable Generate() Hidden = false, ToolInformation = new { - BottomBarSection = 0 + BottomBarSection = 0, + ClassName = typeof(BeaverGeneratorTool).FullName } }); diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolConfigurator.cs index 54df3e90..4aa2d90c 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolConfigurator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolConfigurator.cs @@ -8,7 +8,6 @@ public class BotGeneratorToolConfigurator : IConfigurator { public void Configure(IContainerDefinition containerDefinition) { - containerDefinition.MultiBind().To().AsSingleton(); containerDefinition.MultiBind().To().AsSingleton(); } } \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolFactory.cs deleted file mode 100644 index e0f0bf44..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolFactory.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Timberborn.BotsUI; -using Timberborn.ToolSystem; - -namespace TimberApi.Tools.ToolSystem.Tools.BotGenerator; - -public class BotGeneratorToolFactory(BotGeneratorTool botGeneratorTool) : IToolFactory -{ - public string Id => "BotGeneratorTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - return botGeneratorTool; - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolGenerator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolGenerator.cs index 999a522e..af6467d4 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolGenerator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/BotGenerator/BotGeneratorToolGenerator.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Newtonsoft.Json; using TimberApi.SpecificationSystem; +using Timberborn.BotsUI; namespace TimberApi.Tools.ToolSystem.Tools.BotGenerator; @@ -11,7 +12,7 @@ public IEnumerable Generate() var json = JsonConvert.SerializeObject(new { Id = "BotGenerator", - Type = "BotGeneratorTool", + Type = "GenericTool", Layout = "GrouplessRed", Order = 70, NameLocKey = "Cursor", @@ -21,7 +22,8 @@ public IEnumerable Generate() Hidden = false, ToolInformation = new { - BottomBarSection = 0 + BottomBarSection = 0, + ClassName = typeof(BotGeneratorTool).FullName } }); diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolConfigurator.cs index 8bf3c04e..48baf927 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolConfigurator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolConfigurator.cs @@ -8,7 +8,6 @@ public class CursorToolConfigurator : IConfigurator { public void Configure(IContainerDefinition containerDefinition) { - containerDefinition.MultiBind().To().AsSingleton(); containerDefinition.MultiBind().To().AsSingleton(); } } \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolFactory.cs deleted file mode 100644 index fc8b385d..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolFactory.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Timberborn.CursorToolSystem; -using Timberborn.ToolSystem; - -namespace TimberApi.Tools.ToolSystem.Tools.Cursor; - -public class CursorToolFactory(CursorTool cursorTool) : IToolFactory -{ - public string Id => "CursorTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - cursorTool.ToolGroup = toolGroup; - - return cursorTool; - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolGenerator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolGenerator.cs index 6ff6f90b..203bd346 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolGenerator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/Cursor/CursorToolGenerator.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Newtonsoft.Json; using TimberApi.SpecificationSystem; +using Timberborn.CursorToolSystem; namespace TimberApi.Tools.ToolSystem.Tools.Cursor; @@ -11,7 +12,7 @@ public IEnumerable Generate() var json = JsonConvert.SerializeObject(new { Id = "Cursor", - Type = "CursorTool", + Type = "GenericTool", Layout = "GrouplessRed", Order = 0, NameLocKey = "Cursor", @@ -21,7 +22,8 @@ public IEnumerable Generate() Hidden = false, ToolInformation = new { - BottomBarSection = 0 + BottomBarSection = 0, + ClassName = typeof(CursorTool).FullName, } }); diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolConfigurator.cs deleted file mode 100644 index e6b6230c..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolConfigurator.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Bindito.Core; - -namespace TimberApi.Tools.ToolSystem.Tools.Empty; - -[Context("Game")] -public class EmptyToolConfigurator : IConfigurator -{ - public void Configure(IContainerDefinition containerDefinition) - { - containerDefinition.MultiBind().To().AsSingleton(); - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolFactory.cs deleted file mode 100644 index 62c954f0..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/Empty/EmptyToolFactory.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Timberborn.ToolSystem; - -namespace TimberApi.Tools.ToolSystem.Tools.Empty; - -public class EmptyToolFactory : IToolFactory -{ - public string Id => "EmptyTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - return new EmptyTool(); - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolConfigurator.cs new file mode 100644 index 00000000..77de2c7b --- /dev/null +++ b/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolConfigurator.cs @@ -0,0 +1,14 @@ +using Bindito.Core; +using TimberApi.SpecificationSystem; +using TimberApi.Tools.ToolSystem.Tools.Planting; + +namespace TimberApi.Tools.ToolSystem.Tools.GenericTool; + +[Context("Game")] +public class GenericToolConfigurator : IConfigurator +{ + public void Configure(IContainerDefinition containerDefinition) + { + containerDefinition.MultiBind().To().AsSingleton(); + } +} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolFactory.cs new file mode 100644 index 00000000..47255e5d --- /dev/null +++ b/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolFactory.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using TimberApi.DependencyContainerSystem; +using TimberApi.SingletonSystem; +using Timberborn.Persistence; +using Timberborn.ToolSystem; + +namespace TimberApi.Tools.ToolSystem.Tools.GenericTool; + +public class GenericToolFactory : BaseToolFactory, IEarlyLoadableSingleton +{ + private readonly PropertyKey _classnameKey = new("ClassName"); + + public override string Id => "GenericTool"; + + private List _toolTypes = null!; + + protected override Tool CreateTool(ToolSpecification toolSpecification, GenericToolToolInformation toolInformation, ToolGroup? toolGroup) + { + if (DependencyContainer.GetInstance(GetTypeFromName(toolInformation.ClassName)) is not Tool tool) + { + throw new Exception($"GenericToolFactory could not find the Tool {toolInformation.ClassName}"); + } + + tool.ToolGroup = toolGroup; + + return tool; + } + + protected override GenericToolToolInformation DeserializeToolInformation(IObjectLoader objectLoader) + { + return new GenericToolToolInformation(objectLoader.Get(_classnameKey)); + } + + public void EarlyLoad() + { + _toolTypes = AppDomain.CurrentDomain + .GetAssemblies() + .SelectMany(assembly => assembly.GetTypes()) + .Where(t => t.IsSubclassOf(typeof(Tool))) + .ToList(); + } + + private Type GetTypeFromName(string classNameOrFullName) + { + return _toolTypes.SingleOrDefault(type => type.FullName == classNameOrFullName || type.Name == classNameOrFullName) ?? throw new Exception($"GenericToolFactory could not find the Tool {classNameOrFullName}"); + } +} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolToolInformation.cs b/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolToolInformation.cs new file mode 100644 index 00000000..3997ee01 --- /dev/null +++ b/Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolToolInformation.cs @@ -0,0 +1,6 @@ +namespace TimberApi.Tools.ToolSystem.Tools.GenericTool; + +public class GenericToolToolInformation(string className) +{ + public string ClassName { get; } = className; +} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxTool.cs b/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxTool.cs index 262a19f1..ae3c768f 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxTool.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxTool.cs @@ -3,20 +3,11 @@ namespace TimberApi.Tools.ToolSystem.Tools.SettingBox; -public class SettingBoxTool : Tool, IUnselectableTool +public class SettingBoxTool(IOptionsBox optionsBox) : Tool, IUnselectableTool { - private readonly IOptionsBox _optionsBox; - - - public SettingBoxTool(ToolGroup? toolGroup, IOptionsBox optionsBox) - { - ToolGroup = toolGroup; - _optionsBox = optionsBox; - } - public override void Enter() { - _optionsBox.Show(); + optionsBox.Show(); } public override void Exit() diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolConfigurator.cs index ee503844..53de5a2e 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolConfigurator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolConfigurator.cs @@ -8,7 +8,7 @@ public class SettingBoxToolConfigurator : IConfigurator { public void Configure(IContainerDefinition containerDefinition) { - containerDefinition.MultiBind().To().AsSingleton(); + containerDefinition.Bind().AsSingleton(); containerDefinition.MultiBind().To().AsSingleton(); } } \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolFactory.cs deleted file mode 100644 index c571f737..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolFactory.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Timberborn.Options; -using Timberborn.ToolSystem; - -namespace TimberApi.Tools.ToolSystem.Tools.SettingBox; - -public class SettingBoxToolFactory(IOptionsBox optionsBox) : IToolFactory -{ - public string Id => "SettingBoxTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - return new SettingBoxTool(toolGroup, optionsBox); - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolGenerator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolGenerator.cs index 3ac9d573..115ec154 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolGenerator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/SettingBox/SettingBoxToolGenerator.cs @@ -16,7 +16,7 @@ private static GeneratedSpecification SettingBoxTool() var json = JsonConvert.SerializeObject(new { Id = "SettingBox", - Type = "SettingBoxTool", + Type = "GenericTool", Layout = "GrouplessRed", Order = 1000, NameLocKey = "CAN NOT BE MODIFIED", @@ -26,7 +26,8 @@ private static GeneratedSpecification SettingBoxTool() Hidden = false, ToolInformation = new { - BottomBarSection = 2 + BottomBarSection = 2, + ClassName = typeof(SettingBoxTool).FullName } }); diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaSelectionToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaSelectionToolFactory.cs deleted file mode 100644 index f18e5fee..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaSelectionToolFactory.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Timberborn.ForestryUI; -using Timberborn.ToolSystem; - -namespace TimberApi.Tools.ToolSystem.Tools.TreeCuttingArea; - -public class TreeCuttingAreaSelectionToolFactory(TreeCuttingAreaSelectionTool treeCuttingAreaSelectionTool) - : IToolFactory -{ - public string Id => "TreeCuttingAreaSelectionTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - treeCuttingAreaSelectionTool.ToolGroup = toolGroup; - - return treeCuttingAreaSelectionTool; - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolConfigurator.cs index d6c3b62d..188a2f83 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolConfigurator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolConfigurator.cs @@ -9,7 +9,5 @@ public class TreeCuttingAreaToolConfigurator : IConfigurator public void Configure(IContainerDefinition containerDefinition) { containerDefinition.MultiBind().To().AsSingleton(); - containerDefinition.MultiBind().To().AsSingleton(); - containerDefinition.MultiBind().To().AsSingleton(); } } \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolGenerator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolGenerator.cs index 165d6f58..2973e807 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolGenerator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaToolGenerator.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Newtonsoft.Json; using TimberApi.SpecificationSystem; +using Timberborn.ForestryUI; namespace TimberApi.Tools.ToolSystem.Tools.TreeCuttingArea; @@ -19,7 +20,7 @@ private static GeneratedSpecification TreeCuttingAreaUnselectionTool() { Id = "TreeCuttingAreaUnselection", GroupId = "TreeCutting", - Type = "TreeCuttingAreaUnselectionTool", + Type = "GenericTool", Layout = "Default", Order = 1000, NameLocKey = "CAN NOT BE MODIFIED", @@ -29,7 +30,8 @@ private static GeneratedSpecification TreeCuttingAreaUnselectionTool() Hidden = false, ToolInformation = new { - BottomBarSection = 0 + BottomBarSection = 0, + ClassName = typeof(TreeCuttingAreaUnselectionTool).FullName } }); @@ -42,7 +44,7 @@ private static GeneratedSpecification TreeCuttingAreaSelectionTool() { Id = "TreeCuttingAreaSelection", GroupId = "TreeCutting", - Type = "TreeCuttingAreaSelectionTool", + Type = "GenericTool", Layout = "Default", Order = 0, NameLocKey = "CAN NOT BE MODIFIED", @@ -52,7 +54,8 @@ private static GeneratedSpecification TreeCuttingAreaSelectionTool() Hidden = false, ToolInformation = new { - BottomBarSection = 0 + BottomBarSection = 0, + ClassName = typeof(TreeCuttingAreaSelectionTool).FullName } }); diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaUnselectionToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaUnselectionToolFactory.cs deleted file mode 100644 index 2273c553..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/TreeCuttingArea/TreeCuttingAreaUnselectionToolFactory.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Timberborn.ForestryUI; -using Timberborn.ToolSystem; - -namespace TimberApi.Tools.ToolSystem.Tools.TreeCuttingArea; - -public class TreeCuttingAreaUnselectionToolFactory(TreeCuttingAreaUnselectionTool treeCuttingAreaUnselectionTool) - : IToolFactory -{ - public string Id => "TreeCuttingAreaUnselectionTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - treeCuttingAreaUnselectionTool.ToolGroup = toolGroup; - - return treeCuttingAreaUnselectionTool; - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolConfigurator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolConfigurator.cs index 7ee82fe4..3addfd3b 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolConfigurator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolConfigurator.cs @@ -8,7 +8,6 @@ public class WaterGeneratorToolConfigurator : IConfigurator { public void Configure(IContainerDefinition containerDefinition) { - containerDefinition.MultiBind().To().AsSingleton(); containerDefinition.MultiBind().To().AsSingleton(); } } \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolFactory.cs b/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolFactory.cs deleted file mode 100644 index 7e0d8aed..00000000 --- a/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolFactory.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Timberborn.ToolSystem; -using Timberborn.WaterBrushesUI; - -namespace TimberApi.Tools.ToolSystem.Tools.WaterGenerator; - -public class WaterGeneratorToolFactory(WaterHeightBrushTool waterHeightBrushTool) : IToolFactory -{ - public string Id => "WaterGeneratorTool"; - - public Tool Create(ToolSpecification toolSpecification, ToolGroup? toolGroup = null) - { - waterHeightBrushTool.ToolGroup = toolGroup; - return waterHeightBrushTool; - } -} \ No newline at end of file diff --git a/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolGenerator.cs b/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolGenerator.cs index cc80927b..e3aee356 100644 --- a/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolGenerator.cs +++ b/Src/TimberApi.Tools/ToolSystem/Tools/WaterGenerator/WaterGeneratorToolGenerator.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Newtonsoft.Json; using TimberApi.SpecificationSystem; +using Timberborn.WaterBrushesUI; namespace TimberApi.Tools.ToolSystem.Tools.WaterGenerator; @@ -11,7 +12,7 @@ public IEnumerable Generate() var json = JsonConvert.SerializeObject(new { Id = "WaterGenerator", - Type = "WaterGeneratorTool", + Type = "GenericTool", Layout = "GrouplessRed", Order = 80, NameLocKey = "Cursor", @@ -21,7 +22,8 @@ public IEnumerable Generate() Hidden = false, ToolInformation = new { - BottomBarSection = 0 + BottomBarSection = 0, + ClassName = typeof(WaterHeightBrushTool).FullName } }); From 577722e3c2670479e775c52b66693ec0782d2574 Mon Sep 17 00:00:00 2001 From: KYPremco Date: Fri, 15 Nov 2024 20:12:12 +0100 Subject: [PATCH 02/18] Added scene property to Tool specifications --- .../ToolSystem/ToolSpecification.cs | 10 ++++++-- .../ToolSpecificationDeserliaser.cs | 2 ++ .../ToolSystem/ToolSpecificationService.cs | 1 + .../Extensions/IObjectLoaderExtensions.cs | 25 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Src/TimberApi.Tools/ToolSystem/ToolSpecification.cs b/Src/TimberApi.Tools/ToolSystem/ToolSpecification.cs index f41e63be..01fad261 100644 --- a/Src/TimberApi.Tools/ToolSystem/ToolSpecification.cs +++ b/Src/TimberApi.Tools/ToolSystem/ToolSpecification.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using Timberborn.SerializationSystem; namespace TimberApi.Tools.ToolSystem; @@ -5,6 +7,7 @@ namespace TimberApi.Tools.ToolSystem; public class ToolSpecification( string id, string? groupId, + IEnumerable scenes, string section, string type, string layout, @@ -15,16 +18,17 @@ public class ToolSpecification( bool hidden, bool devMode, ObjectSave? toolInformation) - : ToolSpecification(id, groupId, section, type, layout, order, icon, nameLocKey, descriptionLocKey, + : ToolSpecification(id, groupId, scenes, section, type, layout, order, icon, nameLocKey, descriptionLocKey, hidden, devMode, toolInformation); public class ToolSpecification( string id, string? groupId, + IEnumerable scenes, string section, string type, - string layout, + string layout, int order, string icon, string nameLocKey, @@ -36,6 +40,8 @@ public class ToolSpecification( public string Id { get; } = id; public string? GroupId { get; } = groupId; + + public HashSet Scenes { get; } = scenes.ToHashSet(); public string Section { get; } = section; diff --git a/Src/TimberApi.Tools/ToolSystem/ToolSpecificationDeserliaser.cs b/Src/TimberApi.Tools/ToolSystem/ToolSpecificationDeserliaser.cs index 95a91115..1c70c611 100644 --- a/Src/TimberApi.Tools/ToolSystem/ToolSpecificationDeserliaser.cs +++ b/Src/TimberApi.Tools/ToolSystem/ToolSpecificationDeserliaser.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using TimberApi.Extensions; using Timberborn.Persistence; using Timberborn.SerializationSystem; @@ -17,6 +18,7 @@ public Obsoletable Deserialize(IObjectLoader objectLoader) return new ToolSpecification( objectLoader.Get(new PropertyKey("Id")), objectLoader.GetValueOrNull(new PropertyKey("GroupId")), + objectLoader.GetValueOrDefault(new ListKey("Scenes"), ["Game"]), objectLoader.GetValueOrDefault(new PropertyKey("Section"), "BottomBar"), objectLoader.Get(new PropertyKey("Type")), objectLoader.GetValueOrDefault(new PropertyKey("Layout"), "Default"), diff --git a/Src/TimberApi.Tools/ToolSystem/ToolSpecificationService.cs b/Src/TimberApi.Tools/ToolSystem/ToolSpecificationService.cs index ab57e107..16bf1d26 100644 --- a/Src/TimberApi.Tools/ToolSystem/ToolSpecificationService.cs +++ b/Src/TimberApi.Tools/ToolSystem/ToolSpecificationService.cs @@ -20,6 +20,7 @@ public class ToolSpecificationService( public void Load() { _toolSpecifications = specificationService.GetSpecifications(toolSpecificationDeserializer) + .Where(specification => specification.Scenes.Contains(SceneManager.CurrentScene.ToString())) .ToImmutableDictionary(specification => specification.Id.ToLower()); } diff --git a/Src/TimberApi/Extensions/IObjectLoaderExtensions.cs b/Src/TimberApi/Extensions/IObjectLoaderExtensions.cs index 3b5501a5..30d1166d 100644 --- a/Src/TimberApi/Extensions/IObjectLoaderExtensions.cs +++ b/Src/TimberApi/Extensions/IObjectLoaderExtensions.cs @@ -80,6 +80,31 @@ public static T GetValueOrDefault(this IObjectLoader objectLoader, PropertyKe { return !objectLoader.Has(key) ? defaultValue : objectLoader.Get(key, serializer); } + + public static List GetValueOrDefault(this IObjectLoader objectLoader, ListKey key, List defaultValue) + { + return !objectLoader.Has(key) ? defaultValue : objectLoader.Get(key); + } + + public static List GetValueOrDefault(this IObjectLoader objectLoader, ListKey key, List defaultValue) + { + return !objectLoader.Has(key) ? defaultValue : objectLoader.Get(key); + } + + public static List GetValueOrDefault(this IObjectLoader objectLoader, ListKey key, List defaultValue) + { + return !objectLoader.Has(key) ? defaultValue : objectLoader.Get(key); + } + + public static List GetValueOrDefault(this IObjectLoader objectLoader, ListKey key, List defaultValue) + { + return !objectLoader.Has(key) ? defaultValue : objectLoader.Get(key); + } + + public static List GetValueOrDefault(this IObjectLoader objectLoader, ListKey key, List defaultValue) + { + return !objectLoader.Has(key) ? defaultValue : objectLoader.Get(key); + } public static List GetValueOrEmpty(this IObjectLoader objectLoader, ListKey key, IObjectSerializer serializer) where T : class From 75d45a1b58e4909b0b390d7782f4b50e1ea2942f Mon Sep 17 00:00:00 2001 From: KYPremco Date: Fri, 15 Nov 2024 20:16:27 +0100 Subject: [PATCH 03/18] Updated changelog & manifest --- Changelog.md | 4 ++++ package/manifest.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 69d9da67..1aa338e4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,7 @@ +## 0.7.8.0 +- Added `Scenes` to `ToolSpecification` to specify in which scene the tool should be loaded `Game` is default. +- Added `GenericTool` tool factory type to make compatibility without hard dependency possible for custom mod tools. + ## 0.7.7.2 - Removed debug logging. diff --git a/package/manifest.json b/package/manifest.json index 02718d04..01f98c8d 100644 --- a/package/manifest.json +++ b/package/manifest.json @@ -1,6 +1,6 @@ { "Name": "TimberApi", - "Version": "0.7.7.2", + "Version": "0.7.8.0", "Id": "TimberApi", "MinimumGameVersion": "0.6.9.2", "Description": "TimberAPI supports modders to create new content in Timberborn.", From a60ccd822d69c53e980b13c197698ee658810fbf Mon Sep 17 00:00:00 2001 From: KYPremco Date: Sat, 8 Feb 2025 10:02:36 +0100 Subject: [PATCH 04/18] wip update 7 --- .editorconfig | 2 + .run/Build & Start.run.xml | 2 +- Debug/AutoModStarter/AutoStarterPlugin.cs | 25 ---- .../SpecificationServiceLogger.cs | 1 - Debug/TimberApi.TesterMod/Configurators.cs | 5 +- .../TimberApi.TesterMod.csproj | 4 + .../BottomBarButton.cs | 10 +- .../BottomBarPanel.cs | 6 +- .../BottomBarService.cs | 41 +++-- .../BottomBarSpec.cs | 9 ++ .../Patches/BottomBarConfiguratorPatcher.cs | 9 +- .../BuildingChangeApplier.cs | 2 +- .../BuildingSpecificationGenerator.cs | 8 +- ...BuildingSpecificationObjectDeserializer.cs | 5 + .../BuildingSpecificationService.cs | 3 +- Src/TimberApi.Console/ModStarter.cs | 6 - .../EntityLinkerSystemConfigurator.cs | 2 +- .../PickObjectTool.cs | 10 +- .../ISpecificationModifierResolver.cs | 8 + .../ModStarter.cs | 86 +++++++++++ .../PropertyModifier.cs | 8 + .../SpecificationModifierApplier.cs | 140 ++++++++++++++++++ .../SpecificationModifierConfigurator.cs | 16 ++ .../ModSpecificationModifierResolver.cs | 15 ++ .../Test.cs | 11 ++ ...imberApi.SpecificationPatcherSystem.csproj | 16 +- .../BootstrapperConfigurator.cs | 6 +- .../EarlyLoadPatcher.cs | 2 +- .../GameEarlyLoadPrefabCollection.cs | 2 +- .../GeneratedSpecificationAssetProvider.cs | 16 +- .../GeneratedSpecificationAssetRepository.cs | 6 +- .../ModStarter.cs | 6 - Src/TimberApi.Tools/TimberApi.Tools.csproj | 5 +- .../ToolGroupSystem/IToolGroupFactory.cs | 2 +- .../ToolGroupSystem/ToolGroupConfigurator.cs | 1 - .../ToolGroupSystem/ToolGroupSpec.cs | 24 +++ .../ToolGroupSystem/ToolGroupSpecification.cs | 51 ------- .../ToolGroupSpecificationDeserializer.cs | 48 ------ .../ToolGroupSpecificationService.cs | 18 +-- .../BuilderPriorityToolGroupFactory.cs | 16 +- .../ConstructionModeToolGroupFactory.cs | 16 +- .../Default/DefaultToolGroupFactory.cs | 16 +- .../PlantingModeToolGroupFactory.cs | 16 +- .../TreeCuttingAreaToolGroupFactory.cs | 16 +- .../Factories/ToolGroupButtonBlueFactory.cs | 4 +- .../Factories/ToolGroupButtonBrownFactory.cs | 4 +- .../Factories/ToolGroupButtonGreenFactory.cs | 4 +- .../Factories/ToolGroupButtonRedFactory.cs | 4 +- .../ToolGroupUI/IToolGroupButtonFactory.cs | 4 +- .../ToolSystem/BaseToolFactory.cs | 63 -------- .../ToolSystem/IToolFactory.cs | 2 +- Src/TimberApi.Tools/ToolSystem/ToolService.cs | 4 +- Src/TimberApi.Tools/ToolSystem/ToolSpec.cs | 43 ++++++ ...ificationService.cs => ToolSpecService.cs} | 21 ++- .../ToolSystem/ToolSpecification.cs | 65 -------- .../ToolSpecificationDeserliaser.cs | 41 ----- .../ToolSystem/ToolSystemConfigurator.cs | 3 +- .../BuilderPriorityToolFactory.cs | 20 +-- .../BuilderPriorityToolSpec.cs | 10 ++ .../BuilderPriorityToolToolInformation.cs | 23 --- .../CancelPlantingToolFactory.cs | 2 +- .../BuildingDeconstructionToolFactory.cs | 2 +- .../Demolishing/DeleteRubbleToolFactory.cs | 2 +- .../DemolishableSelectionToolFactory.cs | 2 +- .../DemolishableUnselectionToolFactory.cs | 2 +- .../EntityBlockObjectDeletionToolFactory.cs | 2 +- .../Tools/GenericTool/GenericToolFactory.cs | 22 ++- .../Tools/GenericTool/GenericToolSpec.cs | 9 ++ .../GenericTool/GenericToolToolInformation.cs | 6 - .../PlaceableObjectToolFactory.cs | 28 ++-- .../PlaceableObjectToolGenerator.cs | 4 +- .../PlaceableObjectToolSpec.cs | 9 ++ .../PlaceableObjectToolToolInformation.cs | 6 - .../Tools/Planting/PlantingToolFactory.cs | 27 ++-- .../Tools/Planting/PlantingToolGenerator.cs | 8 +- .../Tools/Planting/PlantingToolSpec.cs | 9 ++ .../Planting/PlantingToolToolInformation.cs | 6 - .../ToolUI/Factories/ToolButtonBlueFactory.cs | 4 +- .../Factories/ToolButtonBrownFactory.cs | 4 +- .../Factories/ToolButtonDefaultFactory.cs | 4 +- .../Factories/ToolButtonGreenFactory.cs | 4 +- .../ToolButtonGrouplessBlueFactory.cs | 4 +- .../ToolButtonGrouplessBrownFactory.cs | 4 +- .../ToolButtonGrouplessGreenFactory.cs | 4 +- .../ToolButtonGrouplessRedFactory.cs | 4 +- .../ToolUI/Factories/ToolButtonRedFactory.cs | 4 +- .../Factories/ToolButtonWonderBlueFactory.cs | 4 +- .../Factories/ToolButtonWonderBrownFactory.cs | 4 +- .../Factories/ToolButtonWonderFactory.cs | 4 +- .../Factories/ToolButtonWonderGreenFactory.cs | 4 +- .../Factories/ToolButtonWonderRedFactory.cs | 4 +- .../ToolUI/IToolButtonFactory.cs | 2 +- Src/TimberApi/SceneManager.cs | 6 +- Src/TimberApi/TimberApi.csproj | 2 +- TimberAPI.sln | 14 +- TimberAPI.sln.DotSettings | 2 + 96 files changed, 646 insertions(+), 614 deletions(-) create mode 100644 .editorconfig delete mode 100644 Debug/AutoModStarter/AutoStarterPlugin.cs create mode 100644 Src/TimberApi.BottomBarSystem/BottomBarSpec.cs delete mode 100644 Src/TimberApi.Console/ModStarter.cs create mode 100644 Src/TimberApi.SpecificationPatcherSystem/ISpecificationModifierResolver.cs create mode 100644 Src/TimberApi.SpecificationPatcherSystem/ModStarter.cs create mode 100644 Src/TimberApi.SpecificationPatcherSystem/PropertyModifier.cs create mode 100644 Src/TimberApi.SpecificationPatcherSystem/SpecificationModifierApplier.cs create mode 100644 Src/TimberApi.SpecificationPatcherSystem/SpecificationModifierConfigurator.cs create mode 100644 Src/TimberApi.SpecificationPatcherSystem/SpecificationModifierResolvers/ModSpecificationModifierResolver.cs create mode 100644 Src/TimberApi.SpecificationPatcherSystem/Test.cs rename Debug/AutoModStarter/AutoModStarter.csproj => Src/TimberApi.SpecificationPatcherSystem/TimberApi.SpecificationPatcherSystem.csproj (64%) create mode 100644 Src/TimberApi.Tools/ToolGroupSystem/ToolGroupSpec.cs delete mode 100644 Src/TimberApi.Tools/ToolGroupSystem/ToolGroupSpecification.cs delete mode 100644 Src/TimberApi.Tools/ToolGroupSystem/ToolGroupSpecificationDeserializer.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/BaseToolFactory.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/ToolSpec.cs rename Src/TimberApi.Tools/ToolSystem/{ToolSpecificationService.cs => ToolSpecService.cs} (64%) delete mode 100644 Src/TimberApi.Tools/ToolSystem/ToolSpecification.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/ToolSpecificationDeserliaser.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/BuilderPriority/BuilderPriorityToolSpec.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/BuilderPriority/BuilderPriorityToolToolInformation.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolSpec.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/GenericTool/GenericToolToolInformation.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/PlaceableObject/PlaceableObjectToolSpec.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/PlaceableObject/PlaceableObjectToolToolInformation.cs create mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/Planting/PlantingToolSpec.cs delete mode 100644 Src/TimberApi.Tools/ToolSystem/Tools/Planting/PlantingToolToolInformation.cs create mode 100644 TimberAPI.sln.DotSettings diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..fd95a82e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,2 @@ +[*.cs] +dotnet_diagnostic.CS8618.severity = none \ No newline at end of file diff --git a/.run/Build & Start.run.xml b/.run/Build & Start.run.xml index 1aeb9841..93ed5629 100644 --- a/.run/Build & Start.run.xml +++ b/.run/Build & Start.run.xml @@ -1,6 +1,6 @@ -