From 983d8e54d2a3c1fc7dc8aaa9f4bfd41816af03f3 Mon Sep 17 00:00:00 2001 From: WrinklyNinja Date: Tue, 19 Aug 2014 17:32:56 +0100 Subject: [PATCH] Improved #ifdef'ing of Windows-specific code. Should help anyone wanting to do a port. --- src/backend/game.cpp | 7 ++++++- src/backend/helpers.cpp | 18 +++++++----------- src/backend/helpers.h | 6 ++++-- src/gui/app.cpp | 2 +- src/gui/handler.cpp | 10 +++++----- src/gui/main_win.cpp | 2 +- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/backend/game.cpp b/src/backend/game.cpp index 916a193e..b66e8343 100644 --- a/src/backend/game.cpp +++ b/src/backend/game.cpp @@ -283,8 +283,10 @@ namespace loot { //First look for local install, then look for Registry. if (gamePath.empty() || !fs::exists(gamePath / "Data" / _masterFile)) { - if (fs::exists(fs::path("..") / "Data" / _masterFile)) + if (fs::exists(fs::path("..") / "Data" / _masterFile)) { gamePath = ".."; +#ifdef _WIN32 + } else { string path; string key_parent = fs::path(registryKey).parent_path().string(); @@ -292,6 +294,7 @@ namespace loot { path = RegKeyStringValue("HKEY_LOCAL_MACHINE", key_parent, key_name); if (!path.empty() && fs::exists(fs::path(path) / "Data" / _masterFile)) gamePath = fs::path(path); +#endif } } @@ -314,12 +317,14 @@ namespace loot { if (fs::exists(fs::path("..") / "Data" / _masterFile)) return true; +#ifdef _WIN32 string path; string key_parent = fs::path(registryKey).parent_path().string(); string key_name = fs::path(registryKey).filename().string(); path = RegKeyStringValue("HKEY_LOCAL_MACHINE", key_parent, key_name); if (!path.empty() && fs::exists(fs::path(path) / "Data" / _masterFile)) return true; +#endif return false; } diff --git a/src/backend/helpers.cpp b/src/backend/helpers.cpp index 455d59d0..659a9455 100644 --- a/src/backend/helpers.cpp +++ b/src/backend/helpers.cpp @@ -43,7 +43,7 @@ #include #include -#if _WIN32 || _WIN64 +#ifdef _WIN32 # ifndef UNICODE # define UNICODE # endif @@ -161,9 +161,9 @@ namespace loot { return out; } +#ifdef _WIN32 //Get registry subkey value string. string RegKeyStringValue(const std::string& keyStr, const std::string& subkey, const std::string& value) { -#if _WIN32 || _WIN64 HKEY hKey, key; DWORD BufferSize = 4096; wchar_t val[4096]; @@ -193,13 +193,11 @@ namespace loot { return ""; } else return ""; -#else - return ""; -#endif } +#endif boost::filesystem::path GetLocalAppDataPath() { -#if _WIN32 || _WIN64 +#ifdef _WIN32 HWND owner = 0; TCHAR path[MAX_PATH]; @@ -210,8 +208,6 @@ namespace loot { return fs::path(path); else return fs::path(""); -#else - return fs::path(""); #endif } @@ -219,7 +215,7 @@ namespace loot { std::string ToFileURL(const fs::path& file) { BOOST_LOG_TRIVIAL(trace) << "Converting file path " << file << " to a URL."; -#if _WIN32 || _WIN64 +#ifdef _WIN32 wstring wstr(MAX_PATH, 0); DWORD len = MAX_PATH; UrlCreateFromPath(ToWinWide(file.string()).c_str(), &wstr[0], &len, NULL); @@ -230,7 +226,7 @@ namespace loot { #endif } -#if _WIN32 || _WIN64 +#ifdef _WIN32 //Helper to turn UTF8 strings into strings that can be used by WinAPI. std::wstring ToWinWide(const std::string& str) { @@ -337,7 +333,7 @@ namespace loot { : verString(ver) {} Version::Version(const fs::path& file) { -#if _WIN32 || _WIN64 +#ifdef _WIN32 DWORD dummy = 0; DWORD size = GetFileVersionInfoSize(file.wstring().c_str(), &dummy); diff --git a/src/backend/helpers.h b/src/backend/helpers.h index 301623d1..017fc6c5 100644 --- a/src/backend/helpers.h +++ b/src/backend/helpers.h @@ -51,16 +51,18 @@ namespace loot { //Converts an integer to a hex string using BOOST's Spirit.Karma. Faster than a stringstream conversion. std::string IntToHexString(const int n); +#ifdef _WIN32 //Get registry subkey value string. std::string RegKeyStringValue(const std::string& keyStr, const std::string& subkey, const std::string& value); +#endif - //Get the local application data path. + //Get the local application data path, within which LOOT's data folder should be stored. boost::filesystem::path GetLocalAppDataPath(); //Turns an absolute filesystem path into a valid file:// URL. std::string ToFileURL(const boost::filesystem::path& file); -#if _WIN32 || _WIN64 +#ifdef _WIN32 //Helper to turn UTF8 strings into strings that can be used by WinAPI. std::wstring ToWinWide(const std::string& str); diff --git a/src/gui/app.cpp b/src/gui/app.cpp index 883e63a5..b9d56cce 100644 --- a/src/gui/app.cpp +++ b/src/gui/app.cpp @@ -70,7 +70,7 @@ namespace loot { // Information used when creating the native window. CefWindowInfo window_info; -#if _WIN32 || _WIN64 +#ifdef _WIN32 // On Windows we need to specify certain flags that will be passed to CreateWindowEx(). window_info.SetAsPopup(NULL, "LOOT"); #endif diff --git a/src/gui/handler.cpp b/src/gui/handler.cpp index e4237d81..423c45b9 100644 --- a/src/gui/handler.cpp +++ b/src/gui/handler.cpp @@ -117,8 +117,8 @@ namespace loot { } else if (request == "getGameData") { BOOST_LOG_TRIVIAL(info) << "Setting LOOT window title bar text to include game name: " << g_app_state.CurrentGame().Name(); -#if defined(OS_WIN) HWND handle = browser->GetHost()->GetWindowHandle(); +#ifdef _WIN32 SetWindowText(handle, ToWinWide("LOOT: " + g_app_state.CurrentGame().Name()).c_str()); #endif @@ -234,9 +234,9 @@ namespace loot { BOOST_LOG_TRIVIAL(info) << "Changing game to that with folder: " << folder; g_app_state.ChangeGame(folder); -#if defined(OS_WIN) BOOST_LOG_TRIVIAL(info) << "Setting LOOT window title bar text to include game name: " << g_app_state.CurrentGame().Name(); HWND handle = browser->GetHost()->GetWindowHandle(); +#ifdef _WIN32 SetWindowText(handle, ToWinWide("LOOT: " + g_app_state.CurrentGame().Name()).c_str()); #endif @@ -291,7 +291,7 @@ namespace loot { text = yout.c_str(); } -#if defined(OS_WIN) +#ifdef _WIN32 if (!OpenClipboard(NULL)) { BOOST_LOG_TRIVIAL(error) << "Failed to open the Windows clipboard."; callback->Failure(-1, "Failed to open the Windows clipboard."); @@ -825,15 +825,15 @@ namespace loot { // CefDisplayHandler methods //-------------------------- -#if _WIN32 || _WIN64 void LootHandler::OnTitleChange(CefRefPtr browser, const CefString& title) { assert(CefCurrentlyOn(TID_UI)); CefWindowHandle hwnd = browser->GetHost()->GetWindowHandle(); +#ifdef _WIN32 SetWindowText(hwnd, std::wstring(title).c_str()); - } #endif + } // CefLifeSpanHandler methods //--------------------------- diff --git a/src/gui/main_win.cpp b/src/gui/main_win.cpp index 42f094d1..bb33ec30 100644 --- a/src/gui/main_win.cpp +++ b/src/gui/main_win.cpp @@ -101,7 +101,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmd // Record command line arguments. CefRefPtr command_line = CefCommandLine::CreateCommandLine(); -#if defined(OS_WIN) +#ifdef _WIN32 command_line->InitFromString(::GetCommandLineW()); #endif if (command_line->HasSwitch("game")) { // Format is: --game=