diff --git a/src/openrct2/cmdline/RootCommands.cpp b/src/openrct2/cmdline/RootCommands.cpp index c48c41d627..a7d1a56c2a 100644 --- a/src/openrct2/cmdline/RootCommands.cpp +++ b/src/openrct2/cmdline/RootCommands.cpp @@ -427,7 +427,7 @@ static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnum } else { - platform_remove_file_associations(); + Platform::RemoveFileAssociations(); } return EXITCODE_OK; } diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 73b1159504..cadbd3e1a3 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -37,6 +37,10 @@ # include +# if _WIN32_WINNT < 0x600 +# define swprintf_s(a, b, c, d, ...) swprintf(a, b, c, ##__VA_ARGS__) +# endif + constexpr wchar_t SOFTWARE_CLASSES[] = L"Software\\Classes"; namespace Platform @@ -366,6 +370,7 @@ namespace Platform SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr); } +# if _WIN32_WINNT >= 0x0600 static HMODULE _dllModule = nullptr; static HMODULE GetDLLModule() { @@ -382,11 +387,13 @@ namespace Platform auto progIdNameW = String::ToWideChar(progIdName); return progIdNameW; } +# endif bool SetUpFileAssociation( const std::string extension, const std::string fileTypeText, const std::string commandText, const std::string commandArgs, const uint32_t iconIndex) { +# if _WIN32_WINNT >= 0x0600 wchar_t exePathW[MAX_PATH]; wchar_t dllPathW[MAX_PATH]; @@ -469,9 +476,44 @@ namespace Platform RegCloseKey(hRootKey); return false; } +# endif return true; } + static void RemoveFileAssociation(const utf8* extension) + { +# if _WIN32_WINNT >= 0x0600 + // [HKEY_CURRENT_USER\Software\Classes] + HKEY hRootKey; + if (RegOpenKeyW(HKEY_CURRENT_USER, SOFTWARE_CLASSES, &hRootKey) == ERROR_SUCCESS) + { + // [hRootKey\.ext] + RegDeleteTreeA(hRootKey, extension); + + // [hRootKey\OpenRCT2.ext] + auto progIdName = get_progIdName(extension); + RegDeleteTreeW(hRootKey, progIdName.c_str()); + + RegCloseKey(hRootKey); + } +# endif + } + + void RemoveFileAssociations() + { + // Remove file extensions + RemoveFileAssociation(".sc4"); + RemoveFileAssociation(".sc6"); + RemoveFileAssociation(".sv4"); + RemoveFileAssociation(".sv6"); + RemoveFileAssociation(".sv7"); + RemoveFileAssociation(".td4"); + RemoveFileAssociation(".td6"); + + // Refresh explorer + SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr); + } + bool HandleSpecialCommandLineArgument(const char* argument) { return false; diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 901c2dd2e4..e1c87a4898 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -51,6 +51,7 @@ namespace Platform bool SetUpFileAssociation( const std::string extension, const std::string fileTypeText, const std::string commandText, const std::string commandArgs, const uint32_t iconIndex); + void RemoveFileAssociations(); #endif bool IsRunningInWine(); diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 839ba24fe1..7413955038 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -501,59 +501,12 @@ bool platform_process_is_elevated() } /////////////////////////////////////////////////////////////////////////////// -// File association setup +// URI protocol association setup /////////////////////////////////////////////////////////////////////////////// # define SOFTWARE_CLASSES L"Software\\Classes" # define MUI_CACHE L"Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache" -static std::wstring get_progIdName(const std::string_view& extension) -{ - auto progIdName = std::string(OPENRCT2_NAME) + std::string(extension); - auto progIdNameW = String::ToWideChar(progIdName); - return progIdNameW; -} - -static void windows_remove_file_association(const utf8* extension) -{ -# if _WIN32_WINNT >= 0x0600 - // [HKEY_CURRENT_USER\Software\Classes] - HKEY hRootKey; - if (RegOpenKeyW(HKEY_CURRENT_USER, SOFTWARE_CLASSES, &hRootKey) == ERROR_SUCCESS) - { - // [hRootKey\.ext] - RegDeleteTreeA(hRootKey, extension); - - // [hRootKey\OpenRCT2.ext] - auto progIdName = get_progIdName(extension); - RegDeleteTreeW(hRootKey, progIdName.c_str()); - - RegCloseKey(hRootKey); - } -# endif -} - -void platform_remove_file_associations() -{ - // Remove file extensions - windows_remove_file_association(".sc4"); - windows_remove_file_association(".sc6"); - windows_remove_file_association(".sv4"); - windows_remove_file_association(".sv6"); - windows_remove_file_association(".sv7"); - windows_remove_file_association(".td4"); - windows_remove_file_association(".td6"); - - // Refresh explorer - SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr); -} - -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// URI protocol association setup -/////////////////////////////////////////////////////////////////////////////// - bool platform_setup_uri_protocol() { # if _WIN32_WINNT >= 0x0600 diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index c528112c2c..3466e3a10f 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -149,7 +149,6 @@ void core_init(); # undef CreateWindow # undef GetMessage -void platform_remove_file_associations(); bool platform_setup_uri_protocol(); // This function cannot be marked as 'static', even though it may seem to be, // as it requires external linkage, which 'static' prevents