Skip to content

Commit

Permalink
Use file version instead of the product version to detect different g…
Browse files Browse the repository at this point in the history
…ame versions
  • Loading branch information
wopss committed Feb 28, 2022
1 parent fcb5a4c commit d002159
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 39 deletions.
18 changes: 12 additions & 6 deletions src/red4ext.dll/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,25 @@ App::App()
spdlog::debug("Base address is {}", reinterpret_cast<void*>(GetModuleHandle(nullptr)));

const auto image = Image::Get();
const auto& ver = image->GetVersion();
spdlog::info("Game version is {}.{}{}", ver.major, ver.minor, ver.patch);

const auto& productVer = image->GetProductVersion();
spdlog::info("Game version is {}.{}{}", productVer.major, productVer.minor, productVer.patch);

const auto& fileVer = image->GetFileVersion();
spdlog::info("File version {}.{}.{}.{}", fileVer.major, fileVer.minor, fileVer.build, fileVer.revision);

if (!image->IsSupported())
{
spdlog::error("This game version ({}.{}{}) is not supported.", ver.major, ver.minor, ver.patch);
spdlog::error("This game version ({}.{}{}) is not supported.", productVer.major, productVer.minor,
productVer.patch);

const auto supportedVer = image->GetSupportedVersion();
fmt::memory_buffer out;
fmt::format_to(std::back_inserter(out), "The current version of the mod supports only version {}.{}{}, ",
supportedVer.major, supportedVer.minor, supportedVer.patch);
fmt::format_to(std::back_inserter(out),
"The current version of RED4ext supports only file version {}.{}.{}.{}, ", supportedVer.major,
supportedVer.minor, supportedVer.build, supportedVer.revision);

if (ver < supportedVer)
if (fileVer < supportedVer)
{
fmt::format_to(std::back_inserter(out), "try downgrading the mod or updating the game.");
}
Expand Down
39 changes: 28 additions & 11 deletions src/red4ext.dll/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

Image::Image()
: m_isCyberpunk(false)
, m_version(RED4EXT_V0_SEMVER(0, 0, 0))
, m_fileVersion(RED4EXT_V0_FILEVER(0, 0, 0, 0))
, m_productVersion(RED4EXT_V0_SEMVER(0, 0, 0))
{
std::wstring fileName;
auto hr = wil::GetModuleFileNameW(nullptr, fileName);
Expand All @@ -21,7 +22,7 @@ Image::Image()
{
SHOW_LAST_ERROR_MESSAGE_FILE_LINE(L"Could not retrieve version info size.\n\nFile name: {}", fileName);
}

// Else, fail silently, executables might not have the version information.
return;
}
Expand Down Expand Up @@ -73,7 +74,7 @@ Image::Image()

if (m_isCyberpunk)
{
VS_FIXEDFILEINFO* fileInfo;
VS_FIXEDFILEINFO* fileInfo = nullptr;
UINT fileInfoBytes;

if (!VerQueryValue(data.get(), L"\\", reinterpret_cast<LPVOID*>(&fileInfo), &fileInfoBytes))
Expand All @@ -91,11 +92,22 @@ Image::Image()
return;
}

uint8_t major = (fileInfo->dwProductVersionMS >> 16) & 0xFF;
uint16_t minor = fileInfo->dwProductVersionMS & 0xFFFF;
uint32_t patch = (fileInfo->dwProductVersionLS >> 16) & 0xFFFF;
{
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;

m_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;

m_version = RED4EXT_SEMVER(major, minor, patch);
m_productVersion = RED4EXT_SEMVER(major, minor, patch);
}
}
}

Expand All @@ -112,15 +124,20 @@ bool Image::IsCyberpunk() const

bool Image::IsSupported() const
{
return m_version == GetSupportedVersion();
return m_fileVersion == GetSupportedVersion();
}

const RED4ext::FileVer& Image::GetFileVersion() const
{
return m_fileVersion;
}

const RED4ext::VersionInfo& Image::GetVersion() const
const RED4ext::SemVer& Image::GetProductVersion() const
{
return m_version;
return m_productVersion;
}

const RED4ext::VersionInfo Image::GetSupportedVersion() const
const RED4ext::FileVer Image::GetSupportedVersion() const
{
return RED4EXT_RUNTIME_LATEST;
}
10 changes: 6 additions & 4 deletions src/red4ext.dll/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ class Image
static Image* Get();

bool IsCyberpunk() const;

bool IsSupported() const;
const RED4ext::VersionInfo& GetVersion() const;
const RED4ext::VersionInfo GetSupportedVersion() const;

const RED4ext::FileVer& GetFileVersion() const;
const RED4ext::SemVer& GetProductVersion() const;
const RED4ext::FileVer GetSupportedVersion() const;

private:
Image();
~Image() = default;

bool m_isCyberpunk;
RED4ext::VersionInfo m_version;
RED4ext::FileVer m_fileVersion;
RED4ext::SemVer m_productVersion;
};
6 changes: 3 additions & 3 deletions src/red4ext.dll/PluginBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class PluginBase

