This repository was archived by the owner on May 25, 2020. It is now read-only.
forked from icecreammatt/MouseInjectorDolphin
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.c
More file actions
329 lines (324 loc) · 13.6 KB
/
Copy pathmain.c
File metadata and controls
329 lines (324 loc) · 13.6 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
324
325
326
327
328
329
//==========================================================================
// Mouse Injector for Dolphin
//==========================================================================
// Copyright (C) 2019-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 "main.h"
#include "memory.h"
#include "mouse.h"
#include "./games/game.h"
enum EDITINGCURRENT {EDITINGSENSITIVITY = 0, EDITINGCROSSHAIR};
static uint8_t mousetoggle = 0;
static uint8_t selectedoption = EDITINGSENSITIVITY;
static uint8_t locksettings = 0;
static uint8_t welcomed = 0;
uint8_t sensitivity = 20;
uint8_t crosshair = 3;
uint8_t invertpitch = 0;
int32_t main(void);
static void quit(void);
static void GUI_Init(void);
static void GUI_Welcome(void);
static void GUI_Interact(void);
static void GUI_Update(void);
static void GUI_ListGames(void);
static void GUI_Clear(void);
static void INI_Load(void);
static void INI_Save(const uint8_t showerror);
//==========================================================================
// Purpose: run everything like a god damn speed demon
//==========================================================================
int32_t main(void)
{
GUI_Init();
if(!MEM_Init()) // close if dolphin was not detected
{
printf("\n Mouse Injector for %s %s\n%s\n\n Dolphin not detected. Closing...", DOLPHINVERSION, BUILDINFO, LINE);
Sleep(3000);
return 0;
}
if(!MOUSE_Init()) // close if mouse was not detected
{
printf("\n Mouse Injector for %s %s\n%s\n\n Mouse not detected. Closing...", DOLPHINVERSION, BUILDINFO, LINE);
MEM_Quit();
Sleep(3000);
return 0;
}
INI_Load(); // load settings
if(!welcomed) // if not welcomed before
GUI_Welcome(); // show welcome message - wait for user input before continuing
MEM_UpdateEmuoffset(); // update emuoffset before refreshing interface
GUI_Update(); // update screen with options
atexit(quit); // set function to run when program is closed
while(1)
{
GUI_Interact(); // check hotkey input
if(mousetoggle)
{
if(GAME_Status()) // if supported game has been detected
{
MOUSE_Update(GAME_Tickrate()); // update xmouse and ymouse vars so injection use latest mouse input
GAME_Inject(); // inject mouselook to game
}
else // dolphin has no game loaded or game not found, wait 100 ms and try again
{
MEM_UpdateEmuoffset();
Sleep(100);
}
}
Sleep(GAME_Tickrate());
}
return 0;
}
//==========================================================================
// Purpose: run when quit
//==========================================================================
static void quit(void)
{
INI_Save(0);
MOUSE_Quit();
MEM_Quit();
}
//==========================================================================
// Purpose: initialize console
//==========================================================================
static void GUI_Init(void)
{
SetConsoleTitle("Mouse Injector");
system("mode 80, 25"); // set window height and width
}
//==========================================================================
// Purpose: prints the welcome message
// Changed Globals: welcomed
//==========================================================================
static void GUI_Welcome(void)
{
printf("\n Mouse Injector for %s %s\n%s\n\n Addendum - Please Read before Use\n\n\n", DOLPHINVERSION, BUILDINFO, LINE);
printf(" 1) This is a unfinished alpha - expect issues and crashes\n\n");
printf(" 2) This only works with the included official %s build\n\n", DOLPHINVERSION);
printf(" 3) Please do not install over different versions of Dolphin\n\n");
printf(" 4) All sub-systems are unsupported - use arrow keys for sentries/cameras\n\n");
printf(" 5) NetPlay is unsupported - it will not synchronize memory injection\n\n");
printf(" 6) Press insert in the main menu to list supported games (NTSC only)\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);
while(!welcomed)
{
if(K_CTRL1) // if user pressed CTRL+1
welcomed = 1;
Sleep(250);
}
}
//==========================================================================
// Purpose: checks keyboard and will update internal values
// Changed Globals: mousetoggle, selectedoption, invertpitch, sensitivity, crosshair, locksettings
//==========================================================================
static void GUI_Interact(void)
{
uint8_t updateinterface = 0, updatequick = 0;
if(K_4) // mouse toggle (4)
{
MOUSE_Lock();
MOUSE_Update(GAME_Tickrate());
mousetoggle = !mousetoggle;
updateinterface = 1;
}
if(K_5 && !locksettings && !updateinterface) // mouse sensitivity (5)
{
selectedoption = EDITINGSENSITIVITY;
updateinterface = 1;
}
if(K_6 && !locksettings && !updateinterface && GAME_CrosshairSwaySupported()) // 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 < 200)
sensitivity++, updateinterface = 1;
if(selectedoption == EDITINGCROSSHAIR && crosshair < 18 && GAME_CrosshairSwaySupported())
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 && GAME_CrosshairSwaySupported())
crosshair--, updateinterface = 1;
updatequick = 1;
}
if(K_INSERT && !locksettings && !updateinterface && !mousetoggle) // show list of supported games (INSERT)
{
GUI_ListGames();
Sleep(10 * 1000); // wait 10 seconds
K_INSERT; // flush input
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();
GAME_Status(); // refresh driver for title
printf("\n Mouse Injector for %s %s\n", GAME_Name(), 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: ");
if(GAME_CrosshairSwaySupported())
printf(crosshair ? "%d%%" : "Locked", crosshair * 100 / 6);
else
printf("Not Available For Game");
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");
}
if(mousetoggle || locksettings)
printf(" Note: [+/-] to Change Values\n%s\n", LINE);
else
printf(" Note: [+/-] to Change Values - [Insert] for Supported Games\n%s\n", LINE);
}
//==========================================================================
// Purpose: print list of supported games
//==========================================================================
// Driver quality report
// TS2: camera/sentry interfaces not supported (hardcoded per level setup)
// TS3: all non-first person interfaces are not supported (many are hardcoded)
// NF: vehicle interface is semi-functional (fudged min/max) - last level is broken
// MOHF: works except for minecart level (level hardcode that breaks turret support)
// MOHER: fully supported
// MOHRS: looking down scope while in turret interface is broken (needs assembly hack)
// CODBRO: fully supported
// DHV: aiming sometimes breaks, sentry interface not supported
// SERIOUS: vehicle/submarine interfaces not supported
//==========================================================================
static void GUI_ListGames(void)
{
GUI_Clear();
printf("\n Supported Games (NTSC Only)\t\tGame IDs Mouse Support\n%s\n\n", LINE);
printf(" TimeSplitters 2\t\t\t GTSE4F\t\t Fair\n\n");
printf(" TimeSplitters: Future Perfect\t G3FE69\t\t Poor\n\n");
printf(" 007: NightFire\t\t\t GO7E69\t\t Poor\n\n");
printf(" Medal of Honor: Frontline\t\t GMFE69\t\t Fair\n\n");
printf(" Medal of Honor: European Assault\t GONE69\t\t Good\n\n");
printf(" Medal of Honor: Rising Sun\t\t GR8E69\t\t Poor\n\n");
printf(" Call of Duty 2: Big Red One\t\t GQCE52\t\t Good\n\n");
printf(" Die Hard: Vendetta\t\t\t GDIE7D\t\t Fair\n\n");
printf(" Serious Sam: Next Encounter\t\t G3BE9G\t\t Fair\n\n");
printf(" Returning to Main Menu in 10 Seconds...\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 exists
{
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, 200);
crosshair = ClampInt(atoi(line[1]), 0, 18);
invertpitch = !(!atoi(line[2]));
locksettings = !(!atoi(line[3]));
welcomed = !(!atoi(line[4]));
}
else
{
MessageBox(HWND_DESKTOP, "Loading mouseinjector.ini failed!\n\nUnknown values detected, loading default settings.", "Error", MB_ICONERROR | MB_OK); // tell the user loading mouseinjector.ini failed
INI_Save(1); // overwrite mouseinjector.ini with valid settings
}
}
else // if loading file failed
{
MessageBox(HWND_DESKTOP, "Loading mouseinjector.ini failed!\n\nAttempting to create mouseinjector.ini file.", "Error", MB_ICONERROR | MB_OK); // tell the user loading mouseinjector.ini failed
INI_Save(1); // create mouseinjector.ini
}
}
//==========================================================================
// Purpose: saves current settings to mouseinjector.ini
//==========================================================================
static void INI_Save(const uint8_t showerror)
{
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 exists
{
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
}
else if(showerror) // if saving file failed
MessageBox(HWND_DESKTOP, "Saving mouseinjector.ini failed!", "Error", MB_ICONERROR | MB_OK); // tell the user saving mouseinjector.ini failed
}