Add ban and kick events

This commit is contained in:
Brant Martin
2018-12-16 20:53:52 -05:00
parent 9a1a31c424
commit 76637b130c
4 changed files with 42 additions and 2 deletions

View File

@@ -32,5 +32,15 @@ namespace Torch.API.Managers
/// <param name="steamId">The SteamID of the player.</param> /// <param name="steamId">The SteamID of the player.</param>
/// <returns>True if the player is banned; otherwise false.</returns> /// <returns>True if the player is banned; otherwise false.</returns>
bool IsBanned(ulong steamId); bool IsBanned(ulong steamId);
/// <summary>
/// Raised when a player is kicked. Passes with SteamID of kicked player.
/// </summary>
event Action<ulong> PlayerKicked;
/// <summary>
/// Raised when a player is banned or unbanned. Passes SteamID of player, and true if banned, false if unbanned.
/// </summary>
event Action<ulong, bool> PlayerBanned;
} }
} }

View File

@@ -27,6 +27,20 @@ namespace Torch.Client.Manager
/// <inheritdoc /> /// <inheritdoc />
public bool IsBanned(ulong steamId) => false; public bool IsBanned(ulong steamId) => false;
/// <inheritdoc />
public event Action<ulong> PlayerKicked
{
add => throw new NotImplementedException();
remove => throw new NotImplementedException();
}
/// <inheritdoc />
public event Action<ulong, bool> PlayerBanned
{
add => throw new NotImplementedException();
remove => throw new NotImplementedException();
}
/// <inheritdoc/> /// <inheritdoc/>
public override void Attach() public override void Attach()
{ {

View File

@@ -64,6 +64,7 @@ namespace Torch.Server.Managers
internal void RaiseClientBanned(ulong user, bool banned) internal void RaiseClientBanned(ulong user, bool banned)
{ {
PlayerBanned?.Invoke(user, banned);
Torch.Invoke(() => Torch.Invoke(() =>
{ {
if(_gameOwnerIds.TryGetValue(user, out ulong owner)) if(_gameOwnerIds.TryGetValue(user, out ulong owner))
@@ -71,10 +72,21 @@ namespace Torch.Server.Managers
}); });
} }
internal void RaiseClientKicked(ulong user)
{
PlayerKicked?.Invoke(user);
}
/// <inheritdoc /> /// <inheritdoc />
public bool IsBanned(ulong steamId) => _isClientBanned.Invoke(MyMultiplayer.Static, steamId) || public bool IsBanned(ulong steamId) => _isClientBanned.Invoke(MyMultiplayer.Static, steamId) ||
MySandboxGame.ConfigDedicated.Banned.Contains(steamId); MySandboxGame.ConfigDedicated.Banned.Contains(steamId);
/// <inheritdoc />
public event Action<ulong> PlayerKicked;
/// <inheritdoc />
public event Action<ulong, bool> PlayerBanned;
/// <inheritdoc/> /// <inheritdoc/>
public override void Attach() public override void Attach()
{ {

View File

@@ -13,16 +13,20 @@ namespace Torch.Server.Managers
[PatchShim] [PatchShim]
internal static class MultiplayerManagerDedicatedPatchShim internal static class MultiplayerManagerDedicatedPatchShim
{ {
private static Logger Log = LogManager.GetCurrentClassLogger();
public static void Patch(PatchContext ctx) public static void Patch(PatchContext ctx)
{ {
ctx.GetPattern(typeof(MyDedicatedServerBase).GetMethod(nameof(MyDedicatedServerBase.BanClient))).Prefixes.Add(typeof(MultiplayerManagerDedicatedPatchShim).GetMethod(nameof(BanPrefix))); ctx.GetPattern(typeof(MyDedicatedServerBase).GetMethod(nameof(MyDedicatedServerBase.BanClient))).Prefixes.Add(typeof(MultiplayerManagerDedicatedPatchShim).GetMethod(nameof(BanPrefix)));
ctx.GetPattern(typeof(MyDedicatedServerBase).GetMethod(nameof(MyDedicatedServerBase.KickClient))).Prefixes.Add(typeof(MultiplayerManagerDedicatedPatchShim).GetMethod(nameof(KickPrefix)));
} }
public static void BanPrefix(ulong userId, bool banned) public static void BanPrefix(ulong userId, bool banned)
{ {
Log.Info($"Caught ban event for {userId}: {banned}");
TorchBase.Instance.CurrentSession.Managers.GetManager<MultiplayerManagerDedicated>().RaiseClientBanned(userId, banned); TorchBase.Instance.CurrentSession.Managers.GetManager<MultiplayerManagerDedicated>().RaiseClientBanned(userId, banned);
} }
public static void KickPrefix(ulong userId)
{
TorchBase.Instance.CurrentSession.Managers.GetManager<MultiplayerManagerDedicated>().RaiseClientKicked(userId);
}
} }
} }