Split static & shared lib

This commit is contained in:
acidicoala
2025-08-23 13:44:17 +05:00
parent b828ecc58a
commit dc086e40e0
48 changed files with 1048 additions and 1318 deletions

View File

@@ -1,53 +0,0 @@
#include <koalabox/http_client.hpp>
#include <koalabox/logger.hpp>
#include "smoke_api/api.hpp"
#include "smoke_api/types.hpp"
namespace api {
struct SteamResponse {
uint32_t success = 0;
std::vector<DLC> dlcs;
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(
SteamResponse,
success,
dlcs
) // NOLINT(misc-const-correctness)
};
std::optional<std::vector<DLC>> fetch_dlcs_from_github(AppId_t app_id) noexcept {
try {
constexpr auto url = "https://raw.githubusercontent.com/"
"acidicoala/public-entitlements/main/steam/v2/dlc.json";
const auto json = koalabox::http_client::get_json(url);
const auto response = json.get<AppDlcNameMap>();
return DLC::get_dlcs_from_apps(response, app_id);
} catch(const nlohmann::json::exception& e) {
LOG_ERROR("Failed to fetch DLC list from GitHub: {}", e.what());
return std::nullopt;
}
}
std::optional<std::vector<DLC>> fetch_dlcs_from_steam(AppId_t app_id) noexcept {
try {
// TODO: Communicate directly with Steam servers.
// ref.: https://github.com/SteamRE/SteamKit
const auto url =
fmt::format("https://store.steampowered.com/dlc/{}/ajaxgetdlclist", app_id);
const auto json = koalabox::http_client::get_json(url);
const auto [success, dlcs] = json.get<SteamResponse>();
if(success != 1) {
throw std::runtime_error("Web API responded with 'success' != 1");
}
return dlcs;
} catch(const std::exception& e) {
LOG_ERROR("Failed to fetch dlc list from Steam: {}", e.what());
return std::nullopt;
}
}
}

View File

@@ -1,9 +0,0 @@
#pragma once
#include "smoke_api/types.hpp"
namespace api {
std::optional<std::vector<DLC>> fetch_dlcs_from_github(AppId_t app_id) noexcept;
std::optional<std::vector<DLC>> fetch_dlcs_from_steam(AppId_t app_id) noexcept;
}

View File

@@ -1,49 +0,0 @@
#include <koalabox/cache.hpp>
#include <koalabox/logger.hpp>
#include "smoke_api/cache.hpp"
constexpr auto KEY_APPS = "apps";
namespace {
AppDlcNameMap get_cached_apps() noexcept {
try {
return koalabox::cache::get(KEY_APPS).get<AppDlcNameMap>();
} catch(const std::exception& e) {
LOG_WARN("Failed to get cached apps: {}", e.what());
return {};
}
}
}
namespace smoke_api::cache {
std::vector<DLC> get_dlcs(AppId_t app_id) noexcept {
try {
LOG_DEBUG("Reading cached DLC IDs for the app: {}", app_id);
const auto apps = get_cached_apps();
return DLC::get_dlcs_from_apps(apps, app_id);
} catch(const std::exception& e) {
LOG_ERROR("Error reading DLCs from disk cache: ", e.what());
return {};
}
}
bool save_dlcs(AppId_t app_id, const std::vector<DLC>& dlcs) noexcept {
try {
LOG_DEBUG("Caching DLC IDs for the app: {}", app_id);
auto apps = get_cached_apps();
apps[std::to_string(app_id)] = App{.dlcs = DLC::get_dlc_map_from_vector(dlcs)};
return koalabox::cache::put(KEY_APPS, nlohmann::json(apps));
} catch(const std::exception& e) {
LOG_ERROR("Error saving DLCs to disk cache: {}", e.what());
return false;
}
}
}

View File

@@ -1,9 +0,0 @@
#pragma once
#include "types.hpp"
namespace smoke_api::cache {
std::vector<DLC> get_dlcs(AppId_t app_id) noexcept;
bool save_dlcs(AppId_t app_id, const std::vector<DLC>& dlcs) noexcept;
}

View File

