Skip to content

Commit

Permalink
add check for duplicate controller/replay registration
Browse files Browse the repository at this point in the history
  • Loading branch information
stewieoO committed Apr 16, 2024
1 parent a6ac3f1 commit a8a7cd8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Nodsoft.WowsReplaysUnpack/ReplayUnpackerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public ReplayUnpackerBuilder AddReplayController<TController, TReplay>()
where TController : class, IReplayController<TReplay>
where TReplay : UnpackedReplay, new()
{
ServiceDescriptor[] existingControllers = Services.Where(s =>
s.ServiceType.IsGenericType &&
s.ServiceType.GetGenericTypeDefinition() == typeof(IReplayController<>))
.ToArray();

foreach (ServiceDescriptor existingController in existingControllers)
{
if (existingController.ServiceType.GenericTypeArguments[0] == typeof(TReplay))
throw new Exception("There can only be one controller per replay type registered");
}

Services.AddScoped<IReplayUnpackerService<TReplay>, ReplayUnpackerService<TReplay>>();
Services.AddScoped<IReplayController<TReplay>, TController>();
return this;
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Responsible for handling parsed network packets and filling the UnpackedReplay w

Your custom replay controller has to implement `IReplayController` but it is strongly suggested
to use `ReplayControllerBase<T>` where T is your custom replay class.
Only one controller can be registered for any replay type.
~~~~
An example of this is the [ExtendedDataController](Nodsoft.WowsReplaysUnpack.ExtendedData/ExtendedDataController.cs).
To use your custom controller add the replay type to the `GetUnpacker()` method.
Expand Down

0 comments on commit a8a7cd8

Please sign in to comment.