Skip to content

Commit

Permalink
fix(di): registered GraphicsDeviceManager on the same level with `G…
Browse files Browse the repository at this point in the history
…ame`

- Fixed double-disposing behaviour issue, when the game is quit (left a comment)
  • Loading branch information
cherrynik committed Feb 27, 2024
1 parent 03c6309 commit 9e45b4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
33 changes: 17 additions & 16 deletions src/Apps/GameDesktop/CompositionRoots/GameCompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using GameDesktop.Resources.Internal;
using LightInject;
using Microsoft.Xna.Framework;
using MonoGame.ImGuiNet;
using Myra;
using Serilog;

namespace GameDesktop.CompositionRoots;
Expand All @@ -16,21 +14,24 @@ internal class GameCompositionRoot : ICompositionRoot

public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton(factory =>
ServiceRegistration[] services = serviceRegistry.AvailableServices.ToArray();

var logger =
(ILogger)services.First(r => r.ServiceType == typeof(ILogger)).Value;
var container =
(IServiceContainer)services.First(r => r.ServiceType == typeof(IServiceContainer)).Value;

Game game = new(logger, container)
{
Game game = new(factory.GetInstance<ILogger>(),
factory.GetInstance<IServiceContainer>())
{
IsMouseVisible = IsMouseVisible,
IsFixedTimeStep = IsFixedTimeStep,
Content = { RootDirectory = AppVariable.ContentRootDirectory, },
};
IsMouseVisible = IsMouseVisible,
IsFixedTimeStep = IsFixedTimeStep,
Content = { RootDirectory = AppVariable.ContentRootDirectory, },
};
serviceRegistry.RegisterInstance(game);

// Hack. Resolving cycle dependency issue (fundamental architecture)
// Implicitly adds itself in the game services container.
new GraphicsDeviceManager(game);

return game;
});
// Hack. Resolving cycle dependency issue (fundamental architecture)
// Implicitly adds itself in the game services container.
GraphicsDeviceManager graphicsDeviceManager = new(game);
serviceRegistry.RegisterInstance(graphicsDeviceManager);
}
}
4 changes: 3 additions & 1 deletion src/Apps/GameDesktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

try
{
using ServiceContainer container = new(
// "Using" keyword should be used either with the container, or with the game instance.
// Otherwise, you'll get the double-disposing behaviour.
ServiceContainer container = new(
new ContainerOptions
{
EnablePropertyInjection = false,
Expand Down

0 comments on commit 9e45b4f

Please sign in to comment.