Removed store mode

This commit is contained in:
acidicoala
2025-08-22 05:35:47 +05:00
parent 3978006e12
commit 28650491b2
2129 changed files with 6991 additions and 8112 deletions

53
src/smoke_api/api.cpp Normal file
View File

@@ -0,0 +1,53 @@
#include <koalabox/http_client.hpp>
#include <koalabox/logger.hpp>
#include "smoke_api/types.hpp"
#include "smoke_api/api.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 {
const 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 response = json.get<SteamResponse>();
if (response.success != 1) {
throw std::runtime_error("Web API responded with 'success' != 1");
}
return response.dlcs;
} catch (const std::exception& e) {
LOG_ERROR("Failed to fetch dlc list from Steam: {}", e.what());
return std::nullopt;
}
}
}

11
src/smoke_api/api.hpp Normal file
View File

@@ -0,0 +1,11 @@
#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;
}

53
src/smoke_api/cache.cpp Normal file
View File

@@ -0,0 +1,53 @@
#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;
}
}
}

11
src/smoke_api/cache.hpp Normal file
View File

@@ -0,0 +1,11 @@
#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,35 +1,23 @@
#include <smoke_api/config.hpp>
#include <core/paths.hpp>
#include <koalabox/util.hpp>
#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;
namespace fs = std::filesystem;
Config instance; // NOLINT(cert-err58-cpp)
// TODO: Refactor to Koalabox
void init_config() {
const auto path = paths::get_config_path();
if (exists(path)) {
try {
const auto config_str = koalabox::io::read_file(path);
instance = Json::parse(config_str).get<Config>();
LOG_DEBUG("Parsed config:\n{}", Json(instance).dump(2));
} catch (const Exception& e) {
const auto message = fmt::format("Error parsing config file: {}", e.what());
koalabox::util::error_box("SmokeAPI Error", message);
}
}
}
Vector<DLC> get_extra_dlcs(AppId_t app_id) {
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 Function<bool()>& original_function) {
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);
@@ -44,21 +32,25 @@ namespace smoke_api::config {
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;
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, Json(status).dump(), original_function(), is_unlocked
app_id_str,
dlc_id_str,
nlohmann::json(status).dump(),
original_function(),
is_unlocked
);
return is_unlocked;
@@ -67,6 +59,6 @@ namespace smoke_api::config {
DLL_EXPORT(void) ReloadConfig() {
LOG_INFO("Reloading config");
init_config();
instance = kb::config::parse<Config>();
}
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include <core/types.hpp>
#include "smoke_api/types.hpp"
namespace smoke_api::config {
@@ -21,40 +21,33 @@ namespace smoke_api::config {
struct Config {
uint32_t $version = 2;
bool logging = false;
bool unlock_family_sharing = true;
AppStatus default_app_status = AppStatus::UNLOCKED;
uint32_t override_app_id = 0;
Map<String, AppStatus> override_app_status;
Map<String, AppStatus> override_dlc_status;
std::map<std::string, AppStatus> override_app_status;
std::map<std::string, AppStatus> override_dlc_status;
AppDlcNameMap extra_dlcs;
bool auto_inject_inventory = true;
Vector<uint32_t> extra_inventory_items;
// We have to use general json type here since the library doesn't support std::optional
Json store_config;
std::vector<uint32_t> extra_inventory_items;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(
Config, // NOLINT(misc-const-correctness)
$version,
logging,
unlock_family_sharing,
default_app_status,
override_app_id,
override_app_status,
override_dlc_status,
extra_dlcs,
auto_inject_inventory,
extra_inventory_items,
store_config
extra_inventory_items
)
};
extern Config instance;
void init_config();
std::vector<DLC> get_extra_dlcs(AppId_t app_id);
Vector<DLC> get_extra_dlcs(AppId_t app_id);
bool is_dlc_unlocked(uint32_t app_id, uint32_t dlc_id, const Function<bool()>& original_function);
bool is_dlc_unlocked(uint32_t app_id, uint32_t dlc_id, const std::function<bool()>& original_function);
DLL_EXPORT(void) ReloadConfig();
}

View File

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

View File

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

View File

@@ -1,24 +1,19 @@
#include "koalabox/paths.hpp"
#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/str.hpp>
#include <koalabox/paths.hpp>
#include <koalabox/util.hpp>
#include <koalabox/win_util.hpp>
#include <build_config.h>
#include <common/steamclient_exports.hpp>
#include <core/globals.hpp>
#include <core/paths.hpp>
#include <smoke_api/config.hpp>
#include <smoke_api/smoke_api.hpp>
#include "build_config.h"
#if COMPILE_STORE_MODE
#include <store_mode/store.hpp>
#endif
#include "exports/steamclient.hpp"
#include "smoke_api/config.hpp"
#include "smoke_api/globals.hpp"
#include "smoke_api/smoke_api.hpp"
// Hooking steam_api has shown itself to be less desirable than steamclient
// for the reasons outlined below:
@@ -39,6 +34,12 @@
// 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) {
@@ -54,107 +55,72 @@ namespace {
void init_proxy_mode() {
LOG_INFO("Detected proxy mode");
override_app_id();
globals::steamapi_module =
koalabox::loader::load_original_library(paths::get_self_path(), STEAMAPI_DLL);
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");
LOG_INFO("Detected hook mode");
override_app_id();
kb::hook::init(true);
koalabox::dll_monitor::init_listener(STEAMCLIENT_DLL, [](const HMODULE& library) {
kb::dll_monitor::init_listener(STEAMCLIENT_DLL, [](const HMODULE& library) {
globals::steamclient_module = library;
DETOUR_STEAMCLIENT(CreateInterface)
koalabox::dll_monitor::shutdown_listener();
kb::dll_monitor::shutdown_listener();
});
}
bool is_valve_steam(const String& exe_name) noexcept {
try {
if (not koalabox::str::eq(exe_name, "steam.exe")) {
return false;
}
// Verify that it's steam from valve,
// and not some other executable coincidentally named steam
const HMODULE steam_handle = koalabox::win_util::get_module_handle_or_throw(nullptr);
const auto manifest = koalabox::win_util::get_module_manifest(steam_handle);
// Steam.exe manifest is expected to contain this string
return manifest.contains("valvesoftware.steam.steam");
} catch (const Exception& e) {
LOG_ERROR("{} -> {}", __func__, e.what());
return false;
}
}
}
namespace smoke_api {
void init(const HMODULE module_handle) {
// FIXME: IMPORTANT! Non ascii paths in directories will result in init errors
try {
DisableThreadLibraryCalls(module_handle);
kb::globals::init_globals(module_handle, PROJECT_NAME);
koalabox::globals::init_globals(module_handle, PROJECT_NAME);
globals::smokeapi_handle = module_handle;
config::init_config();
config::instance = kb::config::parse<config::Config>();
if (config::instance.logging) {
koalabox::logger::init_file_logger(koalabox::paths::get_log_path());
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 Path exe_path = koalabox::win_util::get_module_file_name_or_throw(nullptr);
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, BITNESS);
LOG_DEBUG("Process name: '{}' [{}-bit]", exe_name, kb::util::BITNESS);
const bool is_hook_mode =
koalabox::hook::is_hook_mode(globals::smokeapi_handle, STEAMAPI_DLL);
override_app_id();
if (is_hook_mode) {
koalabox::hook::init(true);
if (is_valve_steam(exe_name)) {
#if COMPILE_STORE_MODE
LOG_INFO("Detected Store mode");
store::init_store_mode();
#endif
} else {
init_hook_mode();
}
if (kb::hook::is_hook_mode(module_handle, STEAMAPI_DLL)) {
init_hook_mode();
} else {
init_proxy_mode();
}
LOG_INFO("Initialization complete");
} catch (const Exception& ex) {
koalabox::util::panic(fmt::format("Initialization error: {}", ex.what()));
} catch (const std::exception& ex) {
kb::util::panic(fmt::format("Initialization error: {}", ex.what()));
}
}
void shutdown() {
try {
if (globals::steamapi_module != nullptr) {
koalabox::win_util::free_library(globals::steamapi_module);
kb::win_util::free_library(globals::steamapi_module);
globals::steamapi_module = nullptr;
}
LOG_INFO("Shutdown complete");
} catch (const Exception& ex) {
LOG_ERROR("Shutdown error: {}", ex.what());
} catch (const std::exception& e) {
const auto msg = std::format("Shutdown error: {}", e.what());
LOG_ERROR(msg);
}
koalabox::logger::shutdown();
kb::logger::shutdown();
}
}

26
src/smoke_api/types.cpp Normal file
View File

@@ -0,0 +1,26 @@
#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;
}

144
src/smoke_api/types.hpp Normal file
View File

@@ -0,0 +1,144 @@
#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";
// TODO: Delete
constexpr auto CLIENT_ENGINE = "CLIENTENGINE_INTERFACE_VERSION";
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);
};