1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-04 13:42:55 +01:00

Part of #11437: Refactor Win32 RemoveFileAssociations to Platform2 (#12773)

Additionally fixes missing changes from previous refactor in #12036 that
borked WinNT 5.1 support
This commit is contained in:
Michał Janiszewski
2020-08-25 23:20:24 +02:00
committed by GitHub
parent 76e7cd2c92
commit 3d12dfec3e
5 changed files with 45 additions and 50 deletions

View File

@@ -427,7 +427,7 @@ static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnum
}
else
{
platform_remove_file_associations();
Platform::RemoveFileAssociations();
}
return EXITCODE_OK;
}

View File

@@ -37,6 +37,10 @@
# include <iterator>
# 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;

View File

@@ -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();

View File

@@ -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

View File

@@ -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