-
Notifications
You must be signed in to change notification settings - Fork 2
/
dllmain.cpp
243 lines (215 loc) · 17.9 KB
/
dllmain.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <bit>
#include <IniReader.h>
#include <injector/injector.hpp>
#include "input.h"
#include "controller.h"
#define CHECK_TIMEOUT 1000 // Check controllers every n ms
bool pressed[14];
// Patches
bool PatchVirtualMemory = false;
bool PatchCalendarCrash = false;
bool PatchInput = false;
ControllerButtons MenuCodes[16];
ControllerButtons RaceCodes[20];
Controllers controllers;
int controlType = 1;
DWORD WINAPI Background(LPVOID unused)
{
while (true)
{
controllers.Tick(1);
Sleep(1);
}
}
void FixCrashOnCalendar()
{
char crashFix[] =
{
0xB0, 0x00, 0x90
};
injector::WriteMemoryRaw(0x004C14CF, crashFix, sizeof(crashFix), true); // Fix crash on calendar
}
void FixVirtualMemory()
{
char memFix[] =
{
0xEB, 0x20
};
// Fix "Juiced requires virtual memory to be enabled"
injector::MakeNOP(0x0059BE39, 2, true);
injector::WriteMemoryRaw(0x0059BE40, memFix, sizeof(memFix), true);
}
extern "C" void /*__declspec(dllexport)*/ __cdecl SetControlType(char* fmt, int i)
{
__asm push eax
controlType = i;
__asm pop eax
}
extern "C" float __declspec(dllexport) __stdcall GetAxle()
{
__asm push ecx
int a, b;
__asm mov a, edi
__asm mov b, esi
__asm pop ecx
if (controlType == ControlType::Menu)
{
switch (a)
{
case AxlesMenu::LookDown:
case AxlesMenu::LookUp:
return controllers.getValue(0, ControllerButtons::RightThumbY);
case AxlesMenu::LookLeft:
case AxlesMenu::LookRight:
return controllers.getValue(0, ControllerButtons::RightThumbX);
case 0:
return controllers.getValue(0, ControllerButtons::LeftThumbX);
case 1:
return controllers.getValue(0, ControllerButtons::RightTrigger);
}
}
else
{
switch (a)
{
case AxlesRace::Steering:
return controllers.getValue(0, ControllerButtons::LeftThumbX);
case AxlesRace::Throttle:
return controllers.getValue(0, ControllerButtons::RightTrigger);
case AxlesRace::Brake:
return controllers.getValue(0, ControllerButtons::LeftTrigger);
case AxlesRace::Reverse:
return controllers.getValue(0, RaceCodes[static_cast<int>(RaceButtons::Reverse)]);
case AxlesRace::Handbrake:
return controllers.getValue(0, RaceCodes[static_cast<int>(RaceButtons::Handbrake)]);
case AxlesRace::LookAround:
return controllers.getValue(0, ControllerButtons::RightThumbX);
case AxlesRace::LookAroundY:
return controllers.getValue(0, ControllerButtons::RightThumbY);
}
}
return 0.0f;
}
void RaceEnd()
{
SetControlType(nullptr, ControlType::Menu);
}
void RaceStart()
{
SetControlType(nullptr, ControlType::Race);
}
__declspec(noinline) int ProcessMenuInput(int key)
{
if (key >= ControllerButtons::End || key < 0)
return 0;
switch (key)
{
case MenuButtons::Down:
case MenuButtons::DownDigital:
return controllers.getPressed(0, ControllerButtons::DPadDown);
case MenuButtons::Up:
case MenuButtons::UpDigital:
return controllers.getPressed(0, ControllerButtons::DPadUp);
case MenuButtons::Left:
case MenuButtons::LeftDigital:
return controllers.getPressed(0, ControllerButtons::DPadLeft);
case MenuButtons::Right:
case MenuButtons::RightDigital:
return controllers.getPressed(0, ControllerButtons::DPadRight);
default:
return controllers.getSinglePress(0, static_cast<ControllerButtons>(MenuCodes[key]));
}
return 0;
}
__declspec(noinline) int ProcessRaceInput(int key)
{
auto button = static_cast<ControllerButtons>(RaceCodes[key]);
auto k = static_cast<RaceButtons>(key);
switch (k)
{
case RaceButtons::Horn:
case RaceButtons::LookBack:
case RaceButtons::Nitro:
return std::bit_cast<int>(controllers.getValue(0, static_cast<ControllerButtons>(RaceCodes[key])));
case RaceButtons::ChangeView:
return controllers.getSinglePress(0, button);
default:
return controllers.getPressed(0, static_cast<ControllerButtons>(RaceCodes[key]));
}
return 0;
}
extern "C" int __declspec(dllexport) __stdcall GetButton()
{
int a, b;
__asm mov a, eax
__asm mov b, edi
return controlType == ControlType::Race ? ProcessRaceInput(a) : ProcessMenuInput(a);
}
void ReadConfig()
{
CIniReader iniReader("fixes.ini");
PatchInput = iniReader.ReadBoolean("Fixes", "PatchInput", false);
PatchVirtualMemory = iniReader.ReadBoolean("Fixes", "PatchVirtualMemory", false);
PatchCalendarCrash = iniReader.ReadBoolean("Fixes", "PatchCalendarCrash", false);
MenuCodes[0] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Left", JOY_DPAD_LEFT));
//MenuCodes[1] = iniReader.ReadInteger("MenuControls", "LeftDifital", JOY_DPAD_LEFT);
MenuCodes[2] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Right", JOY_DPAD_RIGHT));
//MenuCodes[3] = iniReader.ReadInteger("MenuControls", "RightDigital", JOY_DPAD_LEFT);
MenuCodes[4] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Up", JOY_DPAD_UP));
//MenuCodes[5] = iniReader.ReadInteger("MenuControls", "UpDigital", JOY_DPAD_UP);
MenuCodes[6] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Down", JOY_DPAD_DOWN));
//MenuCodes[7] = iniReader.ReadInteger("MenuControls", "DownDigital", JOY_DPAD_DOWN);
MenuCodes[8] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Accept", JOY_BTN_A));
MenuCodes[9] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Menu9", 110));
MenuCodes[10] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "PageDown", JOY_BTN_Y));
MenuCodes[11] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Tab", JOY_BTN_X));
MenuCodes[12] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Back", JOY_BTN_B));
MenuCodes[13] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Back2", 110));
MenuCodes[14] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Menu14", 110));
MenuCodes[15] = static_cast<ControllerButtons>(iniReader.ReadInteger("MenuControls", "Menu15", 110));
RaceCodes[0] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race0", 110));
RaceCodes[1] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Pause", JOY_BTN_START));
RaceCodes[2] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "ChangeView", JOY_BTN_BACK));
RaceCodes[3] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Nitro", JOY_BTN_X));
RaceCodes[4] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Horn", JOY_DPAD_UP));
RaceCodes[5] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "LookBack", JOY_DPAD_DOWN));
RaceCodes[6] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "GearDown", JOY_BTN_B));
RaceCodes[7] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "GearUp", JOY_BTN_A));
RaceCodes[8] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Skip", 110));
RaceCodes[9] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race9", 110));
RaceCodes[10] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race10", 110));
RaceCodes[11] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race11", 110));
RaceCodes[12] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race12", 110));
RaceCodes[13] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race13", 110));
RaceCodes[14] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race14", 110));
RaceCodes[15] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Race15", 110));
RaceCodes[16] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Handbrake", 110));
RaceCodes[17] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Reverse", 110));
RaceCodes[18] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Axle2", 110));
RaceCodes[19] = static_cast<ControllerButtons>(iniReader.ReadInteger("RaceControls", "Axle3", 110));
}
int WINAPI DllMain(HMODULE hInstance, DWORD reason, LPVOID lpReserved)
{
if (reason == DLL_PROCESS_ATTACH)
{
// Clear controller mapping
memset(MenuCodes, 110, sizeof(MenuCodes));
memset(RaceCodes, 110, sizeof(RaceCodes));
for (int i = 0; i < 14; i++)
pressed[i] = false;
ReadConfig();
controlType = ControlType::Menu;
if(PatchCalendarCrash) FixCrashOnCalendar();
if(PatchVirtualMemory) FixVirtualMemory();
if (PatchInput)
{
injector::MakeJMP(0x00401640, GetAxle);
injector::MakeJMP(0x004015D0, GetButton);
injector::MakeCALL(0x0046175E, RaceStart); // Switch control type for race
injector::MakeCALL(0x00450B3B, RaceEnd); // Switch control type for race end
injector::MakeCALL(0x005BB0FE, SetControlType); // Switch control type for pause menu
}
CreateThread(0, 0, Background, NULL, 0, NULL);
}
return TRUE;
}