forked from sims3fiend/Sims3SettingsSetter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection_patch.cpp
More file actions
125 lines (101 loc) · 3.36 KB
/
intersection_patch.cpp
File metadata and controls
125 lines (101 loc) · 3.36 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "intersection_patch.h"
#include <detours/detours.h>
#include "logger.h"
IntersectionPatch* IntersectionPatch::instance = nullptr;
// Calculate offsets at compile time
static const size_t CALLS_OFFSET = OptimizationPatch::GetCurrentWindowOffset() + OptimizationPatch::GetCallsOffset();
IntersectionPatch::IntersectionPatch()
: OptimizationPatch("Intersection", nullptr)
{
instance = this;
}
bool IntersectionPatch::Install() {
if (isEnabled) return true;
auto addr = intersectionAddr.Resolve();
if (!addr) {
LOG_ERROR("[IntersectionPatch] Could not resolve intersection address");
return false;
}
originalFunc = (void*)*addr;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
LONG error = DetourAttach(&originalFunc, OptimizedHook);
if (DetourTransactionCommit() == NO_ERROR) {
isEnabled = true;
LOG_INFO(std::format("[IntersectionPatch] Successfully installed at {:#010x}", *addr));
return true;
}
LOG_ERROR("[IntersectionPatch] Failed to install");
return false;
}
bool IntersectionPatch::Uninstall() {
if (!isEnabled) return true;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&originalFunc, OptimizedHook);
if (DetourTransactionCommit() == NO_ERROR) {
isEnabled = false;
return true;
}
return false;
}
// this is like, idk I feel vulnerable about this code so please don't make fun of it ldsahfhsajdas
__declspec(naked) bool __cdecl IntersectionPatch::OptimizedHook() {
__asm {
// Prologue
push ebp
mov ebp, esp
and esp, 0xfffffff0 // 16-byte alignment for SSE
sub esp, 0x40 // Space for XMM operations
// Load data immediately
mov eax, [ebp + 8] // param1
mov ecx, [eax] // array start
mov edx, [eax + 4] // array end
sub edx, ecx // length in bytes
// Strategic prefetch
prefetchnta[ecx]
prefetchnta[ecx + 64]
// Quick size check (common early-out)
sar edx, 3 // Convert to element count
cmp edx, 3
jbe return_true
// Check alignment for optimal path
test ecx, 0xF
jz aligned_path
// Unaligned path with LDDQU for better performance (?)
lddqu xmm0, [ecx] // Better unaligned load on modern CPUs apparently idfk
lddqu xmm1, [ecx + 16]
jmp process_data
aligned_path:
movaps xmm0, [ecx]
movaps xmm1, [ecx + 16]
process_data:
// Efficient SIMD processing
movaps xmm2, xmm0
movaps xmm3, xmm1
// Optimized shuffling
shufps xmm2, xmm2, 0x88
shufps xmm3, xmm3, 0x88
// calc differences
subps xmm2, xmm3
// Square results (using SSE multiply)
mulps xmm2, xmm2
// Fast comparison
movaps xmm4, [esp + 16]
cmpps xmm2, xmm4, 1
// Quick result extraction
movmskps eax, xmm2
test eax, eax
setnz al
// Fast return
mov esp, ebp
pop ebp
ret 4
align 16
return_true:
mov al, 1
mov esp, ebp
pop ebp
ret 4
}
}