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

View File

@@ -1,49 +0,0 @@
#include <common/app_cache.hpp>
#include <core/paths.hpp>
#include <koalabox/cache.hpp>
#include <koalabox/logger.hpp>
constexpr auto KEY_APPS = "apps";
AppDlcNameMap get_cached_apps() noexcept {
try {
return koalabox::cache::get(KEY_APPS).get<AppDlcNameMap>();
} catch (const Exception& e) {
LOG_WARN("Failed to get cached apps: {}", e.what());
return {};
}
}
namespace smoke_api::app_cache {
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 Exception& e) {
LOG_ERROR("Error reading DLCs from disk cache: ", e.what());
return {};
}
}
bool save_dlcs(AppId_t app_id, const 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, Json(apps));
} catch (const Exception& e) {
LOG_ERROR("Error saving DLCs to disk cache: {}", e.what());
return false;
}
}
}

View File

@@ -1,11 +0,0 @@
#pragma once
#include <core/types.hpp>
namespace smoke_api::app_cache {
Vector<DLC> get_dlcs(AppId_t app_id) noexcept;
bool save_dlcs(AppId_t app_id, const Vector<DLC>& dlcs) noexcept;
}

View File

@@ -1,13 +0,0 @@
#include <core/types.hpp>
#include <common/steamclient_exports.hpp>
#include <steam_impl/steam_client.hpp>
DLL_EXPORT(void*) CreateInterface(const char* interface_string, int* out_result) {
return steam_client::GetGenericInterface(
__func__, interface_string, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(CreateInterface)
return CreateInterface_o(interface_string, out_result);
}
);
}

View File

@@ -1,3 +0,0 @@
#include <core/types.hpp>
DLL_EXPORT(void*) CreateInterface(const char* interface_string, int* out_result);

View File

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

View File

@@ -1,28 +0,0 @@
#include <core/paths.hpp>
#include <core/globals.hpp>
#include <koalabox/loader.hpp>
// TODO: Refactor to KoalaBox
namespace paths {
Path get_self_path() {
static const auto path = koalabox::loader::get_module_dir(globals::smokeapi_handle);
return path;
}
Path get_config_path() {
static const auto path = get_self_path() / "SmokeAPI.config.json";
return path;
}
Path get_cache_path() {
static const auto path = get_self_path() / "SmokeAPI.cache.json";
return path;
}
Path get_log_path() {
static const auto path = get_self_path() / "SmokeAPI.log.log";
return path;
}
}

View File

@@ -1,15 +0,0 @@
#pragma once
#include <koalabox/core.hpp>
namespace paths {
/**
* @return An std::path instance representing the directory containing this DLL
*/
Path get_self_path();
Path get_config_path();
Path get_cache_path();
Path get_log_path();
}

View File

@@ -1,12 +1,13 @@
#include <smoke_api/config.hpp>
#include <core/globals.hpp>
#include <koalabox/hook.hpp>
#include <koalabox/logger.hpp>
#include "smoke_api/config.hpp"
#include "smoke_api/globals.hpp"
// TODO: Detour in hook mode
DLL_EXPORT(bool) SteamAPI_RestartAppIfNecessary(
[[maybe_unused]] const uint32_t unOwnAppID
) {
DLL_EXPORT(bool) SteamAPI_RestartAppIfNecessary(const uint32_t unOwnAppID) {
if (smoke_api::config::instance.override_app_id != 0) {
LOG_DEBUG("{} -> Preventing app restart", __func__);
LOG_DEBUG("{} -> {}. Preventing app restart", unOwnAppID, __func__);
return false;
}

View File

@@ -1,25 +1,27 @@
#include <koalabox/logger.hpp>
#include <koalabox/hook.hpp>
#include <core/globals.hpp>
#include <steam_impl/steam_apps.hpp>
#include <steam_impl/steam_client.hpp>
#include <steam_impl/steam_inventory.hpp>
#include <steam_impl/steam_user.hpp>
#include <steam_impl/steam_impl.hpp>
#include "smoke_api/types.hpp"
#include "smoke_api/globals.hpp"
#include "steam_interface/steam_apps.hpp"
#include "steam_interface/steam_client.hpp"
#include "steam_interface/steam_interface.hpp"
#include "steam_interface/steam_inventory.hpp"
#include "steam_interface/steam_user.hpp"
// ISteamApps
DLL_EXPORT(bool) SteamAPI_ISteamApps_BIsSubscribedApp(void* self, AppId_t dlcID) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
static const auto app_id = steam_interface::get_app_id();
return steam_apps::IsDlcUnlocked(
__func__, app_id, dlcID, [&]() {
__func__, app_id, dlcID, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamApps_BIsSubscribedApp)
return SteamAPI_ISteamApps_BIsSubscribedApp_o(self, dlcID);
}
);
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
@@ -27,15 +29,15 @@ DLL_EXPORT(bool) SteamAPI_ISteamApps_BIsSubscribedApp(void* self, AppId_t dlcID)
DLL_EXPORT(bool) SteamAPI_ISteamApps_BIsDlcInstalled(void* self, AppId_t dlcID) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
static const auto app_id = steam_interface::get_app_id();
return steam_apps::IsDlcUnlocked(
__func__, app_id, dlcID, [&]() {
__func__, app_id, dlcID, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamApps_BIsDlcInstalled)
return SteamAPI_ISteamApps_BIsDlcInstalled_o(self, dlcID);
}
);
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
@@ -43,15 +45,15 @@ DLL_EXPORT(bool) SteamAPI_ISteamApps_BIsDlcInstalled(void* self, AppId_t dlcID)
DLL_EXPORT(int) SteamAPI_ISteamApps_GetDLCCount(void* self) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
static const auto app_id = steam_interface::get_app_id();
return steam_apps::GetDLCCount(
__func__, app_id, [&]() {
__func__, app_id, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamApps_GetDLCCount)
return SteamAPI_ISteamApps_GetDLCCount_o(self);
}
);
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return 0;
}
@@ -66,10 +68,10 @@ DLL_EXPORT(bool) SteamAPI_ISteamApps_BGetDLCDataByIndex(
int cchNameBufferSize
) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
static const auto app_id = steam_interface::get_app_id();
return steam_apps::GetDLCDataByIndex(
__func__, app_id, iDLC, pDlcID, pbAvailable, pchName, cchNameBufferSize,
[&]() {
[&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamApps_BGetDLCDataByIndex)
return SteamAPI_ISteamApps_BGetDLCDataByIndex_o(
self, iDLC, pDlcID, pbAvailable, pchName, cchNameBufferSize
@@ -79,7 +81,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamApps_BGetDLCDataByIndex(
return SteamAPI_ISteamApps_BIsDlcInstalled(self, dlc_id);
}
);
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
@@ -95,12 +97,12 @@ DLL_EXPORT(void*) SteamAPI_ISteamClient_GetISteamGenericInterface(
) {
try {
return steam_client::GetGenericInterface(
__func__, pchVersion, [&]() {
__func__, pchVersion, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamClient_GetISteamGenericInterface)
return SteamAPI_ISteamClient_GetISteamGenericInterface_o(self, hSteamUser, hSteamPipe, pchVersion);
}
);
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
@@ -113,7 +115,7 @@ DLL_EXPORT(EResult) SteamAPI_ISteamInventory_GetResultStatus(
SteamInventoryResult_t resultHandle
) {
return steam_inventory::GetResultStatus(
__func__, resultHandle, [&]() {
__func__, resultHandle, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_GetResultStatus)
return SteamAPI_ISteamInventory_GetResultStatus_o(self, resultHandle);
@@ -127,7 +129,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamInventory_GetItemDefinitionIDs(
uint32_t* punItemDefIDsArraySize
) {
return steam_inventory::GetItemDefinitionIDs(
__func__, pItemDefIDs, punItemDefIDsArraySize, [&]() {
__func__, pItemDefIDs, punItemDefIDsArraySize, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_GetItemDefinitionIDs)
return SteamAPI_ISteamInventory_GetItemDefinitionIDs_o(self, pItemDefIDs, punItemDefIDsArraySize);
@@ -143,7 +145,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamInventory_GetResultItems(
) {
return steam_inventory::GetResultItems(
__func__, resultHandle, pOutItemsArray, punOutItemsArraySize,
[&]() {
[&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_GetResultItems)
return SteamAPI_ISteamInventory_GetResultItems_o(self, resultHandle, pOutItemsArray, punOutItemsArraySize);
@@ -165,7 +167,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamInventory_GetResultItemProperty(
uint32_t* punValueBufferSizeOut
) {
return steam_inventory::GetResultItemProperty(
__func__, resultHandle, unItemIndex, pchPropertyName, pchValueBuffer, punValueBufferSizeOut, [&]() {
__func__, resultHandle, unItemIndex, pchPropertyName, pchValueBuffer, punValueBufferSizeOut, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_GetResultItemProperty)
return SteamAPI_ISteamInventory_GetResultItemProperty_o(
@@ -181,7 +183,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamInventory_CheckResultSteamID(
CSteamID steamIDExpected
) {
return steam_inventory::CheckResultSteamID(
__func__, resultHandle, steamIDExpected, [&]() {
__func__, resultHandle, steamIDExpected, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_CheckResultSteamID)
return SteamAPI_ISteamInventory_CheckResultSteamID_o(self, resultHandle, steamIDExpected);
@@ -194,7 +196,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamInventory_GetAllItems(
SteamInventoryResult_t* pResultHandle
) {
return steam_inventory::GetAllItems(
__func__, pResultHandle, [&]() {
__func__, pResultHandle, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_GetAllItems)
return SteamAPI_ISteamInventory_GetAllItems_o(self, pResultHandle);
@@ -209,7 +211,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamInventory_GetItemsByID(
uint32_t unCountInstanceIDs
) {
return steam_inventory::GetItemsByID(
__func__, pResultHandle, pInstanceIDs, unCountInstanceIDs, [&]() {
__func__, pResultHandle, pInstanceIDs, unCountInstanceIDs, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_GetItemsByID)
return SteamAPI_ISteamInventory_GetItemsByID_o(self, pResultHandle, pInstanceIDs, unCountInstanceIDs);
@@ -224,7 +226,7 @@ DLL_EXPORT(bool) SteamAPI_ISteamInventory_SerializeResult(
uint32_t* punOutBufferSize
) {
return steam_inventory::SerializeResult(
__func__, resultHandle, pOutBuffer, punOutBufferSize, [&]() {
__func__, resultHandle, pOutBuffer, punOutBufferSize, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamInventory_SerializeResult)
return SteamAPI_ISteamInventory_SerializeResult_o(self, resultHandle, pOutBuffer, punOutBufferSize);
@@ -240,15 +242,15 @@ DLL_EXPORT(EUserHasLicenseForAppResult) SteamAPI_ISteamUser_UserHasLicenseForApp
AppId_t dlcID
) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
static const auto app_id = steam_interface::get_app_id();
return steam_user::UserHasLicenseForApp(
__func__, app_id, dlcID, [&]() {
__func__, app_id, dlcID, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamAPI_ISteamUser_UserHasLicenseForApp)
return SteamAPI_ISteamUser_UserHasLicenseForApp_o(self, steamID, dlcID);
}
);
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return k_EUserHasLicenseResultDoesNotHaveLicense;
}

View File

@@ -0,0 +1,21 @@
#include <koalabox/hook.hpp>
#include "smoke_api/globals.hpp"
#include "smoke_api/types.hpp"
#include "steam_interface/steam_client.hpp"
DLL_EXPORT(void*) SteamInternal_FindOrCreateUserInterface(HSteamUser hSteamUser, const char* version) {
return steam_client::GetGenericInterface(__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamInternal_FindOrCreateUserInterface)
return SteamInternal_FindOrCreateUserInterface_o(hSteamUser, version);
});
}
DLL_EXPORT(void*) SteamInternal_CreateInterface(const char* version) {
return steam_client::GetGenericInterface(__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamInternal_CreateInterface)
return SteamInternal_CreateInterface_o(version);
});
}

View File

@@ -0,0 +1,90 @@
#include <regex>
#include <koalabox/hook.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include <koalabox/win_util.hpp>
#include "smoke_api/globals.hpp"
#include "smoke_api/types.hpp"
#include "steam_interface/steam_client.hpp"
namespace {
/**
* Searches the `.rdata` section of the original dll for the full interface version string
* Results are cached for performance.
*/
std::string
get_versioned_interface(const std::string& version_prefix, const std::string& fallback) {
static std::map<std::string, std::string> version_map;
if (not version_map.contains(version_prefix)) {
try {
const std::string rdata = koalabox::win_util::get_pe_section_data_or_throw(
globals::steamapi_module, ".rdata"
);
const std::regex regex(version_prefix + "\\d{3}");
std::smatch match;
if (std::regex_search(rdata, match, regex)) {
version_map[version_prefix] = match[0];
return version_map[version_prefix];
}
throw koalabox::util::exception("No match found for '{}'", version_prefix);
} catch (const std::exception& ex) {
LOG_ERROR(
"Failed to get versioned interface: {}."
"Falling back to version {}",
ex.what(),
fallback
);
version_map[version_prefix] = version_prefix + fallback;
}
}
return version_map[version_prefix];
}
}
DLL_EXPORT(void*) SteamClient() {
static auto version = get_versioned_interface(STEAM_CLIENT, "006");
return steam_client::GetGenericInterface(__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamClient)
return SteamClient_o();
});
}
DLL_EXPORT(void*) SteamApps() {
static auto version = get_versioned_interface(STEAM_APPS, "002");
return steam_client::GetGenericInterface(__func__, version, [&]() {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamApps)
return SteamApps_o();
});
}
DLL_EXPORT(void*) SteamUser() {
static auto version = get_versioned_interface(STEAM_USER, "012");
return steam_client::GetGenericInterface(__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamUser)
return SteamUser_o();
});
}
DLL_EXPORT(void*) SteamInventory() {
static auto version = get_versioned_interface(STEAM_INVENTORY, "001");
return steam_client::GetGenericInterface(__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamInventory)
return SteamInventory_o();
});
}

View File

@@ -0,0 +1,13 @@
#include <koalabox/hook.hpp>
#include "exports/steamclient.hpp"
#include "smoke_api/types.hpp"
#include "steam_interface/steam_client.hpp"
C_DECL(void*) CreateInterface(const char* interface_string, int* out_result) {
return steam_client::GetGenericInterface(__func__, interface_string, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(CreateInterface)
return CreateInterface_o(interface_string, out_result);
});
}

View File

@@ -0,0 +1,5 @@
#pragma once
#include "smoke_api/types.hpp"
C_DECL(void*) CreateInterface(const char* interface_string, int* out_result);

View File

@@ -1,22 +0,0 @@
#include <core/globals.hpp>
#include <steam_impl/steam_client.hpp>
DLL_EXPORT(void*) SteamInternal_FindOrCreateUserInterface(HSteamUser hSteamUser, const char* version) {
return steam_client::GetGenericInterface(
__func__, version, [&]() {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamInternal_FindOrCreateUserInterface)
return SteamInternal_FindOrCreateUserInterface_o(hSteamUser, version);
}
);
}
DLL_EXPORT(void*) SteamInternal_CreateInterface(const char* version) {
return steam_client::GetGenericInterface(
__func__, version, [&]() {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamInternal_CreateInterface)
return SteamInternal_CreateInterface_o(version);
}
);
}

View File

@@ -1,88 +0,0 @@
#include <regex>
#include <koalabox/logger.hpp>
#include <koalabox/win_util.hpp>
#include <koalabox/util.hpp>
#include <core/globals.hpp>
#include <steam_impl/steam_client.hpp>
/**
* Searches the `.rdata` section of the original dll for the full interface version string
* Results are cached for performance.
*/
String get_versioned_interface(const String& version_prefix, const String& fallback) {
static Map<String, String> version_map;
if (not version_map.contains(version_prefix)) {
try {
const String rdata = koalabox::win_util::get_pe_section_data_or_throw(globals::steamapi_module, ".rdata");
const std::regex regex(version_prefix + "\\d{3}");
std::smatch match;
if (std::regex_search(rdata, match, regex)) {
version_map[version_prefix] = match[0];
return version_map[version_prefix];
}
throw koalabox::util::exception("No match found for '{}'", version_prefix);
} catch (const Exception& ex) {
LOG_ERROR(
"Failed to get versioned interface: {}."
"Falling back to version {}", ex.what(), fallback
);
version_map[version_prefix] = version_prefix + fallback;
}
}
return version_map[version_prefix];
}
DLL_EXPORT(void*) SteamClient() {
static auto version = get_versioned_interface(STEAM_CLIENT, "006");
return steam_client::GetGenericInterface(
__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamClient)
return SteamClient_o();
}
);
}
DLL_EXPORT(void*) SteamApps() {
static auto version = get_versioned_interface(STEAM_APPS, "002");
return steam_client::GetGenericInterface(
__func__, version, [&]() {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamApps)
return SteamApps_o();
}
);
}
DLL_EXPORT(void*) SteamUser() {
static auto version = get_versioned_interface(STEAM_USER, "012");
return steam_client::GetGenericInterface(
__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamUser)
return SteamUser_o();
}
);
}
DLL_EXPORT(void*) SteamInventory() {
static auto version = get_versioned_interface(STEAM_INVENTORY, "001");
return steam_client::GetGenericInterface(
__func__, version, [&] {
GET_ORIGINAL_FUNCTION_STEAMAPI(SteamInventory)
return SteamInventory_o();
}
);
}

