Skip to content

Commit

Permalink
Add OS::DLL::Version structure instead of separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ccomrade committed Jan 29, 2024
1 parent 1aa249c commit 81fab8d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 64 deletions.
6 changes: 3 additions & 3 deletions Code/Launcher/LauncherCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ void* LauncherCommon::LoadCrysisWarheadEXE()

int LauncherCommon::GetGameBuild(void* pCrySystem)
{
int gameBuild = OS::DLL::Version::GetPatch(pCrySystem);
if (gameBuild < 0)
OS::DLL::Version version;
if (!OS::DLL::GetVersion(pCrySystem, version))
{
throw StringFormat_SysError("Failed to get the game version!");
}

return gameBuild;
return version.patch;
}

void LauncherCommon::VerifyGameBuild(int gameBuild)
Expand Down
71 changes: 24 additions & 47 deletions Code/Library/OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,44 @@ const char* OS::CmdLine::GetArgValue(const char* arg, const char* defaultValue)
// Modules //
/////////////

static __declspec(noinline) const VS_FIXEDFILEINFO* GetFileInfo(void* dll)
std::size_t OS::DLL::GetPath(void* dll, char* buffer, std::size_t bufferSize)
{
std::size_t length = GetModuleFileNameA(static_cast<HMODULE>(dll), buffer, static_cast<DWORD>(bufferSize));

if (length >= bufferSize)
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
length = 0;
}

return length;
}

bool OS::DLL::GetVersion(void* dll, Version& result)
{
HRSRC resInfo = FindResourceA(static_cast<HMODULE>(dll), MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
if (!resInfo)
{
return NULL;
return false;
}

HGLOBAL resData = LoadResource(static_cast<HMODULE>(dll), resInfo);
if (!resData)
{
return NULL;
return false;
}

const void* versionRes = LockResource(resData);
if (!versionRes)
{
return NULL;
return false;
}

const void* versionResKey = static_cast<const unsigned char*>(versionRes) + 0x6;
if (std::memcmp(versionResKey, L"VS_VERSION_INFO", 0x20) != 0)
{
SetLastError(ERROR_INVALID_DATA);
return NULL;
return false;
}

const void* versionResValue = static_cast<const unsigned char*>(versionResKey) + 0x20 + 0x2;
Expand All @@ -119,51 +132,15 @@ static __declspec(noinline) const VS_FIXEDFILEINFO* GetFileInfo(void* dll)
if (fileInfo->dwSignature != 0xFEEF04BD)
{
SetLastError(ERROR_INVALID_DATA);
return NULL;
}

return fileInfo;
}

std::size_t OS::DLL::GetPath(void* dll, char* buffer, std::size_t bufferSize)
{
std::size_t length = GetModuleFileNameA(static_cast<HMODULE>(dll), buffer, static_cast<DWORD>(bufferSize));

if (length >= bufferSize)
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
length = 0;
return false;
}

return length;
}

int OS::DLL::Version::GetMajor(void* dll)
{
const VS_FIXEDFILEINFO* fileInfo = GetFileInfo(dll);

return (fileInfo) ? HIWORD(fileInfo->dwProductVersionMS) : -1;
}
result.major = HIWORD(fileInfo->dwProductVersionMS);
result.minor = LOWORD(fileInfo->dwProductVersionMS);
result.tweak = HIWORD(fileInfo->dwProductVersionLS);
result.patch = LOWORD(fileInfo->dwProductVersionLS);

int OS::DLL::Version::GetMinor(void* dll)
{
const VS_FIXEDFILEINFO* fileInfo = GetFileInfo(dll);

return (fileInfo) ? LOWORD(fileInfo->dwProductVersionMS) : -1;
}

int OS::DLL::Version::GetTweak(void* dll)
{
const VS_FIXEDFILEINFO* fileInfo = GetFileInfo(dll);

return (fileInfo) ? HIWORD(fileInfo->dwProductVersionLS) : -1;
}

int OS::DLL::Version::GetPatch(void* dll)
{
const VS_FIXEDFILEINFO* fileInfo = GetFileInfo(dll);

return (fileInfo) ? LOWORD(fileInfo->dwProductVersionLS) : -1;
return true;
}

///////////
Expand Down
29 changes: 15 additions & 14 deletions Code/Library/OS.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,23 @@ namespace OS

std::size_t GetPath(void* dll, char* buffer, std::size_t bufferSize);

namespace Version
struct Version
{
int GetMajor(void* dll);
int GetMinor(void* dll);
int GetTweak(void* dll);
int GetPatch(void* dll);
}
unsigned short major;
unsigned short minor;
unsigned short tweak;
unsigned short patch;

Version() : major(), minor(), tweak(), patch() {}
};

bool GetVersion(void* dll, Version& result);
}

namespace EXE
{
using DLL::Version;

inline void* Get()
{
return DLL::Get(NULL);
Expand All @@ -152,12 +158,9 @@ namespace OS
return DLL::GetPath(NULL, buffer, bufferSize);
}

namespace Version
inline bool GetVersion(Version& result)
{
inline int GetMajor() { return DLL::Version::GetMajor(NULL); }
inline int GetMinor() { return DLL::Version::GetMinor(NULL); }
inline int GetTweak() { return DLL::Version::GetTweak(NULL); }
inline int GetPatch() { return DLL::Version::GetPatch(NULL); }
return DLL::GetVersion(NULL, result);
}
}

Expand Down Expand Up @@ -275,9 +278,7 @@ namespace OS
unsigned short second;
unsigned short millisecond;

DateTime() : year(0), month(0), dayOfWeek(0), day(0), hour(0), minute(0), second(0), millisecond(0)
{
}
DateTime() : year(), month(), dayOfWeek(), day(), hour(), minute(), second(), millisecond() {}
};

DateTime GetCurrentDateTimeUTC();
Expand Down

0 comments on commit 81fab8d

Please sign in to comment.