Skip to content

Commit

Permalink
chore: make Module and Version header-only, add some newlines (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirdEyeSqueegee authored Oct 23, 2023
1 parent 6f57d03 commit 36eec91
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 127 deletions.
58 changes: 54 additions & 4 deletions CommonLibSF/include/REL/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,44 @@ namespace REL
{
public:
constexpr Module() = delete;
explicit Module(std::uintptr_t a_base);
explicit constexpr Module(std::string_view a_filePath);

explicit Module(const std::uintptr_t a_base)
{
stl_assert(a_base, "failed to initializing module info with null module base");

_base = a_base;

const auto dosHeader = reinterpret_cast<const WinAPI::IMAGE_DOS_HEADER*>(_base);
const auto ntHeader = stl::adjust_pointer<WinAPI::IMAGE_NT_HEADERS64>(dosHeader, dosHeader->lfanew);
const auto sections = WinAPI::IMAGE_FIRST_SECTION(ntHeader);
const auto size = std::min<std::size_t>(ntHeader->fileHeader.sectionCount, _segments.size());

for (std::size_t i = 0; i < size; ++i) {
const auto& section = sections[i];
if (const auto it = std::ranges::find_if(SEGMENTS.begin(), SEGMENTS.end(), [&](auto&& a_elem) {
constexpr auto size_s = std::extent_v<decltype(section.name)>;
const auto len = (std::min)(a_elem.first.size(), size_s);
return std::memcmp(a_elem.first.data(), section.name, len) == 0 && (section.characteristics & a_elem.second) == a_elem.second;
});
it != SEGMENTS.end()) {
const auto idx = static_cast<std::size_t>(std::distance(SEGMENTS.begin(), it));
_segments[idx] = Segment{ _base, _base + section.virtualAddress, section.virtualSize };
}
}

_file = stl::utf8_to_utf16(WinAPI::GetProcPath(nullptr)).value();
_version = get_file_version(_file).value();
}

explicit constexpr Module(std::string_view a_filePath)
{
const auto base = AsAddress(WinAPI::GetModuleHandle(a_filePath.data())) & ~3;
stl_assert(base,
"failed to initializing module info with file {}",
a_filePath);

*this = Module(base);
}

[[nodiscard]] constexpr auto base() const noexcept { return _base; }

Expand All @@ -68,9 +104,23 @@ namespace REL

[[nodiscard]] constexpr auto version() const noexcept { return _version; }

[[nodiscard]] static Module& get(std::uintptr_t a_address) noexcept;
[[nodiscard]] static Module& get(const std::uintptr_t a_address) noexcept
{
static std::unordered_map<std::uintptr_t, Module> managed;

const auto base = AsAddress(a_address) & ~3;
if (!managed.contains(base)) {
managed.try_emplace(base, base);
}

return managed.at(base);
}

[[nodiscard]] static Module& get(std::string_view a_filePath = {}) noexcept;
[[nodiscard]] static Module& get(const std::string_view a_filePath = {}) noexcept
{
const auto base = AsAddress(WinAPI::GetModuleHandle(a_filePath.empty() ? WinAPI::GetProcPath(nullptr).data() : a_filePath.data()));
return get(base);
}

private:
static constexpr std::array SEGMENTS{
Expand Down
63 changes: 59 additions & 4 deletions CommonLibSF/include/REL/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,38 @@ namespace REL
constexpr Version() noexcept = default;

explicit constexpr Version(const std::array<value_type, 4> a_version) noexcept :
_impl(a_version) {}
_impl(a_version)
{}

constexpr Version(const value_type a_v1, const value_type a_v2 = 0, const value_type a_v3 = 0, const value_type a_v4 = 0) noexcept :
_impl{ a_v1, a_v2, a_v3, a_v4 } {}
_impl{ a_v1, a_v2, a_v3, a_v4 }
{}

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

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

Expand Down Expand Up @@ -51,8 +77,11 @@ namespace REL
}

[[nodiscard]] constexpr value_type major() const noexcept { return _impl[0]; }

[[nodiscard]] constexpr value_type minor() const noexcept { return _impl[1]; }

[[nodiscard]] constexpr value_type patch() const noexcept { return _impl[2]; }

[[nodiscard]] constexpr value_type build() const noexcept { return _impl[3]; }

[[nodiscard]] constexpr std::string string(const std::string_view a_separator = "."sv) const
Expand Down Expand Up @@ -145,7 +174,33 @@ namespace REL
}
}

