Skip to content

Commit

Permalink
Properly dispose of damage hook when reloading, use proper DI
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Jul 20, 2024
1 parent 11e287d commit 0b3f916
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
14 changes: 3 additions & 11 deletions mod/TTT.Roles/RoleBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,14 @@ public class RoleBehavior : IRoleService, IPluginBehavior {
private readonly IRoundService _roundService;
private int _traitorsLeft;

public RoleBehavior(IPlayerService playerService) {
_roundService = new RoundBehavior(this);
public RoleBehavior(IPlayerService playerService, IRoundService roundService) {
_roundService = roundService;
service = playerService;
}

public void Start(BasePlugin parent) {
ModelHandler.RegisterListener(parent);
_roundService.Start(parent);
/*
parent.RegisterEventHandler<EventPlayerConnectFull>(OnPlayerConnect);
parent.RegisterEventHandler<EventRoundFreezeEnd>(OnRoundStart);
parent.RegisterEventHandler<EventRoundEnd>(OnRoundEnd);
parent.RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect);
parent.RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath, HookMode.Pre);
parent.RegisterEventHandler<EventGameStart>(OnMapStart);
*/
// _roundService.Start(parent);
}

public IRoundService GetRoundService() { return _roundService; }
Expand Down
9 changes: 8 additions & 1 deletion mod/TTT.Round/RoundBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Timers;
using TTT.Public.Behaviors;
using TTT.Public.Extensions;
using TTT.Public.Formatting;
using TTT.Public.Mod.Role;
using TTT.Public.Mod.Round;

namespace TTT.Round;

public class RoundBehavior(IRoleService _roleService) : IRoundService {
public class RoundBehavior(IRoleService _roleService)
: IRoundService, IPluginBehavior {
private Round? _round;
private int _roundId = 1;
private RoundStatus _roundStatus = RoundStatus.Paused;
Expand All @@ -25,6 +27,11 @@ public void Start(BasePlugin plugin) {
plugin.AddTimer(3, EndRound, TimerFlags.REPEAT);
}

public void Dispose() {
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(BlockDamage,
HookMode.Pre);
}

public RoundStatus GetRoundStatus() { return _roundStatus; }

public void SetRoundStatus(RoundStatus roundStatus) {
Expand Down
6 changes: 5 additions & 1 deletion mod/TTT.Round/RoundServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using TTT.Public.Extensions;
using TTT.Public.Mod.Round;

namespace TTT.Round;

public static class RoundServiceExtension {
public static void AddRoundService(this IServiceCollection collection) { }
public static void AddRoundService(this IServiceCollection collection) {
collection.AddPluginBehavior<IRoundService, RoundBehavior>();
}
}

0 comments on commit 0b3f916

Please sign in to comment.