Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace SwiftlyS2.Core.Events;
internal class OnCommandExecuteHookEvent : IOnCommandExecuteHookEvent
{
public required string OriginalName { get; init; }
public required string[] OriginalArgs { get; init; }
public string CommandName { get; set; } = string.Empty;

public required HookMode HookMode { get; init; }
Expand Down
17 changes: 14 additions & 3 deletions managed/src/SwiftlyS2.Core/Services/CoreHookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,32 @@ private void HookCommandExecute()

_Logger.LogInformation("Hooking Cmd_ExecuteCommand at {Address}", address);
var commandNameOffset = NativeOffsets.Fetch("CommandNameOffset");
var commandArgsOffset = NativeOffsets.Fetch("CommandArgsOffset");

_ExecuteCommand = _Core.Memory.GetUnmanagedFunctionByAddress<ExecuteCommandDelegate>(address);
_ExecuteCommandGuid = _ExecuteCommand.AddHook((next) =>
{
return (a1, a2, a3, a4, a5) =>
{
var (commandName, commandPtr) = (a5 != nint.Zero && a5 < nint.MaxValue && commandNameOffset != 0) switch
var commandName = (a5 != nint.Zero && a5 < nint.MaxValue && commandNameOffset != 0) switch
{
true when Marshal.ReadIntPtr(new nint(a5 + commandNameOffset)) is var basePtr && basePtr != nint.Zero && basePtr < nint.MaxValue
=> (Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(basePtr)) ?? string.Empty, Marshal.ReadIntPtr(basePtr)),
_ => (string.Empty, nint.Zero)
=> Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(basePtr)) ?? string.Empty,
_ => string.Empty
};
var commandArgs = (a5 != nint.Zero && a5 < nint.MaxValue && commandArgsOffset != 0) switch
{
true => Marshal.PtrToStringAnsi(new nint(a5 + commandArgsOffset)) ?? string.Empty,
_ => string.Empty
};

var argsSplit = commandArgs.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var args = argsSplit.Length > 1 ? argsSplit[1..] : [];

var preEvent = new OnCommandExecuteHookEvent
{
OriginalName = commandName,
OriginalArgs = args,
HookMode = HookMode.Pre
};
EventPublisher.InvokeOnCommandExecuteHook(preEvent);
Expand All @@ -142,6 +152,7 @@ true when Marshal.ReadIntPtr(new nint(a5 + commandNameOffset)) is var basePtr &&
var postEvent = new OnCommandExecuteHookEvent
{
OriginalName = commandName,
OriginalArgs = args,
HookMode = HookMode.Post
};
EventPublisher.InvokeOnCommandExecuteHook(postEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public interface IOnCommandExecuteHookEvent {
/// </summary>
public string OriginalName { get; }

/// <summary>
/// The original command arguments.
/// </summary>
public string[] OriginalArgs { get; }

/// <summary>
/// The command arguments.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions managed/src/TestPlugin/TestPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ public override void Load(bool hotReload)

// Core.Event.OnCommandExecuteHook += (@event) =>
// {
// Console.WriteLine($"[TestPlugin] CommandExecute({@event.HookMode}): {@event.OriginalName}");
// @event.SetCommandName("test");
// if (@event.HookMode == HookMode.Pre) return;
// Core.Logger.LogInformation("CommandExecute: {name} with {args}", @event.OriginalName, @event.OriginalArgs.Length > 0 ? string.Join(" ", @event.OriginalArgs) : "no args");
// // @event.SetCommandName("test");
// };
Core.Engine.ExecuteCommandWithBuffer("@ping", (buffer) =>
{
Expand Down
24 changes: 24 additions & 0 deletions plugin_files/gamedata/cs2/core/offsets.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@
"windows": 1088,
"linux": 1088
},
/*
To locate this offset, first refer to my ExecuteCommandDelegate comments in CoreHookService.cs.
The function prologue disassembly:

.text:00000000001C34E0 mov [rsp-8+arg_8], rbx
.text:00000000001C34E5 mov [rsp-8+arg_18], r9
.text:00000000001C34EA mov [rsp-8+arg_0], rcx
...
.text:00000000001C3509 mov r14, [rbp+0D00h+arg_20]

Following the Windows x64 calling convention, parameters a1-a4 are passed via rcx, rdx, r8, r9 respectively,
while a5 (the first stack parameter) is loaded into r14 from [rbp+0D00h+arg_20].

By setting a breakpoint at the mov instruction above and executing an invalid command in the CS2 server console,
we can inspect the memory region pointed to by r14.
The command string with arguments appears at offset 0x20 from the base address in r14.

Note: Unlike CommandNameOffset (which points to null-byte ('\0') delimited tokens),
CommandArgsOffset points to a space-delimited string containing the full command line.
*/
"CommandArgsOffset": {
"windows": 32,
"linux": 32
},
/* From sdk */
"ICvar::FindConCommand": {
"windows": 17,
Expand Down
Loading