@@ -1,69 +0,0 @@
#include <koalabox/config.hpp>
#include <koalabox/io.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include "smoke_api/config.hpp"
namespace smoke_api::config {
namespace kb = koalabox;
Config instance; // NOLINT(cert-err58-cpp)
std::vector<DLC> get_extra_dlcs(const AppId_t app_id) {
return DLC::get_dlcs_from_apps(instance.extra_dlcs, app_id);
}
bool is_dlc_unlocked(
AppId_t app_id,
AppId_t dlc_id,
const std::function<bool()>& original_function
) {
auto status = instance.default_app_status;
const auto app_id_str = std::to_string(app_id);
if(instance.override_app_status.contains(app_id_str)) {
status = instance.override_app_status[app_id_str];
}
const auto dlc_id_str = std::to_string(dlc_id);
if(instance.override_dlc_status.contains(dlc_id_str)) {
status = instance.override_dlc_status[dlc_id_str];
}
bool is_unlocked;
switch(status) {
case AppStatus::UNLOCKED:
is_unlocked = true;
break;
case AppStatus::LOCKED:
is_unlocked = false;
break;
case AppStatus::ORIGINAL:
case AppStatus::UNDEFINED:
is_unlocked = original_function();
break;
}
LOG_TRACE(
"App ID: {}, DLC ID: {}, Status: {}, Original: {}, Unlocked: {}",
app_id_str,
dlc_id_str,
nlohmann::json(status).dump(),
original_function(),
is_unlocked
);
return is_unlocked;
}
DLL_EXPORT(void) ReloadConfig
(
)
{
LOG_INFO("Reloading config");
instance = kb::config::parse<Config>();
}
}

View File

@@ -1,64 +0,0 @@
#pragma once
#include "smoke_api/types.hpp"
namespace smoke_api::config {
enum class AppStatus {
UNDEFINED,
ORIGINAL,
UNLOCKED,
LOCKED,
};
NLOHMANN_JSON_SERIALIZE_ENUM(
AppStatus,
// @formatter:off
{
{AppStatus::UNDEFINED, nullptr},
{AppStatus::ORIGINAL, "original"},
{AppStatus::UNLOCKED, "unlocked"},
{AppStatus::LOCKED, "locked"},
}
// @formatter:on
)
struct Config {
uint32_t $version = 2;
bool logging = false;
AppStatus default_app_status = AppStatus::UNLOCKED;
uint32_t override_app_id = 0;
std::map<std::string, AppStatus> override_app_status;
std::map<std::string, AppStatus> override_dlc_status;
AppDlcNameMap extra_dlcs;
bool auto_inject_inventory = true;
std::vector<uint32_t> extra_inventory_items;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(
Config,
// NOLINT(misc-const-correctness)
$version,
logging,
default_app_status,
override_app_id,
override_app_status,
override_dlc_status,
extra_dlcs,
auto_inject_inventory,
extra_inventory_items
)
};
extern Config instance;
std::vector<DLC> get_extra_dlcs(AppId_t app_id);
bool is_dlc_unlocked(
uint32_t app_id,
uint32_t dlc_id,
const std::function<bool()>& original_function
);
DLL_EXPORT(void) ReloadConfig
(
);
}

View File

@@ -1,4 +0,0 @@
namespace globals {
HMODULE steamapi_module = nullptr;
HMODULE steamclient_module = nullptr;
}

View File

@@ -1,6 +0,0 @@
#pragma once
namespace globals {
extern HMODULE steamclient_module;
extern HMODULE steamapi_module;
}

View File

