Skip to content

Commit 9815aee

Browse files
committed
Add a console failback option in case the GUI does not work
1 parent e01542f commit 9815aee

File tree

3 files changed

+113
-53
lines changed

3 files changed

+113
-53
lines changed

3rdparty/imgui/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ add_library(imgui
77
imgui/imgui_widgets.cpp
88
imgui/backends/imgui_impl_win32.cpp
99
imgui/backends/imgui_impl_vulkan.cpp
10-
imgui/imgui_demo.cpp
10+
#imgui/imgui_demo.cpp
1111
)
1212

1313
target_include_directories(imgui PUBLIC imgui)

src/CMakeLists.txt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
add_executable(EDVoice WIN32
1+
set(EDVOICE_SOURCES
22
main.cpp
33
EDVoiceApp.cpp
44
util/EliteFileUtil.cpp
55
watchers/JournalWatcher.cpp
66
watchers/StatusWatcher.cpp
7-
8-
GUI/EDVoiceGUI.cpp
9-
GUI/Vulkan/VkAdapter.cpp
10-
GUI/Vulkan/Swapchain.cpp
11-
GUI/Vulkan/VkUtil.cpp
127

138
voicepack/Enum.cpp
149
voicepack/AudioPlayer.cpp
@@ -21,19 +16,41 @@ add_executable(EDVoice WIN32
2116
../assets/edvoice.rc
2217
)
2318

19+
set(EDVOICE_SOURCES_GUI
20+
GUI/EDVoiceGUI.cpp
21+
GUI/Vulkan/VkAdapter.cpp
22+
GUI/Vulkan/Swapchain.cpp
23+
GUI/Vulkan/VkUtil.cpp
24+
)
25+
26+
# GUI version
27+
add_executable(EDVoice WIN32 ${EDVOICE_SOURCES} ${EDVOICE_SOURCES_GUI})
28+
29+
target_compile_definitions(EDVoice PRIVATE GUI_MODE)
30+
2431
target_include_directories(EDVoice PRIVATE ../3rdparty)
2532
target_include_directories(EDVoice PRIVATE ../plugins/include)
2633

2734
target_link_libraries(EDVoice PRIVATE imgui)
2835
target_compile_definitions(EDVoice PRIVATE UNICODE _UNICODE)
2936

37+
# CLI version without GUI
38+
add_executable(EDVoice-cli WIN32 ${EDVOICE_SOURCES})
39+
40+
target_include_directories(EDVoice-cli PRIVATE ../3rdparty)
41+
target_include_directories(EDVoice-cli PRIVATE ../plugins/include)
42+
43+
target_compile_definitions(EDVoice-cli PRIVATE UNICODE _UNICODE)
44+
3045
if (BUILD_MEDICORP)
31-
target_compile_definitions(EDVoice PRIVATE BUILD_MEDICORP)
3246
message(STATUS "Building with MediCorp support")
47+
48+
target_compile_definitions(EDVoice PRIVATE BUILD_MEDICORP)
49+
target_compile_definitions(EDVoice-cli PRIVATE BUILD_MEDICORP)
3350
endif()
3451

3552
# target_compile_definitions(EDVoice PRIVATE BUILD_MEDICORP)
3653
# target_compile_definitions(EDVoice PRIVATE VULKAN_DEBUG_LAYER)
3754

38-
install(TARGETS EDVoice
55+
install(TARGETS EDVoice EDVoice-cli
3956
RUNTIME DESTINATION .)

src/main.cpp

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
#include <filesystem>
33

44
#include "EDVoiceApp.h"
5-
#include "GUI/EDVoiceGUI.h"
5+
6+
#include <conio.h>
7+
#include <windows.h>
8+
9+
void run_failback_cli(
10+
const std::filesystem::path& execPath,
11+
const std::filesystem::path& configFile);
12+
13+
#ifdef GUI_MODE
614

715
// ----------------------------------------------------------------------------
816
// GUI
917
// ----------------------------------------------------------------------------
1018

11-
#if 1
12-
#include <windows.h>
19+
#include <sstream>
20+
#include "GUI/EDVoiceGUI.h"
1321

1422
int WINAPI wWinMain(
1523
_In_ HINSTANCE hInstance,
@@ -24,76 +32,111 @@ int WINAPI wWinMain(
2432
const std::filesystem::path execPath = std::filesystem::path(szArgList[0]).parent_path();
2533
const std::filesystem::path configFile = execPath / "config" / "default.json";
2634

27-
EDVoiceGUI gui(execPath, configFile, hInstance, nShowCmd);
35+
std::ostringstream local;
36+
std::cout.rdbuf(local.rdbuf());
37+
std::cerr.rdbuf(local.rdbuf());
38+
39+
bool failbackMode = false;
40+
41+
try {
42+
EDVoiceGUI gui(execPath, configFile, hInstance, nShowCmd);
43+
gui.run();
44+
} catch (const std::exception& e) {
45+
std::cerr << "[FATAL ] Exception: " << e.what() << std::endl;
46+
failbackMode = true;
47+
} catch (...) {
48+
std::cerr << "[FATAL ] Exception unknown" << std::endl;
49+
failbackMode = true;
50+
}
51+
52+
if (failbackMode) {
53+
std::ofstream out("EDVoiceCrash.log", std::ios::app);
54+
out << local.str();
55+
out.close();
56+
57+
// Clear WM_QUIT messages
58+
MSG msg;
59+
60+
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
61+
if (msg.message == WM_QUIT) continue;
62+
63+
TranslateMessage(&msg);
64+
DispatchMessage(&msg);
65+
}
66+
67+
MessageBoxA(NULL, "Could not load the GUI. A Log file EDVoiceCrash.log was created. Please send it for bug review.\nTrying to launch in failback mode...\no7", "EDVoice", MB_OK | MB_ICONERROR);
68+
69+
run_failback_cli(execPath, configFile);
70+
}
2871

29-
gui.run();
30-
3172
return 0;
3273
}
3374

34-
#else
35-
#include <windows.h>
36-
#include <fcntl.h>
37-
#include <io.h>
75+
#else // !GUI_MODE
76+
77+
// ----------------------------------------------------------------------------
78+
// Console mode
79+
// ----------------------------------------------------------------------------
3880

3981
int WINAPI wWinMain(
4082
_In_ HINSTANCE hInstance,
4183
_In_opt_ HINSTANCE hPrevInstance,
4284
_In_ LPWSTR lpCmdLine,
4385
_In_ int nShowCmd)
4486
{
45-
// --- Créer une console ---
46-
AllocConsole();
47-
48-
// Rediriger stdout, stderr et stdin vers la console
49-
FILE* fp;
50-
freopen_s(&fp, "CONOUT$", "w", stdout);
51-
freopen_s(&fp, "CONOUT$", "w", stderr);
52-
freopen_s(&fp, "CONIN$", "r", stdin);
53-
54-
std::cout << "Console initialized ✅" << std::endl;
55-
56-
// --- Parsing arguments ---
5787
LPWSTR* szArgList;
5888
int argCount;
5989
szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
6090

6191
const std::filesystem::path execPath = std::filesystem::path(szArgList[0]).parent_path();
6292
const std::filesystem::path configFile = execPath / "config" / "default.json";
6393

64-
// --- Lancer ton GUI ---
65-
EDVoiceGUI gui(execPath, configFile, hInstance, nShowCmd);
66-
gui.run();
94+
run_failback_cli(execPath, configFile);
6795

68-
// --- Libérer la console à la fermeture ---
69-
FreeConsole();
7096
return 0;
7197
}
7298

73-
#endif
99+
#endif
74100

75-
// ----------------------------------------------------------------------------
76101

77-
#if 0
102+
LRESULT CALLBACK w32WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
103+
{
104+
switch (msg) {
105+
case WM_CLOSE:
106+
::DestroyWindow(hWnd);
107+
return 0;
108+
109+
case WM_DESTROY:
110+
::PostQuitMessage(0);
111+
return 0;
112+
}
113+
114+
return ::DefWindowProcW(hWnd, msg, wParam, lParam);
115+
}
78116

79-
int main(int argc, char* argv[])
117+
118+
void run_failback_cli(
119+
const std::filesystem::path& execPath,
120+
const std::filesystem::path& configFile)
80121
{
81-
const std::filesystem::path execPath = std::filesystem::path(argv[0]).parent_path();
82-
// Load default config or the one specified in command line
83-
std::filesystem::path configFile;
122+
AllocConsole();
84123

85-
if (argc < 2) {
86-
std::cout << "Using default configuration" << std::endl;
87-
configFile = std::filesystem::current_path() / "config" / "default.json";
88-
}
89-
else {
90-
configFile = argv[1];
91-
}
124+
FILE* fp;
125+
freopen_s(&fp, "CONOUT$", "w", stdout);
126+
freopen_s(&fp, "CONOUT$", "w", stderr);
127+
freopen_s(&fp, "CONIN$", "r", stdin);
92128

93129
EDVoiceApp app(execPath, configFile);
94130

95-
app.run();
131+
MSG msg;
132+
HWND hConsole = GetConsoleWindow();
96133

97-
return 0;
134+
while (IsWindow(hConsole) && GetMessage(&msg, NULL, 0, 0)) {
135+
TranslateMessage(&msg);
136+
DispatchMessage(&msg);
137+
hConsole = GetConsoleWindow(); // Update in case the console is closed
138+
}
139+
140+
FreeConsole();
98141
}
99-
#endif
142+

0 commit comments

Comments
 (0)