virtual const std::wstring_view GetName() const = 0;
virtual const std::wstring_view GetAuthor() const = 0;
virtual const RED4ext::VersionInfo& GetVersion() const = 0;
virtual const RED4ext::VersionInfo& GetRuntimeVersion() const = 0;
virtual const RED4ext::VersionInfo& GetSdkVersion() const = 0;
virtual const RED4ext::SemVer& GetVersion() const = 0;
virtual const RED4ext::FileVer& GetRuntimeVersion() const = 0;
virtual const RED4ext::SemVer& GetSdkVersion() const = 0;

const std::filesystem::path& GetPath() const;
HMODULE GetModule() const;
Expand Down
15 changes: 8 additions & 7 deletions src/red4ext.dll/Systems/PluginSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define MINIMUM_API_VERSION RED4EXT_API_VERSION_0
#define LATEST_API_VERSION RED4EXT_API_VERSION_LATEST

#define MINIMUM_SDK_VERSION RED4EXT_SDK_0_2_0
#define MINIMUM_SDK_VERSION RED4EXT_SDK_0_3_0
#define LATEST_SDK_VERSION RED4EXT_SDK_LATEST

#define LOG_FS_ERROR(text, ec) \
Expand Down Expand Up @@ -208,15 +208,16 @@ void PluginSystem::Load(const std::filesystem::path& aPath, bool aSearchLoadDir)
const auto& pluginVersion = plugin->GetVersion();

const auto image = Image::Get();
const auto& runtime = image->GetVersion();
const auto& fileVer = image->GetFileVersion();
const auto& productVer = image->GetProductVersion();

const auto& requestedRuntime = plugin->GetRuntimeVersion();
if (requestedRuntime != RED4EXT_RUNTIME_INDEPENDENT && requestedRuntime != runtime)
if (requestedRuntime != RED4EXT_RUNTIME_INDEPENDENT && requestedRuntime != fileVer)
{
spdlog::warn(L"{} (version: {}) is not incompatible with the current game's version ({}.{}{}). This version of "
L"the plugin supports only version {}.{}{}",
pluginName, std::to_wstring(pluginVersion), runtime.major, runtime.minor, runtime.patch,
requestedRuntime.major, requestedRuntime.minor, requestedRuntime.patch);
spdlog::warn(L"{} (version: {}) is not incompatible with the current game's version. This version of the "
L"plugin supports only runtime version {}.{}.{}.{}",
pluginName, std::to_wstring(pluginVersion), requestedRuntime.major, requestedRuntime.minor,
requestedRuntime.build, requestedRuntime.revision);
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/red4ext.dll/v0/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ v0::Plugin::Plugin(const std::filesystem::path& aPath, wil::unique_hmodule aModu
: PluginBase(aPath, std::move(aModule))
, m_info{}
, m_sdk{}
, m_runtime(Image::Get()->GetVersion())
, m_runtime(Image::Get()->GetProductVersion())
, m_logger{}
, m_hooking{}
, m_gameStates{}
Expand Down Expand Up @@ -71,17 +71,17 @@ const std::wstring_view v0::Plugin::GetAuthor() const
return m_info.author;
}

const RED4ext::VersionInfo& v0::Plugin::GetVersion() const
const RED4ext::SemVer& v0::Plugin::GetVersion() const
{
return m_info.version;
}

const RED4ext::VersionInfo& v0::Plugin::GetRuntimeVersion() const
const RED4ext::FileVer& v0::Plugin::GetRuntimeVersion() const
{
return m_info.runtime;
}

const RED4ext::VersionInfo& v0::Plugin::GetSdkVersion() const
const RED4ext::SemVer& v0::Plugin::GetSdkVersion() const
{
return m_info.sdk;
}
8 changes: 4 additions & 4 deletions src/red4ext.dll/v0/Plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Plugin : public PluginBase

virtual const std::wstring_view GetName() const final;
virtual const std::wstring_view GetAuthor() const final;
virtual const RED4ext::VersionInfo& GetVersion() const final;
virtual const RED4ext::VersionInfo& GetRuntimeVersion() const final;
virtual const RED4ext::VersionInfo& GetSdkVersion() const final;
virtual const RED4ext::SemVer& GetVersion() const final;
virtual const RED4ext::FileVer& GetRuntimeVersion() const final;
virtual const RED4ext::SemVer& GetSdkVersion() const final;

private:
RED4ext::v0::PluginInfo m_info;

RED4ext::v0::Sdk m_sdk;
RED4ext::v0::VersionInfo m_runtime;
RED4ext::v0::SemVer m_runtime;
RED4ext::v0::Logger m_logger;
RED4ext::v0::Hooking m_hooking;
RED4ext::v0::GameStates m_gameStates;
Expand Down

0 comments on commit d002159

Please sign in to comment.