-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathmempatch.cpp
More file actions
86 lines (71 loc) · 2.28 KB
/
Copy pathmempatch.cpp
File metadata and controls
86 lines (71 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* =============================================================================
* CS2Fixes
* Copyright (C) 2023-2026 Source2ZE
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mempatch.h"
#include "common.h"
#include "khook.hpp"
#include "tier0/dbg.h"
#include "tier1/strtools.h"
#include "utils/module.h"
#include "tier0/memdbgon.h"
bool CMemPatch::PerformPatch()
{
// We're patched already
if (m_pOriginalBytes)
return true;
// If we already have an address, no need to look for it again
if (!m_pPatchAddress)
{
m_pPatchAddress = (uintptr_t)g_GameConfig->ResolveSignature(m_pSignatureName);
if (!m_pPatchAddress)
return false;
}
const char* patch = g_GameConfig->GetPatch(m_pszName);
if (!patch)
{
Panic("Failed to find patch for %s\n", m_pszName);
return false;
}
m_pPatch = g_GameConfig->IDASigToUint8Array(patch, m_iPatchLength);
if (!m_pPatch)
return false;
if (V_strcmp(m_pOffsetName, ""))
{
m_iOffset = g_GameConfig->GetOffset(m_pOffsetName);
if (m_iOffset == -1)
{
Panic("Failed to find offset %s for patch %s\n", m_pOffsetName, m_pszName);
return false;
}
m_pPatchAddress += m_iOffset;
}
m_pOriginalBytes = new byte[m_iPatchLength];
V_memcpy(m_pOriginalBytes, (void*)m_pPatchAddress, m_iPatchLength);
Plat_WriteMemory((void*)m_pPatchAddress, (byte*)m_pPatch, m_iPatchLength);
Message("Patched %s at %p\n", m_pszName, m_pPatchAddress);
return true;
}
void CMemPatch::UndoPatch()
{
if (!m_pOriginalBytes)
return;
Message("Undoing patch %s at %p\n", m_pszName, m_pPatchAddress);
Plat_WriteMemory((void*)m_pPatchAddress, m_pOriginalBytes, m_iPatchLength);
delete[] m_pOriginalBytes;
m_pOriginalBytes = nullptr;
}