From 30dbcb0745eeb5803b9258d6f83ef9e41c3042f4 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 16 Dec 2024 14:05:07 +0100 Subject: [PATCH] Code cleanup --- mod/TTT.Detective/DetectiveManager.cs | 90 +++++-- mod/TTT.Roles/Helper/InfoManager.cs | 84 ------ mod/TTT.Roles/RoleManager.cs | 286 +-------------------- public/TTT.Public/Mod/Role/IRoleService.cs | 1 - 4 files changed, 85 insertions(+), 376 deletions(-) diff --git a/mod/TTT.Detective/DetectiveManager.cs b/mod/TTT.Detective/DetectiveManager.cs index 27f3218..a3f37df 100644 --- a/mod/TTT.Detective/DetectiveManager.cs +++ b/mod/TTT.Detective/DetectiveManager.cs @@ -2,7 +2,9 @@ using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Modules.Memory; +using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions; using TTT.Player; +using TTT.Public.Action; using TTT.Public.Behaviors; using TTT.Public.Extensions; using TTT.Public.Formatting; @@ -29,27 +31,49 @@ public void Start(BasePlugin parent) .Where(player => (player.Buttons & PlayerButtons.Use) != 0)) OnPlayerUse(player); }); - - /** - VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(hook => - { - var info = hook.GetParam(1); - if (info.Attacker.Value == null || !info.Attacker.Value.IsValid) return HookResult.Continue; - var attacker = info.Attacker.Value.As(); - if (attacker == hook.GetParam(0)) return HookResult.Continue; - if (info.AmmoType is not TaserAmmoType) return HookResult.Continue; + VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnZeus, HookMode.Pre); - info.Damage = 1f; + } - if (!attacker.IsReal()) return HookResult.Continue; - var ammoType = info.AmmoType; + private HookResult OnZeus(DynamicHook hook) + { + var ent = hook.GetParam(0); - return HookResult.Changed; - }, HookMode.Pre); - */ - } + var playerWhoWasDamaged = player(ent); + if (playerWhoWasDamaged == null) return HookResult.Continue; + + var info = hook.GetParam(1); + + CCSPlayerController? attacker = null; + + if (info.Attacker.Value != null) + { + var playerWhoAttacked = info.Attacker.Value.As(); + + attacker = playerWhoAttacked.Controller.Value.As(); + + } + + if (info.BitsDamageType != DamageTypes_t.DMG_SHOCK) return HookResult.Continue; + if (attacker == null) return HookResult.Continue; + + info.Damage = 0; + + var targetRole = _roleService.GetPlayer(playerWhoWasDamaged); + + Server.NextFrame(() => + { + attacker.PrintToChat( + StringUtils.FormatTTT( + $"You tased player {playerWhoWasDamaged.PlayerName} they are a {targetRole.PlayerRole().FormatRoleFull()}")); + }); + + //_roundService.GetLogsService().AddLog(new MiscAction("tased player " + targetRole.PlayerRole().FormatStringFullAfter(playerWhoWasDamaged.PlayerName), attacker)); + + return HookResult.Stop; + } private void OnPlayerUse(CCSPlayerController player) { @@ -92,4 +116,38 @@ private void IdentifyBody(CCSPlayerController caller) Server.NextFrame(() => { Server.PrintToChatAll(message); }); } + + private static CCSPlayerController? player(CEntityInstance? instance) + { + if (instance == null) + { + return null; + } + + if (instance.DesignerName != "player") + { + return null; + } + + // grab the pawn index + int player_index = (int)instance.Index; + + // grab player controller from pawn + CCSPlayerPawn player_pawn = Utilities.GetEntityFromIndex(player_index); + + // pawn valid + if (player_pawn == null || !player_pawn.IsValid) + { + return null; + } + + // controller valid + if (player_pawn.OriginalController == null || !player_pawn.OriginalController.IsValid) + { + return null; + } + + // any further validity is up to the caller + return player_pawn.OriginalController.Value; + } } \ No newline at end of file diff --git a/mod/TTT.Roles/Helper/InfoManager.cs b/mod/TTT.Roles/Helper/InfoManager.cs index f180d3a..c3b4f39 100644 --- a/mod/TTT.Roles/Helper/InfoManager.cs +++ b/mod/TTT.Roles/Helper/InfoManager.cs @@ -27,8 +27,6 @@ public InfoManager(RoleManager roleService, IRoundService manager, BasePlugin pl { _roleService = roleService; _manager = manager; - plugin.RegisterListener(OnTick); - plugin.AddTimer(0.3f, OnTickAll, TimerFlags.REPEAT); plugin.AddTimer(0.1f, OnTickScoreboard, TimerFlags.REPEAT); plugin.RegisterEventHandler(OnPlayerSpectateChange); @@ -60,88 +58,6 @@ public void OnTickScoreboard() } - public void OnTick() - { - foreach (var gamePlayer in _roleService.Players()) - { - if (_manager.GetRoundStatus() != RoundStatus.Started) return; - - var player = gamePlayer.Player(); - - if (player == null) continue; - if (!player.IsReal()) continue; - - var playerRole = gamePlayer.PlayerRole(); - - if (playerRole == Role.Unassigned) continue; - if (gamePlayer.ShopOpen()) continue; - - if (!player.PawnIsAlive && AdminManager.PlayerHasPermissions(player, "@css/kick")) - { - if (player.ObserverPawn.Value == null || !player.ObserverPawn.Value.IsValid) continue; - if (player.ObserverPawn.Value.ObserverServices?.ObserverTarget.Value is null) continue; - var target = player.ObserverPawn.Value.ObserverServices.ObserverTarget.Value?.As(); - if (target == null) - { - Server.NextFrame(() => - player.PrintToCenterHtml( - $"Your Role: {playerRole.GetCenterRole()}")); - } - else - { - Server.NextFrame(() => player.PrintToCenterHtml( - $"Your Role: {playerRole.GetCenterRole()}
" - + $"{target.PlayerName}'s Role: {_roleService.GetRole(target).GetCenterRole()}")); - } - - continue; - } - - if (!_playerLookAtRole.TryGetValue(player, out var value)) - { - Server.NextFrame(() => - player.PrintToCenterHtml( - $"Your Role: {playerRole.GetCenterRole()}")); - continue; - } - - if (!value.Item1.IsReal()) continue; - - if (value.Item2 == playerRole || playerRole == Role.Traitor || value.Item2 == Role.Detective) - { - Server.NextFrame(() => player.PrintToCenterHtml( - $"Your Role: {playerRole.GetCenterRole()}
" - + $"{value.Item1.PlayerName}'s Role: {value.Item2.GetCenterRole()}")); - } - - - if (playerRole != Role.Innocent && (value.Item2 != Role.Traitor || playerRole != Role.Detective)) continue; - - Server.NextFrame(() => player.PrintToCenterHtml( - $"Your Role: {playerRole.GetCenterRole()}
" - + $"{value.Item1.PlayerName}'s Role: {Role.Unassigned.GetCenterRole()}")); - } - } - - public void OnTickAll() - { - var players = _roleService.GetPlayers().Keys; - - _playerLookAtRole.Clear(); - - foreach (var player in players) - { - if (!player.IsReal()) continue; - - var target = player.GetClientPlayerAimTarget(); - - if (target == null) continue; - if (!target.IsReal()) continue; - - RegisterLookAtRole(player, new Tuple(target, _roleService.GetRole(target))); - } - } - [GameEventHandler] private HookResult OnPlayerSpectateChange(EventSpecTargetUpdated @event, GameEventInfo info) { diff --git a/mod/TTT.Roles/RoleManager.cs b/mod/TTT.Roles/RoleManager.cs index 1d907e3..bcbdf65 100644 --- a/mod/TTT.Roles/RoleManager.cs +++ b/mod/TTT.Roles/RoleManager.cs @@ -46,49 +46,6 @@ public void Start(BasePlugin parent) parent.RegisterEventHandler(OnRoundEnd); parent.RegisterEventHandler(OnPlayerDisconnect); parent.RegisterEventHandler(OnPlayerDeath, HookMode.Pre); - parent.RegisterEventHandler(OnMapStart); - parent.RegisterEventHandler(Event_PlayerSpawn, HookMode.Post); - - VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(hook => - { - var ent = hook.GetParam(0); - - var playerWhoWasDamaged = player(ent); - - if (playerWhoWasDamaged == null) return HookResult.Continue; - - var info = hook.GetParam(1); - - CCSPlayerController? attacker = null; - - if (info.Attacker.Value != null) - { - var playerWhoAttacked = info.Attacker.Value.As(); - - attacker = playerWhoAttacked.Controller.Value.As(); - - } - - if (info.BitsDamageType != DamageTypes_t.DMG_SHOCK) return HookResult.Continue; - if (attacker == null) return HookResult.Continue; - - info.Damage = 0; - - var targetRole = GetPlayer(playerWhoWasDamaged); - - Server.NextFrame(() => - { - attacker.PrintToChat( - StringUtils.FormatTTT( - $"You tased player {playerWhoWasDamaged.PlayerName} they are a {targetRole.PlayerRole().FormatRoleFull()}")); - }); - - _roundService.GetLogsService().AddLog(new MiscAction("tased player " + targetRole.PlayerRole().FormatStringFullAfter(playerWhoWasDamaged.PlayerName), attacker)); - - return HookResult.Stop; - - }, HookMode.Pre); - } [GameEventHandler] @@ -116,12 +73,6 @@ private HookResult OnPlayerConnect(EventPlayerConnectFull @event, GameEventInfo return HookResult.Continue; } - - [GameEventHandler] - private HookResult OnMapStart(EventGameStart @event, GameEventInfo info) - { - return HookResult.Continue; - } [GameEventHandler] private HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) @@ -147,18 +98,7 @@ private HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) Server.NextFrame(() => playerWhoWasDamaged.CommitSuicide(false, true)); - Server.NextFrame(() => - { - Server.PrintToChatAll(StringUtils.FormatTTT($"{GetRole(playerWhoWasDamaged).FormatStringFullAfter(" has been found.")}")); - - if (attacker == playerWhoWasDamaged || attacker == null) return; - - attacker.ModifyScoreBoard(); - - playerWhoWasDamaged.PrintToChat(StringUtils.FormatTTT( - $"You were killed by {GetRole(attacker).FormatStringFullAfter(" " + attacker.PlayerName)}.")); - attacker.PrintToChat(StringUtils.FormatTTT($"You killed {GetRole(playerWhoWasDamaged).FormatStringFullAfter(" " + playerWhoWasDamaged.PlayerName)}.")); - }); + Server.NextFrame(() => SendDeathMessage(playerWhoWasDamaged, attacker)); return HookResult.Continue; } @@ -289,232 +229,28 @@ public void Clear() _infoManager.Reset(); foreach (var key in GetPlayers()) key.Value.SetPlayerRole(Role.Unassigned); } - - public void ApplyColorFromRole(CCSPlayerController player, Role role) - { - switch (role) - { - case Role.Traitor: - ApplyTraitorColor(player); - break; - case Role.Detective: - ApplyDetectiveColor(player); - break; - case Role.Innocent: - ApplyInnocentColor(player); - break; - case Role.Unassigned: - default: - break; - } - } public bool IsInnocent(CCSPlayerController player) { return GetPlayer(player).PlayerRole() == Role.Innocent; } - private void SetColors() - { - foreach (var key in Players()) - { - if (key.PlayerRole() != Role.Innocent) return; - ApplyInnocentColor(key.Player()); - } - } - - private void RemoveColors() - { - var players = Utilities.GetPlayers() - .Where(player => player.IsValid).ToList(); - - foreach (var player in players) - { - if (player.Pawn.Value == null) return; - player.Pawn.Value.RenderMode = RenderMode_t.kRenderTransColor; - player.Pawn.Value.Render = Color.FromArgb(254, 255, 255, 255); - Utilities.SetStateChanged(player.Pawn.Value, "CBaseModelEntity", "m_clrRender"); - } - } - - private void SetColorTeam(CBaseEntity entity, string className, string fieldName, Role role = Role.Innocent) - { - Guard.IsValidEntity(entity); - - if (!Schema.IsSchemaFieldNetworked(className, fieldName)) - { - Application.Instance.Logger.LogWarning("Field {ClassName}:{FieldName} is not networked, but SetStateChanged was called on it.", className, fieldName); - return; - } - - foreach (var player in GetPlayers().Values.Where(player => player.PlayerRole() == role)) - { - Guard.IsValidEntity(player.Player()); - - var playerObj = player.Player(); - - int offset = Schema.GetSchemaOffset(className, fieldName); - - VirtualFunctions.StateChanged(entity.NetworkTransmitComponent.Handle, entity.Handle, offset, -1, (short)playerObj.Index); - } - } - - private void ApplyDetectiveColor(CCSPlayerController player) - { - if (!player.IsReal() || player.Pawn.Value == null) - return; - - player.Pawn.Value.RenderMode = RenderMode_t.kRenderTransColor; - player.Pawn.Value.Render = Color.Blue; - SetColorTeam(player, "CBaseModelEntity", "m_clrRender", Role.Detective); - } - - private void ApplyTraitorColor(CCSPlayerController player) - { - if (!player.IsReal() || player.Pawn.Value == null) - return; - - player.Pawn.Value.RenderMode = RenderMode_t.kRenderGlow; - player.Pawn.Value.Render = Color.Red; - SetColorTeam(player, "CBaseModelEntity", "m_clrRender", Role.Traitor); - //apply for traitors only somehow? - } - - private void ApplyInnocentColor(CCSPlayerController player) - { - if (!player.IsReal() || player.Pawn.Value == null) - return; - - player.Pawn.Value.RenderMode = RenderMode_t.kRenderGlow; - player.Pawn.Value.Render = Color.Green; - - SetColorTeam(player, "CBaseModelEntity", "m_clrRender"); - } - private Role GetWinner() { return _traitorsLeft == 0 ? Role.Traitor : Role.Innocent; } - - private HookResult Event_PlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) - { - if (!@event.Userid.IsValid) - { - return HookResult.Continue; - } - - CCSPlayerController player = @event.Userid; - - if(!player.IsReal()) - { - return HookResult.Continue; - } - - if (!player.PlayerPawn.IsValid) - { - return HookResult.Continue; - } - - CHandle pawn = player.PlayerPawn; - - Server.NextFrame(() => PlayerSpawnNextFrame(player, pawn)); - - return HookResult.Continue; - } - - - private readonly WIN_LINUX OnCollisionRulesChangedOffset = new(173, 172); - - private void PlayerSpawnNextFrame(CCSPlayerController player, CHandle pawn) - { - pawn.Value.Collision.CollisionGroup = (byte)CollisionGroup.COLLISION_GROUP_DISSOLVING; - - pawn.Value.Collision.CollisionAttribute.CollisionGroup = (byte)CollisionGroup.COLLISION_GROUP_DISSOLVING; - - VirtualFunctionVoid collisionRulesChanged = - new VirtualFunctionVoid(pawn.Value.Handle, OnCollisionRulesChangedOffset.Get()); - - collisionRulesChanged.Invoke(pawn.Value.Handle); - } - public static CCSPlayerController? player(CEntityInstance? instance) - { - if (instance == null) - { - return null; - } - - if (instance.DesignerName != "player") - { - return null; - } - - // grab the pawn index - int player_index = (int)instance.Index; - - // grab player controller from pawn - CCSPlayerPawn player_pawn = Utilities.GetEntityFromIndex(player_index); - - // pawn valid - if (player_pawn == null || !player_pawn.IsValid) - { - return null; - } - - // controller valid - if (player_pawn.OriginalController == null || !player_pawn.OriginalController.IsValid) - { - return null; - } - - // any further validity is up to the caller - return player_pawn.OriginalController.Value; - } -} - - - -internal static class IsValid -{ - public static bool PlayerIndex(uint playerIndex) - { - if(playerIndex == 0) - { - return false; - } - - if(!(1 <= playerIndex && playerIndex <= Server.MaxPlayers)) - { - return false; - } - - return true; - } -} - -public class WIN_LINUX -{ - [JsonPropertyName("Windows")] - public T Windows { get; private set; } - - [JsonPropertyName("Linux")] - public T Linux { get; private set; } - - public WIN_LINUX(T windows, T linux) + private void SendDeathMessage(CCSPlayerController playerWhoWasDamaged, CCSPlayerController attacker) { - this.Windows = windows; - this.Linux = linux; - } + Server.PrintToChatAll(StringUtils.FormatTTT($"{GetRole(playerWhoWasDamaged).FormatStringFullAfter(" has been found.")}")); + + if (attacker == playerWhoWasDamaged || attacker == null) return; + + attacker.ModifyScoreBoard(); + + playerWhoWasDamaged.PrintToChat(StringUtils.FormatTTT( + $"You were killed by {GetRole(attacker).FormatStringFullAfter(" " + attacker.PlayerName)}.")); + attacker.PrintToChat(StringUtils.FormatTTT($"You killed {GetRole(playerWhoWasDamaged).FormatStringFullAfter(" " + playerWhoWasDamaged.PlayerName)}.")); - public T Get() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return this.Windows; - } - else - { - return this.Linux; - } } } \ No newline at end of file diff --git a/public/TTT.Public/Mod/Role/IRoleService.cs b/public/TTT.Public/Mod/Role/IRoleService.cs index dbd67c5..08ea9fa 100644 --- a/public/TTT.Public/Mod/Role/IRoleService.cs +++ b/public/TTT.Public/Mod/Role/IRoleService.cs @@ -15,7 +15,6 @@ public interface IRoleService : IPlayerService void AddDetective(CCSPlayerController player); void AddTraitor(CCSPlayerController player); void AddInnocents(IEnumerable players); - public void ApplyColorFromRole(CCSPlayerController player, Role role); void Clear(); }