-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.cpp
More file actions
441 lines (394 loc) · 19.1 KB
/
main.cpp
File metadata and controls
441 lines (394 loc) · 19.1 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
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
432
433
434
435
436
437
438
439
440
441
//Common launcher program
//This just launches Bridge Command or
//Map Controller executable depending
//on which button the user presses
/*
* Icons from :
* https://github.com/dubdubdubco/iconicicons.git
* CC0 Public domain
*/
#ifdef _MSC_VER
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
#include "irrlicht.h"
#include <iostream>
#include <thread>
#include "../IniFile.hpp"
#include "../Lang.hpp"
#include "../Utilities.hpp"
#include "../Constants.hpp"
//headers for execl
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#else
#include <unistd.h>
#endif
//Mac OS:
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
// Irrlicht Namespaces
//using namespace irr;
const int FONT_SIZE_DEFAULT = 12;
//Global definition for ini logger
namespace IniFile {
irr::ILogger* irrlichtLogger = 0;
}
const irr::s32 BC_BUTTON = 1;
const irr::s32 MC_BUTTON = 2;
const irr::s32 RP_BUTTON = 3;
const irr::s32 ED_BUTTON = 4;
const irr::s32 MH_BUTTON = 5;
const irr::s32 INI_BC_BUTTON = 6;
const irr::s32 INI_MC_BUTTON = 7;
const irr::s32 INI_RP_BUTTON = 8;
const irr::s32 INI_MH_BUTTON = 9;
const irr::s32 DOC_BUTTON = 10;
const irr::s32 USER_BUTTON = 11;
const irr::s32 EXIT_BUTTON = 12;
std::string userFolder;
//Event receiver: This does the actual launching
class Receiver : public irr::IEventReceiver
{
public:
Receiver() { }
virtual bool OnEvent(const irr::SEvent& event)
{
if (event.EventType == irr::EET_GUI_EVENT) {
if (event.GUIEvent.EventType == irr::gui::EGET_BUTTON_CLICKED ) {
irr::s32 id = event.GUIEvent.Caller->getID();
if (id == EXIT_BUTTON) {
exit(EXIT_SUCCESS);
}
#ifndef _WIN32
int pid = fork(); // posix only (GNU/Linux, MacOS)
if (pid > 0) return false;
#endif
if (id == BC_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-bc.exe", NULL, NULL, SW_SHOW);
//_execl("./bridgecommand-bc.exe", "bridgecommand-bc.exe", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/bc.app/Contents/MacOS/bc", "bc", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-bc", "bridgecommand-bc", NULL);
#endif
#endif
}
if (id == MC_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-mc.exe", NULL, NULL, SW_SHOW);
//_execl("./bridgecommand-mc.exe", "bridgecommand-mc.exe", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/mc.app/Contents/MacOS/mc", "mc", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-mc", "bridgecommand-mc", NULL);
#endif
#endif
}
if (id == RP_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-rp.exe", NULL, NULL, SW_SHOW);
//_execl("./bridgecommand-rp.exe", "bridgecommand-rp.exe", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/rp.app/Contents/MacOS/rp", "rp", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-rp", "bridgecommand-rp", NULL);
#endif
#endif
}
if (id == ED_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-ed.exe", NULL, NULL, SW_SHOW);
//_execl("./bridgecommand-ed.exe", "bridgecommand-ed.exe", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/ed.app/Contents/MacOS/ed", "ed", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-ed", "bridgecommand-ed", NULL);
#endif
#endif
}
if (id == MH_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-mh.exe", NULL, NULL, SW_SHOW);
//_execl("./bridgecommand-mh.exe", "bridgecommand-mh.exe", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/mh.app/Contents/MacOS/mh", "mh", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-mh", "bridgecommand-mh", NULL);
#endif
#endif
}
if (id == INI_BC_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-ini.exe", NULL, NULL, SW_SHOW);
//_execl("./bridgecommand-ini.exe", "bridgecommand-ini.exe", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/ini.app/Contents/MacOS/ini", "ini", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-ini", "bridgecommand-ini", NULL);
#endif
#endif
}
if (id == INI_MC_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-ini.exe", "-M", NULL, SW_SHOW);
//_execl("./bridgecommand-ini.exe", "bridgecommand-ini.exe", "-M", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/ini.app/Contents/MacOS/ini", "ini", "-M", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-ini", "bridgecommand-ini", "-M", NULL);
#endif
#endif
}
if (id == INI_RP_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-ini.exe", "-R", NULL, SW_SHOW);
//_execl("./bridgecommand-ini.exe", "bridgecommand-ini.exe", "-R", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/ini.app/Contents/MacOS/ini", "ini", "-R", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-ini", "bridgecommand-ini", "-R", NULL);
#endif
#endif
}
if (id == INI_MH_BUTTON) {
#ifdef _WIN32
ShellExecute(NULL, NULL, "bridgecommand-ini.exe", "-H", NULL, SW_SHOW);
//_execl("./bridgecommand-ini.exe", "bridgecommand-ini.exe", "-H", NULL);
#else
#ifdef __APPLE__
//APPLE
execl("../Helpers/ini.app/Contents/MacOS/ini", "ini", "-H", NULL);
#else
//Other (assumed posix)
execl("./bridgecommand-ini", "bridgecommand-ini", "-H", NULL);
#endif
#endif
}
if (id == DOC_BUTTON) {
#ifdef _WIN32
//CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
ShellExecute(NULL, TEXT("open"), TEXT("doc\\index.html"), NULL, NULL, SW_SHOWNORMAL);
//Sleep(5000);
//exit(EXIT_SUCCESS);
#else
#ifdef __APPLE__
//APPLE
execl("/usr/bin/open", "open", "../Resources/doc/index.html", NULL);
#else
//Other (assumed posix)
#ifdef FOR_DEB
execl("/usr/bin/xdg-open", "xdg-open", "/usr/share/doc/bridgecommand/index.html", NULL);
//If execuation gets to this point, it has failed to launch help. Try to fall back to online documentation
chdir("/usr/bin"); // If firefox is running in a snap or similar, launching can fail if it can't access the current dir
execl("/usr/bin/xdg-open", "xdg-open", "https://www.bridgecommand.co.uk/Documentation", NULL);
#else
execl("/usr/bin/xdg-open", "xdg-open", "doc/index.html", NULL);
//If execuation gets to this point, it has failed to launch help. Try to fall back to online documentation
chdir("/usr/bin"); // If firefox is running in a snap or similar, launching can fail if it can't access the current dir
execl("/usr/bin/xdg-open", "xdg-open", "https://www.bridgecommand.co.uk/Documentation", NULL);
#endif // FOR_DEB
#endif
#endif
}
if (id == USER_BUTTON) {
#ifdef _WIN32
//CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
ShellExecute(NULL, TEXT("open"), TEXT(userFolder.c_str()), NULL, NULL, SW_SHOWNORMAL);
//Sleep(5000);
//exit(EXIT_SUCCESS);
#else
#ifdef __APPLE__
//APPLE
std::cout << userFolder << std::endl;
execl("/usr/bin/open", "open", userFolder.c_str(), NULL);
#else
//Other (assumed posix)
execl("/usr/bin/xdg-open", "xdg-open", userFolder.c_str(), NULL);
#endif
#endif
}
}
}
if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
if (event.KeyInput.Key == irr::KEY_ESCAPE ) {
exit(EXIT_SUCCESS);
}
}
return false;
}
};
int main (int argc, char ** argv)
{
if ((argc>1)&&(strcmp(argv[1],"--version")==0)) {
std::cout << LONGVERSION << std::endl;
exit(EXIT_SUCCESS);
}
#ifdef FOR_DEB
chdir("/usr/share/bridgecommand");
#endif // FOR_DEB
//Mac OS:
//Find starting folder
#ifdef __APPLE__
char exePath[1024];
uint32_t pathSize = sizeof(exePath);
std::string exeFolderPath = "";
if (_NSGetExecutablePath(exePath, &pathSize) == 0) {
std::string exePathString(exePath);
size_t pos = exePathString.find_last_of("\\/");
if (std::string::npos != pos) {
exeFolderPath = exePathString.substr(0, pos);
}
}
//change up from BridgeCommand.app/Contents/MacOS to ../Resources
exeFolderPath.append("/../Resources");
//change to this path now
chdir(exeFolderPath.c_str());
//Note, we use this again after the createDevice call
#endif
//User read/write location - look in here first and the exe folder second for files
userFolder = Utilities::getUserDir();
//Read basic ini settings
std::string iniFilename = "bc5.ini";
//Use local ini file if it exists
if (Utilities::pathExists(userFolder + iniFilename)) {
iniFilename = userFolder + iniFilename;
}
std::string modifier = IniFile::iniFileToString(iniFilename, "lang");
if (modifier.length()==0) {
modifier = "en"; //Default
}
std::string languageFile = "languageLauncher-";
languageFile.append(modifier);
languageFile.append(".txt");
if (Utilities::pathExists(userFolder + languageFile)) {
languageFile = userFolder + languageFile;
}
Lang language(languageFile);
int fontSize = FONT_SIZE_DEFAULT;
float fontScale = IniFile::iniFileTof32(iniFilename, "font_scale");
if (fontScale > 1) {
fontSize = (int)(fontSize * fontScale + 0.5);
} else {
fontScale = 1.0;
}
irr::u32 graphicsWidth = 300;
irr::u32 graphicsHeight = 620;
irr::u32 graphicsDepth = 32;
bool fullScreen = false;
irr::IrrlichtDevice* device = irr::createDevice(irr::video::EDT_OPENGL, irr::core::dimension2d<irr::u32>(graphicsWidth,graphicsHeight),graphicsDepth,fullScreen,false,false,0);
irr::video::IVideoDriver* driver = device->getVideoDriver();
irr::video::ITexture* imgTexture = driver->getTexture("media/logo.png");
irr::core::dimension2d<irr::u32> imgSize = imgTexture->getSize();
driver->OnResize(irr::core::dimension2d<irr::u32>(imgSize.Width, graphicsHeight));
#ifdef __APPLE__
//Mac OS - cd back to original dir - seems to be changed during createDevice
irr::io::IFileSystem* fileSystem = device->getFileSystem();
if (fileSystem==0) {
exit(EXIT_FAILURE); //Could not get file system TODO: Message for user
std::cout << "Could not get filesystem" << std::endl;
}
fileSystem->changeWorkingDirectoryTo(exeFolderPath.c_str());
#endif
device->setWindowCaption(L"Bridge Command");
irr::gui::IGUISkin* newskin = device->getGUIEnvironment()->createSkin(irr::gui::EGST_WINDOWS_CLASSIC);
device->getGUIEnvironment()->setSkin(newskin);
std::string fontName = IniFile::iniFileToString(iniFilename, "font");
std::string fontPath = "media/fonts/" + fontName + "/" + fontName + "-" + std::to_string(fontSize) + ".xml";
irr::gui::IGUIFont *font = device->getGUIEnvironment()->getFont(fontPath.c_str());
if (font == NULL) {
std::cout << "Could not load font, using fallback" << std::endl;
} else {
//set skin default font
device->getGUIEnvironment()->getSkin()->setFont(font);
device->getGUIEnvironment()->getSkin()->setFont(device->getGUIEnvironment()->getBuiltInFont(), irr::gui::EGDF_TOOLTIP);
}
//Add launcher buttons with layout in viewport due to scaling
short bC = graphicsWidth / 20; // padding ...
short bR = bC / 3 * 2 + 1;
short bW = graphicsWidth - (2 * bC); // size ...
short bH = 20 * (fontSize / (FONT_SIZE_DEFAULT * 1.0)) + 1;
short x1 = bC; // location ...
short x2 = x1 + bW;
short y1, y2;
device->getGUIEnvironment()->addImage(imgTexture, irr::core::position2d<int>(bC, 10));
y1 = imgSize.Height + 2*bR; y2 = y1 + 2*bH;
irr::gui::IGUIButton* launchBC = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,BC_BUTTON,language.translate("startBC").c_str()); //i18n
launchBC->setImage(driver->getTexture("media/startBC.png"));
launchBC->setUseAlphaChannel();
y1 = y2 + 3*bR; y2 = y1 + bH; irr::gui::IGUIButton* launchED = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,ED_BUTTON,language.translate("startED").c_str()); //i18n
launchED->setImage(driver->getTexture("media/startED.png"));
launchED->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* launchMC = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,MC_BUTTON,language.translate("startMC").c_str()); //i18n
launchMC->setImage(driver->getTexture("media/startMC.png"));
launchMC->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* launchRP = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,RP_BUTTON,language.translate("startRP").c_str()); //i18n
launchRP->setImage(driver->getTexture("media/startRP.png"));
launchRP->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* launchMH = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,MH_BUTTON,language.translate("startMH").c_str()); //i18n
launchMH->setImage(driver->getTexture("media/startMH.png"));
launchMH->setUseAlphaChannel();
y1 = y2 + 3*bR; y2 = y1 + bH; irr::gui::IGUIButton* launchINIBC = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,INI_BC_BUTTON,language.translate("startINIBC").c_str()); //i18n
launchINIBC->setImage(driver->getTexture("media/settings.png"));
launchINIBC->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* launchINIMC = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,INI_MC_BUTTON,language.translate("startINIMC").c_str()); //i18n
launchINIMC->setImage(driver->getTexture("media/settings.png"));
launchINIMC->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* launchINIRP = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,INI_RP_BUTTON,language.translate("startINIRP").c_str()); //i18n
launchINIRP->setImage(driver->getTexture("media/settings.png"));
launchINIRP->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* launchINIMH = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,INI_MH_BUTTON,language.translate("startINIMH").c_str()); //i18n
launchINIMH->setImage(driver->getTexture("media/settings.png"));
launchINIMH->setUseAlphaChannel();
y1 = y2 + 3*bR; y2 = y1 + bH; irr::gui::IGUIButton* launchDOC = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,DOC_BUTTON,language.translate("startDOC").c_str()); //i18n
launchDOC->setImage(driver->getTexture("media/startDOC.png"));
launchDOC->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* launchFOLDER= device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,USER_BUTTON,language.translate("user").c_str()); //i18n
launchFOLDER->setImage(driver->getTexture("media/user.png"));
launchFOLDER->setUseAlphaChannel();
y1 = y2 + bR; y2 = y1 + bH; irr::gui::IGUIButton* leave = device->getGUIEnvironment()->addButton(irr::core::rect<irr::s32>(x1,y1,x2,y2),0,EXIT_BUTTON,language.translate("leave").c_str()); //i18n
leave->setImage(driver->getTexture("media/leave.png"));
leave->setUseAlphaChannel();
std::string version = "v" + LONGVERSION;
irr::core::stringw wVer(version.c_str());
y1 = y2 + bR; y2 = y1 + bH; device->getGUIEnvironment()->addStaticText(wVer.c_str(), irr::core::rect<irr::s32>(180+ wVer.size(), y1, x2, y2), true);
device->getGUIEnvironment()->setFocus(launchBC);
Receiver receiver;
device->setEventReceiver(&receiver);
#ifdef FOR_DEB
chdir("/usr/bin");
#endif // FOR_DEB
while (device->run()) {
driver->beginScene(irr::video::ECBF_COLOR | irr::video::ECBF_DEPTH, irr::video::SColor(0, 200, 200, 200));
device->getGUIEnvironment()->drawAll();
driver->endScene();
device->sleep(100);
}
return EXIT_FAILURE;
}