Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Implementing memory patches #160

Closed
wants to merge 27 commits into from

Conversation

KillStr3aK
Copy link
Contributor

@KillStr3aK KillStr3aK commented Dec 1, 2023

util related things and CMemPatch is borrowed from cs2fixes

this is a pretty wanky implementation, I think we should drop the gameconfig dependency from CMemPatch as thats why I made it like how it is currently.

assume we have the following gamedata:

{
    "ServerMovementUnlock": {
      "signatures": {
        "library": "server",
        "windows": "\\x76\\x2A\\xF2\\x0F\\x10\\x57\\x2A\\xF3\\x0F\\x10\\x2A\\x2A\\x0F\\x28\\xCA\\xF3\\x0F\\x59\\xC0",
        "linux": "\\x0F\\x87\\x2A\\x2A\\x2A\\x2A\\x49\\x8B\\x7C\\x24\\x2A\\xE8\\x2A\\x2A\\x2A\\x2A\\x66\\x0F\\xEF\\xED\\x66\\x0F\\xD6\\x85\\x2A\\x2A\\x2A\\x2A"
      },

      // we are still inside 'ServerMovementUnlock' so the patch would be for the signature above.
      "patches": {
        "windows": "\\xEB",
        "linux": "\\x90\\x90\\x90\\x90\\x90\\x90"
      }
    }
}
// this is a local build so no logger instance as I have not installed transitive packages
public class SamplePlugin : BasePlugin
{
    public override string ModuleName => "Sample Plugin";

    public override string ModuleVersion => "1.0.0";

    private MemoryPatch patch = new MemoryPatch("ServerMovementUnlock", "ServerMovementUnlock");

    public override void Load(bool hotReload)
    {
        if (patch.PerformPatch())
        {
            Console.WriteLine("Performed patch {0} ({1})", patch.Name, patch.Signature);
        }
        else
        {
            Console.WriteLine("Unable to perform patch {0} ({1})", patch.Name, patch.Signature);
        }
    }

    public override void Unload(bool hotReload)
    {
        patch.UndoPatch();
    }
}

hotreloading the plugin will undo the patch, then perform it again:

image

also patches performed using the wrapper method is automatically reverted on unload:

public class SamplePlugin : BasePlugin
{
    public override string ModuleName => "Sample Plugin";

    public override string ModuleVersion => "1.0.0";

    public override void Load(bool hotReload)
    {
        CreateMemoryPatch("ServerMovementUnlock", "ServerMovementUnlock");
    }
}

image

Caution

Plugins that create their patches using the wrapper classes only and call PerformPatch are responsible for reverting the patch on unload. Creating patches with CreateMemoryPatch will be automatically reverted.

@KillStr3aK KillStr3aK changed the title Implementing memory patches [Draft] Implementing memory patches Dec 2, 2023
@KillStr3aK
Copy link
Contributor Author

This will require change or rehaul in the future for sure, but its something we can use.
Since the core does not require any patch I've disabled auto performing them on load to give full control to the plugins, this can be changed later on if we'd need any patch to be performed internally

@KillStr3aK KillStr3aK marked this pull request as ready for review December 12, 2023 18:20
@KillStr3aK KillStr3aK changed the title [Draft] Implementing memory patches Implementing memory patches Dec 12, 2023
@KillStr3aK KillStr3aK marked this pull request as draft January 6, 2024 08:18
@KillStr3aK KillStr3aK changed the title Implementing memory patches [DRAFT] Implementing memory patches Jan 6, 2024
@KillStr3aK
Copy link
Contributor Author

This will be moved to managed code as we can have memory patches in managed code, I'll create a new pr later on

@KillStr3aK KillStr3aK closed this Jan 31, 2024
@Oylneechan
Copy link

Oylneechan commented Mar 3, 2024

I know this is already closed, but FYI I did test on my own server. it seem like the signature and patches can't be in other gamedata json file, except the main gamedata.json file

@KillStr3aK
Copy link
Contributor Author

this will be recreated in managed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants