Skip to content

Commit

Permalink
Fixed event dispatcher creating a new instance each time (kill autofa…
Browse files Browse the repository at this point in the history
…c with hammers).
  • Loading branch information
DanielWillett committed Dec 20, 2024
1 parent 8a3dfdf commit 7099397
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 89 deletions.
2 changes: 1 addition & 1 deletion UncreatedWarfare.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Project("{911E67C6-3D85-4FCE-B560-20A9C3E3FF48}") = "Unturned", "C:\SteamCMD\ste
Executable = C:\SteamCMD\steamapps\common\U3DS\Unturned.exe
RemoteMachine = DANIEL-DESKTOP
StartingDirectory = C:\SteamCMD\steamapps\common\U3DS
Arguments = -batchmode -nographics -SkipAssets +insecureserver/UncreatedSeason4
Arguments = -batchmode -nographics +insecureserver/UncreatedSeason4
Environment = Default
LaunchingEngine = 5fff7536-0c87-462d-8fd2-7971d948e6dc
UseLegacyDebugEngines = No
Expand Down
105 changes: 60 additions & 45 deletions UncreatedWarfare/Commands/WarfareDev/DebugEventTestCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DanielWillett.ReflectionTools;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics;
using Uncreated.Warfare.Events;
Expand All @@ -13,22 +14,36 @@ internal sealed class DebugEventTestCommand : IExecutableCommand
// uncomment service registrations for TestEventService1-7 in WarfareModule for full tests

private readonly EventDispatcher _eventDispatcher;
private readonly IServiceProvider _serviceProvider;

public required CommandContext Context { get; init; }

public DebugEventTestCommand(EventDispatcher eventDispatcher)
public DebugEventTestCommand(EventDispatcher eventDispatcher, IServiceProvider serviceProvider)
{
_eventDispatcher = eventDispatcher;
_serviceProvider = serviceProvider;
}

public async UniTask ExecuteAsync(CancellationToken token)
{
Context.AssertRanByPlayer();
TestEventService1 ev1 = _serviceProvider.GetRequiredService<TestEventService1>();
TestEventService2 ev2 = _serviceProvider.GetRequiredService<TestEventService2>();
TestEventService3 ev3 = _serviceProvider.GetRequiredService<TestEventService3>();
TestEventService4 ev4 = _serviceProvider.GetRequiredService<TestEventService4>();
TestEventService5 ev5 = _serviceProvider.GetRequiredService<TestEventService5>();
TestEventService6 ev6 = _serviceProvider.GetRequiredService<TestEventService6>();
TestEventService7 ev7 = _serviceProvider.GetRequiredService<TestEventService7>();

EventModelParent3 args = new EventModelParent3
{
Player = Context.Player
};
Context.Logger.LogInformation("hashes: 1: {0}, 2: {1}, 3: {2}, 4: {3}, 5: {4}, 6: {5}, 7: {6}",
ev1.GetHashCode(), ev2.GetHashCode(), ev3.GetHashCode(), ev4.GetHashCode(), ev5.GetHashCode(),
ev6.GetHashCode(), ev7.GetHashCode());

// Context.AssertRanByPlayer();

EventModelParent3 args = new EventModelParent3();
//{
// Player = Context.Player
//};

Stopwatch sw = Stopwatch.StartNew();

Expand All @@ -40,7 +55,7 @@ public async UniTask ExecuteAsync(CancellationToken token)
}
}

public class TestEventService1 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>, IEventListener<object>
public class TestEventService1 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>////, IEventListener<object>
{
private readonly ILogger<TestEventService1> _logger;

Expand All @@ -59,13 +74,13 @@ void IEventListener<EventModelParent2>.HandleEvent(EventModelParent2 e, IService
_logger.LogDebug("Event2 triggered: {0}", e.GetType());
}

void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
{
_logger.LogDebug("Object triggered: {0}", e.GetType());
}
//void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
//{
// _logger.LogDebug("Object triggered: {0}", e.GetType());
//}
}

