Skip to content

Commit 881ee32

Browse files
committed
AddServices and AddConfiguration can now be called multiple times
1 parent d02cd22 commit 881ee32

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

BenMakesGames.PlayPlayMini/BenMakesGames.PlayPlayMini.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Company>Ben Hendel-Doying</Company>
66
<Description>An opinionated framework for making smallish games with MonoGame.</Description>
77
<Copyright>2021-2024 Ben Hendel-Doying</Copyright>
8-
<Version>5.4.1</Version>
8+
<Version>5.5.0</Version>
99

1010
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1111
<PackageTags>monogame game engine framework di state</PackageTags>

BenMakesGames.PlayPlayMini/GameStateManagerBuilder.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class GameStateManagerBuilder
2727

2828
private AssetCollection GameAssets { get; } = new();
2929

30-
private Action<ContainerBuilder, IConfiguration, ServiceWatcher>? AddServicesCallback { get; set; }
31-
private Action<IConfigurationBuilder>? ConfigurationCallback { get; set; }
30+
private List<Action<ContainerBuilder, IConfiguration, ServiceWatcher>> AddServicesCallbacks { get; } = [ ];
31+
private List<Action<IConfigurationBuilder>> ConfigurationCallbacks { get; } = [ ];
3232
private string WindowTitle { get; set; } = "PlayPlayMini Game";
3333
private (int Width, int Height, int Zoom) WindowSize { get; set; } = (1920 / 3, 1080 / 3, 2);
3434
private bool FixedTimeStep { get; set; }
@@ -108,30 +108,21 @@ public GameStateManagerBuilder AddAssets(IList<IAsset> assets)
108108

109109
public GameStateManagerBuilder AddServices(Action<ContainerBuilder, IConfiguration, ServiceWatcher> callback)
110110
{
111-
if (AddServicesCallback is not null)
112-
throw new ArgumentException("AddServices may only be called once!");
113-
114-
AddServicesCallback = callback;
111+
AddServicesCallbacks.Add(callback);
115112

116113
return this;
117114
}
118115

119116
public GameStateManagerBuilder AddServices(Action<ContainerBuilder, IConfiguration> callback)
120117
{
121-
if (AddServicesCallback is not null)
122-
throw new ArgumentException("AddServices may only be called once!");
123-
124-
AddServicesCallback = (s, c, _) => callback(s, c);
118+
AddServicesCallbacks.Add((s, c, _) => callback(s, c));
125119

126120
return this;
127121
}
128122

129123
public GameStateManagerBuilder AddConfiguration(Action<IConfigurationBuilder> callback)
130124
{
131-
if (ConfigurationCallback is not null)
132-
throw new ArgumentException("AddConfiguration may only be called once!");
133-
134-
ConfigurationCallback = callback;
125+
ConfigurationCallbacks.Add(callback);
135126

136127
return this;
137128
}
@@ -154,7 +145,8 @@ public void Run()
154145
.AddJsonFile(Path.Combine("Content", "appsettings.json"), optional: true, reloadOnChange: false) // TODO: does this work on Android?
155146
;
156147

157-
ConfigurationCallback?.Invoke(configBuilder);
148+
foreach(var callback in ConfigurationCallbacks)
149+
callback(configBuilder);
158150

159151
var configuration = configBuilder.Build();
160152

@@ -215,7 +207,8 @@ public void Run()
215207
.As(typeof(ILogger<>))
216208
.SingleInstance();
217209

218-
AddServicesCallback?.Invoke(builder, configuration, serviceWatcher);
210+
foreach(var callback in AddServicesCallbacks)
211+
callback(builder, configuration, serviceWatcher);
219212

220213
if(InitialGameState is null)
221214
throw new ArgumentException("No initial game state set! You must call GameStateManagerBuilder's SetInitialGameState method before calling its Run method.");

0 commit comments

Comments
 (0)