2023-01-15 15:57:26 +01:00
|
|
|
#include "FFPlatform_private.h"
|
2023-06-29 10:31:20 +08:00
|
|
|
#include "common/io/io.h"
|
2023-02-02 19:17:51 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-05-27 22:30:38 +08:00
|
|
|
#include "util/windows/unicode.h"
|
2023-01-15 15:57:26 +01:00
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#include <shlobj.h>
|
2023-06-29 10:31:20 +08:00
|
|
|
|
2023-01-15 15:57:26 +01:00
|
|
|
static void getHomeDir(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
PWSTR pPath;
|
|
|
|
|
if(SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_Profile, KF_FLAG_DEFAULT, NULL, &pPath)))
|
|
|
|
|
{
|
2023-01-15 16:26:08 +01:00
|
|
|
ffStrbufSetWS(&platform->homeDir, pPath);
|
|
|
|
|
ffStrbufReplaceAllC(&platform->homeDir, '\\', '/');
|
|
|
|
|
ffStrbufEnsureEndsWithC(&platform->homeDir, '/');
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
CoTaskMemFree(pPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getCacheDir(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
PWSTR pPath;
|
|
|
|
|
if(SUCCEEDED(SHGetKnownFolderPath(&FOLDERID_LocalAppData, KF_FLAG_DEFAULT, NULL, &pPath)))
|
|
|
|
|
{
|
2023-01-15 16:26:08 +01:00
|
|
|
ffStrbufSetWS(&platform->cacheDir, pPath);
|
|
|
|
|
ffStrbufReplaceAllC(&platform->cacheDir, '\\', '/');
|
|
|
|
|
ffStrbufEnsureEndsWithC(&platform->cacheDir, '/');
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-01-15 16:26:08 +01:00
|
|
|
ffStrbufAppend(&platform->cacheDir, &platform->homeDir);
|
|
|
|
|
ffStrbufAppendS(&platform->cacheDir, "AppData/Local/");
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
CoTaskMemFree(pPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void platformPathAddKnownFolder(FFlist* dirs, REFKNOWNFOLDERID folderId)
|
|
|
|
|
{
|
|
|
|
|
PWSTR pPath;
|
|
|
|
|
if(SUCCEEDED(SHGetKnownFolderPath(folderId, 0, NULL, &pPath)))
|
|
|
|
|
{
|
2023-06-29 10:31:20 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateWS(pPath);
|
|
|
|
|
ffStrbufReplaceAllC(&buffer, '\\', '/');
|
|
|
|
|
ffStrbufEnsureEndsWithC(&buffer, '/');
|
|
|
|
|
if (!ffListContains(dirs, &buffer, (void*) ffStrbufEqual))
|
|
|
|
|
ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer);
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
CoTaskMemFree(pPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void platformPathAddEnvSuffix(FFlist* dirs, const char* env, const char* suffix)
|
|
|
|
|
{
|
|
|
|
|
const char* value = getenv(env);
|
|
|
|
|
if(!ffStrSet(value))
|
|
|
|
|
return;
|
|
|
|
|
|
2023-06-29 10:31:20 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateA(64);
|
|
|
|
|
ffStrbufAppendS(&buffer, value);
|
|
|
|
|
ffStrbufReplaceAllC(&buffer, '\\', '/');
|
|
|
|
|
ffStrbufEnsureEndsWithC(&buffer, '/');
|
|
|
|
|
if (suffix)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&buffer, suffix);
|
|
|
|
|
ffStrbufEnsureEndsWithC(&buffer, '/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ffPathExists(buffer.chars, FF_PATHTYPE_DIRECTORY) && !ffListContains(dirs, &buffer, (void*) ffStrbufEqual))
|
|
|
|
|
ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer);
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getConfigDirs(FFPlatform* platform)
|
|
|
|
|
{
|
2023-02-06 22:45:55 +08:00
|
|
|
if(getenv("MSYSTEM"))
|
2023-01-15 15:57:26 +01:00
|
|
|
{
|
|
|
|
|
// We are in MSYS2 / Git Bash
|
|
|
|
|
platformPathAddEnvSuffix(&platform->configDirs, "HOME", ".config/");
|
2023-06-29 10:31:20 +08:00
|
|
|
platformPathAddEnvSuffix(&platform->configDirs, "HOME", NULL);
|
2023-02-06 22:45:55 +08:00
|
|
|
platformPathAddEnvSuffix(&platform->configDirs, "MINGW_PREFIX", "etc");
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 22:45:55 +08:00
|
|
|
ffPlatformPathAddHome(&platform->configDirs, platform, ".config/");
|
2023-06-29 10:31:20 +08:00
|
|
|
platformPathAddKnownFolder(&platform->configDirs, &FOLDERID_ProgramData);
|
2023-02-06 22:45:55 +08:00
|
|
|
platformPathAddKnownFolder(&platform->configDirs, &FOLDERID_RoamingAppData);
|
|
|
|
|
platformPathAddKnownFolder(&platform->configDirs, &FOLDERID_LocalAppData);
|
|
|
|
|
ffPlatformPathAddHome(&platform->configDirs, platform, "");
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-15 16:45:16 +01:00
|
|
|
static void getDataDirs(FFPlatform* platform)
|
2023-01-15 15:57:26 +01:00
|
|
|
{
|
|
|
|
|
if(getenv("MSYSTEM") && getenv("HOME"))
|
|
|
|
|
{
|
|
|
|
|
// We are in MSYS2 / Git Bash
|
|
|
|
|
platformPathAddEnvSuffix(&platform->dataDirs, "HOME", ".local/share/");
|
2023-06-29 10:31:20 +08:00
|
|
|
platformPathAddEnvSuffix(&platform->dataDirs, "HOME", NULL);
|
2023-02-06 22:45:55 +08:00
|
|
|
platformPathAddEnvSuffix(&platform->dataDirs, "MINGW_PREFIX", "share");
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
2023-02-06 22:45:55 +08:00
|
|
|
ffPlatformPathAddHome(&platform->dataDirs, platform, ".local/share/");
|
2023-06-29 10:31:20 +08:00
|
|
|
platformPathAddKnownFolder(&platform->dataDirs, &FOLDERID_ProgramData);
|
2023-02-06 22:45:55 +08:00
|
|
|
platformPathAddKnownFolder(&platform->dataDirs, &FOLDERID_RoamingAppData);
|
|
|
|
|
platformPathAddKnownFolder(&platform->dataDirs, &FOLDERID_LocalAppData);
|
|
|
|
|
ffPlatformPathAddHome(&platform->dataDirs, platform, "");
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getUserName(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufEnsureFree(&platform->userName, 64);
|
2023-01-16 01:18:13 +08:00
|
|
|
DWORD len = (DWORD) ffStrbufGetFree(&platform->userName);
|
|
|
|
|
if(GetUserNameA(platform->userName.chars, &len))
|
|
|
|
|
platform->userName.length = (uint32_t) len;
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getHostName(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufEnsureFree(&platform->hostName, 64);
|
2023-01-16 01:18:13 +08:00
|
|
|
DWORD len = (DWORD) ffStrbufGetFree(&platform->hostName);
|
|
|
|
|
if(GetComputerNameExA(ComputerNameDnsHostname, platform->hostName.chars, &len))
|
|
|
|
|
platform->hostName.length = (uint32_t) len;
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getDomainName(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufEnsureFree(&platform->domainName, 64);
|
2023-01-16 01:18:13 +08:00
|
|
|
DWORD len = (DWORD) ffStrbufGetFree(&platform->domainName);
|
|
|
|
|
if(GetComputerNameExA(ComputerNameDnsDomain, platform->domainName.chars, &len))
|
|
|
|
|
platform->domainName.length = (uint32_t) len;
|
2023-01-15 15:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getSystemName(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&platform->systemName, "Windows_NT");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getSystemReleaseAndVersion(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
HKEY hKey;
|
|
|
|
|
if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DWORD bufSize;
|
|
|
|
|
|
|
|
|
|
char currentVersion[32];
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
DWORD currentMajorVersionNumber;
|
|
|
|
|
DWORD currentMinorVersionNumber;
|
|
|
|
|
bufSize = sizeof(currentMajorVersionNumber);
|
|
|
|
|
if(RegGetValueW(hKey, NULL, L"CurrentMajorVersionNumber", RRF_RT_REG_DWORD, NULL, ¤tMajorVersionNumber, &bufSize) == ERROR_SUCCESS &&
|
|
|
|
|
RegGetValueW(hKey, NULL, L"CurrentMinorVersionNumber", RRF_RT_REG_DWORD, NULL, ¤tMinorVersionNumber, &bufSize) == ERROR_SUCCESS
|
|
|
|
|
)
|
|
|
|
|
snprintf(currentVersion, sizeof(currentVersion), "%u.%u", (unsigned)currentMajorVersionNumber, (unsigned)currentMinorVersionNumber);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bufSize = sizeof(currentVersion);
|
|
|
|
|
if(RegGetValueA(hKey, NULL, "CurrentVersion", RRF_RT_REG_SZ, NULL, currentVersion, &bufSize) != ERROR_SUCCESS)
|
|
|
|
|
strcpy(currentVersion, "0.0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char currentBuildNumber[32];
|
|
|
|
|
bufSize = sizeof(currentBuildNumber);
|
|
|
|
|
if(RegGetValueA(hKey, NULL, "CurrentBuildNumber", RRF_RT_REG_SZ, NULL, currentBuildNumber, &bufSize) != ERROR_SUCCESS)
|
|
|
|
|
strcpy(currentBuildNumber, "0");
|
|
|
|
|
|
|
|
|
|
DWORD ubr;
|
|
|
|
|
bufSize = sizeof(ubr);
|
2023-05-27 22:30:38 +08:00
|
|
|
if(RegGetValueW(hKey, NULL, L"UBR", RRF_RT_REG_DWORD, NULL, &ubr, &bufSize) != ERROR_SUCCESS || bufSize != sizeof(ubr))
|
2023-01-15 15:57:26 +01:00
|
|
|
ubr = 0;
|
|
|
|
|
|
|
|
|
|
ffStrbufAppendF(&platform->systemRelease, "%s.%s.%u", currentVersion, currentBuildNumber, (unsigned)ubr);
|
|
|
|
|
|
|
|
|
|
ffStrbufEnsureFree(&platform->systemVersion, 256);
|
2023-01-16 01:18:13 +08:00
|
|
|
bufSize = (DWORD) ffStrbufGetFree(&platform->systemVersion);
|
|
|
|
|
if(RegGetValueA(hKey, NULL, "DisplayVersion", RRF_RT_REG_SZ, NULL, platform->systemVersion.chars, &bufSize) == ERROR_SUCCESS)
|
|
|
|
|
platform->systemVersion.length = (uint32_t) bufSize;
|
2023-01-15 15:57:26 +01:00
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getSystemArchitecture(FFPlatform* platform)
|
|
|
|
|
{
|
|
|
|
|
SYSTEM_INFO sysInfo = {0};
|
|
|
|
|
GetNativeSystemInfo(&sysInfo);
|
|
|
|
|
|
|
|
|
|
switch(sysInfo.wProcessorArchitecture)
|
|
|
|
|
{
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_AMD64:
|
2023-01-15 16:45:16 +01:00
|
|
|
ffStrbufAppendS(&platform->systemArchitecture, "x86_64");
|
2023-01-15 15:57:26 +01:00
|
|
|
break;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_IA64:
|
2023-01-15 16:45:16 +01:00
|
|
|
ffStrbufAppendS(&platform->systemArchitecture, "ia64");
|
2023-01-15 15:57:26 +01:00
|
|
|
break;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_INTEL:
|
2023-01-15 16:45:16 +01:00
|
|
|
ffStrbufAppendS(&platform->systemArchitecture, "i686");
|
2023-01-15 15:57:26 +01:00
|
|
|
break;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_ARM64:
|
2023-01-15 16:45:16 +01:00
|
|
|
ffStrbufAppendS(&platform->systemArchitecture, "aarch64");
|
2023-01-15 15:57:26 +01:00
|
|
|
break;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_ARM:
|
2023-01-15 16:45:16 +01:00
|
|
|
ffStrbufAppendS(&platform->systemArchitecture, "arm");
|
2023-01-15 15:57:26 +01:00
|
|
|
break;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_PPC:
|
2023-01-15 16:45:16 +01:00
|
|
|
ffStrbufAppendS(&platform->systemArchitecture, "ppc");
|
2023-01-15 15:57:26 +01:00
|
|
|
break;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_MIPS:
|
2023-01-15 16:45:16 +01:00
|
|
|
ffStrbufAppendS(&platform->systemArchitecture, "mips");
|
2023-01-15 15:57:26 +01:00
|
|
|
break;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_UNKNOWN:
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 16:06:50 +01:00
|
|
|
void ffPlatformInitImpl(FFPlatform* platform)
|
2023-01-15 15:57:26 +01:00
|
|
|
{
|
|
|
|
|
getHomeDir(platform);
|
|
|
|
|
getCacheDir(platform);
|
|
|
|
|
getConfigDirs(platform);
|
|
|
|
|
getDataDirs(platform);
|
|
|
|
|
|
|
|
|
|
getUserName(platform);
|
|
|
|
|
getHostName(platform);
|
|
|
|
|
getDomainName(platform);
|
|
|
|
|
|
|
|
|
|
getSystemName(platform);
|
|
|
|
|
getSystemReleaseAndVersion(platform);
|
|
|
|
|
getSystemArchitecture(platform);
|
|
|
|
|
}
|