public class TestEventService2 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>, IEventListener<object>
public class TestEventService2 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>////, IEventListener<object>
{
private readonly ILogger<TestEventService2> _logger;

Expand All @@ -85,14 +100,14 @@ void IEventListener<EventModelParent2>.HandleEvent(EventModelParent2 e, IService
_logger.LogDebug("Event2 triggered: {0}", e.GetType());
}

void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
{
_logger.LogDebug("Object triggered: {0}", e.GetType());
}
//void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
//{
// _logger.LogDebug("Object triggered: {0}", e.GetType());
//}
}


public class TestEventService3 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>, IEventListener<object>
public class TestEventService3 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>//, IEventListener<object>
{
private readonly ILogger<TestEventService3> _logger;

Expand All @@ -112,14 +127,14 @@ void IEventListener<EventModelParent2>.HandleEvent(EventModelParent2 e, IService
_logger.LogDebug("Event2 triggered: {0}", e.GetType());
}

void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
{
_logger.LogDebug("Object triggered: {0}", e.GetType());
}
//void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
//{
// _logger.LogDebug("Object triggered: {0}", e.GetType());
//}
}


public class TestEventService4 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>, IEventListener<object>
public class TestEventService4 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>//, IEventListener<object>
{
private readonly ILogger<TestEventService4> _logger;

Expand All @@ -138,15 +153,15 @@ void IEventListener<EventModelParent2>.HandleEvent(EventModelParent2 e, IService
_logger.LogDebug("Event2 triggered: {0}", e.GetType());
}

[EventListener(Priority = 1)]
void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
{
_logger.LogDebug("Object triggered: {0}", e.GetType());
}
//[EventListener(Priority = 1)]
//void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
//{
// _logger.LogDebug("Object triggered: {0}", e.GetType());
//}
}


public class TestEventService5 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>, IEventListener<object>
public class TestEventService5 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>//, IEventListener<object>
{
private readonly ILogger<TestEventService5> _logger;

Expand All @@ -165,15 +180,15 @@ void IEventListener<EventModelParent2>.HandleEvent(EventModelParent2 e, IService
_logger.LogDebug("Event2 triggered: {0}", e.GetType());
}

[EventListener(Priority = -1)]
void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
{
_logger.LogDebug("Object triggered: {0}", e.GetType());
}
//[EventListener(Priority = -1)]
//void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
//{
// _logger.LogDebug("Object triggered: {0}", e.GetType());
//}
}


public class TestEventService6 : IEventListener<EventModelParent1>, IAsyncEventListener<EventModelParent2>, IEventListener<EventModelParent2>, IEventListener<object>
public class TestEventService6 : IEventListener<EventModelParent1>, IAsyncEventListener<EventModelParent2>, IEventListener<EventModelParent2>//, IEventListener<object>
{
private readonly ILogger<TestEventService6> _logger;

Expand All @@ -199,15 +214,15 @@ UniTask IAsyncEventListener<EventModelParent2>.HandleEventAsync(EventModelParent
return UniTask.CompletedTask;
}

[EventListener(MustRunInstantly = true)]
void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
{
_logger.LogDebug("Object triggered: {0}", e.GetType());
}
//[EventListener(MustRunInstantly = true)]
//void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
//{
// _logger.LogDebug("Object triggered: {0}", e.GetType());
//}
}


public class TestEventService7 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>, IEventListener<object>
public class TestEventService7 : IEventListener<EventModelParent1>, IEventListener<EventModelParent2>//, IEventListener<object>
{
private readonly ILogger<TestEventService7> _logger;

Expand All @@ -227,12 +242,12 @@ void IEventListener<EventModelParent2>.HandleEvent(EventModelParent2 e, IService
_logger.LogDebug("Event2 triggered: {0}", e.GetType());
}

void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
{
_logger.LogDebug("Object triggered: {0}", e.GetType());
}
//void IEventListener<object>.HandleEvent(object e, IServiceProvider serviceProvider)
//{
// _logger.LogDebug("Object triggered: {0}", e.GetType());
//}
}

public abstract class EventModelParent1 : PlayerEvent;
public abstract class EventModelParent1;
public abstract class EventModelParent2 : EventModelParent1;
public class EventModelParent3 : EventModelParent2;
Loading

0 comments on commit 7099397

Please sign in to comment.