Skip to content

Commit

Permalink
feat: add opt in to require minimal VR address lib version (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlayaN authored Jun 15, 2024
1 parent 2afe3d5 commit 6b0750a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/REL/ID.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ namespace REL
return static_cast<std::size_t>(it->offset);
}

#ifdef SKYRIMVR
bool IsVRAddressLibraryAtLeastVersion(const char* pluginName, const char* minimalVRAddressLibVersion, bool showWindowsMessage = false) const
{
const auto minimalVersion = REL::Version(minimalVRAddressLibVersion);

bool validVersion = minimalVersion <= _vrAddressLibraryVersion;

if (!validVersion && showWindowsMessage) {
REX::W32::MessageBoxA(NULL, std::format("You need version: {} of VR Address Library for SKSEVR, you have version: {}", minimalVersion.string(), _vrAddressLibraryVersion.string()).c_str(), pluginName, 0x00000000L | 0x00000030L);
REX::W32::TerminateProcess(REX::W32::GetCurrentProcess(), 0);
return false;
}

return validVersion;
}
#endif // SKYRIMVR

private:
friend Offset2ID;

Expand Down Expand Up @@ -249,6 +266,7 @@ namespace REL
auto mapname = L"CommonLibSSEOffsets-v2-"s;
mapname += a_version.wstring();
in.read_row(address_count, version);
_vrAddressLibraryVersion = Version(version);
const auto byteSize = static_cast<std::size_t>(address_count * sizeof(mapping_t));
if (!_mmap.open(mapname, byteSize) &&
!_mmap.create(mapname, byteSize)) {
Expand Down Expand Up @@ -399,6 +417,10 @@ namespace REL

detail::memory_map _mmap;
std::span<mapping_t> _id2offset;

#ifdef SKYRIMVR
Version _vrAddressLibraryVersion;
#endif // SKYRIMVR
};

class ID
Expand Down
26 changes: 26 additions & 0 deletions include/REL/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ namespace REL
_impl{ a_v1, a_v2, a_v3, a_v4 }
{}

explicit constexpr Version(std::string_view a_version)
{
std::array<value_type, 4> powers{ 1, 1, 1, 1 };
std::size_t position = 0;
for (std::size_t i = 0; i < a_version.size(); ++i) {
if (a_version[i] == '.') {
if (++position == powers.size()) {
throw std::invalid_argument("Too many parts in version number.");
}
} else {
powers[position] *= 10;
}
}
position = 0;
for (std::size_t i = 0; i < a_version.size(); ++i) {
if (a_version[i] == '.') {
++position;
} else if (a_version[i] < '0' || a_version[i] > '9') {
throw std::invalid_argument("Invalid character in version number.");
} else {
powers[position] /= 10;
_impl[position] += static_cast<value_type>((a_version[i] - '0') * powers[position]);
}
}
}

[[nodiscard]] constexpr reference operator[](std::size_t a_idx) noexcept { return _impl[a_idx]; }
[[nodiscard]] constexpr const_reference operator[](std::size_t a_idx) const noexcept { return _impl[a_idx]; }

Expand Down

0 comments on commit 6b0750a

Please sign in to comment.