-
Notifications
You must be signed in to change notification settings - Fork 3
/
startup.cpp
303 lines (269 loc) · 7.48 KB
/
startup.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
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
#include "asspull.h"
#include "resource.h"
#include <io.h>
#include <fcntl.h>
#include <commctrl.h>
#include <ShlObj.h>
#include <time.h>
extern "C" {
#include "musashi/m68k.h"
}
CSimpleIni ini;
extern unsigned int biosSize, romSize;
extern int firstDiskDrive;
extern void LoadROM(const WCHAR* path);
extern void MainLoop();
extern FILE* logFile;
extern SDL_GameControllerButton buttonMap[12];
const WCHAR* buttonNames[] =
{
L"up", L"right", L"down", L"left",
L"a", L"b", L"x", L"y",
L"leftshoulder", L"rightshoulder",
L"start", L"select",
};
const WCHAR* buttonDefaults[] =
{
L"dpup", L"dpright", L"dpdown", L"dpleft",
L"a", L"b", L"x", L"y",
L"leftshoulder", L"rightshoulder",
L"start", L"back",
};
WCHAR paramLoad[512] = { 0 };
struct {
WCHAR code[16];
int num;
} langs[64];
int langCt = 0;
int langID = 0;
extern void AssociateFiletypes();
BOOL CALLBACK GetLangProc(HMODULE mod, LPCTSTR type, LPCTSTR name, WORD lang, LONG_PTR lParam)
{
langs[langCt].num = lang;
GetLocaleInfo(MAKELCID(lang, SORT_DEFAULT), LOCALE_SNAME, langs[langCt].code, 16);
langCt++;
if (langCt == 64)
return FALSE;
return TRUE;
}
void GetSettings()
{
ini.SetSpaces(false);
ini.SetMultiKey(false);
ini.SetMultiLine(false);
ini.SetUnicode(true);
PWSTR path = NULL;
auto res = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &path); res;
wcscpy(UI::settingsFile, L"Clunibus.ini");
auto atts = GetFileAttributes(UI::settingsFile);
if (atts == INVALID_FILE_ATTRIBUTES)
{
wcscpy(UI::settingsFile, path);
wcscat(UI::settingsFile, L"\\Clunibus.ini");
atts = GetFileAttributes(UI::settingsFile);
}
CoTaskMemFree(path);
ini.LoadFile(UI::settingsFile);
UI::fpsCap = ini.GetBoolValue(L"video", L"fpsCap", false);
UI::fpsVisible = ini.GetBoolValue(L"video", L"showFps", true);
UI::reloadROM = ini.GetBoolValue(L"misc", L"reloadRom", false);
UI::reloadIMG = ini.GetBoolValue(L"misc", L"reloadImg", false);
key2joy = (int)ini.GetBoolValue(L"input", L"key2joy", true);
Discord::enabled = ini.GetBoolValue(L"misc", L"discord", false);
EnumResourceLanguages(NULL, RT_MENU, MAKEINTRESOURCE(IDR_MAINMENU), GetLangProc, 0);
langID = (int)ini.GetLongValue(L"misc", L"lang", 0);
if (langID == 0)
{
//try as a string
auto locStr = ini.GetValue(L"misc", L"lang", L"en-us");
for (int i = 0; i < langCt; i++)
if (!_wcsnicmp(locStr, langs[i].code, 2))
langID = langs[i].num;
for (int i = 0; i < langCt; i++)
if (!_wcsnicmp(locStr, langs[i].code, 5))
langID = langs[i].num;
}
if (langID != 0)
{
//no need to check if a resource with this langID exists, it'll just default out.
SetThreadUILanguage(langID);
}
int argc = 0;
auto argv = CommandLineToArgvW(GetCommandLine(), &argc);
for (int i = 1; i < argc; i++)
{
if (argv[i][0] == L'-')
{
if (!wcscmp(argv[i], L"--fpscap"))
UI::fpsCap = false;
else if (!wcscmp(argv[i], L"--noreload"))
UI::reloadROM = false;
else if (!wcscmp(argv[i], L"--noremount"))
UI::reloadIMG = false;
else if (!wcscmp(argv[i], L"--nodiscord"))
Discord::enabled = false;
else if (!wcscmp(argv[i], L"--fullscreen"))
UI::startFullscreen = true;
else if (!wcscmp(argv[i], L"--poweron"))
pauseState = pauseNot;
else if (!wcscmp(argv[i], L"--associate"))
{
AssociateFiletypes();
_exit(1);
}
}
else if (i == 1)
{
wcscpy(paramLoad, argv[i]);
}
}
LocalFree(argv);
auto rtcEpoch = 441763200 - (long)time(NULL);
Registers::RTCOffset = ini.GetLongValue(L"misc", L"rtcOffset", rtcEpoch);
if (Registers::RTCOffset == rtcEpoch)
ini.SetLongValue(L"misc", L"rtcOffset", rtcEpoch);
for (int i = 0; i < 12; i++)
{
auto button = ini.GetValue(L"input", buttonNames[i], buttonDefaults[i]);
buttonMap[i] = SDL_GameControllerGetButtonFromStringW(button);
}
}
void InitializeDevices()
{
//Absolutely always load Input as #0
inputDev = new InputOutputDevice();
devices[0] = inputDev;
for (int i = 1; i < MAXDEVS; i++)
{
WCHAR key[32];
WCHAR dft[64] = { 0 };
const WCHAR* thing;
_itow(i, key, 10);
thing = ini.GetValue(L"devices", key, dft);
if (!wcslen(thing)) continue;
if (!wcscmp(thing, L"diskDrive"))
{
devices[i] = new DiskDrive(0);
//Log(L"Attached a \x1b[1mdiskette drive\x1b[0m as device \x1b[1m#%d\x1b[0m.", i);
thing = ini.GetValue(L"devices/diskDrive", key, L"");
if (UI::reloadIMG && wcslen(thing))
{
auto err = ((DiskDrive*)devices[i])->Mount(thing);
if (err)
{
if (UI::ReportLoadingFail(IDS_DISKIMAGEERROR, err, i, thing, true))
UI::EjectDisk(i);
}
//else
// Log(logNormal, L"Loaded \x1b[96m%s\x1b[0m into device \x1b[1m#%d\x1b[0m.", thing, i);
}
}
else if (!wcscmp(thing, L"hardDrive"))
{
devices[i] = new DiskDrive(1);
//Log(logNormal, L"Attached a \x1b[1mhard disk drive\x1b[0m as device \x1b[1m#%d\x1b[0m.", i);
thing = ini.GetValue(L"devices/hardDrive", key, L"");
if (UI::reloadIMG && wcslen(thing))
{
auto err = ((DiskDrive*)devices[i])->Mount(thing);
if (err)
{
if (UI::ReportLoadingFail(IDS_DISKIMAGEERROR, err, i, thing, true))
UI::EjectDisk(i);
}
//else
// Log(logNormal, L"Loaded \x1b[96m%s\x1b[0m into device \x1b[1m#%d\x1b[0m.", thing, i);
}
}
else if (!wcscmp(thing, L"linePrinter"))
{
devices[i] = new LinePrinter();
//Log(logNormal, L"Attached a \x1b[1mline printer\x1b[0m as device \x1b[1m#%d\x1b[0m.", i);
}
//else Log(logWarning, L"Don't know what \x1b[101m\"%s\"\x1b[0m is to connect as device \x1b[1m#%d\x1b[0m.", thing, i);
}
FindFirstDrive();
}
void Preload()
{
const WCHAR* thing = ini.GetValue(L"media", L"bios", L""); //"roms\\ass-bios.apb");
WCHAR thePath[FILENAME_MAX] = L"roms\\ass-bios.apb";
if (!wcslen(thing))
{
askForBIOS:
//thing = "roms\\ass-bios.apb";
if (UI::ShowFileDlg(false, thePath, 256, L"A3X BIOS files (*.apb)|*.apb"))
{
thing = thePath;
ini.SetValue(L"media", L"bios", thePath);
ini.SaveFile(UI::settingsFile, false);
}
else
{
return;
}
}
Log(logNormal, UI::GetString(IDS_LOADINGBIOS), thing);
if (LoadFile(romBIOS, thing, &biosSize))
goto askForBIOS;
biosSize = RoundUp(biosSize);
thing = ini.GetValue(L"media", L"rom", L"");
if (UI::reloadROM && wcslen(thing) && paramLoad[0] == 0)
{
//Log(logNormal, L"Loading ROM \x1b[96m%s\x1b[0m...", thing);
LoadROM(thing);
//pauseState = pauseNot;
}
else if (paramLoad[0])
{
//Log(logNormal, L"Command-line loading ROM \x1b[96m%s\x1b[0m...", paramLoad);
LoadROM((const WCHAR*)paramLoad);
pauseState = pauseNot;
}
}
#if _CONSOLE
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
#else
int main(int argc, char** argv)
#endif
{
argc, argv;
GetModuleFileName(NULL, UI::startingPath, 256);
WCHAR* lastSlash = wcsrchr(UI::startingPath, L'\\');
*lastSlash = 0;
if (!wcsncmp(lastSlash - 5, L"Debug", 6))
{
lastSlash = wcsrchr(UI::startingPath, L'\\');
*lastSlash = 0;
}
GetSettings();
#if _CONSOLE
SetConsoleCP(CP_UTF8);
auto throwAway = _setmode(_fileno(stdout), _O_U16TEXT); throwAway;
DWORD mode = 0;
GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode);
mode |= 4;
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), mode);
#endif
if (SDL_Init(SDL_INIT_VIDEO | SDL_VIDEO_OPENGL | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0)
return 0;
if (Video::Initialize() < 0)
return 0;
InitializeDevices();
UI::Initialize();
if (InitMemory() < 0)
return 0;
if (Sound::Initialize() < 0)
return 0;
Discord::Initialize();
Preload();
MainLoop();
Discord::Shutdown();
Sound::Shutdown();
Video::Shutdown();
SDL_Quit();
if (logFile != NULL)
fclose(logFile);
return 0;
}