@@ -1,130 +0,0 @@
#include <koalabox/config.hpp>
#include <koalabox/dll_monitor.hpp>
#include <koalabox/globals.hpp>
#include <koalabox/hook.hpp>
#include <koalabox/loader.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/paths.hpp>
#include <koalabox/util.hpp>
#include <koalabox/win_util.hpp>
#include "build_config.h"
#include "smoke_api/smoke_api.hpp"
#include "exports/steamclient.hpp"
#include "smoke_api/config.hpp"
#include "smoke_api/globals.hpp"
// Hooking steam_api has shown itself to be less desirable than steamclient
// for the reasons outlined below:
//
// Calling original in flat functions will actually call the hooked functions
// because the original function redirects the execution to a function taken
// from self pointer, which would have been hooked by SteamInternal_*Interface
// functions.
//
// Furthermore, turns out that many flat functions share the same body,
// which looks like the following snippet:
//
// mov rax, qword ptr ds:[rcx]
// jmp qword ptr ds:[rax+immediate]
//
// This means that we end up inadvertently hooking unintended functions.
// Given that hooking steam_api has no apparent benefits, but has inherent flaws,
// the support for it has been dropped from this project.
namespace {
namespace kb = koalabox;
namespace fs = std::filesystem;
#define DETOUR_STEAMCLIENT(FUNC) \
kb::hook::detour_or_warn(globals::steamclient_module, #FUNC, reinterpret_cast<uintptr_t>(FUNC));
void override_app_id() {
const auto override_app_id = smoke_api::config::instance.override_app_id;
if(override_app_id == 0) {
return;
}
spdlog::default_logger_raw();
LOG_DEBUG("Overriding app id to {}", override_app_id);
SetEnvironmentVariable(TEXT("SteamAppId"), std::to_wstring(override_app_id).c_str());
}
void init_proxy_mode() {
LOG_INFO("Detected proxy mode");
const auto self_path = kb::paths::get_self_path();
globals::steamapi_module = kb::loader::load_original_library(self_path, STEAMAPI_DLL);
}
void init_hook_mode() {
LOG_INFO("Detected hook mode");
kb::hook::init(true);
kb::dll_monitor::init_listener(
STEAMCLIENT_DLL,
[](const HMODULE& library) {
globals::steamclient_module = library;
DETOUR_STEAMCLIENT(CreateInterface)
kb::dll_monitor::shutdown_listener();
}
);
}
}
namespace smoke_api {
void init(const HMODULE module_handle) {
// FIXME: IMPORTANT! Non ascii paths in directories will result in init errors
try {
kb::globals::init_globals(module_handle, PROJECT_NAME);
config::instance = kb::config::parse<config::Config>();
if(config::instance.logging) {
kb::logger::init_file_logger(kb::paths::get_log_path());
}
// This kind of timestamp is reliable only for CI builds, as it will reflect the
// compilation time stamp only when this file gets recompiled.
LOG_INFO("{} v{} | Compiled at '{}'", PROJECT_NAME, PROJECT_VERSION, __TIMESTAMP__);
const fs::path exe_path = kb::win_util::get_module_file_name_or_throw(nullptr);
const auto exe_name = exe_path.filename().string();
LOG_DEBUG("Process name: '{}' [{}-bit]", exe_name, kb::util::BITNESS);
override_app_id();
if(kb::hook::is_hook_mode(module_handle, STEAMAPI_DLL)) {
init_hook_mode();
} else {
init_proxy_mode();
}
LOG_INFO("Initialization complete");
} catch(const std::exception& ex) {
kb::util::panic(fmt::format("Initialization error: {}", ex.what()));
}
}
void shutdown() {
try {
if(globals::steamapi_module != nullptr) {
kb::win_util::free_library(globals::steamapi_module);
globals::steamapi_module = nullptr;
}
LOG_INFO("Shutdown complete");
} catch(const std::exception& e) {
const auto msg = std::format("Shutdown error: {}", e.what());
LOG_ERROR(msg);
}
kb::logger::shutdown();
}
}

View File

@@ -1,7 +0,0 @@
#pragma once
namespace smoke_api {
void init(HMODULE module_handle);
void shutdown();
}

View File

@@ -1,26 +0,0 @@
#include "smoke_api/types.hpp"
std::vector<DLC> DLC::get_dlcs_from_apps(const AppDlcNameMap& apps, AppId_t app_id) {
std::vector<DLC> dlcs;
const auto app_id_str = std::to_string(app_id);
if(apps.contains(app_id_str)) {
const auto& app = apps.at(app_id_str);
for(auto const& [id, name] : app.dlcs) {
dlcs.emplace_back(id, name);
}
}
return dlcs;
}
DlcNameMap DLC::get_dlc_map_from_vector(const std::vector<DLC>& dlcs) {
DlcNameMap map;
for(const auto& dlc : dlcs) {
map[dlc.get_id_str()] = dlc.get_name();
}
return map;
}

View File

@@ -1,141 +0,0 @@
#pragma once
#include <cstdint>
#include <map>
#include <string>
#include <nlohmann/json.hpp>
/**
* By default, virtual functions are declared with __thiscall
* convention, which is normal since they are class members.
* But it presents an issue for us, since we cannot pass *this
* pointer as a function argument. This is because *this
* pointer is passed via register ECX in __thiscall
* convention. Hence, to resolve this issue we declare our
* hooked functions with __fastcall convention, to trick
* the compiler into reading ECX & EDX registers as 1st
* and 2nd function arguments respectively. Similarly, __fastcall
* makes the compiler push the first argument into the ECX register,
* which mimics the __thiscall calling convention. Register EDX
* is not used anywhere in this case, but we still pass it along
* to conform to the __fastcall convention. This all applies
* to the x86 architecture.
*
* In x86-64 however, there is only one calling convention,
* so __fastcall is simply ignored. However, RDX in this case
* will store the 1st actual argument to the function, so we
* have to omit it from the function signature.
*
* The macros below implement the above-mentioned considerations.
*/
#ifdef _WIN64
#define PARAMS(...) void *RCX, __VA_ARGS__
#define ARGS(...) RCX, __VA_ARGS__
#define THIS RCX
#else
#define PARAMS(...) const void *ECX, const void *EDX, __VA_ARGS__
#define ARGS(...) ECX, EDX, __VA_ARGS__
#define THIS ECX
#endif
// Names beginning with $ designate macros that are not meant to be used directly by the sources
// consuming this file
// IMPORTANT: DLL_EXPORT is hardcoded in exports_generator.cpp,
// so any name changes here must be reflected there as well.
#define DLL_EXPORT(TYPE) extern "C" [[maybe_unused]] __declspec(dllexport) TYPE __cdecl
#define VIRTUAL(TYPE) __declspec(noinline) TYPE __fastcall
#define C_DECL(TYPE) extern "C" __declspec(noinline) TYPE __cdecl
// TODO: Replace with direct call
#define GET_ORIGINAL_HOOKED_FUNCTION(FUNC) \
static const auto FUNC##_o = koalabox::hook::get_original_hooked_function(#FUNC, FUNC);
#define ORIGINAL_FUNCTION_STEAMAPI(FUNC) \
koalabox::hook::get_original_function(globals::steamapi_module, #FUNC, FUNC)
// TODO: Rename to DEFINE_ORIGINAL_FUNCTION_STEAMAPI
#define GET_ORIGINAL_FUNCTION_STEAMAPI(FUNC) \
static const auto FUNC##_o = ORIGINAL_FUNCTION_STEAMAPI(FUNC);
#define DETOUR_ADDRESS(FUNC, ADDRESS) \
koalabox::hook::detour_or_warn(ADDRESS, #FUNC, reinterpret_cast<uintptr_t>(FUNC));
#define _DETOUR(FUNC, NAME, MODULE_HANDLE) \
koalabox::hook::detour_or_warn(MODULE_HANDLE, NAME, reinterpret_cast<uintptr_t>(FUNC));
constexpr auto STEAM_APPS = "STEAMAPPS_INTERFACE_VERSION";
constexpr auto STEAM_CLIENT = "SteamClient";
constexpr auto STEAM_USER = "SteamUser";
constexpr auto STEAM_INVENTORY = "STEAMINVENTORY_INTERFACE_V";
using AppId_t = uint32_t;
using SteamInventoryResult_t = uint32_t;
using SteamItemInstanceID_t = uint64_t;
using SteamItemDef_t = uint32_t;
using HSteamPipe = uint32_t;
using HSteamUser = uint32_t;
using CSteamID = uint64_t;
using EResult = uint32_t;
struct SteamItemDetails_t {
SteamItemInstanceID_t m_itemId;
uint32_t m_iDefinition;
uint16_t m_unQuantity;
uint16_t m_unFlags; // see ESteamItemFlags
};
// results from UserHasLicenseForApp
enum EUserHasLicenseForAppResult {
k_EUserHasLicenseResultHasLicense = 0, // User has a license for specified app
k_EUserHasLicenseResultDoesNotHaveLicense = 1,
// User does not have a license for the specified app
k_EUserHasLicenseResultNoAuth = 2, // User has not been authenticated
};
// These aliases exist solely to increase code readability
using AppIdKey = std::string;
using DlcIdKey = std::string;
using DlcNameValue = std::string;
using DlcNameMap = std::map<DlcIdKey, DlcNameValue>;
struct App {
DlcNameMap dlcs;
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(App, dlcs) // NOLINT(misc-const-correctness)
};
using AppDlcNameMap = std::map<AppIdKey, App>;
class DLC {
// These 2 names must match the property names from Steam API
std::string appid;
std::string name;
public:
explicit DLC() = default;
explicit DLC(std::string appid, std::string name) : appid{std::move(appid)},
name{std::move(name)} {}
[[nodiscard]] std::string get_id_str() const {
return appid;
}
[[nodiscard]] uint32_t get_id() const {
return std::stoi(appid);
}
[[nodiscard]] std::string get_name() const {
return name;
}
NLOHMANN_DEFINE_TYPE_INTRUSIVE(DLC, appid, name)
static std::vector<DLC> get_dlcs_from_apps(const AppDlcNameMap& apps, AppId_t app_id);
static DlcNameMap get_dlc_map_from_vector(const std::vector<DLC>& vector);
};