Raised This Month: $51 Target: $400
 12% 

[EXTENSION] MemPatch


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 11-16-2011 , 17:10   [EXTENSION] MemPatch
Reply With Quote #1

Description:
This extension lets you change memory while the server is running. The idea comes from CS:S DM originally. Then someone (quack) pointed out that an extension already existed (http://forums.alliedmods.net/showthread.php?t=104136). Which did more than my extension, but at the same time I didn't really like how it was done. So I incorporated some of the features from that extension into this one at the same time making it better and easier to use (hopefully). The big/useful addition that I added was the ability to restore the patch easily (It will restore to what was saved on setup) also how byte patches worked (uses CS:S DM's method). As a note this can crash a server very easily if you dont know what you are doing.

The include:
PHP Code:
#if defined _mempatch_included
 #endinput
#endif
#define _mempatch_included

enum MemPatchType
{
    
MEM_PATCH_BYTES 0,
    
MEM_PATCH_INT8 1,
    
MEM_PATCH_UINT8 2,
    
MEM_PATCH_INT16 3,
    
MEM_PATCH_UINT16 4,
    
MEM_PATCH_INT32 5,
    
MEM_PATCH_UINT32 6,
    
MEM_PATCH_FLOAT 7,
    
MEM_PATCH_DOUBLE 8,
    
MEM_PATCH_STRING 9
};

/*
 * Setups a new bytes patch
 *
 * @param gameconf    Handle to gamedata
 * @param sig        Signature string in the gamedata to use
 * @param offset    Offset string in the gamedata to use
 * @param patch        Patch string in gamedata to use Note: Must be a key value, and the key name must be followed by the OS Example MemPatch_Windows, MemPatch_Linux. MemPatch would be passed.
 *
 * @return Handle to the patch
*/
native Handle:SetupMemoryPatchBytes(Handle:gameconf, const String:sig[], const String:offset[], const String:patch[]);

/*
 * Setups a new int patch
 *
 * @param gameconf    Handle to gamedata
 * @param sig        Signature string in the gamedata to use
 * @param offset    Offset string in the gamedata to use
 * @param type        Size of int and type (unsigned/signed)
 *
 * @return Handle to the patch
*/
native Handle:SetupMemoryPatchInt(Handle:gameconf, const String:sig[], const String:offset[], MemPatchType:type=MEM_PATCH_INT32);

/*
 * Setups a new float patch
 *
 * @param gameconf    Handle to gamedata
 * @param sig        Signature string in the gamedata to use
 * @param offset    Offset string in the gamedata to use
 * @param type        Size of float (float/double)
 *
 * @return Handle to the patch
*/
native Handle:SetupMemoryPatchFloat(Handle:gameconf, const String:sig[], const String:offset[], MemPatchType:type=MEM_PATCH_FLOAT);

/*
 * Setups a new string patch
 *
 * @param gameconf    Handle to gamedata
 * @param sig        Signature string in the gamedata to use
 * @param offset    Offset string in the gamedata to use
 * @param size        Size of the string being patched
 *
 * @return Handle to the patch
*/
native Handle:SetupMemoryPatchString(Handle:gameconf, const String:sig[], const String:offset[], size);

/*
 * Reads the current int at the memory location used by the patch
 *
 * @param mempatch    Handle to memory patch to get value for
 *
 * @return Int value of the patch location
*/
native ReadMemoryInt(Handle:mempatch);

/*
 * Reads the current float at the memory location used by the patch
 *
 * @param mempatch    Handle to memory patch to get value for
 *
 * @return Float value of the patch location
*/
native Float:ReadMemoryFloat(Handle:mempatch);

/*
 * Reads the current string at the memory location used by the patch
 *
 * @param mempatch    Handle to memory patch to get value for
 * @param buffer    Buffer to store the return string
 * @param buffersize    Size of buffer
 *
 * @return bytes copied to buffer
*/
native ReadMemoryString(Handle:mempatchString:buffer[], buffersize);

/*
 * Reads a number of bytes and returns and opcode string
 *
 * @param gameconf        Handle to gamedata file
 * @param sig            Signature string in the gamedata to use
 * @param offset        Offset string in the gamedata to use
 * @param size            Number of bytes to read
 * @param buffer        Buffer to store the opcodes
 * @param buffersize    Size of buffer
 *
 * @noreturn
*/
native ReadMemoryBytes(Handle:gameconf, const String:sig[], const String:offset[], size, const String:buffer[], buffersize);

/*
 * Patches the int at the memory location used by the patch
 *
 * @param mempatch    Handle to memory patch to get value for
 * @param value        Value to set memory location to
 *
 * @noreturn
*/

native MemoryPatchInt(Handle:mempatchvalue);

/*
 * Patches the float at the memory location used by the patch
 *
 * @param mempatch    Handle to memory patch to get value for
 * @param value        Value to set memory location to
 *
 * @noreturn
*/
native MemoryPatchFloat(Handle:mempatchFloat:value);

/*
 * Patches the string at the memory location used by the patch
 *
 * @param mempatch    Handle to memory patch to get value for
 * @param value        Value to set memory location to
 *
 * @noreturn
*/
native MemoryPatchString(Handle:mempatch, const String:value[]);

/*
 * Patches bytes to the patch provided on setup
 *
 * @param mempatch    Handle to memory patch to get value for
 *
 * @noreturn
*/
native MemoryPatchBytes(Handle:mempatch);

/*
 * Restores a patch to the value gather on setup
 *
 * @param mempatch    Handle to memory patch to restore
 *
 * @noreturn
*/
native RestoreMemoryPatch(Handle:mempatch);

/*
 * Returns an array of byte patter offsets
 * 
 * @param gameconf    Handle to gamedata file
 * @param sig        Signature string in the gamedata to use
 * @param offset    Offset to start from 
 * @param bytes        Number of bytes to search through to find the patter
 * @param match        Bytes pattern to match
 * @param matches    Array to store offsets where match occured
 * @param arraysize    Size of array being passed for storing matches
 *
 * @return matches found
*/
native GetBytePatternOffsets(Handle:gameconf, const String:sig[], offsetbytes, const String:match[], matches[], arraysize);

/*
 * Returns an array of byte patter offsets
 * 
 * @param gameconf    Handle to gamedata file
 * @param sig        Signature string in the gamedata to use
 * @param offset    Offset to start from 
 * @param type        Type of patch to do
 * @param size        Used if the patch type is a string else leave as 0
 * @param patch        Used if patch type is BYTES patch else leave empty
 *
 * @return mempatch handle
*/
native Handle:SetupOffsetMemoryPatch(Handle:gameconf, const String:sig[], offsetMemPatchType:type=MEM_PATCH_BYTESsize 0, const String:patch[] = "");

public 
Extension:__ext_mempatch =
{
    
name "mempatch",
    
file "mempatch.ext",
#if defined AUTOLOAD_EXTENSIONS
    
autoload 1,
#else
    
autoload 0,
#endif
#if defined REQUIRE_EXTENSIONS
    
required 1,
#else
    
required 0,
#endif
};

#if !defined REQUIRE_EXTENSIONS
public __ext_mempatch_SetNTVOptional()
{
    
MarkNativeAsOptional("SetupMemoryPatchBytes");
    
MarkNativeAsOptional("SetupMemoryPatchInt");
    
MarkNativeAsOptional("SetupMemoryPatchFloat");
    
MarkNativeAsOptional("SetupMemoryPatchString");
    
MarkNativeAsOptional("ReadMemoryInt");
    
MarkNativeAsOptional("ReadMemoryFloat");
    
MarkNativeAsOptional("ReadMemoryString");
    
MarkNativeAsOptional("ReadMemoryBytes");
    
MarkNativeAsOptional("MemoryPatchInt");
    
MarkNativeAsOptional("MemoryPatchFloat");
    
MarkNativeAsOptional("MemoryPatchString");
    
MarkNativeAsOptional("MemoryPatchBytes");
    
MarkNativeAsOptional("RestoreMemoryPatch");
    
MarkNativeAsOptional("GetBytePatternOffsets");
    
MarkNativeAsOptional("SetupOffsetMemoryPatch");
}
#endif 
FAQ:
Q: That sounds cool and all but what can you do with this really?
A: Well here is an example plugin!
PHP Code:
#include <sourcemod>
#include <mempatch>

new Handle:clientsettings;
new 
Handle:desc;
new 
Handle:hostageuse;
new 
Handle:m60drop;
new 
Handle:spread;

public 
Plugin:myinfo 
{
    
name "MemPatch",
    
author "Dr!fter",
    
description "Mem patch test",
    
version "1.0.0"
}
public 
OnPluginStart()
{
    
decl String:descname[32];
    
GetGameDescription(descnamesizeof(descname), true);
    
decl String:name[32];
    
GetGameFolderName(namesizeof(name));
    new 
Handle:gameconf LoadGameConfigFile("mempatchtest.games");
    if(
strcmp(name"cstrike") == 0)
    {
        
clientsettings SetupMemoryPatchBytes(gameconf"ClientSettings""ClientSettingsOffset""ClientSettingsPatch");
        new 
String:buffer[((4*4)+1)];//This should be ((number of bytes to read * 4) +1)
        
ReadMemoryBytes(gameconf"ClientSettings""ClientSettingsOffset"4buffersizeof(buffer));
        
PrintToServer("The bytes before patch read are %s"buffer);
        
MemoryPatchBytes(clientsettings);
        
ReadMemoryBytes(gameconf"ClientSettings""ClientSettingsOffset"4buffersizeof(buffer));
        
PrintToServer("The bytes after patch read are %s"buffer);
        
        
desc SetupMemoryPatchString(gameconf"DescriptionName"""strlen(descname)+1);
        new 
String:temp[strlen(descname)+1];
        
ReadMemoryString(desctempstrlen(descname)+1);
        
PrintToServer("The current description is %s"temp);
        
MemoryPatchString(desc"I Like Cookies");
        
ReadMemoryString(desctempstrlen(descname)+1);
        
PrintToServer("Patched description is %s"temp);
        
        
hostageuse SetupMemoryPatchInt(gameconf"GivesCtUseBonus""GivesCtUseBonusOffset"MEM_PATCH_UINT16);
        
PrintToServer("Current hostage money reward is %i"ReadMemoryInt(hostageuse));
        
MemoryPatchInt(hostageuse800);
        
PrintToServer("New hostage money reward is %i"ReadMemoryInt(hostageuse));
        
        
spread SetupMemoryPatchFloat(gameconf"M3Spread""");
        
PrintToServer("Current M3 spread is %f"ReadMemoryFloat(spread));
        
MemoryPatchFloat(spread0.0);
        
PrintToServer("New M3 spread is %f"ReadMemoryFloat(spread));
    }
    else if(
strcmp(name"left4dead2") == 0)
    {
        
m60drop SetupMemoryPatchBytes(gameconf"M60PrimaryAttack""M60PrimaryAttackOffset""M60PrimaryAttackPatch");
        new 
String:buffer[((4*4)+1)];//This should be ((number of bytes to read * 4) +1)
        
ReadMemoryBytes(gameconf"M60PrimaryAttack""M60PrimaryAttackOffset"4buffersizeof(buffer));
        
PrintToServer("The bytes before patch read are %s"buffer);
        
MemoryPatchBytes(m60drop);
        
ReadMemoryBytes(gameconf"M60PrimaryAttack""M60PrimaryAttackOffset"4buffersizeof(buffer));
        
PrintToServer("The bytes after patch read are %s"buffer);
        
        
        
desc SetupMemoryPatchString(gameconf"DescriptionName"""strlen(descname)+1);
        new 
String:temp[strlen(descname)+1];
        
ReadMemoryString(desctempstrlen(descname)+1);
        
PrintToServer("The current description is %s"temp);
        
MemoryPatchString(desc"Cookiez");
        
ReadMemoryString(desctempstrlen(descname)+1);
        
PrintToServer("Patched description is %s"temp);
    }
    
CloseHandle(gameconf);
}
public 
OnPluginEnd()
{
    
decl String:name[32];
    
GetGameFolderName(namesizeof(name));
    if(
strcmp(name"cstrike") == 0)
    {
        
RestoreMemoryPatch(clientsettings);
        
CloseHandle(clientsettings);
        
RestoreMemoryPatch(desc);
        
CloseHandle(desc);
        
RestoreMemoryPatch(hostageuse);
        
CloseHandle(hostageuse);
        
RestoreMemoryPatch(spread);
        
CloseHandle(spread);
    }
    else if(
strcmp(name"left4dead2") == 0)
    {
        
RestoreMemoryPatch(m60drop);
        
CloseHandle(m60drop);
        
RestoreMemoryPatch(desc);
        
CloseHandle(desc);
    }

What the above code does is change the game description (note you really shouldn't use this for that...) Allows name changes in cs:s. Changes the amount of money you get as a ct for trying to rescue a hostage. Also changes a constant float used by M3 Spread. In l4d2 it patches the m60 drop and changes the description name (again don't use this for that...)

Thanks:
BAILOPAN - for CS:S DM code that was used in this extension.
raydan - for ideas/methods that were used in his extension.
psychonic - for dealing with my general bitching and explaining how this all works.

As a note if someone has any changes I should make to improve this please post them! This extension is at the limit of what I know so if I made a mistake I'd appreciate being told how to improve it.

This was tested in the following:
CS:S - Windows
CS:S - Linux
Left4Dead2 - Windows
Left4Dead2 - Linux
Attached Files
File Type: zip MemPatch 0.0.2.zip (65.9 KB, 1643 views)
File Type: zip MemPatch 0.0.2-src.zip (17.5 KB, 666 views)

Last edited by Dr!fter; 01-13-2012 at 14:21.
Dr!fter is offline
jungjunghoo
Senior Member
Join Date: Sep 2009
Old 11-18-2011 , 07:08   Re: [EXTENSION] MemPatch
Reply With Quote #2

Is this right just I'm using this extension, My serveer more stable by memory?

Last edited by jungjunghoo; 11-18-2011 at 07:11.
jungjunghoo is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 01-13-2012 , 14:41   Re: [EXTENSION] MemPatch
Reply With Quote #3

Updated the extension. Added 2 new natives to allow searching x number of bytes for a pattern and get offsets to the pattern.
Dr!fter is offline
TheHyena
Junior Member
Join Date: Dec 2011
Old 02-10-2012 , 03:35   Re: [EXTENSION] MemPatch
Reply With Quote #4

nvm i got it to work

Last edited by TheHyena; 02-10-2012 at 06:55.
TheHyena is offline
klausenbusk
AlliedModders Donor
Join Date: Jan 2011
Old 02-10-2012 , 17:16   Re: [EXTENSION] MemPatch
Reply With Quote #5

Can we edit max enity?
klausenbusk is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 02-11-2012 , 08:45   Re: [EXTENSION] MemPatch
Reply With Quote #6

Quote:
Originally Posted by klausenbusk View Post
Can we edit max enity?
it is a client side too, so nope, we can not.
.Dare Devil. is offline
Despirator
Senior Member
Join Date: Jun 2011
Location: Kazakhstan ->Shymkent
Old 11-27-2012 , 09:46   Re: [EXTENSION] MemPatch
Reply With Quote #7

game description changes only on plugin start and never later even native RestoreMemoryPatch does nothing on it
Despirator is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 11-27-2012 , 09:55   Re: [EXTENSION] MemPatch
Reply With Quote #8

Quote:
Originally Posted by Despirator View Post
game description changes only on plugin start and never later even native RestoreMemoryPatch does nothing on it
If this is an Orange Box game, then SteamTools can change the game description at any time.

If this is any older game, SDKHooks can change the game description at any time.

If this is CS:S or CS:GO... er... I think SDKHooks works at plugin start.

In theory, SteamTools can be ported to work on CS:S and CS:GO, but I don't think anyone has done it yet. CS:S used to work back when it was an Orange Box game, after all.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 11-27-2012 at 09:56.
Powerlord is offline
minimoney1
SourceMod Donor
Join Date: Dec 2010
Old 11-27-2012 , 20:01   Re: [EXTENSION] MemPatch
Reply With Quote #9

Quote:
Originally Posted by Powerlord View Post
If this is an Orange Box game, then SteamTools can change the game description at any time.

If this is any older game, SDKHooks can change the game description at any time.

If this is CS:S or CS:GO... er... I think SDKHooks works at plugin start.

In theory, SteamTools can be ported to work on CS:S and CS:GO, but I don't think anyone has done it yet. CS:S used to work back when it was an Orange Box game, after all.
CS:S still works, but the new version is only compiled for Source 2009. I'm still running the older version with no problems on my server.
__________________
Need help? PM me or add me on Steam.
My Steam




Quote:
Originally Posted by Rp.KryptoNite View Post
For some reason his Plugin never worked for me ,
@credits were added
im not stealing any plugins dude its my THING
minimoney1 is offline
ph
AlliedModders Donor
Join Date: Mar 2006
Old 12-02-2012 , 17:05   Re: [EXTENSION] MemPatch
Reply With Quote #10

Can I just use the extension? Or do I need a plugin as well.
__________________
ph is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:35.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode