-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathApp.xaml.cpp
232 lines (207 loc) · 6.68 KB
/
App.xaml.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
/*
* OpenKneeboard
*
* Copyright (C) 2022 Fred Emmott <[email protected]>
*
* This program 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; version 2.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
// clang-format off
#include "pch.h"
#include "App.xaml.h"
// clang-format on
#include <Dbghelp.h>
#include <OpenKneeboard/GamesList.h>
#include <OpenKneeboard/KneeboardState.h>
#include <OpenKneeboard/OpenXRMode.h>
#include <OpenKneeboard/RuntimeFiles.h>
#include <OpenKneeboard/SHM.h>
#include <OpenKneeboard/config.h>
#include <OpenKneeboard/dprint.h>
#include <OpenKneeboard/version.h>
#include <Psapi.h>
#include <ShlObj.h>
#include <appmodel.h>
#include <fmt/chrono.h>
#include <signal.h>
#include <chrono>
#include <exception>
#include <filesystem>
#include <set>
#include "CheckDCSHooks.h"
#include "CheckRuntimeFiles.h"
#include "Globals.h"
#include "MainWindow.xaml.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Navigation;
using namespace OpenKneeboardApp::implementation;
using namespace OpenKneeboardApp;
using namespace OpenKneeboard;
#include <WindowsAppSDK-VersionInfo.h>
#include <mddbootstrap.h>
#pragma comment(lib, "Dbghelp.lib")
static std::filesystem::path gDumpDirectory;
static bool gDumped = false;
static void CreateDump(LPEXCEPTION_POINTERS exceptionPointers) {
if (gDumped) {
return;
}
gDumped = true;
#ifdef DEBUG
if (IsDebuggerPresent()) {
__debugbreak();
}
#endif
const auto processId = GetCurrentProcessId();
auto fileName = fmt::format(
L"OpenKneeboard-{:%Y%m%dT%H%M%S}-{}.{}.{}.{}-{}.dmp",
std::chrono::system_clock::now(),
Version::Major,
Version::Minor,
Version::Patch,
Version::Build,
processId);
auto filePath = (gDumpDirectory / fileName).wstring();
winrt::handle dumpFile {CreateFileW(
filePath.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL)};
MINIDUMP_EXCEPTION_INFORMATION exceptionInfo {
.ThreadId = GetCurrentThreadId(),
.ExceptionPointers = exceptionPointers,
.ClientPointers /* exception in debugger target */ = false,
};
EXCEPTION_RECORD exceptionRecord {};
CONTEXT exceptionContext {};
EXCEPTION_POINTERS fakeExceptionPointers;
if (!exceptionPointers) {
::RtlCaptureContext(&exceptionContext);
exceptionRecord.ExceptionCode = STATUS_BREAKPOINT;
fakeExceptionPointers = {&exceptionRecord, &exceptionContext};
exceptionInfo.ExceptionPointers = &fakeExceptionPointers;
}
MiniDumpWriteDump(
GetCurrentProcess(),
processId,
dumpFile.get(),
static_cast<MINIDUMP_TYPE>(
MiniDumpWithIndirectlyReferencedMemory | MiniDumpWithProcessThreadData
| MiniDumpWithUnloadedModules | MiniDumpWithThreadInfo),
&exceptionInfo,
/* user stream = */ nullptr,
/* callback = */ nullptr);
}
LONG __callback WINAPI
OnUnhandledException(LPEXCEPTION_POINTERS exceptionPointers) {
CreateDump(exceptionPointers);
return EXCEPTION_EXECUTE_HANDLER;
}
static void OnTerminate() {
CreateDump(nullptr);
abort();
}
App::App() {
InitializeComponent();
#ifdef DEBUG
UnhandledException(
[this](IInspectable const&, UnhandledExceptionEventArgs const& e) {
if (IsDebuggerPresent()) {
auto errorMessage = e.Message();
__debugbreak();
}
throw;
});
#endif
}
void App::OnLaunched(LaunchActivatedEventArgs const&) noexcept {
window = make<MainWindow>();
window.Content().as<winrt::Microsoft::UI::Xaml::FrameworkElement>().Loaded(
[=](auto&, auto&) noexcept -> winrt::fire_and_forget {
auto xamlRoot = window.Content().XamlRoot();
co_await CheckRuntimeFiles(xamlRoot);
if (gKneeboard) {
gKneeboard->GetGamesList()->StartInjector();
SetOpenXRModeWithHelperProcess(gKneeboard->GetVRConfig().mOpenXRMode);
}
co_await CheckAllDCSHooks(xamlRoot);
});
window.Activate();
}
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
wchar_t* savedGamesBuffer = nullptr;
winrt::check_hresult(
SHGetKnownFolderPath(FOLDERID_SavedGames, NULL, NULL, &savedGamesBuffer));
gDumpDirectory = std::filesystem::path {std::wstring_view {savedGamesBuffer}}
/ "OpenKneeboard";
std::filesystem::create_directories(gDumpDirectory);
SetUnhandledExceptionFilter(&OnUnhandledException);
set_terminate(&OnTerminate);
DPrintSettings::Set({
.prefix = "OpenKneeboard-WinUI3",
});
winrt::handle mutex {
CreateMutexW(nullptr, TRUE, OpenKneeboard::ProjectNameW)};
if (GetLastError() == ERROR_ALREADY_EXISTS) {
auto name = fmt::format(L"Local\\{}.hwnd", OpenKneeboard::ProjectNameW);
winrt::handle hwndFile {
OpenFileMapping(PAGE_READWRITE, FALSE, name.c_str())};
if (!hwndFile) {
return 1;
}
void* mapping
= MapViewOfFile(hwndFile.get(), FILE_MAP_READ, 0, 0, sizeof(HWND));
auto hwnd = *reinterpret_cast<HWND*>(mapping);
UnmapViewOfFile(mapping);
ShowWindow(hwnd, SW_SHOWNORMAL) && SetForegroundWindow(hwnd);
return 0;
}
UINT32 packageNameLen {0};
const bool isPackaged = GetCurrentPackageFullName(&packageNameLen, nullptr)
== ERROR_INSUFFICIENT_BUFFER;
if (!isPackaged) {
PACKAGE_VERSION minimumVersion {WINDOWSAPPSDK_RUNTIME_VERSION_UINT64};
winrt::check_hresult(MddBootstrapInitialize(
WINDOWSAPPSDK_RELEASE_MAJORMINOR,
WINDOWSAPPSDK_RELEASE_VERSION_TAG_W,
minimumVersion));
}
{
void(WINAPI * pfnXamlCheckProcessRequirements)();
auto module = ::LoadLibrary(L"Microsoft.ui.xaml.dll");
if (module) {
pfnXamlCheckProcessRequirements
= reinterpret_cast<decltype(pfnXamlCheckProcessRequirements)>(
GetProcAddress(module, "XamlCheckProcessRequirements"));
if (pfnXamlCheckProcessRequirements) {
(*pfnXamlCheckProcessRequirements)();
}
::FreeLibrary(module);
}
}
winrt::init_apartment(winrt::apartment_type::single_threaded);
::winrt::Microsoft::UI::Xaml::Application::Start([](auto&&) {
::winrt::make<::winrt::OpenKneeboardApp::implementation::App>();
});
if (!isPackaged) {
MddBootstrapShutdown();
}
return 0;
}