[[nodiscard]] std::optional<Version> get_file_version(stl::zwstring a_filename);
[[nodiscard]] inline std::optional<Version> get_file_version(const stl::zwstring a_filename)
{
std::uint32_t dummy{};
std::vector<char> buf(WinAPI::GetFileVersionInfoSize(a_filename.data(), std::addressof(dummy)));
if (buf.empty()) {
return std::nullopt;
}

if (!WinAPI::GetFileVersionInfo(a_filename.data(), 0, buf.size(), buf.data())) {
return std::nullopt;
}

void* verBuf{};
std::uint32_t verLen{};
if (!WinAPI::VerQueryValue(buf.data(), L"\\StringFileInfo\\040904B0\\ProductVersion", std::addressof(verBuf), std::addressof(verLen))) {
return std::nullopt;
}

Version version;
std::wistringstream ss(std::wstring(static_cast<const wchar_t*>(verBuf), verLen));
std::wstring token;
for (std::size_t i = 0; i < 4 && std::getline(ss, token, L'.'); ++i) {
version[i] = static_cast<std::uint16_t>(std::stoi(token));
}

return version;
}
}

#ifdef __cpp_lib_format
Expand Down
4 changes: 4 additions & 0 deletions CommonLibSF/include/SFSE/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
namespace SFSE
{
void Init(const LoadInterface* a_intfc, bool a_log = true) noexcept;

void RegisterForAPIInitEvent(const std::function<void()>& a_fn);

PluginHandle GetPluginHandle() noexcept;

const TrampolineInterface* GetTrampolineInterface() noexcept;

const MessagingInterface* GetMessagingInterface() noexcept;

const MenuInterface* GetMenuInterface() noexcept;

Trampoline& GetTrampoline();

void AllocTrampoline(std::size_t a_size, bool a_trySFSEReserve = true);
}
1 change: 1 addition & 0 deletions CommonLibSF/include/SFSE/IAT.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SFSE
{
[[nodiscard]] std::uintptr_t GetIATAddr(std::string_view a_dll, std::string_view a_function);

[[nodiscard]] std::uintptr_t GetIATAddr(void* a_module, std::string_view a_dll, std::string_view a_function);

[[nodiscard]] constexpr void* GetIATPtr(std::string_view a_dll, std::string_view a_function);
Expand Down
4 changes: 4 additions & 0 deletions CommonLibSF/include/SFSE/InputMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ namespace SFSE::InputMap
};

std::uint32_t GamepadMaskToKeycode(std::uint32_t a_keyMask);

std::uint32_t GamepadKeycodeToMask(std::uint32_t a_keyCode);

std::string GetKeyName(std::uint32_t a_keyCode);

std::string GetKeyboardKeyName(std::uint32_t a_keyCode);

std::string GetMouseButtonName(std::uint32_t a_keyCode);

std::string GetGamepadButtonName(std::uint32_t a_keyCode);
}
6 changes: 6 additions & 0 deletions CommonLibSF/include/SFSE/Interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace SFSE
{
public:
[[nodiscard]] REL::Version RuntimeVersion() const;

[[nodiscard]] std::uint32_t SFSEVersion() const;

protected:
Expand All @@ -35,7 +36,9 @@ namespace SFSE
};

[[nodiscard]] PluginHandle GetPluginHandle() const;

const PluginInfo* GetPluginInfo(const char* a_name) const;

[[nodiscard]] void* QueryInterface(std::uint32_t a_id) const;
};

Expand Down Expand Up @@ -68,7 +71,9 @@ namespace SFSE
[[nodiscard]] std::uint32_t Version() const;

bool Dispatch(std::uint32_t a_messageType, void* a_data, std::uint32_t a_dataLen, const char* a_receiver) const;

bool RegisterListener(EventCallback a_callback) const;

bool RegisterListener(const char* a_sender, EventCallback a_callback) const;

protected:
Expand All @@ -86,6 +91,7 @@ namespace SFSE
[[nodiscard]] std::uint32_t Version() const;

[[nodiscard]] void* AllocateFromBranchPool(std::size_t a_size) const;

[[nodiscard]] void* AllocateFromLocalPool(std::size_t a_size) const;

private:
Expand Down
1 change: 1 addition & 0 deletions CommonLibSF/include/SFSE/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace SFSE::log
SFSE_MAKE_SOURCE_LOGGER(critical, critical);

[[nodiscard]] std::optional<std::filesystem::path> log_directory();

void init();
}

Expand Down
3 changes: 2 additions & 1 deletion CommonLibSF/include/SFSE/Trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace SFSE
Trampoline(Trampoline&& a_rhs) noexcept { move_from(std::move(a_rhs)); }

explicit Trampoline(const std::string_view a_name) :
_name(a_name) {}
_name(a_name)
{}

~Trampoline() { release(); }

Expand Down
60 changes: 0 additions & 60 deletions CommonLibSF/src/REL/Module.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions CommonLibSF/src/REL/Version.cpp

This file was deleted.

3 changes: 3 additions & 0 deletions CommonLibSF/src/SFSE/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ namespace SFSE

private:
APIStorage() noexcept = default;

~APIStorage() noexcept = default;

constexpr APIStorage(const APIStorage&) = delete;

constexpr APIStorage(APIStorage&&) = delete;

constexpr APIStorage& operator=(const APIStorage&) = delete;

constexpr APIStorage& operator=(APIStorage&&) = delete;
};

Expand Down
Loading

0 comments on commit 36eec91

Please sign in to comment.