View File

@@ -1,81 +0,0 @@
#include <game_mode/virtuals/steam_api_virtuals.hpp>
#include <koalabox/util.hpp>
#include <steam_impl/steam_apps.hpp>
#include <steam_impl/steam_impl.hpp>
VIRTUAL(bool) ISteamApps_BIsSubscribedApp(PARAMS(AppId_t dlc_id)) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
return steam_apps::IsDlcUnlocked(
__func__, app_id, dlc_id, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_BIsSubscribedApp)
return ISteamApps_BIsSubscribedApp_o(ARGS(dlc_id));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}
VIRTUAL(bool) ISteamApps_BIsDlcInstalled(PARAMS(AppId_t dlc_id)) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
return steam_apps::IsDlcUnlocked(
__func__, app_id, dlc_id, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_BIsDlcInstalled)
return ISteamApps_BIsDlcInstalled_o(ARGS(dlc_id));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}
VIRTUAL(int) ISteamApps_GetDLCCount(PARAMS()) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
return steam_apps::GetDLCCount(
__func__, app_id, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_GetDLCCount)
return ISteamApps_GetDLCCount_o(ARGS());
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return 0;
}
}
VIRTUAL(bool) ISteamApps_BGetDLCDataByIndex(
PARAMS(
int iDLC,
AppId_t* p_dlc_id,
bool* pbAvailable,
char* pchName,
int cchNameBufferSize
)
) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
return steam_apps::GetDLCDataByIndex(
__func__, app_id, iDLC, p_dlc_id, pbAvailable, pchName, cchNameBufferSize,
[&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_BGetDLCDataByIndex)
return ISteamApps_BGetDLCDataByIndex_o(
ARGS(iDLC, p_dlc_id, pbAvailable, pchName, cchNameBufferSize)
);
},
[&](AppId_t dlc_id) {
return ISteamApps_BIsDlcInstalled(ARGS(dlc_id));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}

View File

@@ -1,85 +0,0 @@
#include <game_mode/virtuals/steam_api_virtuals.hpp>
#include <steam_impl/steam_client.hpp>
VIRTUAL(void*) ISteamClient_GetISteamApps(
PARAMS(
HSteamUser hSteamUser,
HSteamPipe hSteamPipe,
const char* version
)
) {
try {
return steam_client::GetGenericInterface(
__func__, version, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamApps)
return ISteamClient_GetISteamApps_o(ARGS(hSteamUser, hSteamPipe, version));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}
VIRTUAL(void*) ISteamClient_GetISteamUser(
PARAMS(
HSteamUser hSteamUser,
HSteamPipe hSteamPipe,
const char* version
)
) {
try {
return steam_client::GetGenericInterface(
__func__, version, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamUser)
return ISteamClient_GetISteamUser_o(ARGS(hSteamUser, hSteamPipe, version));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}
VIRTUAL(void*) ISteamClient_GetISteamGenericInterface(
PARAMS(
HSteamUser hSteamUser,
HSteamPipe hSteamPipe,
const char* pchVersion
)
) {
try {
return steam_client::GetGenericInterface(
__func__, pchVersion, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamGenericInterface)
return ISteamClient_GetISteamGenericInterface_o(ARGS(hSteamUser, hSteamPipe, pchVersion));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}
VIRTUAL(void*) ISteamClient_GetISteamInventory(
PARAMS(
HSteamUser hSteamUser,
HSteamPipe hSteamPipe,
const char* pchVersion
)
) {
try {
return steam_client::GetGenericInterface(
__func__, pchVersion, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamInventory)
return ISteamClient_GetISteamInventory_o(ARGS(hSteamUser, hSteamPipe, pchVersion));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}

View File

@@ -1,126 +0,0 @@
#include <game_mode/virtuals/steam_api_virtuals.hpp>
#include <steam_impl/steam_inventory.hpp>
VIRTUAL(EResult) ISteamInventory_GetResultStatus(PARAMS(SteamInventoryResult_t resultHandle)) {
return steam_inventory::GetResultStatus(
__func__, resultHandle, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetResultStatus)
return ISteamInventory_GetResultStatus_o(ARGS(resultHandle));
}
);
}
VIRTUAL(bool) ISteamInventory_GetResultItems(
PARAMS(
SteamInventoryResult_t resultHandle,
SteamItemDetails_t* pOutItemsArray,
uint32_t * punOutItemsArraySize
)
) {
return steam_inventory::GetResultItems(
__func__, resultHandle, pOutItemsArray, punOutItemsArraySize,
[&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetResultItems)
return ISteamInventory_GetResultItems_o(ARGS(resultHandle, pOutItemsArray, punOutItemsArraySize));
},
[&](SteamItemDef_t* pItemDefIDs, uint32_t* punItemDefIDsArraySize) {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetItemDefinitionIDs)
return ISteamInventory_GetItemDefinitionIDs_o(ARGS(pItemDefIDs, punItemDefIDsArraySize));
}
);
}
VIRTUAL(bool) ISteamInventory_GetResultItemProperty(
PARAMS(
SteamInventoryResult_t resultHandle,
uint32_t unItemIndex,
const char* pchPropertyName,
char* pchValueBuffer,
uint32_t * punValueBufferSizeOut
)
) {
return steam_inventory::GetResultItemProperty(
__func__, resultHandle, unItemIndex, pchPropertyName, pchValueBuffer, punValueBufferSizeOut, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetResultItemProperty)
return ISteamInventory_GetResultItemProperty_o(
ARGS(resultHandle, unItemIndex, pchPropertyName, pchValueBuffer, punValueBufferSizeOut)
);
}
);
}
VIRTUAL(bool) ISteamInventory_CheckResultSteamID(
PARAMS(
SteamInventoryResult_t resultHandle,
CSteamID steamIDExpected
)
) {
return steam_inventory::CheckResultSteamID(
__func__, resultHandle, steamIDExpected, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_CheckResultSteamID)
return ISteamInventory_CheckResultSteamID_o(ARGS(resultHandle, steamIDExpected));
}
);
}
VIRTUAL(bool) ISteamInventory_GetAllItems(PARAMS(SteamInventoryResult_t* pResultHandle)) {
return steam_inventory::GetAllItems(
__func__, pResultHandle, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetAllItems)
return ISteamInventory_GetAllItems_o(ARGS(pResultHandle));
}
);
}
VIRTUAL(bool) ISteamInventory_GetItemsByID(
PARAMS(
SteamInventoryResult_t* pResultHandle,
const SteamItemInstanceID_t* pInstanceIDs,
uint32_t unCountInstanceIDs
)
) {
return steam_inventory::GetItemsByID(
__func__, pResultHandle, pInstanceIDs, unCountInstanceIDs, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetItemsByID)
return ISteamInventory_GetItemsByID_o(ARGS(pResultHandle, pInstanceIDs, unCountInstanceIDs));
}
);
}
VIRTUAL(bool) ISteamInventory_SerializeResult(
PARAMS(
SteamInventoryResult_t resultHandle,
void* pOutBuffer,
uint32_t * punOutBufferSize
)
) {
return steam_inventory::SerializeResult(
__func__, resultHandle, pOutBuffer, punOutBufferSize, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_SerializeResult)
return ISteamInventory_SerializeResult_o(ARGS(resultHandle, pOutBuffer, punOutBufferSize));
}
);
}
VIRTUAL(bool) ISteamInventory_GetItemDefinitionIDs(
PARAMS(
SteamItemDef_t* pItemDefIDs,
uint32_t * punItemDefIDsArraySize
)
) {
return steam_inventory::GetItemDefinitionIDs(
__func__, pItemDefIDs, punItemDefIDsArraySize, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetItemDefinitionIDs)
return ISteamInventory_GetItemDefinitionIDs_o(ARGS(pItemDefIDs, punItemDefIDsArraySize));
}
);
}

View File

@@ -1,20 +0,0 @@
#include <game_mode/virtuals/steam_api_virtuals.hpp>
#include <steam_impl/steam_user.hpp>
#include <steam_impl/steam_impl.hpp>
#include <koalabox/logger.hpp>
VIRTUAL(EUserHasLicenseForAppResult) ISteamUser_UserHasLicenseForApp(PARAMS(CSteamID steamID, AppId_t dlc_id)) {
try {
static const auto app_id = steam_impl::get_app_id_or_throw();
return steam_user::UserHasLicenseForApp(
__func__, app_id, dlc_id, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamUser_UserHasLicenseForApp)
return ISteamUser_UserHasLicenseForApp_o(ARGS(steamID, dlc_id));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return k_EUserHasLicenseResultDoesNotHaveLicense;
}
}

View File

@@ -1,8 +1,8 @@
#include <Windows.h>
#include <smoke_api/smoke_api.hpp>
// This header will be populated at build time
#include <linker_exports.h>
#include "linker_exports_for_steam_api.h"
#include "linker_exports_for_version.h"
EXTERN_C [[maybe_unused]] BOOL WINAPI
DllMain(const HMODULE module_handle, const DWORD reason, LPVOID) {

View File

@@ -1,35 +1,40 @@
#include <core/api.hpp>
#include <koalabox/logger.hpp>
#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;
Vector<DLC> dlcs;
std::vector<DLC> dlcs;
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(SteamResponse, success, dlcs) // NOLINT(misc-const-correctness)
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(
SteamResponse, success, dlcs
) // NOLINT(misc-const-correctness)
};
std::optional<Vector<DLC>> fetch_dlcs_from_github(AppId_t app_id) noexcept {
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* 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 Json::exception& e) {
} catch (const nlohmann::json::exception& e) {
LOG_ERROR("Failed to fetch dlc list from GitHub: {}", e.what());
return std::nullopt;
}
}
std::optional<Vector<DLC>> fetch_dlcs_from_steam(AppId_t app_id) noexcept {
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 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>();
@@ -39,7 +44,7 @@ namespace api {
}
return response.dlcs;
} catch (const Exception& e) {
} 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

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

View File

@@ -1,12 +1,8 @@
#pragma once
#include <koalabox/core.hpp>
namespace globals {
extern HMODULE smokeapi_handle;
extern HMODULE steamclient_module;
extern HMODULE steamapi_module;
extern HMODULE vstdlib_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();
}
}

View File

