This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaindll.c
More file actions
323 lines (318 loc) · 12.5 KB
/
maindll.c
File metadata and controls
323 lines (318 loc) · 12.5 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//==========================================================================
// Mouse Injector for PCSX2
//==========================================================================
// Copyright (C) 2020 Carnivorous
// All rights reserved.
//
// Mouse Injector is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// 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, visit http://www.gnu.org/licenses/gpl-2.0.html
//==========================================================================
#include <stdio.h>
#include <stdint.h>
#include <windows.h>
#include "maindll.h"
#include "hook.h"
#include "mouse.h"
#include "./games/game.h"
enum EDITINGCURRENT {EDITINGSENSITIVITY = 0, EDITINGCROSSHAIR};
static HANDLE threadhandle = 0; // thread identifier
static uint8_t mousetoggle = 0;
static uint8_t selectedoption = EDITINGSENSITIVITY;
static uint8_t locksettings = 0;
static uint8_t welcomed = 0;
static uint8_t stopthread = 1;
uint8_t sensitivity = 20;
uint8_t crosshair = 3;
uint8_t invertpitch = 0;
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
static DWORD WINAPI InjectThread();
static void GUI_Init(void);
static void GUI_Quit(void);
static void GUI_Welcome(void);
static void GUI_Interact(void);
static void GUI_Update(void);
static void GUI_Clear(void);
static void INI_Load(void);
static void INI_Save(void);
//==========================================================================
// Purpose: first called upon launch
// Changed Globals: stopthread, threadhandle
//==========================================================================
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if(fdwReason == DLL_PROCESS_ATTACH)
{
GUI_Init(); // create console window for interface
if(!HOOK_Init()) // if dll failed to load (not found/unable to read)
{
printf("\n Mouse Injector for %s %s\n%s\n\n LilyPad.dll was not found. Closing...", PCSX2VERSION, BUILDINFO, LINE);
Sleep(3000);
return FALSE;
}
if(!HOOK_LoadLilyCalls()) // if lilypad calls didn't hook successfully (missing function/incompatible API)
{
printf("\n Mouse Injector for %s %s\n%s\n\n LilyPad could not be hooked (unsupported API). Closing...", PCSX2VERSION, BUILDINFO, LINE);
Sleep(3000);
return FALSE;
}
if(!MOUSE_Init()) // if mouse was not detect
{
printf("\n Mouse Injector for %s %s\n%s\n\n Mouse not detected. Closing...", PCSX2VERSION, BUILDINFO, LINE);
Sleep(3000);
return FALSE;
}
if(stopthread) // check if thread isn't running already
{
stopthread = 0;
threadhandle = CreateThread(NULL, 0, InjectThread, NULL, 0, NULL); // start thread, return thread identifier
}
INI_Load(); // load settings
if(!welcomed) // if not welcomed before
GUI_Welcome(); // show welcome message
else
GUI_Update(); // update screen with options
}
else if(fdwReason == DLL_PROCESS_DETACH)
{
GUI_Quit();
INI_Save();
HOOK_Quit();
MOUSE_Quit();
if(!stopthread) // check if thread is running
{
stopthread = 1;
CloseHandle(threadhandle);
Sleep(50); // wait for thread to exit, 50ms is plenty of time (pcsx2 crashes if you don't wait long enough... so pls keep this here)
}
}
return TRUE;
}
//==========================================================================
// Purpose: Polls ManyMouse for input and injects into game
//==========================================================================
static DWORD WINAPI InjectThread()
{
while(!stopthread)
{
GUI_Interact(); // check hotkey input
if(mousetoggle && memready)
{
if(GAME_Status()) // if supported game has been detected
{
MOUSE_Update(); // update xmouse and ymouse vars so injection use latest mouse input
GAME_Inject(); // inject mouselook to game
}
else // pcsx2 has no game loaded or game not found, wait 100 ms and try again
Sleep(100);
}
Sleep(TICKRATE);
}
return 0;
}
//==========================================================================
// Purpose: init console window for gui
//==========================================================================
static void GUI_Init(void)
{
if(AllocConsole() != 0) // if created console
{
AttachConsole(GetCurrentProcessId()); // attach console to pcsx2 process
SetConsoleTitle("Mouse Injector");
system("mode 80, 25"); // set window height and width
freopen("CON", "w", stdout);
}
GUI_Clear();
}
//==========================================================================
// Purpose: free and close console window
//==========================================================================
static void GUI_Quit(void)
{
FreeConsole();
}
//==========================================================================
// Purpose: prints the welcome message
//==========================================================================
static void GUI_Welcome(void)
{
GUI_Clear();
printf("\n Mouse Injector for %s %s\n%s\n\n Addendum - Please Read before Use\n\n\n", PCSX2VERSION, BUILDINFO, LINE);
printf(" 1) This is a unfinished test, expect issues and crashes\n\n");
printf(" 2) Made for PCSX2 1.5.0, newer versions may not work with plugin\n\n");
printf(" 3) Only supports TimeSplitters (SLUS-20090) and Black (SLUS-21376)\n\n");
printf(" 4) You must use Windows Messaging for mouse input or injection won't work\n\n");
printf(" 5) Dynamic controller remapping is unsupported - use arrow keys for menu\n\n");
printf(" 6) Split-screen multiplayer and MapMaker are unsupported\n\n");
printf(" 7) Read readme.txt for a quick start guide - thank you and enjoy\n\n\n");
printf(" Press CTRL+1 to confirm you've read this message...\n%s\n", LINE);
}
//==========================================================================
// Purpose: checks keyboard and will update internal values
// Changed Globals: welcomed, mousetoggle, selectedoption, invertpitch, sensitivity, crosshair, locksettings
//==========================================================================
static void GUI_Interact(void)
{
if(!welcomed) // if not welcomed before
{
if(K_CTRL1) // accept welcome message (CTRL+1)
{
welcomed = 1;
GUI_Update();
}
return;
}
uint8_t updateinterface = 0, updatequick = 0;
if(K_4) // mouse toggle (4)
{
MOUSE_Lock();
MOUSE_Update();
mousetoggle = !mousetoggle;
updateinterface = 1;
}
if(K_5 && !locksettings && !updateinterface) // mouse sensitivity (5)
{
selectedoption = EDITINGSENSITIVITY;
updateinterface = 1;
}
if(K_6 && !locksettings && !updateinterface) // crosshair sensitivity (6)
{
selectedoption = EDITINGCROSSHAIR;
updateinterface = 1;
}
if(K_7 && !locksettings && !updateinterface) // invert pitch toggle (7)
{
invertpitch = !invertpitch;
updateinterface = 1;
}
if(K_PLUS && !locksettings && !updateinterface) // numpad plus (+)
{
if(selectedoption == EDITINGSENSITIVITY && sensitivity < 100)
sensitivity++, updateinterface = 1;
if(selectedoption == EDITINGCROSSHAIR && crosshair < 18)
crosshair++, updateinterface = 1;
updatequick = 1;
}
if(K_MINUS && !locksettings && !updateinterface) // numpad minus (-)
{
if(selectedoption == EDITINGSENSITIVITY && sensitivity > 1)
sensitivity--, updateinterface = 1;
if(selectedoption == EDITINGCROSSHAIR && crosshair > 0)
crosshair--, updateinterface = 1;
updatequick = 1;
}
if(K_CTRL0 && !updateinterface) // hide/show settings (CTRL+0)
{
locksettings = !locksettings;
updateinterface = 1;
}
if(updateinterface)
{
GUI_Update();
Sleep(updatequick ? 100 : 200);
}
}
//==========================================================================
// Purpose: update interface
//==========================================================================
static void GUI_Update(void)
{
GUI_Clear();
if(memready) // memory is safe to read, check for detected game
printf("\n Mouse Injector for %s %s\n", GAME_Status() ? GAME_Name() : PCSX2VERSION, BUILDINFO); // title
else
printf("\n Mouse Injector for %s %s\n", PCSX2VERSION, BUILDINFO); // title
printf("%s\n\n Main Menu - Press [#] to Use Menu\n\n\n", LINE);
printf(mousetoggle ? " [4] - [ON] Mouse Injection\n\n" : " [4] - [OFF] Mouse Injection\n\n");
if(!locksettings)
{
printf(" [5] - Mouse Sensitivity: %d%%", sensitivity * 5);
printf(selectedoption == EDITINGSENSITIVITY ? " [+/-]\n\n" : "\n\n");
printf(" [6] - Crosshair Sway: ");
printf(crosshair ? "%d%%" : "Locked", crosshair * 100 / 6);
printf(selectedoption == EDITINGCROSSHAIR ? " [+/-]\n\n" : "\n\n");
printf(invertpitch ? " [7] - [ON] Invert Pitch\n\n" : " [7] - [OFF] Invert Pitch\n\n");
printf("\n\n\n\n\n");
printf(" [CTRL+0] - Lock Settings\n\n");
}
else
{
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf(" [CTRL+0] - Unlock Settings\n\n");
}
printf(" Note: [+/-] to Change Values\n%s\n", LINE);
}
//==========================================================================
// Purpose: clear screen without using system("cls")
//==========================================================================
static void GUI_Clear(void)
{
DWORD n; // number of characters written
DWORD size; // number of visible characters
COORD coord = {0}; // top left screen position
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE consolehandle = GetStdHandle(STD_OUTPUT_HANDLE); // get a handle to the console
GetConsoleScreenBufferInfo(consolehandle, &csbi);
size = csbi.dwSize.X * csbi.dwSize.Y; // find the number of characters to overwrite
FillConsoleOutputCharacter(consolehandle, TEXT(' '), size, coord, &n); // overwrite the screen buffer with whitespace
GetConsoleScreenBufferInfo(consolehandle, &csbi);
FillConsoleOutputAttribute(consolehandle, csbi.wAttributes, size, coord, &n);
SetConsoleCursorPosition(consolehandle, coord); // reset the cursor to the top left position
}
//==========================================================================
// Purpose: loads settings stored in mouseinjector.ini
// Changes Globals: sensitivity, crosshair, invertpitch, locksettings, welcomed
//==========================================================================
static void INI_Load(void)
{
FILE *fileptr; // create a file pointer and open mouseinjector.ini from same dir as our program
if((fileptr = fopen("mouseinjector.ini", "r")) == NULL) // if the INI doesn't exist
{
INI_Save(); // create mouseinjector.ini
return;
}
char line[128][128]; // char array used for file to write to
char lines[128]; // maximum lines read size
uint8_t counter = 0; // used to assign each line to a array
while(fgets(lines, sizeof(lines), fileptr) != NULL && counter < 11) // read the first 10 lines
{
strcpy(line[counter], lines); // read file lines and assign value to line array
counter++; // add 1 to counter, so the next line can be read
}
fclose(fileptr); // close the file stream
if(counter == 5) // check if mouseinjector.ini length is valid
{
sensitivity = ClampInt(atoi(line[0]), 1, 100);
crosshair = ClampInt(atoi(line[1]), 0, 18);
invertpitch = !(!atoi(line[2]));
locksettings = !(!atoi(line[3]));
welcomed = !(!atoi(line[4]));
}
else
{
GUI_Clear();
printf("\n Mouse Injector for %s %s\n%s\n\n Loading mouseinjector.ini failed, using default settings...", PCSX2VERSION, BUILDINFO, LINE);
Sleep(3000);
INI_Save(); // overwrite mouseinjector.ini with valid settings
}
}
//==========================================================================
// Purpose: saves current settings to mouseinjector.ini
//==========================================================================
static void INI_Save(void)
{
FILE *fileptr; // create a file pointer and open mouseinjector.ini from same dir as our program
if((fileptr = fopen("mouseinjector.ini", "w")) == NULL) // if the INI doesn't exist
return;
fprintf(fileptr, "%u\n%u\n%u\n%u\n%u", sensitivity, crosshair, invertpitch, locksettings, welcomed); // write current settings to mouseinjector.ini
fclose(fileptr); // close the file stream
}