diff --git a/UnleashedRecomp/main.cpp b/UnleashedRecomp/main.cpp index 7cdc605d..3228d1b7 100644 --- a/UnleashedRecomp/main.cpp +++ b/UnleashedRecomp/main.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -146,6 +147,9 @@ int main(int argc, char *argv[]) timeBeginPeriod(1); #endif + if (!os::registry::Init()) + LOGN_WARNING("OS doesn't support registry"); + os::logger::Init(); bool forceInstaller = false; diff --git a/UnleashedRecomp/os/linux/process_linux.cpp b/UnleashedRecomp/os/linux/process_linux.cpp index 64fb5262..3cd37835 100644 --- a/UnleashedRecomp/os/linux/process_linux.cpp +++ b/UnleashedRecomp/os/linux/process_linux.cpp @@ -32,7 +32,7 @@ std::filesystem::path os::process::GetWorkingDirectory() // TODO bool os::process::SetWorkingDirectory(const std::filesystem::path& path) { - return false; + return chdir(path.c_str()) == 0; } bool os::process::StartProcess(const std::filesystem::path& path, const std::vector& args, std::filesystem::path work) diff --git a/UnleashedRecomp/os/linux/registry_linux.inl b/UnleashedRecomp/os/linux/registry_linux.inl index 429ba5ea..459db772 100644 --- a/UnleashedRecomp/os/linux/registry_linux.inl +++ b/UnleashedRecomp/os/linux/registry_linux.inl @@ -1,5 +1,11 @@ #include +// TODO: Implement +inline bool os::registry::Init() +{ + return false; +} + // TODO: read from file? template bool os::registry::ReadValue(const std::filesystem::path& path, const std::string& name, T& data) diff --git a/UnleashedRecomp/os/registry.h b/UnleashedRecomp/os/registry.h index d71c5944..28cd7993 100644 --- a/UnleashedRecomp/os/registry.h +++ b/UnleashedRecomp/os/registry.h @@ -2,11 +2,13 @@ namespace os::registry { - template - bool ReadValue(const std::filesystem::path& path, const std::string& name, T& data); + bool Init(); template - bool WriteValue(const std::filesystem::path& path, const std::string& name, const T& data); + bool ReadValue(const std::string& name, T& data); + + template + bool WriteValue(const std::string& name, const T& data); } #if _WIN32 diff --git a/UnleashedRecomp/os/win32/registry_win32.inl b/UnleashedRecomp/os/win32/registry_win32.inl index bffa322f..3866f4db 100644 --- a/UnleashedRecomp/os/win32/registry_win32.inl +++ b/UnleashedRecomp/os/win32/registry_win32.inl @@ -1,45 +1,19 @@ #include #include -static const std::unordered_map g_rootKeys = +inline const wchar_t* g_registryRoot = L"Software\\UnleashedRecomp"; + +inline bool os::registry::Init() { - { "HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT }, - { "HKEY_CURRENT_USER", HKEY_CURRENT_USER }, - { "HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE }, - { "HKEY_USERS", HKEY_USERS }, - { "HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG } -}; - -static HKEY ParseRootKey(const std::string& name) -{ - auto it = g_rootKeys.find(name); - - if (it == g_rootKeys.end()) - return nullptr; - - return it->second; + return true; } template -bool os::registry::ReadValue(const std::filesystem::path& path, const std::string& name, T& data) +bool os::registry::ReadValue(const std::string& name, T& data) { - auto pathStr = path.string(); - auto pathSeparator = pathStr.find('\\'); - - if (pathSeparator == std::string::npos) - return false; - - auto rootKey = pathStr.substr(0, pathSeparator); - auto subKey = pathStr.substr(pathSeparator + 1); - - HKEY hRootKey = ParseRootKey(rootKey); - - if (!hRootKey) - return false; - HKEY hKey; - if (RegOpenKeyExA(hRootKey, subKey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) + if (RegOpenKeyExW(HKEY_CURRENT_USER, g_registryRoot, 0, KEY_READ, &hKey) != ERROR_SUCCESS) return false; BYTE buffer[512]; @@ -83,30 +57,17 @@ bool os::registry::ReadValue(const std::filesystem::path& path, const std::strin } template -bool os::registry::WriteValue(const std::filesystem::path& path, const std::string& name, const T& data) +bool os::registry::WriteValue(const std::string& name, const T& data) { - auto pathStr = path.string(); - auto pathSeparator = pathStr.find('\\'); - - if (pathSeparator == std::string::npos) - return false; - - auto rootKey = pathStr.substr(0, pathSeparator); - auto subKey = pathStr.substr(pathSeparator + 1); - - HKEY hRootKey = ParseRootKey(rootKey); - - if (!hRootKey) - return false; - HKEY hKey; - if (RegCreateKeyExA(hRootKey, subKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS) + if (RegCreateKeyExW(HKEY_CURRENT_USER, g_registryRoot, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS) return false; BYTE* pData = nullptr; DWORD dataSize = 0; DWORD dataType = 0; + bool wideString = false; if constexpr (std::is_same_v) { @@ -126,12 +87,35 @@ bool os::registry::WriteValue(const std::filesystem::path& path, const std::stri dataSize = sizeof(T); dataType = REG_QWORD; } + else if constexpr (std::is_same_v) + { + pData = (BYTE*)data.c_str(); + dataSize = (wcslen((const wchar_t*)pData) + 1) * sizeof(wchar_t); + dataType = REG_SZ; + wideString = true; + } else { static_assert(false, "Unsupported data type."); } - auto result = RegSetValueExA(hKey, name.c_str(), 0, dataType, (const BYTE*)pData, dataSize); + LSTATUS result = ERROR_INVALID_FUNCTION; + if (wideString) + { + wchar_t wideName[128]; + int wideNameSize = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name.size(), wideName, sizeof(wideName)); + if (wideNameSize == 0) + { + return false; + } + + wideName[wideNameSize] = 0; + result = RegSetValueExW(hKey, wideName, 0, dataType, pData, dataSize); + } + else + { + result = RegSetValueExA(hKey, name.c_str(), 0, dataType, pData, dataSize); + } RegCloseKey(hKey); diff --git a/UnleashedRecomp/user/registry.cpp b/UnleashedRecomp/user/registry.cpp index 9e6ca087..a0263d3d 100644 --- a/UnleashedRecomp/user/registry.cpp +++ b/UnleashedRecomp/user/registry.cpp @@ -5,17 +5,10 @@ void Registry::Load() { - std::filesystem::path path = "HKEY_CURRENT_USER\\Software\\UnleashedRecomp"; - - os::registry::ReadValue(path, STR(ExecutableFilePath), ExecutableFilePath); - os::registry::ReadValue(path, STR(RootDirectoryPath), RootDirectoryPath); + os::registry::ReadValue(STR(RootDirectoryPath), RootDirectoryPath); } void Registry::Save() { - std::filesystem::path path = "HKEY_CURRENT_USER\\Software\\UnleashedRecomp"; - - ExecutableFilePath = os::process::GetExecutablePath().string(); - - os::registry::WriteValue(path, STR(ExecutableFilePath), ExecutableFilePath); + os::registry::WriteValue(STR(ExecutableFilePath), os::process::GetExecutablePath()); } diff --git a/UnleashedRecomp/user/registry.h b/UnleashedRecomp/user/registry.h index f8977500..b5e21b50 100644 --- a/UnleashedRecomp/user/registry.h +++ b/UnleashedRecomp/user/registry.h @@ -3,7 +3,6 @@ class Registry { public: - inline static std::string ExecutableFilePath; inline static std::string RootDirectoryPath; static void Load();