From 6ff9f0471fc1042691d31c92b8a8c5134d60c5e7 Mon Sep 17 00:00:00 2001 From: Eideren Date: Tue, 21 Jan 2025 21:56:01 +0100 Subject: [PATCH] Core: Refactor following changes introduced in PR #2599 --- .../Thumbnails/ThumbnailGenerator.cs | 7 +-- .../DebugRenderProcessor.cs | 2 +- .../Stride.BepuPhysics/BepuConfiguration.cs | 39 +++++++++++- .../BepuSimulationExtensions.cs | 5 +- .../Components/ISimulationUpdate.cs | 4 +- .../Definitions/ServicesHelper.cs | 59 ------------------- .../Systems/CollidableGizmo.cs | 7 +-- .../Systems/CollidableProcessor.cs | 5 +- .../Systems/ConstraintProcessor.cs | 2 +- .../Systems/PhysicsGameSystem.cs | 6 +- .../Systems/ShapeCacheSystem.cs | 4 +- .../Regression/UITestGameBase.cs | 8 +-- .../Stride.UI/Renderers/ElementRenderer.cs | 9 +-- .../Stride.UI/Rendering/UI/UIRenderFeature.cs | 10 +--- sources/engine/Stride.UI/UISystem.cs | 8 ++- sources/engine/Stride.Video/VideoProcessor.cs | 9 +-- sources/engine/Stride.Video/VideoSystem.cs | 10 +++- 17 files changed, 74 insertions(+), 120 deletions(-) delete mode 100644 sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/ServicesHelper.cs diff --git a/sources/editor/Stride.Editor/Thumbnails/ThumbnailGenerator.cs b/sources/editor/Stride.Editor/Thumbnails/ThumbnailGenerator.cs index 800aaba08e..9abaa48958 100644 --- a/sources/editor/Stride.Editor/Thumbnails/ThumbnailGenerator.cs +++ b/sources/editor/Stride.Editor/Thumbnails/ThumbnailGenerator.cs @@ -115,14 +115,13 @@ public ThumbnailGenerator(EffectCompilerBase effectCompiler) GraphicsDeviceService = new GraphicsDeviceServiceLocal(Services, GraphicsDevice); Services.AddService(GraphicsDeviceService); - var uiSystem = new UISystem(Services); - Services.AddService(uiSystem); - var physicsSystem = new Bullet2PhysicsSystem(Services); Services.AddService(physicsSystem); - gameSystems = new GameSystemCollection(Services) { fontSystem, uiSystem, physicsSystem }; + gameSystems = new GameSystemCollection(Services) { fontSystem, physicsSystem }; Services.AddService(gameSystems); + + Services.GetOrCreate(); Simulation.DisableSimulation = true; //make sure we do not simulate physics within the editor // initialize base services diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics.Debug/DebugRenderProcessor.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics.Debug/DebugRenderProcessor.cs index 0946a9699e..427c0489a7 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics.Debug/DebugRenderProcessor.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics.Debug/DebugRenderProcessor.cs @@ -78,7 +78,7 @@ protected override void OnSystemAdd() { SinglePassWireframeRenderFeature wireframeRenderFeature; - ServicesHelper.LoadBepuServices(Services, out _, out _shapeCacheSystem, out _); + _shapeCacheSystem = Services.GetOrCreate(); _game = Services.GetSafeServiceAs(); _sceneSystem = Services.GetSafeServiceAs(); diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuConfiguration.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuConfiguration.cs index f4603bc150..4b24ce99e8 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuConfiguration.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuConfiguration.cs @@ -1,13 +1,50 @@ // Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) // Distributed under the MIT license. See the LICENSE.md file in the project root for more information. +using Stride.BepuPhysics.Systems; using Stride.Core; +using Stride.Core.Diagnostics; using Stride.Data; +using Stride.Engine.Design; +using Stride.Games; namespace Stride.BepuPhysics; [DataContract] [Display("Bepu Configuration")] -public class BepuConfiguration : Configuration +public class BepuConfiguration : Configuration, IService { public List BepuSimulations = new(); + + private static readonly Logger _logger = GlobalLogger.GetLogger("BepuService"); + + public static IService NewInstance(IServiceRegistry services) + { + BepuConfiguration config; + if (services.GetService() is { } settings) + { + config = settings.Settings.Configurations.Get(); + if (settings.Settings.Configurations.Configurations.Any(x => x.Configuration is BepuConfiguration) == false) + _logger.Warning("Creating a default configuration for Bepu as none were set up in your game's settings."); + } + else + config = new BepuConfiguration { BepuSimulations = [new BepuSimulation()] }; + + if (config.BepuSimulations.Count == 0) + { + _logger.Warning("No simulations configured for Bepu, please add one in your game's configuration."); + config.BepuSimulations.Add(new BepuSimulation()); + } + + var systems = services.GetSafeServiceAs(); + PhysicsGameSystem? physicsGameSystem = null; + foreach (var system in systems) + { + if (system is PhysicsGameSystem pgs) + physicsGameSystem = pgs; + } + if (physicsGameSystem == null) + systems.Add(new PhysicsGameSystem(config, services)); + + return config; + } } diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuSimulationExtensions.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuSimulationExtensions.cs index 8fe4a5ceaa..39f90ec5f3 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuSimulationExtensions.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/BepuSimulationExtensions.cs @@ -16,10 +16,7 @@ public static class BepuSimulationExtensions public static BepuSimulation GetSimulation(this Entity entity) { var services = entity.EntityManager.Services; - var config = services.GetService(); - if (config == null) - ServicesHelper.LoadBepuServices(services, out config, out _, out _); - + var config = services.GetOrCreate(); return SceneBasedSimulationSelector.Shared.Pick(config, entity); } } diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Components/ISimulationUpdate.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Components/ISimulationUpdate.cs index 1fbd1c6014..efdbaa4423 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Components/ISimulationUpdate.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Components/ISimulationUpdate.cs @@ -68,12 +68,12 @@ public void SystemAdded(IServiceRegistry registryParam) { _services = registryParam; registryParam.AddService(this); - ServicesHelper.LoadBepuServices(registryParam, out _config, out _, out _); + _config = registryParam.GetOrCreate(); } public void SystemRemoved() { - _services!.RemoveService(); + _services!.RemoveService(this); } public void RebindSimulation(ISimulationUpdate item) diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/ServicesHelper.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/ServicesHelper.cs deleted file mode 100644 index b09eee2b0b..0000000000 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Definitions/ServicesHelper.cs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) -// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. - -using Stride.BepuPhysics.Systems; -using Stride.Core; -using Stride.Core.Diagnostics; -using Stride.Engine.Design; -using Stride.Games; - -namespace Stride.BepuPhysics.Definitions; - -internal static class ServicesHelper -{ - private static readonly Logger _logger = GlobalLogger.GetLogger("BepuService"); - - public static void LoadBepuServices(IServiceRegistry services, out BepuConfiguration configOut, out ShapeCacheSystem shapeCacheOut, out PhysicsGameSystem systemOut) - { - var config = services.GetService(); - if (config == null) - { - if (services.GetService() is { } settings) - { - config = settings.Settings.Configurations.Get(); - if (settings.Settings.Configurations.Configurations.Any(x => x.Configuration is BepuConfiguration) == false) - _logger.Warning("Creating a default configuration for Bepu as none were set up in your game's settings."); - } - else - config = new BepuConfiguration{ BepuSimulations = [new BepuSimulation()] }; - - services.AddService(config); - - if (config.BepuSimulations.Count == 0) - { - _logger.Warning("No simulations configured for Bepu, please add one in your game's configuration."); - config.BepuSimulations.Add(new BepuSimulation()); - } - } - - configOut = config; - - var shapeCache = services.GetService(); - if (shapeCache == null) - services.AddService(shapeCache = new(services)); - - shapeCacheOut = shapeCache; - - var systems = services.GetSafeServiceAs(); - PhysicsGameSystem? physicsGameSystem = null; - foreach (var system in systems) - { - if (system is PhysicsGameSystem pgs) - physicsGameSystem = pgs; - } - if (physicsGameSystem == null) - systems.Add(physicsGameSystem = new PhysicsGameSystem(services)); - - systemOut = physicsGameSystem; - } -} diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableGizmo.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableGizmo.cs index 2e0444d0be..5ad69df87d 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableGizmo.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableGizmo.cs @@ -109,12 +109,7 @@ public void Initialize(IServiceRegistry services, Scene editorScene) private void PrepareModels() { - var bepuShapeCacheSys = _services.GetService(); - if (bepuShapeCacheSys == null) - { - bepuShapeCacheSys = new ShapeCacheSystem(_services); - _services.AddService(bepuShapeCacheSys); - } + var bepuShapeCacheSys = _services.GetOrCreate(); var graphicsDevice = _services.GetSafeServiceAs().GraphicsDevice; if (_component.Collider is MeshCollider meshCollider && (meshCollider.Model.Meshes.Count == 0 || meshCollider.Model == null!/*May be null in editor*/)) diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableProcessor.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableProcessor.cs index 6e89b4b51d..26b1061a20 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableProcessor.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/CollidableProcessor.cs @@ -34,9 +34,8 @@ public CollidableProcessor() protected override void OnSystemAdd() { - ServicesHelper.LoadBepuServices(Services, out var config, out var shapes, out _); - BepuConfiguration = config; - ShapeCache = shapes; + BepuConfiguration = Services.GetOrCreate(); + ShapeCache = Services.GetOrCreate(); } public override unsafe void Draw(RenderContext context) // While this is not related to drawing, we're doing this in draw as it runs after the TransformProcessor updates WorldMatrix diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ConstraintProcessor.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ConstraintProcessor.cs index bd358fa158..e7dc3743b8 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ConstraintProcessor.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ConstraintProcessor.cs @@ -18,7 +18,7 @@ public ConstraintProcessor() protected override void OnSystemAdd() { - ServicesHelper.LoadBepuServices(Services, out _bepuConfiguration, out _, out _); + _bepuConfiguration = Services.GetOrCreate(); } protected override void OnEntityComponentAdding(Entity entity, ConstraintComponentBase component, ConstraintComponentBase data) diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/PhysicsGameSystem.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/PhysicsGameSystem.cs index 9efe0828d4..74ea659928 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/PhysicsGameSystem.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/PhysicsGameSystem.cs @@ -11,9 +11,9 @@ internal class PhysicsGameSystem : GameSystemBase { private BepuConfiguration _bepuConfiguration; - public PhysicsGameSystem(IServiceRegistry registry) : base(registry) + public PhysicsGameSystem(BepuConfiguration configuration, IServiceRegistry registry) : base(registry) { - _bepuConfiguration = registry.GetService(); + _bepuConfiguration = configuration; UpdateOrder = SystemsOrderHelper.ORDER_OF_GAME_SYSTEM; Enabled = true; //enabled by default @@ -35,4 +35,4 @@ public override void Update(GameTime time) bepuSim.Update(elapsed); } } -} \ No newline at end of file +} diff --git a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ShapeCacheSystem.cs b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ShapeCacheSystem.cs index c3aa11c8e1..25101a7ac2 100644 --- a/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ShapeCacheSystem.cs +++ b/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ShapeCacheSystem.cs @@ -19,7 +19,7 @@ namespace Stride.BepuPhysics.Systems; -internal class ShapeCacheSystem : IDisposable +internal class ShapeCacheSystem : IDisposable, IService { internal readonly BasicMeshBuffers _boxShapeData; internal readonly BasicMeshBuffers _cylinderShapeData; @@ -424,4 +424,6 @@ public void GetBuffers(out VertexPosition3[] vertices, out int[] indices) // /!\ THIS MAY RUN OUTSIDE OF THE MAIN THREAD /!\ } } + + public static IService NewInstance(IServiceRegistry services) => new ShapeCacheSystem(services); } diff --git a/sources/engine/Stride.UI.Tests/Regression/UITestGameBase.cs b/sources/engine/Stride.UI.Tests/Regression/UITestGameBase.cs index 84554097a4..039e3896e8 100644 --- a/sources/engine/Stride.UI.Tests/Regression/UITestGameBase.cs +++ b/sources/engine/Stride.UI.Tests/Regression/UITestGameBase.cs @@ -93,13 +93,7 @@ protected override async Task LoadContent() UIComponent.ResolutionStretch = ResolutionStretch.FixedWidthFixedHeight; Scene.Entities.Add(UIRoot); - UI = Services.GetService(); - if (UI == null) - { - UI = new UISystem(Services); - Services.AddService(UI); - GameSystems.Add(UI); - } + UI = Services.GetOrCreate(); Camera = new Entity("Scene camera") { new CameraComponent { Slot = SceneSystem.GraphicsCompositor.Cameras[0].ToSlotId() } }; Camera.Transform.Position = new Vector3(0, 0, 1000); diff --git a/sources/engine/Stride.UI/Renderers/ElementRenderer.cs b/sources/engine/Stride.UI/Renderers/ElementRenderer.cs index 726181a1aa..24b86db702 100644 --- a/sources/engine/Stride.UI/Renderers/ElementRenderer.cs +++ b/sources/engine/Stride.UI/Renderers/ElementRenderer.cs @@ -59,14 +59,7 @@ public ElementRenderer(IServiceRegistry services) Content = services.GetSafeServiceAs(); GraphicsDeviceService = services.GetSafeServiceAs(); - UI = services.GetService(); - if (UI == null) - { - UI = new UISystem(services); - services.AddService(UI); - var gameSystems = services.GetService(); - gameSystems?.Add(UI); - } + UI = services.GetOrCreate(); } /// diff --git a/sources/engine/Stride.UI/Rendering/UI/UIRenderFeature.cs b/sources/engine/Stride.UI/Rendering/UI/UIRenderFeature.cs index fbe5c6d5ae..04b5fd3465 100644 --- a/sources/engine/Stride.UI/Rendering/UI/UIRenderFeature.cs +++ b/sources/engine/Stride.UI/Rendering/UI/UIRenderFeature.cs @@ -55,17 +55,9 @@ protected override void InitializeCore() Name = "UIComponentRenderer"; game = RenderSystem.Services.GetService(); input = RenderSystem.Services.GetService(); - uiSystem = RenderSystem.Services.GetService(); + uiSystem = RenderSystem.Services.GetOrCreate(); graphicsDeviceService = RenderSystem.Services.GetSafeServiceAs(); - if (uiSystem == null) - { - var gameSytems = RenderSystem.Services.GetSafeServiceAs(); - uiSystem = new UISystem(RenderSystem.Services); - RenderSystem.Services.AddService(uiSystem); - gameSytems.Add(uiSystem); - } - rendererManager = new RendererManager(new DefaultRenderersFactory(RenderSystem.Services)); batch = uiSystem.Batch; diff --git a/sources/engine/Stride.UI/UISystem.cs b/sources/engine/Stride.UI/UISystem.cs index 6e09cbd4a7..595f1fbe2d 100644 --- a/sources/engine/Stride.UI/UISystem.cs +++ b/sources/engine/Stride.UI/UISystem.cs @@ -14,7 +14,7 @@ namespace Stride.UI /// /// Interface of the UI system. /// - public class UISystem : GameSystemBase + public class UISystem : GameSystemBase, IService { internal UIBatch Batch { get; private set; } @@ -29,6 +29,8 @@ public class UISystem : GameSystemBase public UISystem(IServiceRegistry registry) : base(registry) { + var gameSystems = registry.GetService(); + gameSystems?.Add(this); } public override void Initialize() @@ -166,5 +168,7 @@ private void UpdateKeyEvents() UIElement.FocusedElement?.RaiseKeyDownEvent(new KeyEventArgs { Key = key, Input = input }); } } + + public static IService NewInstance(IServiceRegistry services) => new UISystem(services); } -} \ No newline at end of file +} diff --git a/sources/engine/Stride.Video/VideoProcessor.cs b/sources/engine/Stride.Video/VideoProcessor.cs index e5029b758d..4192edec0f 100644 --- a/sources/engine/Stride.Video/VideoProcessor.cs +++ b/sources/engine/Stride.Video/VideoProcessor.cs @@ -121,14 +121,7 @@ protected override void OnEntityComponentRemoved(Entity entity, VideoComponent c /// protected override void OnSystemAdd() { - var videoSystem = Services.GetService(); - if (videoSystem == null) - { - videoSystem = new VideoSystem(Services); - Services.AddService(videoSystem); - var gameSystems = Services.GetSafeServiceAs(); - gameSystems.Add(videoSystem); - } + Services.GetOrCreate(); } } } diff --git a/sources/engine/Stride.Video/VideoSystem.cs b/sources/engine/Stride.Video/VideoSystem.cs index 5dfdce3c2d..b7f7f3c293 100644 --- a/sources/engine/Stride.Video/VideoSystem.cs +++ b/sources/engine/Stride.Video/VideoSystem.cs @@ -7,11 +7,19 @@ namespace Stride.Video { - public partial class VideoSystem : GameSystemBase + public partial class VideoSystem : GameSystemBase, IService { public VideoSystem([NotNull] IServiceRegistry registry) : base(registry) { } + + public static IService NewInstance(IServiceRegistry services) + { + var instance = new VideoSystem(services); + var gameSystems = services.GetSafeServiceAs(); + gameSystems.Add(instance); + return instance; + } } }