Skip to content

Commit

Permalink
yoink RED4ext's version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
maximegmd committed Jan 9, 2024
1 parent 18e08a8 commit ca95e7c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 83 deletions.
146 changes: 75 additions & 71 deletions src/Image.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <stdafx.h>

#include <string>
#include <sstream>

#include "Image.h"

#include "RED4ext/Api/FileVer.hpp"


struct PdbInfo
{
DWORD Signature;
Expand All @@ -21,88 +23,90 @@ struct ImageVersion

void Image::Initialize()
{
const ImageVersion cVersions[] = {
{{0x2B, 0x4E, 0x65, 0x3D, 0xD4, 0x68, 0xC7, 0x42, 0xBF, 0xC9, 0x58, 0xDC, 0x38, 0xD4, 0x2A, 0x36}, MakeVersion(1, 4)},
{{0x93, 0x5B, 0x36, 0x35, 0xDF, 0xA8, 0xE7, 0x41, 0x91, 0x8A, 0x64, 0x64, 0xF7, 0xA4, 0xF0, 0x8E}, MakeVersion(1, 5)},
{{0x67, 0xFB, 0x96, 0x6B, 0xAA, 0x3D, 0x57, 0x4E, 0x93, 0x8F, 0x1C, 0xC5, 0x85, 0xc6, 0xF5, 0x29}, MakeVersion(1, 6)},
{{0xE0, 0xC2, 0x94, 0x64, 0x5C, 0xB4, 0x32, 0x45, 0x95, 0x10, 0x09, 0xA1, 0x0F, 0xB2, 0x53, 0xF8}, MakeVersion(1, 10)},
{{0x8C, 0x13, 0x59, 0xA9, 0x7E, 0x6E, 0x49, 0x4F, 0x82, 0xF9, 0xCF, 0x58, 0x71, 0x6B, 0x7D, 0x3A}, MakeVersion(1, 11)},
{{0x7B, 0x51, 0xF5, 0x2C, 0x87, 0xD0, 0xFF, 0x40, 0x83, 0xE5, 0xAA, 0x6C, 0x07, 0xE9, 0x95, 0x20}, MakeVersion(1, 12)},
{{0x17, 0x1B, 0x2C, 0x4A, 0xC7, 0xA5, 0x38, 0x49, 0x8C, 0x43, 0x20, 0xFC, 0x47, 0x14, 0xF2, 0x33}, MakeVersion(1, 20)},
{{0xEA, 0xD7, 0xE8, 0xCB, 0x7F, 0x9D, 0x6C, 0x4F, 0x9E, 0x3E, 0x99, 0x13, 0x92, 0x8F, 0x46, 0x62}, MakeVersion(1, 21)},
{{0xD0, 0x43, 0x98, 0xA6, 0x91, 0xBD, 0xC3, 0x40, 0x85, 0x97, 0xC7, 0xC1, 0x1A, 0xC9, 0x50, 0x91}, MakeVersion(1, 22)},
{{0x2E, 0x36, 0x66, 0x8E, 0x25, 0x0E, 0xE6, 0x43, 0xBD, 0xEF, 0x47, 0xA1, 0x3E, 0xF7, 0x7F, 0xA6}, MakeVersion(1, 23)},
{{0xE7, 0x28, 0xD7, 0x5D, 0xB8, 0xF9, 0x79, 0x4D, 0x89, 0x76, 0x16, 0x64, 0xAB, 0x1E, 0xA0, 0x47}, MakeVersion(1, 30)},
{{0x8E, 0x08, 0xA2, 0x5B, 0xC7, 0xDD, 0xD5, 0x48, 0xB4, 0xE9, 0x90, 0x38, 0x15, 0xB8, 0xBB, 0xEB}, MakeVersion(1, 31)},
{{0x5B, 0x6C, 0xAD, 0x70, 0x67, 0x2b, 0x54, 0x40, 0xBC, 0x45, 0x06, 0x60, 0x44, 0xA9, 0x12, 0x61}, MakeVersion(1, 50)},
{{0x73, 0x05, 0x22, 0x1B, 0x23, 0x65, 0x14, 0x4E, 0xB0, 0xE0, 0xAE, 0xC3, 0x4E, 0xD6, 0x4F, 0xBF}, MakeVersion(1, 5001)},
{{0xDB, 0xE2, 0xBB, 0x94, 0x3B, 0x18, 0xB0, 0x4C, 0xAB, 0x56, 0xB6, 0xB8, 0x9A, 0x7E, 0xD2, 0xA5}, MakeVersion(1, 5002)},
{{0xB0, 0x99, 0x3F, 0x8E, 0xB2, 0x57, 0x46, 0x41, 0xA3, 0x77, 0x3B, 0x98, 0x97, 0x15, 0xC4, 0xBB}, MakeVersion(1, 52)},
{{0x69, 0x41, 0x44, 0x27, 0xDB, 0x02, 0x65, 0x4B, 0xA5, 0x7F, 0x5B, 0x1C, 0xD6, 0x79, 0x94, 0x32}, MakeVersion(1, 52)}, // Exclusive steam patch
{{0x4E, 0xE9, 0x22, 0x2A, 0x9D, 0xB5, 0xC6, 0x40, 0xA7, 0xD4, 0xAB, 0x3B, 0xCA, 0x7C, 0x30, 0x06}, MakeVersion(1, 60)},
{{0x75, 0xF0, 0x99, 0xC1, 0x83, 0x84, 0xF9, 0x43, 0x95, 0x00, 0x4E, 0x7D, 0x93, 0x73, 0x84, 0xDF}, MakeVersion(1, 61)},
{{0x5D, 0x30, 0xA1, 0x30, 0x0F, 0x7F, 0x5E, 0x4C, 0xA9, 0xBA, 0xD8, 0x43, 0xD9, 0xAE, 0x85, 0x50}, MakeVersion(1, 61)},
{{0xD6, 0xFF, 0xA3, 0xE9, 0xCA, 0x20, 0x9A, 0x4F, 0xA6, 0x2A, 0x7B, 0x8B, 0x33, 0xF7, 0x9D, 0x02}, MakeVersion(1, 6201)},
{{0x1C, 0xF0, 0xFC, 0x79, 0x23, 0x64, 0x67, 0x49, 0xBC, 0x54, 0xBA, 0x1E, 0xAC, 0xBC, 0xEB, 0xDA}, MakeVersion(1, 6202)},
{{0x50, 0xd2, 0x43, 0xd9, 0x99, 0x74, 0x1d, 0x46, 0xaf, 0x27, 0xdc, 0x1f, 0x4d, 0x81, 0x43, 0x9f}, MakeVersion(1, 63)},
{{0x26, 0xd4, 0xd1, 0xf8, 0x2a, 0x64, 0x77, 0x4e, 0xa2, 0xde, 0x12, 0x85, 0xce, 0x8a, 0x08, 0x05}, MakeVersion(1, 6301)},
{{0xd3, 0x6a, 0x87, 0x91, 0x64, 0xb1, 0x07, 0x44, 0xb5, 0x82, 0xbd, 0x9f, 0x38, 0x1e, 0x93, 0x1f}, MakeVersion(2, 0)},
{{0xd4, 0x2d, 0xce, 0x5f, 0x13, 0x89, 0xeb, 0x4b, 0xbc, 0x86, 0xa9, 0x34, 0xee, 0x15, 0x5d, 0x39}, MakeVersion(2, 1)},
{{0x3a, 0x88, 0x95, 0xad, 0xd8, 0x44, 0x15, 0x44, 0x8a, 0xaf, 0x9d, 0xbb, 0x58, 0xb3, 0x64, 0x47}, MakeVersion(2, 2)},
{{0xe6, 0x4f, 0x39, 0xe9, 0xe7, 0xec, 0xf9, 0x46, 0xbe, 0x4b, 0x73, 0xbf, 0x76, 0x86, 0xca, 0xb1}, MakeVersion(2, 1000)},
};

mem::module mainModule = mem::module::main();

auto* pImage = GetModuleHandle(nullptr);

const auto sectionHeaders = mainModule.section_headers();

base_address = reinterpret_cast<uintptr_t>(pImage);

for (const auto& cHeader : sectionHeaders)
std::wstring fileName;
TCHAR exePathBuf[MAX_PATH] = {0};
GetModuleFileNameW(GetModuleHandle(nullptr), exePathBuf, static_cast<DWORD>(std::size(exePathBuf)));

fileName = exePathBuf;

auto size = GetFileVersionInfoSize(fileName.c_str(), nullptr);
if (!size)
{
if (memcmp(cHeader.Name, ".text", 5) == 0)
{
TextRegion = mem::region(base_address + cHeader.VirtualAddress, cHeader.Misc.VirtualSize);
break;
}
return;
}

std::unique_ptr<uint8_t[]> data(new (std::nothrow) uint8_t[size]());
if (!data)
{
return;
}

if (!GetFileVersionInfo(fileName.c_str(), 0, size, data.get()))
{
return;
}

struct LangAndCodePage
{
WORD language;
WORD codePage;
}* translations;
uint32_t translationsBytes;

if (!VerQueryValue(data.get(), L"\\VarFileInfo\\Translation", reinterpret_cast<void**>(&translations), &translationsBytes))
{
return;
}

auto& dosHeader = mainModule.dos_header();
auto* pFileHeader = reinterpret_cast<IMAGE_FILE_HEADER*>(base_address + dosHeader.e_lfanew + 4);
const auto* pOptionalHeader = reinterpret_cast<IMAGE_OPTIONAL_HEADER*>(reinterpret_cast<char*>(pFileHeader) + sizeof(IMAGE_FILE_HEADER));
const auto* pDataDirectory = &pOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
const auto* pDebugDirectory = reinterpret_cast<IMAGE_DEBUG_DIRECTORY*>(base_address + pDataDirectory->VirtualAddress);
bool isGame = false;

// Check to see that the data has the right type
if (IMAGE_DEBUG_TYPE_CODEVIEW == pDebugDirectory->Type)
for (uint32_t i = 0; i < (translationsBytes / sizeof(LangAndCodePage)); i++)
{
auto* pdb_info = reinterpret_cast<PdbInfo*>(base_address + pDebugDirectory->AddressOfRawData);
if (0 == memcmp(&pdb_info->Signature, "RSDS", 4))
wchar_t* productName;
auto subBlock = fmt::format(L"\\StringFileInfo\\{:04x}{:04x}\\ProductName", translations[i].language, translations[i].codePage);

if (VerQueryValue(data.get(), subBlock.c_str(), reinterpret_cast<void**>(&productName), &translationsBytes))
{
for (const auto& v : cVersions)
constexpr std::wstring_view expectedProductName = L"Cyberpunk 2077";
if (productName == expectedProductName)
{
if (memcmp(&pdb_info->Guid, v.Guid, 16) == 0)
{
version = v.Version;
break;
}
isGame = true;
break;
}
}
}

if (version == 0)
{
Log::Error("Unrecognized game version:");
if (isGame)
{
VS_FIXEDFILEINFO* fileInfo = nullptr;
UINT fileInfoBytes;

if (!VerQueryValue(data.get(), L"\\", reinterpret_cast<LPVOID*>(&fileInfo), &fileInfoBytes))
{
return;
}

std::stringstream stream;
for (const auto c : pdb_info->Guid)
stream << std::hex << static_cast<uint32_t>(c) << " ";
constexpr auto signature = 0xFEEF04BD;
if (fileInfo->dwSignature != signature)
{
return;
}

Log::Error(stream.str());
Log::Error("CyberEngineTweaks requires an update, aborting loading:");
{
uint16_t major = (fileInfo->dwFileVersionMS >> 16) & 0xFF;
uint16_t minor = fileInfo->dwFileVersionMS & 0xFFFF;
uint16_t build = (fileInfo->dwFileVersionLS >> 16) & 0xFFFF;
uint16_t revision = fileInfo->dwFileVersionLS & 0xFFFF;

throw std::runtime_error("Abort loading");
}
FileVersion = RED4EXT_FILEVER(major, minor, build, revision);
}

{
uint8_t major = (fileInfo->dwProductVersionMS >> 16) & 0xFF;
uint16_t minor = fileInfo->dwProductVersionMS & 0xFFFF;
uint32_t patch = (fileInfo->dwProductVersionLS >> 16) & 0xFFFF;

SemVersion = RED4EXT_SEMVER(major, minor, patch);
}
}
}
14 changes: 6 additions & 8 deletions src/Image.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#pragma once

#include "RED4ext/Api/v0/FileVer.hpp"
#include "RED4ext/Api/SemVer.hpp"


struct Image
{
void Initialize();

static std::tuple<uint32_t, uint16_t> GetSupportedVersion() noexcept { return std::make_tuple(2, 1000); }

static uint64_t MakeVersion(const uint32_t acMajor, const uint16_t acMinor) noexcept { return static_cast<uint64_t>(acMajor) << 32 | static_cast<uint64_t>(acMinor) << 16; }

[[nodiscard]] std::tuple<uint32_t, uint16_t> GetVersion() const noexcept
{
return std::make_tuple(static_cast<uint32_t>(version >> 32), static_cast<uint16_t>((version >> 16) & 0xFFFF));
}

uint64_t version{0};
uintptr_t base_address;
mem::region TextRegion;
RED4ext::v0::FileVer FileVersion{0,0,0,0};
RED4ext::v0::SemVer SemVersion{0, 0, 0, 0};
};
8 changes: 4 additions & 4 deletions src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Paths.h"
#include "Utils.h"
#include "RED4ext/Api/Runtime.hpp"

void PatchesSettings::Load(const nlohmann::json& aConfig)
{
Expand Down Expand Up @@ -177,16 +178,15 @@ Options::Options(Paths& aPaths)

GameImage.Initialize();

if (GameImage.version)
if (GameImage.FileVersion.major != 0)
{
Log::Info("CET version {} [{}]", CET_BUILD_COMMIT, CET_BUILD_BRANCH);
auto [major, minor] = GameImage.GetVersion();
Log::Info("Game version {}.{:02d}", major, minor);
Log::Info("Game version {}.{}.{}.{}", GameImage.FileVersion.major, GameImage.FileVersion.minor, GameImage.FileVersion.build, GameImage.FileVersion.revision);
Log::Info("Root path: \"{}\"", UTF16ToUTF8(aPaths.GameRoot().native()));
Log::Info("Cyber Engine Tweaks path: \"{}\"", UTF16ToUTF8(aPaths.CETRoot().native()));
Log::Info("Lua scripts search path: \"{}\"", UTF16ToUTF8(aPaths.ModsRoot().native()));

if (GameImage.GetVersion() != Image::GetSupportedVersion())
if (GameImage.FileVersion != RED4EXT_RUNTIME_LATEST)
{
const auto [smajor, sminor] = Image::GetSupportedVersion();
Log::Error("Unsupported game version! Only {}.{:02d} is supported.", smajor, sminor);
Expand Down

0 comments on commit ca95e7c

Please sign in to comment.