-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
431 lines (373 loc) · 12.2 KB
/
main.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <windows.h>
#include <commctrl.h>
#include <shlobj.h>
#include <stdio.h>
#include "resource.h"
#include <string>
#include <fstream>
HINSTANCE hInst;
enum EmuType
{
FCEUX,
Mesen,
Bizhawk,
Generic
};
struct NesSetup
{
EmuType m_Type = FCEUX;
std::string m_EmuPath;
std::string m_RomPath;
std::string m_EasyConnectInfo;
std::string m_CommandLine;
void Save()
{
CreateFolder("");
WriteToFile(GetSetupFileName());
}
void Load()
{
ReadFromFile(GetSetupFileName());
}
void CreateFolder(const std::string& data)
{
std::string folder = GetDocumentFolder();
CreateDirectory(folder.c_str(), NULL);
if (data != "")
{
folder.append(data);
CreateDirectory(folder.c_str(), NULL);
}
}
void WriteToFile(const std::string& data)
{
std::ofstream out(data.c_str());
out << "EmuType=" << (int)m_Type << std::endl;
out << "EmuPath=" << m_EmuPath << std::endl;
out << "RomPath=" << m_RomPath << std::endl;
out << "EasyConnectInfo=" << m_EasyConnectInfo << std::endl;
}
void ReadFromFile(const std::string& data)
{
std::ifstream in(data.c_str());
std::string line;
while (std::getline(in, line))
{
auto index = line.find_first_of('=');
if (index != std::string::npos)
{
std::string first = line.substr(0, index);
std::string second = line.substr(index + 1);
if (first == "EmuType")
{
m_Type = (EmuType)atoi(second.c_str());
}
else if (first == "EmuPath")
{
m_EmuPath = second;
}
else if (first == "RomPath")
{
m_RomPath = second;
}
else if (first == "EasyConnectInfo")
{
m_EasyConnectInfo = second;
}
}
}
if (m_CommandLine != "")
{
m_EasyConnectInfo = m_CommandLine;
}
}
std::string GetEmuFolder()
{
auto lastSlash = m_EmuPath.find_last_of("/\\");
if (lastSlash == std::string::npos)
{
return "";
}
return m_EmuPath.substr(0, lastSlash);
}
std::string GetDocumentFolder()
{
char my_documents[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);
std::string ret = ".";
if (result == S_OK)
{
ret = my_documents;
}
ret.append("/NesTrisLauncher");
return ret;
}
std::string GetSetupFileName()
{
std::string ret = GetDocumentFolder();
ret.append("/config.ini");
return ret;
}
void WriteAllLuaScript()
{
// this needs to happen in the right order, because it only adds one level per time
CreateFolder("/lua");
CreateFolder("/lua/nestrischamps");
CreateFolder("/lua/nestrischamps/lib");
CreateFolder("/lua/nestrischamps/emus");
WriteLuaScript(IDR_Nestrischamps, "/lua", "nestrischamps.lua");
WriteLuaScript(IDR_FrameManager, "/lua/nestrischamps", "frameManager.lua");
WriteLuaScript(IDR_Getters, "/lua/nestrischamps", "getters.lua");
WriteLuaScript(IDR_Playfield, "/lua/nestrischamps", "playfield.lua");
WriteLuaScript(IDR_Util, "/lua/nestrischamps", "util.lua");
WriteLuaScript(IDR_Variables, "/lua/nestrischamps", "variables.lua");
WriteLuaScript(IDR_Lib_Bit, "/lua/nestrischamps/lib", "bit.lua");
WriteLuaScript(IDR_Lib_Websocket, "/lua/nestrischamps/lib", "websocket.lua");
WriteLuaScript(IDR_Emus_Bizhawk, "/lua/nestrischamps/emus", "bizhawk.lua");
WriteLuaScript(IDR_Emus_Fceux, "/lua/nestrischamps/emus", "fceux.lua");
WriteLuaScript(IDR_Emus_Mesen, "/lua/nestrischamps/emus", "mesen.lua");
}
void WriteLuaScript(UINT res, const std::string& folder, const std::string& file)
{
std::string FullFileName = GetDocumentFolder();
FullFileName.append(folder);
FullFileName.append("/");
FullFileName.append(file);
HRSRC hRes = FindResource(NULL,
MAKEINTRESOURCE(res),
"DATA");
HGLOBAL hResourceLoaded = LoadResource(NULL, hRes);
char* lpResLock = (char *)LockResource(hResourceLoaded);
DWORD dwSizeRes = SizeofResource(NULL, hRes);
std::ofstream out;
out.open(FullFileName.c_str(), std::ios::out|std::ios::binary|std::ios::trunc);
out.write((const char *) lpResLock, dwSizeRes);
}
void StartEmulator()
{
switch (m_Type)
{
case FCEUX:
StartFCEUX();
break;
case Mesen:
StartMesen();
break;
case Bizhawk:
StartBizhawk();
break;
case Generic:
StartGeneric();
break;
}
}
void StartFCEUX()
{
std::string luaScript = GetDocumentFolder();
luaScript.append("/lua/nestrischamps.lua");
std::string luaConfig = GetDocumentFolder();
luaConfig.append("/lua/easyconnect.lua");
{
std::ofstream out;
out.open(luaConfig.c_str(), std::ios::out|std::ios::trunc);
out << "DEFAULTEASYCONNECT= \"" << m_EasyConnectInfo << "\"" << std::endl;
}
std::string param;
param.append("-lua \"");
param.append(luaScript);
param.append("\" \"");
param.append(m_RomPath);
param.append("\"");
ShellExecute(
NULL,
NULL,
m_EmuPath.c_str(),
param.c_str(),
GetEmuFolder().c_str(),
SW_SHOW);
}
void StartMesen()
{
std::string luaFolder = GetDocumentFolder();
luaFolder.append("/lua");
std::string luaScript = luaFolder;
luaScript.append("/nestrischamps.lua");
std::string luaConfig = luaFolder;
luaConfig.append("/easyconnect.lua");
std::string mesenExecutableCopy = GetDocumentFolder();
mesenExecutableCopy.append("/mesen.exe");
{
std::ifstream src(m_EmuPath.c_str(), std::ios::binary);
std::ofstream dest(mesenExecutableCopy.c_str(), std::ios::binary);
dest << src.rdbuf();
}
{
std::ofstream out;
out.open(luaConfig.c_str(), std::ios::out|std::ios::trunc);
out << "DEFAULTEASYCONNECT= \"" << m_EasyConnectInfo << "\"" << std::endl;
}
std::string param;
param.append("\"");
param.append(m_RomPath);
param.append("\" \"");
param.append(luaScript);
param.append("\"");
ShellExecute(
NULL,
NULL,
mesenExecutableCopy.c_str(),
param.c_str(),
GetDocumentFolder().c_str(),
SW_SHOW);
}
void StartBizhawk()
{
std::string luaFolder = GetDocumentFolder();
luaFolder.append("/lua");
std::string luaScript = luaFolder;
luaScript.append("/nestrischamps.lua");
std::string luaConfig = luaFolder;
luaConfig.append("/easyconnect.lua");
{
std::ofstream out;
out.open(luaConfig.c_str(), std::ios::out|std::ios::trunc);
out << "DEFAULTEASYCONNECT= \"" << m_EasyConnectInfo << "\"" << std::endl;
}
std::string param;
param.append("\"");
param.append(m_RomPath);
param.append("\" --lua=\"");
param.append(luaScript);
param.append("\"");
ShellExecute(
NULL,
NULL,
m_EmuPath.c_str(),
param.c_str(),
GetEmuFolder().c_str(),
SW_SHOW);
}
void StartGeneric()
{
std::string param;
param.append("\"");
param.append(m_RomPath);
param.append("\" \"");
param.append(m_EasyConnectInfo);
param.append("\" \"");
param.append(GetDocumentFolder());
param.append("\"");
ShellExecute(
NULL,
NULL,
m_EmuPath.c_str(),
param.c_str(),
GetEmuFolder().c_str(),
SW_SHOW);
}
};
NesSetup g_setupData;
void BrowseFile(
HWND hwndOwner,
UINT dlgItem,
const char* title,
const char* filter)
{
OPENFILENAME ofn = {0}; // common dialog box structure
char textBuf[2048]= {0};
GetDlgItemText(hwndOwner, dlgItem, (LPSTR)&textBuf, 2048);
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hInstance = NULL;
ofn.hwndOwner = hwndOwner;
ofn.lpstrFile = textBuf;
ofn.nMaxFile = 2047;
ofn.lpstrFilter = filter;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrTitle = title;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_NOVALIDATE;
if (GetOpenFileName(&ofn) == TRUE)
{
SetDlgItemText(hwndOwner, dlgItem, textBuf);
}
}
BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
g_setupData.Load();
SendDlgItemMessage(hwndDlg, EMUCOMBO, CB_ADDSTRING, 0, (LPARAM)"FCEUX");
SendDlgItemMessage(hwndDlg, EMUCOMBO, CB_ADDSTRING, 0, (LPARAM)"Mesen");
SendDlgItemMessage(hwndDlg, EMUCOMBO, CB_ADDSTRING, 0, (LPARAM)"Bizhawk");
SendDlgItemMessage(hwndDlg, EMUCOMBO, CB_ADDSTRING, 0, (LPARAM)"Generic");
SendDlgItemMessage(hwndDlg, EMUCOMBO, CB_SETCURSEL, g_setupData.m_Type, 0);
SendDlgItemMessage(hwndDlg, EMUPATH, EM_SETLIMITTEXT, 2047, 0);
SetDlgItemText(hwndDlg, EMUPATH, g_setupData.m_EmuPath.substr(0, 2046).c_str());
SendDlgItemMessage(hwndDlg, ROMPATH, EM_SETLIMITTEXT, 2047, 0);
SetDlgItemText(hwndDlg, ROMPATH, g_setupData.m_RomPath.substr(0, 2046).c_str());
SendDlgItemMessage(hwndDlg, CONNECT, EM_SETLIMITTEXT, 2047, 0);
SetDlgItemText(hwndDlg, CONNECT, g_setupData.m_EasyConnectInfo.substr(0, 2046).c_str());
}
return TRUE;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case STARTBUT:
case SAVEBUT:
{
g_setupData.m_Type = (EmuType)SendDlgItemMessage(hwndDlg, EMUCOMBO, CB_GETCURSEL, 0, 0);
char textBuf[2048];
GetDlgItemText(hwndDlg, EMUPATH, (LPSTR)&textBuf, 2048);
g_setupData.m_EmuPath = textBuf;
GetDlgItemText(hwndDlg, ROMPATH, (LPSTR)&textBuf, 2048);
g_setupData.m_RomPath = textBuf;
GetDlgItemText(hwndDlg, CONNECT, (LPSTR)&textBuf, 2048);
g_setupData.m_EasyConnectInfo = textBuf;
g_setupData.Save();
g_setupData.WriteAllLuaScript();
if (LOWORD(wParam) == STARTBUT)
{
g_setupData.StartEmulator();
}
}
EndDialog(hwndDlg, 0);
break;
case EMUBUT:
BrowseFile(
hwndDlg,
EMUPATH,
"Select emulator executable",
"Executable (*.exe)\0*.exe\0Batch File (*.bat)\0*.bat\0All Files (*.*)\0*.*\0");
break;
case ROMBUT:
BrowseFile(
hwndDlg,
ROMPATH,
"Select Nes Tetris ROM",
"Nes ROM File (*.exe)\0*.nes\0All Files (*.*)\0*.*\0");
break;
}
}
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
g_setupData.m_CommandLine = lpCmdLine;
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}