Skip to content

Commit

Permalink
Feat/debug (#19)
Browse files Browse the repository at this point in the history
* Initial stuff for debug

* More classes!

* Base implementation
  • Loading branch information
MSWS authored Feb 5, 2024
1 parent c8ec9cd commit 5deb951
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Jailbreak.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Rebel", "mod\Jail
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Logs", "mod\Jailbreak.Logs\Jailbreak.Logs.csproj", "{CE6EC648-E7F9-4CE7-B28F-8C7995830F35}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Debug", "mod\Jailbreak.Debug\Jailbreak.Debug.csproj", "{4F10E2B5-E8BB-4253-9BE6-9187575ADB13}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -68,6 +70,10 @@ Global
{CE6EC648-E7F9-4CE7-B28F-8C7995830F35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE6EC648-E7F9-4CE7-B28F-8C7995830F35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE6EC648-E7F9-4CE7-B28F-8C7995830F35}.Release|Any CPU.Build.0 = Release|Any CPU
{4F10E2B5-E8BB-4253-9BE6-9187575ADB13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F10E2B5-E8BB-4253-9BE6-9187575ADB13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F10E2B5-E8BB-4253-9BE6-9187575ADB13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F10E2B5-E8BB-4253-9BE6-9187575ADB13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9135CCC9-66C5-4A9C-AE3C-91475B5F0437} = {177DA48D-8306-4102-918D-992569878581}
Expand All @@ -79,5 +85,6 @@ Global
{FC2D6F50-BCFF-41E6-A965-6C73CC01C3BF} = {CDCDE44E-01D2-4B76-99DA-A57E1E956038}
{CB2391A1-6CDD-496F-B8D6-674FD6268038} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
{CE6EC648-E7F9-4CE7-B28F-8C7995830F35} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
{4F10E2B5-E8BB-4253-9BE6-9187575ADB13} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
EndGlobalSection
EndGlobal
34 changes: 34 additions & 0 deletions lang/Jailbreak.English/Generic/GenericCommandNotifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Core;
using Jailbreak.Formatting.Logistics;
using Jailbreak.Formatting.Objects;
using Jailbreak.Formatting.Views;

namespace Jailbreak.English.Generic;

public class GenericCommandNotifications : IGenericCommandNotifications, ILanguage<Formatting.Languages.English>
{
public static FormatObject PREFIX =
new HiddenFormatObject($" {ChatColors.Darkred}[{ChatColors.LightRed}JB{ChatColors.Darkred}]")
{
// Hide in panorama and center text
Plain = false,
Panorama = false,
Chat = true,
};

public IView PlayerNotFound(string query)
{
return new SimpleView(writer =>
writer
.Line(PREFIX, $"Player '{query}' not found!"));
}

public IView PlayerFoundMultiple(string query)
{
return new SimpleView(writer =>
writer
.Line(PREFIX, $"Multiple players found for '{query}'!"));
}
}
46 changes: 46 additions & 0 deletions mod/Jailbreak.Debug/DebugCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using Jailbreak.Debug.Subcommands;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;

namespace Jailbreak.Debug;

// css_debug [subcommand] [args] -> subcommand [args]
public class DebugCommand : IPluginBehavior
{
private readonly Dictionary<string, Subcommands.AbstractCommand> commands = new();

public DebugCommand(IServiceProvider serviceProvider)
{
commands.Add("markrebel", new MarkRebel(serviceProvider));
}

[RequiresPermissions("@css/root")]
public void Command_Debug(CCSPlayerController? executor, CommandInfo info)
{
if (executor == null)
{
return;
}

if (info.ArgCount == 1)
{
foreach (var command in commands)
{
info.ReplyToCommand(command.Key);
}

return;
}

if (!commands.TryGetValue(info.GetArg(1), out var subcommand))
{
info.ReplyToCommand("Invalid subcommand");
return;
}

subcommand.OnCommand(executor, new WrappedInfo(info));
}
}
12 changes: 12 additions & 0 deletions mod/Jailbreak.Debug/DebugServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Jailbreak.Public.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace Jailbreak.Debug;

public static class DebugServiceExtension
{
public static void AddJailbreakDebug(this IServiceCollection services)
{
services.AddPluginBehavior<DebugCommand>();
}
}
14 changes: 14 additions & 0 deletions mod/Jailbreak.Debug/Jailbreak.Debug.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\public\Jailbreak.Formatting\Jailbreak.Formatting.csproj" />
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj" />
</ItemGroup>

</Project>
145 changes: 145 additions & 0 deletions mod/Jailbreak.Debug/Subcommands/AbstractCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Commands.Targeting;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views;
using Microsoft.Extensions.DependencyInjection;

namespace Jailbreak.Debug.Subcommands;

public abstract class AbstractCommand
{
protected IServiceProvider services;
private IGenericCommandNotifications lang;

protected AbstractCommand(IServiceProvider services)
{
this.services = services;
lang = services.GetRequiredService<IGenericCommandNotifications>();
}

public abstract void OnCommand(CCSPlayerController? executor, WrappedInfo info);

protected TargetResult? GetTarget(WrappedInfo command, int argIndex = 1,
Func<CCSPlayerController, bool>? predicate = null)
{
return GetTarget(command.info, argIndex + 1, predicate);
}

protected TargetResult? GetVulnerableTarget(WrappedInfo command, int argIndex = 1,
Func<CCSPlayerController, bool>? predicate = null)
{
return GetVulnerableTarget(command.info, argIndex + 1, predicate);
}

protected TargetResult? GetTarget(CommandInfo command, int argIndex = 1,
Func<CCSPlayerController, bool>? predicate = null)
{
var matches = command.GetArgTargetResult(argIndex);

matches.Players = matches.Players.Where(player =>
player is { IsValid: true, Connected: PlayerConnectedState.PlayerConnected }).ToList();
if (predicate != null)
matches.Players = matches.Players.Where(predicate).ToList();

if (!matches.Any())
{
if (command.CallingPlayer != null)
lang.PlayerNotFound(command.GetArg(argIndex)).ToPlayerChat(command.CallingPlayer);
return null;
}

if (matches.Count() > 1 && command.GetArg(argIndex).StartsWith('@'))
return matches;

if (matches.Count() == 1 || !command.GetArg(argIndex).StartsWith('@'))
return matches;

if (command.CallingPlayer != null)
lang.PlayerFoundMultiple(command.GetArg(argIndex)).ToPlayerChat(command.CallingPlayer);
return null;
}

internal TargetResult? GetVulnerableTarget(CommandInfo command, int argIndex = 1,
Func<CCSPlayerController, bool>? predicate = null)
{
return GetTarget(command, argIndex,
(p) => command.CallingPlayer == null ||
command.CallingPlayer.CanTarget(p) && (predicate == null || predicate(p)));
}

internal TargetResult? GetSingleTarget(CommandInfo command, int argIndex = 1)
{
var matches = command.GetArgTargetResult(argIndex);

if (!matches.Any())
{
if (command.CallingPlayer != null)
lang.PlayerNotFound(command.GetArg(argIndex)).ToPlayerChat(command.CallingPlayer);
return null;
}

if (matches.Count() > 1)
{
if (command.CallingPlayer != null)
lang.PlayerFoundMultiple(command.GetArg(argIndex)).ToPlayerChat(command.CallingPlayer);
return null;
}

return matches;
}

internal string GetTargetLabel(CommandInfo info, int argIndex = 1)
{
switch (info.GetArg(argIndex))
{
case "@all":
return "all players";
case "@bots":
return "all bots";
case "@humans":
return "all humans";
case "@alive":
return "alive players";
case "@dead":
return "dead players";
case "@!me":
return "all except self";
case "@me":
return info.CallingPlayer == null ? "Console" : info.CallingPlayer.PlayerName;
case "@ct":
return "all CTs";
case "@t":
return "all Ts";
case "@spec":
return "all spectators";
default:
var player = info.GetArgTargetResult(argIndex).FirstOrDefault();
if (player != null)
return player.PlayerName;
return "unknown";
}
}


internal string GetTargetLabels(CommandInfo info, int argIndex = 1)
{
string label = GetTargetLabel(info, argIndex);
if (label.ToLower().EndsWith("s"))
return label + "'";
return label + "'s";
}
}

public static class CommandExtensions

{
internal static bool CanTarget(this CCSPlayerController controller, CCSPlayerController target)
{
if (!target.IsValid) return false;
if (target.Connected != PlayerConnectedState.PlayerConnected) return false;
if (target.IsBot || target.IsHLTV) return true;
return AdminManager.CanPlayerTarget(controller, target);
}
}
45 changes: 45 additions & 0 deletions mod/Jailbreak.Debug/Subcommands/MarkRebel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Mod.Rebel;
using Microsoft.Extensions.DependencyInjection;

namespace Jailbreak.Debug.Subcommands;

// css_markrebel [player] <duration>
public class MarkRebel : AbstractCommand
{
public MarkRebel(IServiceProvider services) : base(services)
{
}

public override void OnCommand(CCSPlayerController? executor, WrappedInfo info)
{
if (info.ArgCount == 1)
{
info.ReplyToCommand("Specify target?");
return;
}

var target = GetVulnerableTarget(info);
if (target == null)
return;

var duration = 120;
if(info.ArgCount == 3)
{
if (!int.TryParse(info.GetArg(2), out duration))
{
info.ReplyToCommand("Invalid duration");
return;
}
}

foreach (var player in target.Players)
{
services.GetRequiredService<IRebelService>().MarkRebel(player, duration);
}
info.ReplyToCommand($"Marked {target.Players.Count()} players as rebels for {duration} seconds");
}
}
29 changes: 29 additions & 0 deletions mod/Jailbreak.Debug/Subcommands/WrappedInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;

namespace Jailbreak.Debug.Subcommands;

public class WrappedInfo
{
public readonly CommandInfo info;

public WrappedInfo(CommandInfo info)
{
this.info = info;
}

public CCSPlayerController? CallingPlayer => info.CallingPlayer;

public IntPtr Handle => info.Handle;

public int ArgCount => info.ArgCount - 1;

public string ArgString => info.ArgString[(info.ArgString.IndexOf(' ') + 1)..];

public string GetCommandString => info.GetCommandString[(info.GetCommandString.IndexOf(' ') + 1)..];

public string ArgByIndex(int index) => info.ArgByIndex(index + 1);
public string GetArg(int index) => info.GetArg(index + 1);

public void ReplyToCommand(string message, bool console = false) => info.ReplyToCommand(message, console);
}
2 changes: 1 addition & 1 deletion mod/Jailbreak.Logs/LogsServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Jailbreak.Logs;

public static class LogsServiceExtension
{
public static void AddLogsService(this IServiceCollection services)
public static void AddJailbreakLogs(this IServiceCollection services)
{
services.AddPluginBehavior<ILogService, LogsManager>();

Expand Down
2 changes: 1 addition & 1 deletion mod/Jailbreak.Rebel/RebelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public long GetRebelTimeLeft(CCSPlayerController player)
return 0;
}

public bool MarkRebel(CCSPlayerController player, long time)
public bool MarkRebel(CCSPlayerController player, long time = 120)
{
if (!rebelTimes.ContainsKey(player))
{
Expand Down
4 changes: 4 additions & 0 deletions public/Jailbreak.Formatting/Logistics/LanguageConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public LanguageConfig(IServiceCollection collection)
_collection = collection;
}

public void WithGenericCommand<TGenericCommand>()
where TGenericCommand : class, ILanguage<TDialect>, IGenericCommandNotifications
=> _collection.AddSingleton<IGenericCommandNotifications, TGenericCommand>();

public void WithRatio<TRatio>()
where TRatio : class, ILanguage<TDialect>, IRatioNotifications
=> _collection.AddSingleton<IRatioNotifications, TRatio>();
Expand Down
Loading

0 comments on commit 5deb951

Please sign in to comment.