Make more stuff loggable (#885)

This commit is contained in:
kizer 2022-06-18 04:06:38 +09:00 committed by GitHub
parent cd2649fec1
commit 04cdd3338f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 15 deletions

View file

@ -26,6 +26,10 @@ namespace bootconfig {
return utils::get_env<bool>(L"DALAMUD_SHOW_CONSOLE"); return utils::get_env<bool>(L"DALAMUD_SHOW_CONSOLE");
} }
inline bool is_disable_fallback_console() {
return utils::get_env<bool>(L"DALAMUD_DISABLE_FALLBACK_CONSOLE");
}
inline bool is_wait_debugger() { inline bool is_wait_debugger() {
return utils::get_env<bool>(L"DALAMUD_WAIT_DEBUGGER"); return utils::get_env<bool>(L"DALAMUD_WAIT_DEBUGGER");
} }

View file

@ -11,18 +11,38 @@ HINSTANCE g_hGameInstance = GetModuleHandleW(nullptr);
DllExport DWORD WINAPI Initialize(LPVOID lpParam, HANDLE hMainThreadContinue) { DllExport DWORD WINAPI Initialize(LPVOID lpParam, HANDLE hMainThreadContinue) {
if (bootconfig::is_show_console()) if (bootconfig::is_show_console())
ConsoleSetup(L"Dalamud Boot"); ConsoleSetup(L"Dalamud Boot");
logging::update_dll_load_status(true);
if (const auto logFilePath = utils::get_env<std::wstring>("DALAMUD_BOOT_LOGFILE"); logFilePath.empty()) if (const auto logFilePath = utils::get_env<std::wstring>("DALAMUD_BOOT_LOGFILE"); logFilePath.empty()) {
if (!bootconfig::is_show_console() && !bootconfig::is_disable_fallback_console())
ConsoleSetup(L"Dalamud Boot - Fallback Console");
logging::I("No log file path given; not logging to file."); logging::I("No log file path given; not logging to file.");
else { } else {
try { try {
logging::start_file_logging(logFilePath, !bootconfig::is_show_console()); logging::start_file_logging(logFilePath, !bootconfig::is_show_console());
logging::I("Logging to file: {}", logFilePath); logging::I("Logging to file: {}", logFilePath);
} catch (const std::exception& e) { } catch (const std::exception& e) {
if (!bootconfig::is_show_console() && !bootconfig::is_disable_fallback_console())
ConsoleSetup(L"Dalamud Boot - Fallback Console");
logging::E("Couldn't open log file: {}", logFilePath); logging::E("Couldn't open log file: {}", logFilePath);
logging::E("Error: {} / {}", errno, e.what()); logging::E("Error: {} / {}", errno, e.what());
} }
} }
auto minHookLoaded = false;
if (const auto mhStatus = MH_Initialize(); mhStatus == MH_OK) {
logging::I("MinHook initialized.");
minHookLoaded = true;
} else if (mhStatus == MH_ERROR_ALREADY_INITIALIZED) {
logging::I("MinHook already initialized.");
minHookLoaded = true;
} else {
logging::E("Failed to initialize MinHook (status={}({}))", MH_StatusToString(mhStatus), static_cast<int>(mhStatus));
}
logging::I("Dalamud.Boot Injectable, (c) 2021 XIVLauncher Contributors"); logging::I("Dalamud.Boot Injectable, (c) 2021 XIVLauncher Contributors");
logging::I("Built at: " __DATE__ "@" __TIME__); logging::I("Built at: " __DATE__ "@" __TIME__);
@ -30,9 +50,13 @@ DllExport DWORD WINAPI Initialize(LPVOID lpParam, HANDLE hMainThreadContinue) {
if (bootconfig::wait_messagebox() & bootconfig::WaitMessageboxFlags::BeforeInitialize) if (bootconfig::wait_messagebox() & bootconfig::WaitMessageboxFlags::BeforeInitialize)
MessageBoxW(nullptr, L"Press OK to continue", L"Dalamud Boot", MB_OK); MessageBoxW(nullptr, L"Press OK to continue", L"Dalamud Boot", MB_OK);
logging::I("Applying fixes..."); if (minHookLoaded) {
xivfixes::apply_all(true); logging::I("Applying fixes...");
logging::I("Fixes OK"); xivfixes::apply_all(true);
logging::I("Fixes OK");
} else {
logging::W("Skipping fixes, as MinHook has failed to load.");
}
if (bootconfig::is_wait_debugger()) { if (bootconfig::is_wait_debugger()) {
logging::I("Waiting for debugger to attach..."); logging::I("Waiting for debugger to attach...");
@ -104,20 +128,19 @@ BOOL APIENTRY DllMain(const HMODULE hModule, const DWORD dwReason, LPVOID lpRese
switch (dwReason) { switch (dwReason) {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
g_hModule = hModule; g_hModule = hModule;
if (const auto mhStatus = MH_Initialize(); MH_OK != mhStatus) {
logging::E("Failed to initialize MinHook (status={})", static_cast<int>(mhStatus));
return FALSE;
}
logging::update_dll_load_status(true);
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
// process is terminating; don't bother cleaning up
if (lpReserved)
return TRUE;
logging::update_dll_load_status(false); logging::update_dll_load_status(false);
xivfixes::apply_all(false); xivfixes::apply_all(false);
if (const auto mhStatus = MH_Uninitialize(); MH_OK != mhStatus) { MH_DisableHook(MH_ALL_HOOKS);
if (const auto mhStatus = MH_Uninitialize(); MH_OK != mhStatus && MH_ERROR_NOT_INITIALIZED != mhStatus) {
logging::E("Failed to uninitialize MinHook (status={})", static_cast<int>(mhStatus)); logging::E("Failed to uninitialize MinHook (status={})", static_cast<int>(mhStatus));
__fastfail(logging::MinHookUnload); __fastfail(logging::MinHookUnload);
} }

View file

@ -212,9 +212,7 @@ void xivfixes::prevent_devicechange_crashes(bool bApply) {
s_hookWndProc.emplace("FFXIVGAME:WndProc (prevent_devicechange_crashes)", hWnd); s_hookWndProc.emplace("FFXIVGAME:WndProc (prevent_devicechange_crashes)", hWnd);
s_hookWndProc->set_detour([](HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -> LRESULT { s_hookWndProc->set_detour([](HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -> LRESULT {
if (uMsg == WM_CREATE) { if (uMsg == WM_DEVICECHANGE && wParam == DBT_DEVNODES_CHANGED) {
s_hookCreateWindowExA.reset();
} else if (uMsg == WM_DEVICECHANGE && wParam == DBT_DEVNODES_CHANGED) {
if (!GetGetInputDeviceManager(hWnd)()) { if (!GetGetInputDeviceManager(hWnd)()) {
logging::I("{} WndProc(0x{:X}, WM_DEVICECHANGE, DBT_DEVNODES_CHANGED, {}) called but the game does not have InputDeviceManager initialized; doing nothing.", LogTag, reinterpret_cast<size_t>(hWnd), lParam); logging::I("{} WndProc(0x{:X}, WM_DEVICECHANGE, DBT_DEVNODES_CHANGED, {}) called but the game does not have InputDeviceManager initialized; doing nothing.", LogTag, reinterpret_cast<size_t>(hWnd), lParam);
return 0; return 0;