@@ -1,13 +1,13 @@
#include <core/types.hpp>
#include "smoke_api/types.hpp"
Vector<DLC> DLC::get_dlcs_from_apps(const AppDlcNameMap& apps, AppId_t app_id) {
Vector<DLC> dlcs;
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) {
for (auto const& [id, name] : app.dlcs) {
dlcs.emplace_back(id, name);
}
}
@@ -15,10 +15,10 @@ Vector<DLC> DLC::get_dlcs_from_apps(const AppDlcNameMap& apps, AppId_t app_id) {
return dlcs;
}
DlcNameMap DLC::get_dlc_map_from_vector(const Vector<DLC>& dlcs) {
DlcNameMap DLC::get_dlc_map_from_vector(const std::vector<DLC>& dlcs) {
DlcNameMap map;
for (const auto& dlc: dlcs) {
for (const auto& dlc : dlcs) {
map[dlc.get_id_str()] = dlc.get_name();
}

View File

@@ -1,9 +1,9 @@
#pragma once
#include <koalabox/core.hpp>
// ReSharper disable once CppUnusedIncludeDirective
#include <koalabox/hook.hpp> // Used by several macros
#include <koalabox/logger.hpp>
#include <cstdint>
#include <map>
#include <string>
#include <nlohmann/json.hpp>
/**
@@ -24,7 +24,7 @@
*
* In x86-64 however, there is only one calling convention,
* so __fastcall is simply ignored. However, RDX in this case
* will store_mode the 1st actual argument to the function, so we
* 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.
@@ -47,6 +47,7 @@
#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) \
@@ -62,22 +63,15 @@
#define DETOUR_ADDRESS(FUNC, ADDRESS) \
koalabox::hook::detour_or_warn(ADDRESS, #FUNC, reinterpret_cast<uintptr_t>(FUNC));
#define $DETOUR(FUNC, NAME, MODULE_HANDLE) \
#define _DETOUR(FUNC, NAME, MODULE_HANDLE) \
koalabox::hook::detour_or_warn(MODULE_HANDLE, NAME, reinterpret_cast<uintptr_t>(FUNC));
#define DETOUR_STEAMCLIENT(FUNC) $DETOUR(FUNC, #FUNC, globals::steamclient_module)
#define DETOUR_VSTDLIB(FUNC) $DETOUR(vstdlib::FUNC, #FUNC, globals::vstdlib_module)
#ifdef _WIN64
#define COMPILE_STORE_MODE 0
#else
#define COMPILE_STORE_MODE 1
#endif
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;
@@ -106,10 +100,10 @@ enum EUserHasLicenseForAppResult {
// These aliases exist solely to increase code readability
using AppIdKey = String;
using DlcIdKey = String;
using DlcNameValue = String;
using DlcNameMap = Map<DlcIdKey, DlcNameValue>;
using AppIdKey = std::string;
using DlcIdKey = std::string;
using DlcNameValue = std::string;
using DlcNameMap = std::map<DlcIdKey, DlcNameValue>;
struct App {
DlcNameMap dlcs;
@@ -117,34 +111,34 @@ struct App {
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(App, dlcs) // NOLINT(misc-const-correctness)
};
using AppDlcNameMap = Map<AppIdKey, App>;
using AppDlcNameMap = std::map<AppIdKey, App>;
class DLC {
private:
// These 2 names must match the property names from Steam API
String appid;
String name;
std::string appid;
std::string name;
public:
explicit DLC() = default;
explicit DLC(String appid, String name) : appid{std::move(appid)}, name{std::move(name)} {}
explicit DLC(std::string appid, std::string name)
: appid{std::move(appid)}, name{std::move(name)} {}
[[nodiscard]] String get_id_str() const {
[[nodiscard]] std::string get_id_str() const {
return appid;
};
}
[[nodiscard]] uint32_t get_id() const {
return std::stoi(appid);
};
}
[[nodiscard]] String get_name() const {
[[nodiscard]] std::string get_name() const {
return name;
};
}
NLOHMANN_DEFINE_TYPE_INTRUSIVE(DLC, appid, name)
static Vector<DLC> get_dlcs_from_apps(const AppDlcNameMap& apps, AppId_t app_id);
static std::vector<DLC> get_dlcs_from_apps(const AppDlcNameMap& apps, AppId_t app_id);
static DlcNameMap get_dlc_map_from_vector(const Vector<DLC>& vector);
static DlcNameMap get_dlc_map_from_vector(const std::vector<DLC>& vector);
};

View File

@@ -1,32 +0,0 @@
#pragma once
#include <core/types.hpp>
namespace steam_apps {
bool IsDlcUnlocked(
const String& function_name,
AppId_t app_id,
AppId_t dlc_id,
const Function<bool()>& original_function
);
int GetDLCCount(
const String& function_name,
AppId_t app_id,
const Function<int()>& original_function
);
bool GetDLCDataByIndex(
const String& function_name,
AppId_t app_id,
int iDLC,
AppId_t* pDlcId,
bool* pbAvailable,
char* pchName,
int cchNameBufferSize,
const Function<bool()>& original_function,
const Function<bool(AppId_t)>& is_originally_unlocked // Aux function to resolve original dlc status
);
}

View File

@@ -1,21 +0,0 @@
#include <steam_impl/steam_client.hpp>
#include <steam_impl/steam_impl.hpp>
#include <koalabox/logger.hpp>
namespace steam_client {
void* GetGenericInterface(
const String& function_name,
const String& interface_version,
const Function<void*()>& original_function
) {
auto* const interface = original_function();
LOG_DEBUG("{} -> '{}' @ {}", function_name, interface_version, interface);
steam_impl::hook_virtuals(interface, interface_version);
return interface;
}
}

View File

@@ -1,13 +0,0 @@
#pragma once
#include <core/types.hpp>
namespace steam_client {
void* GetGenericInterface(
const String& function_name,
const String& interface_version,
const Function<void*()>& original_function
);
}

View File

@@ -1,301 +0,0 @@
#include <ranges>
#include <steam_impl/steam_impl.hpp>
#include <game_mode/virtuals/steam_api_virtuals.hpp>
#include <common/steamclient_exports.hpp>
#include <build_config.h>
#include <koalabox/util.hpp>
#include <koalabox/win_util.hpp>
#include <koalabox/logger.hpp>
#include <polyhook2/Misc.hpp>
#if COMPILE_STORE_MODE
#include <store_mode/steamclient/steamclient.hpp>
#endif
namespace steam_impl {
typedef Map<String, Map<int, int>> FunctionOrdinalMap;
FunctionOrdinalMap steam_client_ordinal_map = { // NOLINT(cert-err58-cpp)
{"ISteamClient_GetISteamUser",
{
{6, 6},
{7, 5},
}
},
{"ISteamClient_GetISteamUtils",
{
{6, 12},
{7, 9},
}
},
{"ISteamClient_GetISteamGenericInterface",
{
{7, 14},
{8, 13},
{12, 12},
}
},
{"ISteamClient_GetISteamApps",
{
{6, 16},
{7, 18},
{8, 15},
{9, 16},
{12, 15},
}
},
{"ISteamClient_GetISteamInventory",
{
{17, 34},
{18, 35},
{21, 33},
}
}
};
FunctionOrdinalMap steam_apps_ordinal_map = { // NOLINT(cert-err58-cpp)
{"ISteamApps_BIsSubscribedApp", {{2, 6}}},
{"ISteamApps_BIsDlcInstalled", {{2, 7}}},
{"ISteamApps_GetDLCCount", {{2, 10}}},
{"ISteamApps_BGetDLCDataByIndex", {{2, 11}}},
};
FunctionOrdinalMap steam_user_ordinal_map = { // NOLINT(cert-err58-cpp)
{"ISteamUser_UserHasLicenseForApp", {
{12, 15},
{13, 16},
{15, 17},
{23, 18},
}}
};
FunctionOrdinalMap steam_utils_ordinal_map = { // NOLINT(cert-err58-cpp)
{"ISteamUtils_GetAppID", {{2, 9}}}
};
FunctionOrdinalMap steam_inventory_ordinal_map = { // NOLINT(cert-err58-cpp)
{"ISteamInventory_GetResultStatus", {{1, 0}}},
{"ISteamInventory_GetResultItems", {{1, 1}}},
{"ISteamInventory_GetResultItemProperty", {{2, 2}}},
{"ISteamInventory_CheckResultSteamID", {{1, 3}, {2, 4}}},
{"ISteamInventory_GetAllItems", {{1, 5}, {2, 6}}},
{"ISteamInventory_GetItemsByID", {{1, 6}, {2, 7}}},
{"ISteamInventory_SerializeResult", {{1, 7}, {2, 8}}},
{"ISteamInventory_GetItemDefinitionIDs", {{1, 20}, {2, 21}}},
};
int extract_version_number(
const String& version_string,
const String& prefix,
const int min_version,
const int max_version
) {
LOG_DEBUG("Hooking interface '{}'", version_string);
try {
const auto version_number = stoi(version_string.substr(prefix.length()));
if (version_number < min_version) {
LOG_WARN("Legacy version of {}: {}", version_string, version_number);
}
if (version_number > max_version) {
LOG_WARN(
"Unsupported new version of {}: {}. Fallback version {} will be used",
version_string, version_number, max_version
);
}
return version_number;
} catch ([[maybe_unused]] const std::exception& ex) {
koalabox::util::panic(
"Failed to extract version number from: '{}'. Reason: {}",
version_string, ex.what()
);
}
}
int get_ordinal(
const FunctionOrdinalMap& ordinal_map,
const String& function_name,
int interface_version
) {
const auto& map = ordinal_map.at(function_name);
for (auto [version, ordinal] : std::ranges::reverse_view(map)) {
if (interface_version >= version) {
return ordinal;
}
}
koalabox::util::panic(
"Invalid interface version ({}) for function {}",
interface_version, function_name
);
}
#define HOOK_VIRTUALS(MAP, FUNC) \
koalabox::hook::swap_virtual_func( \
interface, \
#FUNC, \
get_ordinal(MAP, #FUNC, version_number), \
reinterpret_cast<uintptr_t>(FUNC) \
);
#define HOOK_STEAM_CLIENT(FUNC) HOOK_VIRTUALS(steam_client_ordinal_map, FUNC)
#define HOOK_STEAM_APPS(FUNC) HOOK_VIRTUALS(steam_apps_ordinal_map, FUNC)
#define HOOK_STEAM_USER(FUNC) HOOK_VIRTUALS(steam_user_ordinal_map, FUNC)
#define HOOK_STEAM_INVENTORY(FUNC) HOOK_VIRTUALS(steam_inventory_ordinal_map, FUNC)
void hook_virtuals(void* interface, const String& version_string) {
if (interface == nullptr) {
// Game has tried to use an interface before initializing steam api
return;
}
static Set<void*> hooked_interfaces;
if (hooked_interfaces.contains(interface)) {
LOG_DEBUG("Interface {} at {} has already been hooked.", version_string, interface);
}
static Mutex section;
const MutexLockGuard guard(section);
if (version_string.starts_with(STEAM_CLIENT)) {
const auto version_number = extract_version_number(version_string, STEAM_CLIENT, 6, 21);
HOOK_STEAM_CLIENT(ISteamClient_GetISteamApps)
HOOK_STEAM_CLIENT(ISteamClient_GetISteamUser)
if (version_number >= 7) {
HOOK_STEAM_CLIENT(ISteamClient_GetISteamGenericInterface)
}
if (version_number >= 17) {
HOOK_STEAM_CLIENT(ISteamClient_GetISteamInventory)
}
} else if (version_string.starts_with(STEAM_APPS)) {
const auto version_number = extract_version_number(version_string, STEAM_APPS, 2, 8);
HOOK_STEAM_APPS(ISteamApps_BIsSubscribedApp)
if (version_number >= 3) {
HOOK_STEAM_APPS(ISteamApps_BIsDlcInstalled)
}
if (version_number >= 4) {
HOOK_STEAM_APPS(ISteamApps_GetDLCCount)
HOOK_STEAM_APPS(ISteamApps_BGetDLCDataByIndex)
}
} else if (version_string.starts_with(STEAM_USER)) {
const auto version_number = extract_version_number(version_string, STEAM_USER, 9, 23);
if (version_number >= 12) {
HOOK_STEAM_USER(ISteamUser_UserHasLicenseForApp)
}
} else if (version_string.starts_with(STEAM_INVENTORY)) {
const auto version_number = extract_version_number(
version_string, STEAM_INVENTORY, 1, 3
);
HOOK_STEAM_INVENTORY(ISteamInventory_GetResultStatus)
HOOK_STEAM_INVENTORY(ISteamInventory_GetResultItems)
HOOK_STEAM_INVENTORY(ISteamInventory_CheckResultSteamID)
HOOK_STEAM_INVENTORY(ISteamInventory_GetAllItems)
HOOK_STEAM_INVENTORY(ISteamInventory_GetItemsByID)
HOOK_STEAM_INVENTORY(ISteamInventory_SerializeResult)
HOOK_STEAM_INVENTORY(ISteamInventory_GetItemDefinitionIDs)
if (version_number >= 2) {
HOOK_STEAM_INVENTORY(ISteamInventory_GetResultItemProperty)
}
} else if (
version_string.starts_with(CLIENT_ENGINE) &&
!hooked_interfaces.contains(interface)
) {
#if COMPILE_STORE_MODE
store::steamclient::process_client_engine(reinterpret_cast<uintptr_t>(interface));
#endif
} else {
return;
}
hooked_interfaces.insert(interface);
}
HSteamPipe get_steam_pipe_or_throw() {
const auto& steam_api_module = koalabox::win_util::get_module_handle_or_throw(STEAMAPI_DLL);
void* GetHSteamPipe_address;
try {
GetHSteamPipe_address = (void*)koalabox::win_util::get_proc_address_or_throw(
steam_api_module, "SteamAPI_GetHSteamPipe"
);
} catch (...) {
GetHSteamPipe_address = (void*)koalabox::win_util::get_proc_address_or_throw(
steam_api_module, "GetHSteamPipe"
);
}
typedef HSteamPipe (__cdecl*GetHSteamPipe_t)();
const auto GetHSteamPipe_o = (GetHSteamPipe_t)GetHSteamPipe_address;
return GetHSteamPipe_o();
}
template <typename F>
F get_virtual_function(void* interface, int ordinal) {
auto* v_table = (void***)interface;
return (F)(*v_table)[ordinal];
}
template <typename F, typename... Args>
auto call_virtual_function(void* interface, F function, Args... args) {
#ifdef _WIN64
void* RCX = interface;
#else
void* ECX = interface;
void* EDX = nullptr;
#endif
return function(ARGS(args...));
}
AppId_t get_app_id_or_throw() {
// Get CreateInterface
const auto& steam_client_module =
koalabox::win_util::get_module_handle_or_throw(STEAMCLIENT_DLL);
auto* CreateInterface_address = (void*)koalabox::win_util::get_proc_address_or_throw(
steam_client_module, "CreateInterface"
);
auto* CreateInterface_o = PLH::FnCast(CreateInterface_address, CreateInterface);
// Get ISteamClient
int result;
auto* i_steam_client = CreateInterface_o("SteamClient006", &result);
if (i_steam_client == nullptr) {
throw koalabox::util::exception(
"Failed to obtain SteamClient006 interface. Result: {}",
result
);
}
// Get GetISteamUtils
typedef void*** (__fastcall*GetISteamUtils_t)(
PARAMS(HSteamPipe hSteamPipe, const char* version)
);
const auto steam_utils_ordinal = steam_client_ordinal_map["ISteamClient_GetISteamUtils"][6];
const auto GetISteamUtils_o = get_virtual_function<GetISteamUtils_t>(
i_steam_client, steam_utils_ordinal);
// Get ISteamUtils
const auto steam_pipe = get_steam_pipe_or_throw();
auto* i_steam_utils = call_virtual_function(
i_steam_client, GetISteamUtils_o, steam_pipe, "SteamUtils002"
);
// Get GetAppID
typedef uint32_t (__fastcall*GetAppID_t)(PARAMS());
const auto get_app_id_ordinal = steam_utils_ordinal_map["ISteamUtils_GetAppID"][2];
const auto GetAppID_o = get_virtual_function<GetAppID_t>(i_steam_utils, get_app_id_ordinal);
return call_virtual_function(i_steam_utils, GetAppID_o);
}
}

View File

@@ -1,11 +0,0 @@
#pragma once
#include <core/types.hpp>
namespace steam_impl {
void hook_virtuals(void* interface, const String& version_string);
uint32_t get_app_id_or_throw();
}

View File

@@ -1,14 +0,0 @@
#pragma once
#include <core/types.hpp>
namespace steam_user {
EUserHasLicenseForAppResult UserHasLicenseForApp(
const String& function_name,
AppId_t appId,
AppId_t dlcId,
const Function<EUserHasLicenseForAppResult()>& original_function
);
}

View File

@@ -1,10 +1,13 @@
#include <steam_impl/steam_apps.hpp>
#include <common/app_cache.hpp>
#include <smoke_api/config.hpp>
#include <set>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include <core/types.hpp>
#include <core/api.hpp>
#include "smoke_api/api.hpp"
#include "smoke_api/cache.hpp"
#include "smoke_api/config.hpp"
#include "smoke_api/types.hpp"
#include "steam_interface/steam_apps.hpp"
namespace {
/// Steamworks may max GetDLCCount value at 64, depending on how much unowned DLCs the user has.
@@ -12,10 +15,10 @@ namespace {
/// This means we have to get extra DLC IDs from local config, remote config, or cache.
constexpr auto MAX_DLC = 64;
Map<AppId_t, Vector<DLC>> app_dlcs; // NOLINT(cert-err58-cpp)
Set<AppId_t> fully_fetched; // NOLINT(cert-err58-cpp)
std::map<AppId_t, std::vector<DLC>> app_dlcs; // NOLINT(cert-err58-cpp)
std::set<AppId_t> fully_fetched; // NOLINT(cert-err58-cpp)
String get_app_id_log(const AppId_t app_id) {
std::string get_app_id_log(const AppId_t app_id) {
return app_id ? fmt::format("App ID: {:>8}, ", app_id) : "";
}
@@ -24,8 +27,8 @@ namespace {
* @return boolean indicating if the function was able to successfully fetch DLC IDs from all sources.
*/
void fetch_and_cache_dlcs(AppId_t app_id) {
static Mutex mutex;
const MutexLockGuard guard(mutex);
static std::mutex section;
const std::lock_guard lock(section);
if (app_id == 0) {
LOG_ERROR("{} -> App ID is 0", __func__);
@@ -41,9 +44,9 @@ namespace {
// Any of the sources might fail, so we try to get optimal result
// by aggregating results from all the sources into a single set.
Vector<DLC> aggregated_dlcs;
std::vector<DLC> aggregated_dlcs;
const auto append_dlcs = [&](const Vector<DLC>& dlc_list, const String& source_name) {
const auto append_dlcs = [&](const std::vector<DLC>& dlc_list, const std::string& source_name) {
LOG_DEBUG("App ID {} has {} DLCs defined in {}", app_id, dlc_list.size(), source_name);
// Append DLCs to aggregated DLCs
std::ranges::copy(dlc_list, std::back_inserter(aggregated_dlcs));
@@ -64,23 +67,23 @@ namespace {
if (github_dlcs_opt && steam_dlcs_opt) {
fully_fetched.insert(app_id);
} else {
append_dlcs(smoke_api::app_cache::get_dlcs(app_id), "disk cache");
append_dlcs(smoke_api::cache::get_dlcs(app_id), "disk cache");
}
// Cache DLCs in memory and cache for future use
app_dlcs[app_id] = aggregated_dlcs;
smoke_api::app_cache::save_dlcs(app_id, aggregated_dlcs);
smoke_api::cache::save_dlcs(app_id, aggregated_dlcs);
}
}
namespace steam_apps {
bool IsDlcUnlocked(
const String& function_name,
const std::string& function_name,
AppId_t app_id,
AppId_t dlc_id,
const Function<bool()>& original_function
const std::function<bool()>& original_function
) {
try {
const auto unlocked = smoke_api::config::is_dlc_unlocked(app_id, dlc_id, original_function);
@@ -88,13 +91,13 @@ namespace steam_apps {
LOG_INFO("{} -> {}DLC ID: {:>8}, Unlocked: {}", function_name, get_app_id_log(app_id), dlc_id, unlocked);
return unlocked;
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("Uncaught exception: {}", e.what());
return false;
}
}
int GetDLCCount(const String& function_name, const AppId_t app_id, const Function<int()>& original_function) {
int GetDLCCount(const std::string& function_name, const AppId_t app_id, const std::function<int()>& original_function) {
try {
const auto total_count = [&](int count) {
LOG_INFO("{} -> Responding with DLC count: {}", function_name, count);
@@ -117,27 +120,27 @@ namespace steam_apps {
fetch_and_cache_dlcs(app_id);
return total_count(static_cast<int>(app_dlcs[app_id].size()));
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("Uncaught exception: {}", function_name, e.what());
return 0;
}
}
bool GetDLCDataByIndex(
const String& function_name,
const std::string& function_name,
AppId_t app_id,
int iDLC,
AppId_t* pDlcId,
bool* pbAvailable,
char* pchName,
int cchNameBufferSize,
const Function<bool()>& original_function,
const Function<bool(AppId_t)>& is_originally_unlocked
const std::function<bool()>& original_function,
const std::function<bool(AppId_t)>& is_originally_unlocked
) {
try {
LOG_DEBUG("{} -> {}index: {:>3}", function_name, get_app_id_log(app_id), iDLC);
const auto print_dlc_info = [&](const String& tag) {
const auto print_dlc_info = [&](const std::string& tag) {
LOG_INFO(
R"({} -> [{:^12}] {}index: {:>3}, DLC ID: {:>8}, available: {:5}, name: "{}")",
function_name, tag, get_app_id_log(app_id), iDLC, *pDlcId, *pbAvailable, pchName
@@ -183,7 +186,7 @@ namespace steam_apps {
}
return success;
} catch (const Exception& e) {
} catch (const std::exception& e) {
LOG_ERROR("{} -> Uncaught exception: {}", function_name, e.what());
return false;
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include "smoke_api/types.hpp"
namespace steam_apps {
bool IsDlcUnlocked(
const std::string& function_name,
AppId_t app_id,
AppId_t dlc_id,
const std::function<bool()>& original_function
);
int GetDLCCount(
const std::string& function_name,
AppId_t app_id,
const std::function<int()>& original_function
);
bool GetDLCDataByIndex(
const std::string& function_name,
AppId_t app_id,
int iDLC,
AppId_t* pDlcId,
bool* pbAvailable,
char* pchName,
int cchNameBufferSize,
const std::function<bool()>& original_function,
const std::function<bool(AppId_t)>& is_originally_unlocked // Aux function to resolve original dlc status
);
}

View File

@@ -0,0 +1,22 @@
#include <koalabox/logger.hpp>
#include "steam_interface/steam_client.hpp"
#include "steam_interface/steam_interface.hpp"
namespace steam_client {
void* GetGenericInterface(
const std::string& function_name,
const std::string& interface_version,
const std::function<void*()>& original_function
) {
auto* const interface = original_function();
LOG_DEBUG("{} -> '{}' @ {}", function_name, interface_version, interface);
steam_interface::hook_virtuals(interface, interface_version);
return interface;
}
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include "smoke_api/types.hpp"
namespace steam_client {
void* GetGenericInterface(
const std::string& function_name,
const std::string& interface_version,
const std::function<void*()>& original_function
);
}

View File

@@ -0,0 +1,241 @@
#include <ranges>
#include <set>
#include <battery/embed.hpp>
#include <polyhook2/Misc.hpp>
#include <koalabox/hook.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include <koalabox/win_util.hpp>
#include "build_config.h"
#include "steam_interface/steam_interface.hpp"
#include "virtuals/steam_api_virtuals.hpp"
namespace {
struct interface_entry {
// function_name must match the function identifier to be able to call original functions
std::string function_name; // e.g. "ISteamClient_GetISteamApps"
uintptr_t function_address; // e.g. ISteamClient_GetISteamApps
};
struct interface_data {
std::string fallback_version; // e.g. "SteamClient021"
std::map<std::string, interface_entry> entry_map;
// e.g. {ENTRY(ISteamClient, GetISteamApps), ...}
};
std::map<std::string, interface_data> get_virtual_hook_map() {
#define ENTRY(INTERFACE, FUNC) \
{ \
#FUNC, { \
#INTERFACE "_" #FUNC, reinterpret_cast<uintptr_t>(INTERFACE##_##FUNC) \
} \
}
return {
{STEAM_CLIENT,
interface_data{
.fallback_version = "SteamClient021",
.entry_map =
{
ENTRY(ISteamClient, GetISteamApps),
ENTRY(ISteamClient, GetISteamUser),
ENTRY(ISteamClient, GetISteamGenericInterface),
ENTRY(ISteamClient, GetISteamInventory),
}
}},
{STEAM_APPS,
interface_data{
.fallback_version = "STEAMAPPS_INTERFACE_VERSION008",
.entry_map =
{
ENTRY(ISteamApps, BIsSubscribedApp),
ENTRY(ISteamApps, BIsDlcInstalled),
ENTRY(ISteamApps, GetDLCCount),
ENTRY(ISteamApps, BGetDLCDataByIndex),
}
}},
{STEAM_USER,
interface_data{
.fallback_version = "SteamUser023",
.entry_map =
{
ENTRY(ISteamUser, UserHasLicenseForApp),
}
}},
{STEAM_INVENTORY,
interface_data{
.fallback_version = "STEAMINVENTORY_INTERFACE_V003",
.entry_map =
{
ENTRY(ISteamInventory, GetResultStatus),
ENTRY(ISteamInventory, GetResultItems),
ENTRY(ISteamInventory, CheckResultSteamID),
ENTRY(ISteamInventory, GetAllItems),
ENTRY(ISteamInventory, GetItemsByID),
ENTRY(ISteamInventory, SerializeResult),
ENTRY(ISteamInventory, GetItemDefinitionIDs),
}
}},
};
}
nlohmann::json read_interface_lookup() {
const auto lookup_str = b::embed<"res/interface_lookup.json">().str();
return nlohmann::json::parse(lookup_str);
}
const nlohmann::json& find_lookup(
const std::string& interface_version, //
const std::string& fallback_version
) {
static const auto lookup = read_interface_lookup();
if (lookup.contains(interface_version)) {
return lookup[interface_version];
}
LOG_WARN(
"Interface version '{}' not found in lookup map. Using fallback: '{}'",
interface_version,
fallback_version
);
return lookup[fallback_version];
}
}
namespace steam_interface {
namespace kb = koalabox;
void hook_virtuals(void* interface, const std::string& version_string) {
if (interface == nullptr) {
// Game has tried to use an interface before initializing steam api
return;
}
static std::set<void*> processed_interfaces;
if (processed_interfaces.contains(interface)) {
LOG_DEBUG("Interface {} at {} has already been processed.", version_string, interface);
return;
}
static std::mutex section;
const std::lock_guard guard(section);
static const auto virtual_hook_map = get_virtual_hook_map();
for (const auto& [prefix, data] : virtual_hook_map) {
if (version_string.starts_with(prefix)) {
const auto& lookup = find_lookup(version_string, data.fallback_version);
for (const auto& [function, entry] : data.entry_map) {
if (lookup.contains(function)) {
kb::hook::swap_virtual_func(
interface, entry.function_name, lookup[function], entry.function_address
);
}
}
break;
}
}
processed_interfaces.insert(interface);
}
HSteamPipe get_steam_pipe_or_throw() {
// TODO: Is there a more elegant way of getting steam pipe?
const auto& steam_api_module = koalabox::win_util::get_module_handle_or_throw(STEAMAPI_DLL);
void* GetHSteamPipe_address;
try {
GetHSteamPipe_address = (void*)koalabox::win_util::get_proc_address_or_throw(
steam_api_module, "SteamAPI_GetHSteamPipe"
);
} catch (...) {
GetHSteamPipe_address = (void*)koalabox::win_util::get_proc_address_or_throw(
steam_api_module, "GetHSteamPipe"
);
}
typedef HSteamPipe(__cdecl * GetHSteamPipe_t)();
const auto GetHSteamPipe_o = (GetHSteamPipe_t)GetHSteamPipe_address;
return GetHSteamPipe_o();
}
template <typename F> F get_virtual_function(void* interface, int ordinal) {
auto* v_table = (void***)interface;
return (F)(*v_table)[ordinal];
}
template <typename F, typename... Args>
auto call_virtual_function(void* interface, F function, Args... args) {
#ifdef _WIN64
void* RCX = interface;
#else
void* ECX = interface;
void* EDX = nullptr;
#endif
return function(ARGS(args...));
}
// TODO: Test if this actually works!!!
AppId_t get_app_id_or_throw() {
TCHAR buffer[32];
GetEnvironmentVariable(TEXT("SteamAppId"), buffer, sizeof(buffer));
return std::stoi(buffer);
}
AppId_t get_app_id() {
try {
return get_app_id_or_throw();
} catch (...) {
LOG_ERROR("Failed to get app id");
return 0;
}
/*// Get CreateInterface
const auto& steam_client_module =
koalabox::win_util::get_module_handle_or_throw(STEAMCLIENT_DLL);
auto* CreateInterface_address = (void*)koalabox::win_util::get_proc_address_or_throw(
steam_client_module, "CreateInterface"
);
auto* CreateInterface_o = PLH::FnCast(CreateInterface_address, CreateInterface);
// Get ISteamClient
int result;
auto* i_steam_client = CreateInterface_o("SteamClient006", &result);
if (i_steam_client == nullptr) {
throw koalabox::util::exception(
"Failed to obtain SteamClient006 interface. Result: {}", result
);
}
// Get GetISteamUtils
typedef void***(__fastcall * GetISteamUtils_t)(
PARAMS(HSteamPipe hSteamPipe, const char* version)
);
const auto steam_utils_ordinal = steam_client_ordinal_map["ISteamClient_GetISteamUtils"][6];
const auto GetISteamUtils_o =
get_virtual_function<GetISteamUtils_t>(i_steam_client, steam_utils_ordinal);
// Get ISteamUtils
const auto steam_pipe = get_steam_pipe_or_throw();
auto* i_steam_utils =
call_virtual_function(i_steam_client, GetISteamUtils_o, steam_pipe, "SteamUtils002");
// Get GetAppID
typedef uint32_t(__fastcall * GetAppID_t)(PARAMS());
const auto get_app_id_ordinal = steam_utils_ordinal_map["ISteamUtils_GetAppID"][2];
const auto GetAppID_o = get_virtual_function<GetAppID_t>(i_steam_utils, get_app_id_ordinal);
return call_virtual_function(i_steam_utils, GetAppID_o);*/
}
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include "smoke_api/types.hpp"
namespace steam_interface {
void hook_virtuals(void* interface, const std::string& version_string);
AppId_t get_app_id_or_throw();
AppId_t get_app_id();
}

View File

@@ -1,38 +1,45 @@
#include <steam_impl/steam_inventory.hpp>
#include <smoke_api/config.hpp>
#include <koalabox/logger.hpp>
#include "smoke_api/config.hpp"
#include "steam_interface/steam_inventory.hpp"
namespace steam_inventory {
EResult GetResultStatus(
const String& function_name,
const std::string& function_name,
const SteamInventoryResult_t resultHandle,
const Function<EResult()>& original_function
const std::function<EResult()>& original_function
) {
const auto status = original_function();
LOG_DEBUG("{} -> handle: {}, status: {}", function_name, resultHandle, (int) status);
LOG_DEBUG(
"{} -> handle: {}, status: {}", function_name, resultHandle, static_cast<int>(status)
);
return status;
}
bool GetResultItems(
const String& function_name,
const std::string& function_name,
const SteamInventoryResult_t resultHandle,
SteamItemDetails_t* pOutItemsArray,
uint32_t* punOutItemsArraySize,
const Function<bool()>& original_function,
const Function<bool(SteamItemDef_t*, uint32_t*)>& get_item_definition_ids
const std::function<bool()>& original_function,
const std::function<bool(SteamItemDef_t*, uint32_t*)>& get_item_definition_ids
) {
static std::mutex section;
const std::lock_guard<std::mutex> guard(section);
const auto success = original_function();
auto print_item = [](const String& tag, const SteamItemDetails_t& item) {
auto print_item = [](const std::string& tag, const SteamItemDetails_t& item) {
LOG_DEBUG(
" [{}] definitionId: {}, itemId: {}, quantity: {}, flags: {}",
tag, item.m_iDefinition, item.m_itemId, item.m_unQuantity, item.m_unFlags
tag,
item.m_iDefinition,
item.m_itemId,
item.m_unQuantity,
item.m_unFlags
);
};
@@ -48,22 +55,26 @@ namespace steam_inventory {
LOG_DEBUG(
"{} -> handle: {}, pOutItemsArray: {}, arraySize: {}",
function_name, resultHandle, fmt::ptr(pOutItemsArray), *punOutItemsArraySize
function_name,
resultHandle,
fmt::ptr(pOutItemsArray),
*punOutItemsArraySize
);
static uint32_t original_count = 0;
const auto injected_count = smoke_api::config::instance.extra_inventory_items.size();
// Automatically get inventory items from steam
static Vector<SteamItemDef_t> auto_inventory_items;
static std::vector<SteamItemDef_t> auto_inventory_items;
if (smoke_api::config::instance.auto_inject_inventory) {
CALL_ONCE({
static std::once_flag inventory_inject_flag;
std::call_once(inventory_inject_flag, [&] {
uint32_t count = 0;
if (get_item_definition_ids(nullptr, &count)) {
auto_inventory_items.resize(count);
get_item_definition_ids(auto_inventory_items.data(), &count);
}
})
});
}
const auto auto_injected_count = auto_inventory_items.size();
@@ -74,7 +85,9 @@ namespace steam_inventory {
*punOutItemsArraySize += auto_injected_count + injected_count;
LOG_DEBUG(
"{} -> Original count: {}, Total count: {}",
function_name, original_count, *punOutItemsArraySize
function_name,
original_count,
*punOutItemsArraySize
);
} else {
// Otherwise, we modify the array
@@ -84,10 +97,10 @@ namespace steam_inventory {
static auto new_item = [](SteamItemDef_t id) {
return SteamItemDetails_t{
.m_itemId=id,
.m_iDefinition=id,
.m_unQuantity=1,
.m_unFlags=0,
.m_itemId = id,
.m_iDefinition = id,
.m_unQuantity = 1,
.m_unFlags = 0,
};
};
@@ -114,16 +127,20 @@ namespace steam_inventory {
}
bool GetResultItemProperty(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
uint32_t unItemIndex,
const char* pchPropertyName,
char* pchValueBuffer,
const uint32_t* punValueBufferSizeOut,
const Function<bool()>& original_function
const std::function<bool()>& original_function
) {
const auto common_info = fmt::format(
"{} -> Handle: {}, Index: {}, Name: '{}'", function_name, resultHandle, unItemIndex, pchPropertyName
"{} -> Handle: {}, Index: {}, Name: '{}'",
function_name,
resultHandle,
unItemIndex,
pchPropertyName
);
const auto success = original_function();
@@ -133,15 +150,17 @@ namespace steam_inventory {
return false;
}
LOG_DEBUG("{}, Buffer: '{}'", common_info, String(pchValueBuffer, *punValueBufferSizeOut - 1));
LOG_DEBUG(
"{}, Buffer: '{}'", common_info, std::string(pchValueBuffer, *punValueBufferSizeOut - 1)
);
return success;
}
bool GetAllItems(
const String& function_name,
const std::string& function_name,
const SteamInventoryResult_t* pResultHandle,
const Function<bool()>& original_function
const std::function<bool()>& original_function
) {
const auto success = original_function();
@@ -150,13 +169,12 @@ namespace steam_inventory {
return success;
}
bool GetItemsByID(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t* pResultHandle,
const SteamItemInstanceID_t* pInstanceIDs,
const uint32_t unCountInstanceIDs,
const Function<bool()>& original_function
const std::function<bool()>& original_function
) {
const auto success = original_function();
@@ -172,29 +190,31 @@ namespace steam_inventory {
}
bool SerializeResult(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
void* pOutBuffer,
uint32_t* punOutBufferSize,
const Function<bool()>& original_function
const std::function<bool()>& original_function
) {
const auto success = original_function();
if (pOutBuffer != nullptr) {
String buffer((char*) pOutBuffer, *punOutBufferSize);
std::string buffer((char*)pOutBuffer, *punOutBufferSize);
LOG_DEBUG("{} -> Handle: {}, Buffer: '{}'", function_name, resultHandle, buffer);
} else {
LOG_DEBUG("{} -> Handle: {}, Size: '{}'", function_name, resultHandle, *punOutBufferSize);
LOG_DEBUG(
"{} -> Handle: {}, Size: '{}'", function_name, resultHandle, *punOutBufferSize
);
}
return success;
}
bool GetItemDefinitionIDs(
const String& function_name,
const std::string& function_name,
const SteamItemDef_t* pItemDefIDs,
uint32_t* punItemDefIDsArraySize,
const Function<bool()>& original_function
const std::function<bool()>& original_function
) {
const auto success = original_function();
@@ -205,6 +225,8 @@ namespace steam_inventory {
if (punItemDefIDsArraySize) {
LOG_DEBUG("{} -> Size: {}", function_name, *punItemDefIDsArraySize);
} else {
return success;
}
if (pItemDefIDs) { // Definitions were copied
@@ -218,16 +240,19 @@ namespace steam_inventory {
}
bool CheckResultSteamID(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
CSteamID steamIDExpected,
const Function<bool()>& original_function
const std::function<bool()>& original_function
) {
const auto result = original_function();
LOG_DEBUG(
"{} -> handle: {}, steamID: {}, original result: {}",
function_name, resultHandle, steamIDExpected, result
function_name,
resultHandle,
steamIDExpected,
result
);
return true;

View File

@@ -1,68 +1,67 @@
#pragma once
#include <core/types.hpp>
#include "smoke_api/types.hpp"
namespace steam_inventory {
EResult GetResultStatus(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
const Function<EResult()>& original_function
const std::function<EResult()>& original_function
);
bool GetResultItems(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
SteamItemDetails_t* pOutItemsArray,
uint32_t* punOutItemsArraySize,
const Function<bool()>& original_function,
const Function<bool(SteamItemDef_t*, uint32_t*)>& get_item_definition_ids
const std::function<bool()>& original_function,
const std::function<bool(SteamItemDef_t*, uint32_t*)>& get_item_definition_ids
);
bool GetResultItemProperty(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
uint32_t unItemIndex,
const char* pchPropertyName,
char* pchValueBuffer,
const uint32_t* punValueBufferSizeOut,
const Function<bool()>& original_function
const std::function<bool()>& original_function
);
bool GetAllItems(
const String& function_name,
const std::string& function_name,
const SteamInventoryResult_t* pResultHandle,
const Function<bool()>& original_function
const std::function<bool()>& original_function
);
bool GetItemsByID(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t* pResultHandle,
const SteamItemInstanceID_t* pInstanceIDs,
uint32_t unCountInstanceIDs,
const Function<bool()>& original_function
const std::function<bool()>& original_function
);
bool SerializeResult(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
void* pOutBuffer,
uint32_t* punOutBufferSize,
const Function<bool()>& original_function
const std::function<bool()>& original_function
);
bool GetItemDefinitionIDs(
const String& function_name,
const std::string& function_name,
const SteamItemDef_t* pItemDefIDs,
uint32_t* punItemDefIDsArraySize,
const Function<bool()>& original_function
const std::function<bool()>& original_function
);
bool CheckResultSteamID(
const String& function_name,
const std::string& function_name,
SteamInventoryResult_t resultHandle,
CSteamID steamIDExpected,
const Function<bool()>& original_function
const std::function<bool()>& original_function
);
}

View File

@@ -1,14 +1,15 @@
#include <steam_impl/steam_user.hpp>
#include <smoke_api/config.hpp>
#include <koalabox/logger.hpp>
#include "smoke_api/config.hpp"
#include "steam_interface/steam_user.hpp"
namespace steam_user {
EUserHasLicenseForAppResult UserHasLicenseForApp(
const String& function_name,
const std::string& function_name,
AppId_t appId,
AppId_t dlcId,
const Function<EUserHasLicenseForAppResult()>& original_function
const std::function<EUserHasLicenseForAppResult()>& original_function
) {
const auto result = original_function();
@@ -17,17 +18,14 @@ namespace steam_user {
return result;
}
const auto has_license = smoke_api::config::is_dlc_unlocked(
appId, dlcId, [&]() {
return result == k_EUserHasLicenseResultHasLicense;
}
);
const auto has_license = smoke_api::config::is_dlc_unlocked(appId, dlcId, [&]() {
return result == k_EUserHasLicenseResultHasLicense;
});
LOG_INFO("{} -> App ID: {:>8}, HasLicense: {}", function_name, dlcId, has_license);
return has_license
? k_EUserHasLicenseResultHasLicense
: k_EUserHasLicenseResultDoesNotHaveLicense;
return has_license ? k_EUserHasLicenseResultHasLicense
: k_EUserHasLicenseResultDoesNotHaveLicense;
}
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "smoke_api/types.hpp"
namespace steam_user {
EUserHasLicenseForAppResult UserHasLicenseForApp(
const std::string& function_name,
AppId_t appId,
AppId_t dlcId,
const std::function<EUserHasLicenseForAppResult()>& original_function
);
}

View File

@@ -1,17 +0,0 @@
#include <store_mode/steamclient/steamclient.hpp>
#include <steam_impl/steam_apps.hpp>
VIRTUAL(bool) IClientAppManager_IsAppDlcInstalled(PARAMS(AppId_t app_id, AppId_t dlc_id)) {
try {
return steam_apps::IsDlcUnlocked(
__func__, app_id, dlc_id, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(IClientAppManager_IsAppDlcInstalled)
return IClientAppManager_IsAppDlcInstalled_o(ARGS(app_id, dlc_id));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}

View File

@@ -1,54 +0,0 @@
#include <store_mode/steamclient/steamclient.hpp>
#include <steam_impl/steam_apps.hpp>
VIRTUAL(int) IClientApps_GetDLCCount(PARAMS(AppId_t appId)) {
try {
return steam_apps::GetDLCCount(
__func__, appId, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientApps_GetDLCCount)
return IClientApps_GetDLCCount_o(ARGS(appId));
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return 0;
}
}
VIRTUAL(bool) IClientApps_BGetDLCDataByIndex(
PARAMS(
AppId_t appID,
int iDLC,
AppId_t* pDlcID,
bool* pbAvailable,
char* pchName,
int cchNameBufferSize
)
) {
try {
return steam_apps::GetDLCDataByIndex(
__func__, appID, iDLC, pDlcID, pbAvailable, pchName, cchNameBufferSize,
[&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientApps_BGetDLCDataByIndex)
return IClientApps_BGetDLCDataByIndex_o(
ARGS(appID, iDLC, pDlcID, pbAvailable, pchName, cchNameBufferSize)
);
},
[&](const AppId_t dlc_id) {
const auto* app_manager_interface = store::steamclient::interface_name_to_address_map["IClientAppManager"];
if (app_manager_interface) {
IClientAppManager_IsAppDlcInstalled(app_manager_interface, EDX, appID, dlc_id);
return true;
}
// Would never happen in practice, as the interfaces would be instantiated almost simultaneously
return false;
}
);
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}

View File

@@ -1,128 +0,0 @@
#include <store_mode/steamclient/steamclient.hpp>
#include <steam_impl/steam_inventory.hpp>
VIRTUAL(EResult) IClientInventory_GetResultStatus(PARAMS(SteamInventoryResult_t resultHandle)) {
return steam_inventory::GetResultStatus(
__func__, resultHandle, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_GetResultStatus)
return IClientInventory_GetResultStatus_o(ARGS(resultHandle));
}
);
}
VIRTUAL(bool) IClientInventory_GetResultItems(
PARAMS(
SteamInventoryResult_t resultHandle,
SteamItemDetails_t* pOutItemsArray, // 1st pass: null, 2nd pass: ptr to out array
uint32_t item_count, // 1st pass: 0, 2nd pass: array size
uint32_t * punOutItemsArraySize // 1st pass: ptr to out array size, 2nd pass: ptr to 0
)
) {
return steam_inventory::GetResultItems(
__func__, resultHandle, pOutItemsArray, punOutItemsArraySize,
[&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_GetResultItems)
*punOutItemsArraySize = item_count;
return IClientInventory_GetResultItems_o(
ARGS(resultHandle, pOutItemsArray, item_count, punOutItemsArraySize)
);
},
[&](SteamItemDef_t* pItemDefIDs, uint32_t* punItemDefIDsArraySize) {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_GetItemDefinitionIDs)
return IClientInventory_GetItemDefinitionIDs_o(
ARGS(pItemDefIDs, *punItemDefIDsArraySize, punItemDefIDsArraySize)
);
}
);
}
VIRTUAL(bool) IClientInventory_GetResultItemProperty(
PARAMS(
SteamInventoryResult_t resultHandle,
uint32_t unItemIndex,
const char* pchPropertyName,
char* pchValueBuffer,
uint32_t item_count,
uint32_t * punValueBufferSizeOut
)
) {
return steam_inventory::GetResultItemProperty(
__func__, resultHandle, unItemIndex, pchPropertyName, pchValueBuffer, punValueBufferSizeOut, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_GetResultItemProperty)
*punValueBufferSizeOut = item_count;
return IClientInventory_GetResultItemProperty_o(
ARGS(resultHandle, unItemIndex, pchPropertyName, pchValueBuffer, item_count, punValueBufferSizeOut)
);
}
);
}
VIRTUAL(bool) IClientInventory_CheckResultSteamID(
PARAMS(
SteamInventoryResult_t resultHandle,
CSteamID steamIDExpected
)
) {
return steam_inventory::CheckResultSteamID(__func__, resultHandle, steamIDExpected, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_CheckResultSteamID)
return IClientInventory_CheckResultSteamID_o(ARGS(resultHandle, steamIDExpected));
});
}
VIRTUAL(bool) IClientInventory_GetAllItems(PARAMS(SteamInventoryResult_t* pResultHandle)) {
return steam_inventory::GetAllItems(__func__, pResultHandle, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_GetAllItems)
return IClientInventory_GetAllItems_o(ARGS(pResultHandle));
});
}
VIRTUAL(bool) IClientInventory_GetItemsByID(
PARAMS(
SteamInventoryResult_t* pResultHandle,
const SteamItemInstanceID_t* pInstanceIDs,
uint32_t unCountInstanceIDs
)
) {
return steam_inventory::GetItemsByID(__func__, pResultHandle, pInstanceIDs, unCountInstanceIDs, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_GetItemsByID)
return IClientInventory_GetItemsByID_o(ARGS(pResultHandle, pInstanceIDs, unCountInstanceIDs));
});
}
VIRTUAL(bool) IClientInventory_SerializeResult(
PARAMS(
SteamInventoryResult_t resultHandle,
void* pOutBuffer,
uint32_t buffer_size,
uint32_t * punOutBufferSize
)
) {
return steam_inventory::SerializeResult(__func__, resultHandle, pOutBuffer, punOutBufferSize, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_SerializeResult)
*punOutBufferSize = buffer_size;
return IClientInventory_SerializeResult_o(ARGS(resultHandle, pOutBuffer, buffer_size, punOutBufferSize));
});
}
VIRTUAL(bool) IClientInventory_GetItemDefinitionIDs(
PARAMS(
SteamItemDef_t* pItemDefIDs, // 1st pass: null, 2nd pass: ptr to out array
uint32_t item_count, // 1st pass: 0, 2nd pass: array size
uint32_t * p_array_size // 1st pass: ptr to out array size, 2nd pass: ptr to 0
)
) {
return steam_inventory::GetItemDefinitionIDs(__func__, pItemDefIDs, p_array_size, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientInventory_GetItemDefinitionIDs)
*p_array_size = item_count;
return IClientInventory_GetItemDefinitionIDs_o(ARGS(pItemDefIDs, item_count, p_array_size));
});
}

View File

@@ -1,19 +0,0 @@
#include <store_mode/steamclient/steamclient.hpp>
#include <steam_impl/steam_apps.hpp>
VIRTUAL(bool) IClientUser_BIsSubscribedApp(PARAMS(AppId_t dlc_id)) {
try {
const auto* utils_interface = store::steamclient::interface_name_to_address_map["IClientUtils"];
const auto app_id = utils_interface ? IClientUtils_GetAppID(utils_interface, EDX) : 0;
return steam_apps::IsDlcUnlocked(__func__, app_id, dlc_id, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(IClientUser_BIsSubscribedApp)
return IClientUser_BIsSubscribedApp_o(ARGS(dlc_id));
});
} catch (const Exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}

View File

@@ -1,7 +0,0 @@
#include <store_mode/steamclient/steamclient.hpp>
VIRTUAL(AppId_t) IClientUtils_GetAppID(PARAMS()) {
GET_ORIGINAL_HOOKED_FUNCTION(IClientUtils_GetAppID)
return IClientUtils_GetAppID_o(ARGS());
}

View File

@@ -1,506 +0,0 @@
#include <koalabox/hook.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include <store_mode/steamclient/steamclient.hpp>
#include <store_mode/store.hpp>
#include <Zydis/DecoderTypes.h>
#include <Zydis/Zydis.h>
namespace store::steamclient {
using namespace koalabox;
Map<String, void*> interface_name_to_address_map; // NOLINT(cert-err58-cpp)
struct InstructionContext {
std::optional<ZydisRegister> base_register;
std::optional<String> function_name;
};
// map<interface name, map<function name, function ordinal>>
Map<String, Map<String, uint32_t>> ordinal_map; // NOLINT(cert-err58-cpp)
const auto MAX_INSTRUCTION_SIZE = 15;
ZydisDecoder decoder = {};
ZydisFormatter formatter = {};
void construct_ordinal_map( // NOLINT(misc-no-recursion)
const String& target_interface,
Map<String, uint32_t>& function_name_to_ordinal_map,
uintptr_t start_address
);
// clang-format off
#define CONSTRUCT_ORDINAL_MAP(INTERFACE) \
construct_ordinal_map(#INTERFACE, ordinal_map[#INTERFACE], function_selector_address);
#define HOOK_FUNCTION(INTERFACE, FUNC) hook::swap_virtual_func_or_throw( \
interface, \
#INTERFACE"_"#FUNC, \
ordinal_map[#INTERFACE][#FUNC], \
reinterpret_cast<uintptr_t>(INTERFACE## _## FUNC) \
);
#define SELECTOR_IMPLEMENTATION(INTERFACE, FUNC_BODY) \
DLL_EXPORT(void) INTERFACE## _Selector( \
void* interface, \
void* arg2, \
void* arg3, \
void* arg4 \
) { \
CALL_ONCE({ \
interface_name_to_address_map[#INTERFACE] = interface; \
[&]()FUNC_BODY(); \
}) \
GET_ORIGINAL_HOOKED_FUNCTION(INTERFACE## _Selector) \
INTERFACE## _Selector_o(interface, arg2, arg3, arg4); \
}
SELECTOR_IMPLEMENTATION(IClientAppManager, {
HOOK_FUNCTION(IClientAppManager, IsAppDlcInstalled)
})
SELECTOR_IMPLEMENTATION(IClientApps, {
HOOK_FUNCTION(IClientApps, GetDLCCount)
HOOK_FUNCTION(IClientApps, BGetDLCDataByIndex)
})
SELECTOR_IMPLEMENTATION(IClientInventory, {
HOOK_FUNCTION(IClientInventory, GetResultStatus)
HOOK_FUNCTION(IClientInventory, GetResultItems)
HOOK_FUNCTION(IClientInventory, GetResultItemProperty)
HOOK_FUNCTION(IClientInventory, CheckResultSteamID)
HOOK_FUNCTION(IClientInventory, GetAllItems)
HOOK_FUNCTION(IClientInventory, GetItemsByID)
HOOK_FUNCTION(IClientInventory, SerializeResult)
HOOK_FUNCTION(IClientInventory, GetItemDefinitionIDs)
})
SELECTOR_IMPLEMENTATION(IClientUser, {
HOOK_FUNCTION(IClientUser, BIsSubscribedApp)
})
SELECTOR_IMPLEMENTATION(IClientUtils, {
HOOK_FUNCTION(IClientUtils, GetAppID)
})
#define DETOUR_SELECTOR(INTERFACE) \
if(interface_name == #INTERFACE){ \
CONSTRUCT_ORDINAL_MAP(INTERFACE) \
DETOUR_ADDRESS(INTERFACE## _Selector, function_selector_address) \
}
// clang-format on
void
detour_interface_selector(const String& interface_name, uintptr_t function_selector_address) {
LOG_DEBUG("Detected interface: '{}'", interface_name);
DETOUR_SELECTOR(IClientAppManager)
DETOUR_SELECTOR(IClientApps)
DETOUR_SELECTOR(IClientInventory)
DETOUR_SELECTOR(IClientUser)
DETOUR_SELECTOR(IClientUtils)
}
uintptr_t get_absolute_address(
const ZydisDecodedInstruction instruction, //
const ZydisDecodedOperand operands[], //
const uintptr_t address
) {
const auto operand = operands[0];
if (operand.imm.is_relative) {
ZyanU64 absolute_address;
ZydisCalcAbsoluteAddress(&instruction, &operand, address, &absolute_address);
return absolute_address;
}
return (uintptr_t)operand.imm.value.u;
}
bool is_push_immediate(
const ZydisDecodedInstruction& instruction, //
const ZydisDecodedOperand operands[]
) {
const auto& operand = operands[0];
return instruction.mnemonic == ZYDIS_MNEMONIC_PUSH &&
operand.type == ZYDIS_OPERAND_TYPE_IMMEDIATE &&
operand.visibility == ZYDIS_OPERAND_VISIBILITY_EXPLICIT &&
operand.encoding == ZYDIS_OPERAND_ENCODING_SIMM16_32_32;
}
std::optional<String> get_string_argument(
const ZydisDecodedInstruction& instruction, //
const ZydisDecodedOperand operands[]
) {
const auto* name_address = reinterpret_cast<char*>(operands[0].imm.value.u);
if (util::is_valid_pointer(name_address)) {
return name_address;
}
return std::nullopt;
}
std::optional<String> get_instruction_string(
const ZydisDecodedInstruction& instruction,
const ZydisDecodedOperand operands[],
const uintptr_t address
) {
const auto buffer_size = 64;
char buffer[buffer_size] = {};
if (ZYAN_SUCCESS(ZydisFormatterFormatInstruction(
&formatter,
&instruction,
operands,
instruction.operand_count,
buffer,
buffer_size,
address,
ZYAN_NULL
))) {
return buffer;
}
return std::nullopt;
}
std::optional<String> find_interface_name(uintptr_t selector_address) {
auto current_address = selector_address;
ZydisDecodedInstruction instruction{};
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
while (ZYAN_SUCCESS(ZydisDecoderDecodeFull(
&decoder, // decoder
reinterpret_cast<void*>(current_address), // buffer
MAX_INSTRUCTION_SIZE, // length
&instruction, // instruction
operands // operands
))) {
// const auto debug_str = get_instruction_string(instruction, current_address);
if (is_push_immediate(instruction, operands)) {
auto string_opt = get_string_argument(instruction, operands);
if (string_opt && string_opt->starts_with("IClient")) {
return string_opt;
}
} else if (instruction.mnemonic == ZYDIS_MNEMONIC_RET) {
break;
}
current_address += instruction.length;
}
// LOG_WARN("Failed to find any interface names at {}", (void*) selector_address);
return std::nullopt;
}
struct full_instruction {
const ZydisDecodedInstruction instruction;
const std::vector<ZydisDecodedOperand> operands;
};
/**
* Recursively walks through the code, until a return instruction is reached.
* Recursion occurs whenever a jump/branch is encountered.
*/
template<typename T>
void visit_code( // NOLINT(misc-no-recursion)
std::set<uintptr_t>& visited_addresses,
uintptr_t start_address,
T context,
const std::function<bool(
const ZydisDecodedInstruction& instruction,
const ZydisDecodedOperand operand[],
const uintptr_t& current_address,
T& context,
const std::list<full_instruction>& instruction_list
)>& callback
) {
LOG_TRACE("{} -> start_address: {}", __func__, reinterpret_cast<void*>(start_address));
if (visited_addresses.contains(start_address)) {
LOG_TRACE("Breaking recursion due to visited address");
return;
}
auto current_address = start_address;
std::list<full_instruction> instruction_list;
ZydisDecodedInstruction instruction{};
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
while (ZYAN_SUCCESS(ZydisDecoderDecodeFull(
&decoder, // decoder
(void*)current_address, // buffer
MAX_INSTRUCTION_SIZE, // length
&instruction, // instructions
operands // operands
))) {
visited_addresses.insert(current_address);
LOG_TRACE(
"{} -> visiting {} │ {}",
__func__,
reinterpret_cast<void*>(current_address),
*get_instruction_string(instruction, operands, current_address)
);
const auto should_return =
callback(instruction, operands, current_address, context, instruction_list);
if (should_return) {
return;
}
if (instruction.meta.category == ZYDIS_CATEGORY_COND_BR) {
const auto jump_taken_destination =
get_absolute_address(instruction, operands, current_address);
const auto jump_not_taken_destination = current_address + instruction.length;
visit_code(visited_addresses, jump_taken_destination, context, callback);
visit_code(visited_addresses, jump_not_taken_destination, context, callback);
LOG_TRACE("{} -> Breaking recursion due to a conditional branch", __func__);
return;
}
const auto& operand = operands[0];
if (instruction.mnemonic == ZYDIS_MNEMONIC_JMP &&
operand.type == ZYDIS_OPERAND_TYPE_IMMEDIATE) {
const auto jump_destination =
get_absolute_address(instruction, operands, current_address);
visit_code(visited_addresses, jump_destination, context, callback);
LOG_TRACE("{} -> Breaking recursion due to an unconditional jump", __func__);
return;
}
if (instruction.mnemonic == ZYDIS_MNEMONIC_JMP &&
operand.type == ZYDIS_OPERAND_TYPE_MEMORY &&
operand.mem.scale == sizeof(uintptr_t) && operand.mem.disp.has_displacement) {
// Special handling for jump tables. Guaranteed to be present in the interface
// selector.
const auto* table = (uintptr_t*)operand.mem.disp.value;
const auto* table_entry = table;
while (util::is_valid_pointer((void*)*table_entry)) {
visit_code(visited_addresses, *table_entry, context, callback);
table_entry++;
}
LOG_TRACE("{} -> Breaking recursion due to a jump table", __func__);
return;
}
if (instruction.mnemonic == ZYDIS_MNEMONIC_RET) {
LOG_TRACE("{} -> Breaking recursion due to return instruction", __func__);
return;
}
// We push items to the front so that it becomes easy to iterate over instructions
// in reverse order of addition.
std::vector<ZydisDecodedOperand> operand_list;
for (auto i = 0U; i < instruction.operand_count; i++) {
operand_list.emplace_back(operands[i]);
}
instruction_list.push_front(
{.instruction = instruction, .operands = std::move(operand_list)}
);
current_address += instruction.length;
}
}
void construct_ordinal_map( // NOLINT(misc-no-recursion)
const String& target_interface,
Map<String, uint32_t>& function_name_to_ordinal_map,
uintptr_t start_address
) {
Set<uintptr_t> visited_addresses;
visit_code<InstructionContext>(
visited_addresses,
start_address,
{},
[&](const ZydisDecodedInstruction& instruction,
const ZydisDecodedOperand operands[],
const auto&,
InstructionContext& context,
const std::list<full_instruction>& instruction_list) {
if (context.function_name &&
function_name_to_ordinal_map.contains(*context.function_name)) {
// Avoid duplicate work
return true;
}
const auto& last = instruction_list.front();
const auto is_mov_base_esp = instruction.mnemonic == ZYDIS_MNEMONIC_MOV &&
instruction.operand_count == 2 &&
operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
operands[1].reg.value == ZYDIS_REGISTER_ESP;
if (!context.base_register && is_mov_base_esp) {
// Save base register
context.base_register = operands[0].reg.value;
} else if ( //
is_push_immediate( last.instruction, last.operands.data() ) &&
is_push_immediate(instruction, operands) && !context.function_name) {
// The very first 2 consecutive pushes indicate interface and function names.
// However, subsequent pushes may contain irrelevant strings.
const auto push_string_1 =
get_string_argument(last.instruction, last.operands.data());
const auto push_string_2 = get_string_argument(instruction, operands);
if (push_string_1 && push_string_2) {
if (*push_string_1 == target_interface) {
context.function_name = push_string_2;
} else if (*push_string_2 == target_interface) {
context.function_name = push_string_1;
}
if (context.function_name &&
function_name_to_ordinal_map.contains(*context.function_name)) {
// Bail early to avoid duplicate work
return true;
}
}
} else if (instruction.mnemonic == ZYDIS_MNEMONIC_CALL) {
// On call instructions we should extract the ordinal
if (context.base_register && context.function_name) {
const auto& base_register = *(context.base_register);
const auto& function_name = *(context.function_name);
std::optional<uint32_t> offset;
auto last_destination_reg = ZYDIS_REGISTER_NONE;
bool is_derived_from_base_reg = false;
const auto& operand = operands[0];
// Sometimes the offset is present in the call instruction itself,
// hence we can immediately obtain it.
if (operand.type == ZYDIS_OPERAND_TYPE_MEMORY &&
operand.mem.base != ZYDIS_REGISTER_NONE) {
offset = static_cast<uint32_t>(operand.mem.disp.value);
last_destination_reg = operand.mem.base;
} else if (operand.type == ZYDIS_OPERAND_TYPE_REGISTER) {
last_destination_reg = operand.reg.value;
}
for (const auto& previous : instruction_list) {
const auto& destination_operand = previous.operands[0];
const auto& source_operand = previous.operands[1];
// Extract offset if necessary
if (previous.instruction.mnemonic == ZYDIS_MNEMONIC_MOV &&
previous.instruction.operand_count == 2 &&
destination_operand.reg.value == last_destination_reg &&
source_operand.type == ZYDIS_OPERAND_TYPE_MEMORY) {
const auto source_mem = source_operand.mem;
if (source_mem.base == base_register &&
source_mem.disp.has_displacement &&
source_mem.disp.value == 8) {
// We have verified that the chain eventually leads up to the
// base register. Hence, we can conclude that the offset is
// valid.
is_derived_from_base_reg = true;
break;
}
// Otherwise, keep going through the chain
last_destination_reg = source_mem.base;
if (!offset) {
offset = static_cast<uint32_t>(source_mem.disp.value);
}
}
}
if (offset && is_derived_from_base_reg) {
const auto ordinal = *offset / sizeof(uintptr_t);
LOG_DEBUG(
"Found function ordinal {}::{}@{}",
target_interface,
function_name,
ordinal
);
function_name_to_ordinal_map[function_name] = ordinal;
return true;
}
}
}
return false;
}
);
}
void process_interface_selector( // NOLINT(misc-no-recursion)
const uintptr_t start_address,
Set<uintptr_t>& visited_addresses
) {
visit_code<nullptr_t>(
visited_addresses,
start_address,
nullptr,
[](const ZydisDecodedInstruction& instruction,
const ZydisDecodedOperand operands[],
const auto& current_address,
auto,
const auto&) {
if (instruction.mnemonic == ZYDIS_MNEMONIC_CALL &&
operands[0].type == ZYDIS_OPERAND_TYPE_IMMEDIATE) {
LOG_TRACE("Found call instruction at {}", (void*)current_address);
const auto function_selector_address =
get_absolute_address(instruction, operands, current_address);
const auto interface_name_opt = find_interface_name(function_selector_address);
if (interface_name_opt) {
const auto& interface_name = *interface_name_opt;
detour_interface_selector(interface_name, function_selector_address);
}
}
return false;
}
);
}
void process_client_engine(uintptr_t interface) {
const auto* steam_client_internal =
((uintptr_t***)interface)[store::config.client_engine_steam_client_internal_ordinal];
const auto interface_selector_address = (*steam_client_internal
)[store::config.steam_client_internal_interface_selector_ordinal];
LOG_DEBUG("Found interface selector at: {}", (void*)interface_selector_address);
if (ZYAN_FAILED(
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LEGACY_32, ZYDIS_STACK_WIDTH_32)
)) {
LOG_ERROR("Failed to initialize zydis decoder");
return;
}
if (ZYAN_FAILED(ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL))) {
LOG_ERROR("Failed to initialize zydis formatter");
return;
}
Set<uintptr_t> visited_addresses;
process_interface_selector(interface_selector_address, visited_addresses);
}
}

View File

@@ -1,39 +0,0 @@
#pragma once
#include <core/types.hpp>
// IClientAppManager
VIRTUAL(bool) IClientAppManager_IsAppDlcInstalled(PARAMS(AppId_t, AppId_t));
// IClientApps
VIRTUAL(int) IClientApps_GetDLCCount(PARAMS(AppId_t));
VIRTUAL(bool) IClientApps_BGetDLCDataByIndex(PARAMS(AppId_t, int, AppId_t*, bool*, char*, int));
// IClientInventory
VIRTUAL(EResult) IClientInventory_GetResultStatus(PARAMS(SteamInventoryResult_t));
VIRTUAL(bool) IClientInventory_GetResultItems(
PARAMS(SteamInventoryResult_t, SteamItemDetails_t*, uint32_t, uint32_t *)
);
VIRTUAL(bool) IClientInventory_GetResultItemProperty(
PARAMS(SteamInventoryResult_t, uint32_t, const char*, char*, uint32_t, uint32_t*)
);
VIRTUAL(bool) IClientInventory_CheckResultSteamID(PARAMS(SteamInventoryResult_t, CSteamID));
VIRTUAL(bool) IClientInventory_GetAllItems(PARAMS(SteamInventoryResult_t*));
VIRTUAL(bool) IClientInventory_GetItemsByID(PARAMS(SteamInventoryResult_t*, const SteamItemInstanceID_t*, uint32_t));
VIRTUAL(bool) IClientInventory_SerializeResult(PARAMS(SteamInventoryResult_t, void*, uint32_t, uint32_t *));
VIRTUAL(bool) IClientInventory_GetItemDefinitionIDs(PARAMS(SteamItemDef_t*, uint32_t, uint32_t *));
// IClientUser
VIRTUAL(bool) IClientUser_BIsSubscribedApp(PARAMS(AppId_t));
// IClientUtils
VIRTUAL(AppId_t) IClientUtils_GetAppID(PARAMS());
namespace store::steamclient {
/// We need this interface in other IClient* functions in order to call other functions
extern Map<String, void*> interface_name_to_address_map;
void process_client_engine(uintptr_t interface);
}

View File

@@ -1,157 +0,0 @@
#include "koalabox/str.hpp"
#include <build_config.h>
#include <common/steamclient_exports.hpp>
#include <koalabox/dll_monitor.hpp>
#include <koalabox/ipc.hpp>
#include <koalabox/logger.hpp>
#include <smoke_api/config.hpp>
#include <store_mode/store.hpp>
#include <store_mode/store_api.hpp>
#include <store_mode/store_cache.hpp>
#include <store_mode/vstdlib/vstdlib.hpp>
#include <core/globals.hpp>
namespace {
using namespace store;
/**
* @return A string representing the source of the config.
*/
void init_store_config() {
const auto print_source = [](const String& source) {
LOG_INFO("Loaded Store config from the {}", source);
};
// First try to read a local config override
const auto& kg_config = smoke_api::config::instance.store_config;
if (!kg_config.is_null()) {
try {
config = kg_config.get<decltype(config)>();
print_source("local config override");
return;
} catch (const Exception& ex) {
LOG_ERROR("Failed to get local store_mode config: {}", ex.what());
}
}
// Then try to get a cached copy of a previously fetched config.
try {
config = store_cache::get_store_config().value();
print_source("disk cache");
} catch (const Exception& ex) {
LOG_ERROR("Failed to get cached store_mode config: {}", ex.what());
print_source("default config bundled in the binary");
// Fallback on the default config, to continue execution immediately.
config = {};
}
// Finally, fetch the remote config from GitHub, and inform user about the need to restart
// Steam, if a new config has been fetched
std::thread([] {
try {
const auto github_config_opt = api::fetch_store_config();
if (!github_config_opt) {
return;
}
const auto github_config = *github_config_opt;
store_cache::save_store_config(github_config);
if (github_config == config) {
(spdlog::default_logger_raw())
->log(
spdlog::source_loc{
"store.cpp", 66, static_cast<const char*>(__FUNCTION__)
},
spdlog::level::debug,
"Fetched Store config is equal to existing config"
);
return;
}
(spdlog::default_logger_raw())
->log(
spdlog::source_loc{"store.cpp", 66, static_cast<const char*>(__FUNCTION__)},
spdlog::level::debug,
"Fetched a new Store config"
);
MessageBoxW(
nullptr,
L"SmokeAPI has downloaded an updated config for Store mode. "
"Please restart Steam in order to apply the new Store config. ",
L"SmokeAPI - Store",
0x00010000L | 0x00000040L | 0x00000000L
);
} catch (const Exception& ex) {
(spdlog::default_logger_raw())
->log(
spdlog::source_loc{"store.cpp", 66, static_cast<const char*>(__FUNCTION__)},
spdlog::level::err,
"Failed to get remote store_mode config: {}",
ex.what()
);
}
}).detach();
}
}
namespace store {
StoreConfig config; // NOLINT(cert-err58-cpp)
void init_store_mode() {
init_store_config();
koalabox::dll_monitor::init_listener(
{VSTDLIB_DLL, STEAMCLIENT_DLL},
[](const HMODULE& module_handle, const String& name) {
try {
if (koalabox::str::eq(name, VSTDLIB_DLL)) {
// VStdLib DLL handles Family Sharing functions
globals::vstdlib_module = module_handle;
if (smoke_api::config::instance.unlock_family_sharing) {
DETOUR_VSTDLIB(Coroutine_Create)
}
} else if (koalabox::str::eq(name, STEAMCLIENT_DLL)) {
// SteamClient DLL handles unlocking functions
globals::steamclient_module = module_handle;
DETOUR_STEAMCLIENT(CreateInterface)
}
if (globals::vstdlib_module != nullptr &&
globals::steamclient_module != nullptr) {
koalabox::dll_monitor::shutdown_listener();
}
} catch (const Exception& ex) {
LOG_ERROR(
"Error listening to DLL load events. Module: '{}', Message: {}",
name,
ex.what()
);
}
}
);
std::thread([=] {
koalabox::ipc::init_pipe_server(
"smokeapi.store.steam",
[](const koalabox::ipc::Request& request) {
koalabox::ipc::Response response;
if (koalabox::str::eq(request.name, "config::reload")) {
smoke_api::config::ReloadConfig();
response.success = true;
} else {
response.success = false;
response.data["error_message"] = "Invalid request name: " + request.name;
}
return response;
}
);
}).detach();
}
}

View File

@@ -1,36 +0,0 @@
#pragma once
#include <core/types.hpp>
namespace store {
// Offset values are interpreted according to pointer arithmetic rules, i.e.
// 1 unit offset represents 4 and 8 bytes in 32-bit and 64-bit architectures respectively.
class StoreConfig {
public:
uint32_t client_engine_steam_client_internal_ordinal = 12;
uint32_t steam_client_internal_interface_selector_ordinal = 18;
uint32_t vstdlib_callback_address_offset = 20;
uint32_t vstdlib_callback_data_offset = 0;
uint32_t vstdlib_callback_interceptor_address_offset = 1;
uint32_t vstdlib_callback_name_offset = 4;
// We do not use *_WITH_DEFAULT macro to ensure that overriding
// the store_mode config requires definition of all keys
NLOHMANN_DEFINE_TYPE_INTRUSIVE(
StoreConfig, // NOLINT(misc-const-correctness)
client_engine_steam_client_internal_ordinal,
steam_client_internal_interface_selector_ordinal,
vstdlib_callback_address_offset,
vstdlib_callback_data_offset,
vstdlib_callback_interceptor_address_offset,
vstdlib_callback_name_offset
)
bool operator==(const StoreConfig& other) const = default;
};
extern StoreConfig config;
void init_store_mode();
}

View File

@@ -1,19 +0,0 @@
#include <store_mode/store_api.hpp>
#include <koalabox/http_client.hpp>
namespace store::api {
std::optional<StoreConfig> fetch_store_config() noexcept {
try {
const String url =
"https://raw.githubusercontent.com/acidicoala/public-entitlements/main/steam/v2/store_config.json";
const auto kg_config_json = koalabox::http_client::get_json(url);
return kg_config_json.get<StoreConfig>();
} catch (const Exception& e) {
LOG_ERROR("Failed to fetch Store config from GitHub: {}", e.what());
return std::nullopt;
}
}
}

View File

@@ -1,10 +0,0 @@
#pragma once
#include <core/types.hpp>
#include <store_mode/store.hpp>
namespace store::api {
std::optional<store::StoreConfig> fetch_store_config() noexcept;
}

View File

@@ -1,30 +0,0 @@
#include <store_mode/store_cache.hpp>
#include <koalabox/cache.hpp>
constexpr auto KEY_KG_CONFIG = "store_config";
namespace store::store_cache {
std::optional<StoreConfig> get_store_config() {
try {
return koalabox::cache::get(KEY_KG_CONFIG, Json(nullptr)).get<StoreConfig>();
} catch (const Exception& e) {
LOG_ERROR("Failed to get cached store_mode config: {}", e.what());
return std::nullopt;
}
}
bool save_store_config(const StoreConfig& config) {
try {
LOG_DEBUG("Caching store_mode config");
return koalabox::cache::put(KEY_KG_CONFIG, Json(config));
} catch (const Exception& e) {
LOG_ERROR("Failed to cache store_mode config: {}", e.what());
return false;
}
}
}

View File

@@ -1,11 +0,0 @@
#pragma once
#include <store_mode/store.hpp>
namespace store::store_cache {
std::optional<StoreConfig> get_store_config();
bool save_store_config(const StoreConfig& config);
}

View File

@@ -1,64 +0,0 @@
#include <store_mode/vstdlib/vstdlib.hpp>
namespace store::vstdlib {
VIRTUAL(bool) SharedLicensesLockStatus(PARAMS(void* arg)) {
LOG_DEBUG("{}(this={}, arg={})", __func__, THIS, arg);
ARGS();
return true;
}
VIRTUAL(bool) SharedLibraryStopPlaying(PARAMS(void* arg)) {
LOG_DEBUG("{}(this={}, arg={})", __func__, THIS, arg);
ARGS();
return true;
}
VIRTUAL(void) VStdLib_Callback_Interceptor(PARAMS(const char** name_ptr)) {
GET_ORIGINAL_HOOKED_FUNCTION(VStdLib_Callback_Interceptor)
VStdLib_Callback_Interceptor_o(ARGS(name_ptr));
static auto lock_status_hooked = false;
static auto stop_playing_hooked = false;
if (lock_status_hooked && stop_playing_hooked) {
return;
}
auto* const data = (CoroutineData*) THIS;
if (data && data->get_callback_name()) {
const auto name = String(data->get_callback_name());
LOG_TRACE("{}(ecx={}, edx={}, name='{}')", __func__, ARGS(), name);
if (name == "SharedLicensesLockStatus" && !lock_status_hooked) {
DETOUR_ADDRESS(SharedLicensesLockStatus, data->get_callback_data()->get_callback_address())
lock_status_hooked = true;
} else if (name == "SharedLibraryStopPlaying" && !stop_playing_hooked) {
DETOUR_ADDRESS(SharedLibraryStopPlaying, data->get_callback_data()->get_callback_address())
stop_playing_hooked = true;
}
}
}
/**
* Initially, callback data passed into this function is not complete,
* hence we must hook an interface method that sets the callback name.
*/
DLL_EXPORT(HCoroutine) Coroutine_Create(void* callback_address, CoroutineData* data) {
GET_ORIGINAL_HOOKED_FUNCTION(Coroutine_Create)
const auto result = Coroutine_Create_o(callback_address, data);
// Coroutine callback appears to be always the same
CALL_ONCE({
LOG_DEBUG("Coroutine_Create -> callback: {}, data: {}", callback_address, fmt::ptr(data));
DETOUR_ADDRESS(
VStdLib_Callback_Interceptor,
data->get_callback_data()->get_callback_intercept_address()
)
})
return result;
}
}

View File

@@ -1,27 +0,0 @@
#include <store_mode/store.hpp>
namespace store::vstdlib {
struct CallbackData {
uintptr_t get_callback_intercept_address() {
return reinterpret_cast<uintptr_t*>(this)[store::config.vstdlib_callback_interceptor_address_offset];
}
uintptr_t get_callback_address() {
return reinterpret_cast<uintptr_t*>(this)[store::config.vstdlib_callback_address_offset];
}
};
struct CoroutineData {
CallbackData* get_callback_data() {
return reinterpret_cast<CallbackData**>(this)[store::config.vstdlib_callback_data_offset];
}
const char* get_callback_name() {
return reinterpret_cast<const char**>(this)[store::config.vstdlib_callback_name_offset];
}
};
typedef uint32_t HCoroutine;
DLL_EXPORT(HCoroutine) Coroutine_Create(void* callback_address, CoroutineData* data);
}

View File

@@ -0,0 +1,77 @@
#include <koalabox/hook.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include "steam_interface/steam_apps.hpp"
#include "steam_interface/steam_interface.hpp"
#include "virtuals/steam_api_virtuals.hpp"
VIRTUAL(bool) ISteamApps_BIsSubscribedApp(PARAMS(AppId_t dlc_id)) {
try {
static const auto app_id = steam_interface::get_app_id();
return steam_apps::IsDlcUnlocked(__func__, app_id, dlc_id, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_BIsSubscribedApp)
return ISteamApps_BIsSubscribedApp_o(ARGS(dlc_id));
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}
VIRTUAL(bool) ISteamApps_BIsDlcInstalled(PARAMS(AppId_t dlc_id)) {
try {
static const auto app_id = steam_interface::get_app_id();
return steam_apps::IsDlcUnlocked(__func__, app_id, dlc_id, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_BIsDlcInstalled)
return ISteamApps_BIsDlcInstalled_o(ARGS(dlc_id));
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}
VIRTUAL(int) ISteamApps_GetDLCCount(PARAMS()) {
try {
static const auto app_id = steam_interface::get_app_id();
return steam_apps::GetDLCCount(__func__, app_id, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_GetDLCCount)
return ISteamApps_GetDLCCount_o(ARGS());
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return 0;
}
}
VIRTUAL(bool)
ISteamApps_BGetDLCDataByIndex(
PARAMS(int iDLC, AppId_t* p_dlc_id, bool* pbAvailable, char* pchName, int cchNameBufferSize)
) {
try {
static const auto app_id = steam_interface::get_app_id();
return steam_apps::GetDLCDataByIndex(
__func__,
app_id,
iDLC,
p_dlc_id,
pbAvailable,
pchName,
cchNameBufferSize,
[&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamApps_BGetDLCDataByIndex)
return ISteamApps_BGetDLCDataByIndex_o(
ARGS(iDLC, p_dlc_id, pbAvailable, pchName, cchNameBufferSize)
);
},
[&](AppId_t dlc_id) { return ISteamApps_BIsDlcInstalled(ARGS(dlc_id)); }
);
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return false;
}
}

View File

@@ -0,0 +1,67 @@
#include <koalabox/hook.hpp>
#include <koalabox/logger.hpp>
#include "steam_interface/steam_client.hpp"
#include "virtuals/steam_api_virtuals.hpp"
VIRTUAL(void*)
ISteamClient_GetISteamApps(PARAMS(HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char* version)
) {
try {
return steam_client::GetGenericInterface(__func__, version, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamApps)
return ISteamClient_GetISteamApps_o(ARGS(hSteamUser, hSteamPipe, version));
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}
VIRTUAL(void*)
ISteamClient_GetISteamUser(PARAMS(HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char* version)
) {
try {
return steam_client::GetGenericInterface(__func__, version, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamUser)
return ISteamClient_GetISteamUser_o(ARGS(hSteamUser, hSteamPipe, version));
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}
VIRTUAL(void*)
ISteamClient_GetISteamGenericInterface(
PARAMS(HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char* pchVersion)
) {
try {
return steam_client::GetGenericInterface(__func__, pchVersion, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamGenericInterface)
return ISteamClient_GetISteamGenericInterface_o(ARGS(hSteamUser, hSteamPipe, pchVersion)
);
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}
VIRTUAL(void*)
ISteamClient_GetISteamInventory(
PARAMS(HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char* pchVersion)
) {
try {
return steam_client::GetGenericInterface(__func__, pchVersion, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamClient_GetISteamInventory)
return ISteamClient_GetISteamInventory_o(ARGS(hSteamUser, hSteamPipe, pchVersion));
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return nullptr;
}
}

View File

@@ -0,0 +1,140 @@
#include <koalabox/hook.hpp>
#include "steam_interface/steam_inventory.hpp"
#include "virtuals/steam_api_virtuals.hpp"
VIRTUAL(EResult) ISteamInventory_GetResultStatus(PARAMS(SteamInventoryResult_t resultHandle)) {
return steam_inventory::GetResultStatus(__func__, resultHandle, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetResultStatus)
return ISteamInventory_GetResultStatus_o(ARGS(resultHandle));
});
}
VIRTUAL(bool)
ISteamInventory_GetResultItems(PARAMS(
SteamInventoryResult_t resultHandle,
SteamItemDetails_t* pOutItemsArray,
uint32_t* punOutItemsArraySize
)) {
return steam_inventory::GetResultItems(
__func__,
resultHandle,
pOutItemsArray,
punOutItemsArraySize,
[&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetResultItems)
return ISteamInventory_GetResultItems_o(
ARGS(resultHandle, pOutItemsArray, punOutItemsArraySize)
);
},
[&](SteamItemDef_t* pItemDefIDs, uint32_t* punItemDefIDsArraySize) {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetItemDefinitionIDs)
return ISteamInventory_GetItemDefinitionIDs_o(ARGS(pItemDefIDs, punItemDefIDsArraySize)
);
}
);
}
VIRTUAL(bool)
ISteamInventory_GetResultItemProperty(PARAMS(
SteamInventoryResult_t resultHandle,
uint32_t unItemIndex,
const char* pchPropertyName,
char* pchValueBuffer,
uint32_t* punValueBufferSizeOut
)) {
return steam_inventory::GetResultItemProperty(
__func__,
resultHandle,
unItemIndex,
pchPropertyName,
pchValueBuffer,
punValueBufferSizeOut,
[&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetResultItemProperty)
return ISteamInventory_GetResultItemProperty_o(ARGS(
resultHandle, unItemIndex, pchPropertyName, pchValueBuffer, punValueBufferSizeOut
));
}
);
}
VIRTUAL(bool)
ISteamInventory_CheckResultSteamID(
PARAMS(SteamInventoryResult_t resultHandle, CSteamID steamIDExpected)
) {
return steam_inventory::CheckResultSteamID(__func__, resultHandle, steamIDExpected, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_CheckResultSteamID)
return ISteamInventory_CheckResultSteamID_o(ARGS(resultHandle, steamIDExpected));
});
}
VIRTUAL(bool) ISteamInventory_GetAllItems(PARAMS(SteamInventoryResult_t* pResultHandle)) {
return steam_inventory::GetAllItems(__func__, pResultHandle, [&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetAllItems)
return ISteamInventory_GetAllItems_o(ARGS(pResultHandle));
});
}
VIRTUAL(bool)
ISteamInventory_GetItemsByID(PARAMS(
SteamInventoryResult_t* pResultHandle,
const SteamItemInstanceID_t* pInstanceIDs,
uint32_t unCountInstanceIDs
)) {
return steam_inventory::GetItemsByID(
__func__,
pResultHandle,
pInstanceIDs,
unCountInstanceIDs,
[&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetItemsByID)
return ISteamInventory_GetItemsByID_o(
ARGS(pResultHandle, pInstanceIDs, unCountInstanceIDs)
);
}
);
}
VIRTUAL(bool)
ISteamInventory_SerializeResult(
PARAMS(SteamInventoryResult_t resultHandle, void* pOutBuffer, uint32_t* punOutBufferSize)
) {
return steam_inventory::SerializeResult(
__func__,
resultHandle,
pOutBuffer,
punOutBufferSize,
[&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_SerializeResult)
return ISteamInventory_SerializeResult_o(
ARGS(resultHandle, pOutBuffer, punOutBufferSize)
);
}
);
}
VIRTUAL(bool)
ISteamInventory_GetItemDefinitionIDs(
PARAMS(SteamItemDef_t* pItemDefIDs, uint32_t* punItemDefIDsArraySize)
) {
return steam_inventory::GetItemDefinitionIDs(
__func__,
pItemDefIDs,
punItemDefIDsArraySize,
[&] {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamInventory_GetItemDefinitionIDs)
return ISteamInventory_GetItemDefinitionIDs_o(ARGS(pItemDefIDs, punItemDefIDsArraySize)
);
}
);
}

View File

@@ -0,0 +1,21 @@
#include <koalabox/hook.hpp>
#include <koalabox/logger.hpp>
#include "steam_interface/steam_interface.hpp"
#include "steam_interface/steam_user.hpp"
#include "virtuals/steam_api_virtuals.hpp"
VIRTUAL(EUserHasLicenseForAppResult)
ISteamUser_UserHasLicenseForApp(PARAMS(CSteamID steamID, AppId_t dlc_id)) {
try {
static const auto app_id = steam_interface::get_app_id();
return steam_user::UserHasLicenseForApp(__func__, app_id, dlc_id, [&]() {
GET_ORIGINAL_HOOKED_FUNCTION(ISteamUser_UserHasLicenseForApp)
return ISteamUser_UserHasLicenseForApp_o(ARGS(steamID, dlc_id));
});
} catch (const std::exception& e) {
LOG_ERROR("{} -> Error: {}", __func__, e.what());
return k_EUserHasLicenseResultDoesNotHaveLicense;
}
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include <core/types.hpp>
#include "smoke_api/types.hpp"
// ISteamApps
VIRTUAL(bool) ISteamApps_BIsSubscribedApp(PARAMS(AppId_t));
@@ -14,15 +14,18 @@ VIRTUAL(void*) ISteamClient_GetISteamUser(PARAMS(HSteamUser, HSteamPipe, const c
VIRTUAL(void*) ISteamClient_GetISteamGenericInterface(PARAMS(HSteamUser, HSteamPipe, const char*));
VIRTUAL(void*) ISteamClient_GetISteamInventory(PARAMS(HSteamUser, HSteamPipe, const char*));
// ISteamInventory
VIRTUAL(EResult) ISteamInventory_GetResultStatus(PARAMS(SteamInventoryResult_t));
VIRTUAL(bool) ISteamInventory_GetResultItems(PARAMS(SteamInventoryResult_t, SteamItemDetails_t*, uint32_t*));
VIRTUAL(bool) ISteamInventory_GetResultItemProperty(
VIRTUAL(bool)
ISteamInventory_GetResultItems(PARAMS(SteamInventoryResult_t, SteamItemDetails_t*, uint32_t*));
VIRTUAL(bool)
ISteamInventory_GetResultItemProperty(
PARAMS(SteamInventoryResult_t, uint32_t, const char*, char*, uint32_t*)
);
VIRTUAL(bool) ISteamInventory_GetAllItems(PARAMS(SteamInventoryResult_t*));
VIRTUAL(bool) ISteamInventory_GetItemsByID(PARAMS(SteamInventoryResult_t*, const SteamItemInstanceID_t*, uint32_t));
VIRTUAL(bool)
ISteamInventory_GetItemsByID(PARAMS(SteamInventoryResult_t*, const SteamItemInstanceID_t*, uint32_t)
);
VIRTUAL(bool) ISteamInventory_SerializeResult(PARAMS(SteamInventoryResult_t, void*, uint32_t*));
VIRTUAL(bool) ISteamInventory_GetItemDefinitionIDs(PARAMS(SteamItemDef_t*, uint32_t*));
VIRTUAL(bool) ISteamInventory_CheckResultSteamID(PARAMS(SteamInventoryResult_t, CSteamID));