This commit is contained in:
acidicoala
2023-01-09 08:02:57 +03:00
parent 6347e3f148
commit ad1578f556
44 changed files with 380 additions and 382 deletions

View File

@@ -1,23 +1,13 @@
#include <steam_impl/steam_apps.hpp>
#include <koalabox/http_client.hpp>
#include <steam_impl/steam_impl.hpp>
#include <smoke_api/app_cache.hpp>
#include <core/config.hpp>
#include <smoke_api/config.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include <steam_functions/steam_functions.hpp>
#include <core/types.hpp>
#include <utility>
#include <core/api.hpp>
namespace steam_apps {
// TODO: Needs to go to API
struct SteamResponse {
uint32_t success = 0;
Vector<DLC> dlcs;
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(SteamResponse, success, dlcs) // NOLINT(misc-const-correctness)
};
/// Steamworks may max GetDLCCount value at 64, depending on how much unowned DLCs the user has.
/// Despite this limit, some games with more than 64 DLCs still keep using this method.
/// This means we have to get extra DLC IDs from local config, remote config, or cache.
@@ -26,38 +16,8 @@ namespace steam_apps {
Map<AppId_t, Vector<DLC>> app_dlcs; // NOLINT(cert-err58-cpp)
Set<AppId_t> fully_fetched; // NOLINT(cert-err58-cpp)
std::optional<Vector<DLC>> fetch_from_github(AppId_t app_id) noexcept {
try {
const auto* url
= "https://raw.githubusercontent.com/acidicoala/public-entitlements/main/steam/v2/dlc.json";
const auto json = koalabox::http_client::fetch_json(url);
const auto response = json.get<AppDlcNameMap>();
return DLC::get_dlcs_from_apps(response, app_id);
} catch (const Json::exception& e) {
LOG_ERROR("Failed to fetch dlc list from GitHub: {}", e.what())
return std::nullopt;
}
}
std::optional<Vector<DLC>> fetch_from_steam(AppId_t app_id) noexcept {
try {
const auto url = fmt::format("https://store.steampowered.com/dlc/{}/ajaxgetdlclist", app_id);
const auto json = koalabox::http_client::fetch_json(url);
LOG_TRACE("Steam response: \n{}", json.dump(2))
const auto response = json.get<SteamResponse>();
if (response.success != 1) {
throw std::runtime_error("Web API responded with 'success' != 1");
}
return response.dlcs;
} catch (const Exception& e) {
LOG_ERROR("Failed to fetch dlc list from Steam: {}", e.what())
return std::nullopt;
}
String get_app_id_log(const AppId_t app_id) {
return app_id ? fmt::format("App ID: {:>8}, ", app_id) : "";
}
/**
@@ -72,7 +32,7 @@ namespace steam_apps {
// No app id means we are operating in game mode.
// Hence, we need to use utility functions to get app id.
try {
app_id = steam_functions::get_app_id_or_throw();
app_id = steam_impl::get_app_id_or_throw();
LOG_INFO("Detected App ID: {}", app_id)
} catch (const Exception& ex) {
LOG_ERROR("Failed to get app ID: {}", ex.what())
@@ -96,19 +56,19 @@ namespace steam_apps {
aggregated_dlcs < append > source;
};
append_dlcs(config::get_extra_dlcs(app_id), "local config");
append_dlcs(smoke_api::config::get_extra_dlcs(app_id), "local config");
const auto github_dlcs = fetch_from_github(app_id);
if (github_dlcs) {
append_dlcs(*github_dlcs, "GitHub repository");
const auto github_dlcs_opt = api::fetch_dlcs_from_github(app_id);
if (github_dlcs_opt) {
append_dlcs(*github_dlcs_opt, "GitHub repository");
}
const auto steam_dlcs = fetch_from_steam(app_id);
if (steam_dlcs) {
append_dlcs(*steam_dlcs, "Steam API");
const auto steam_dlcs_opt = api::fetch_dlcs_from_steam(app_id);
if (steam_dlcs_opt) {
append_dlcs(*steam_dlcs_opt, "Steam API");
}
if (github_dlcs && steam_dlcs) {
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");
@@ -121,10 +81,6 @@ namespace steam_apps {
smoke_api::app_cache::save_dlcs(app_id, aggregated_dlcs);
}
String get_app_id_log(const AppId_t app_id) {
return app_id ? fmt::format("App ID: {:>8}, ", app_id) : "";
}
bool IsDlcUnlocked(
const String& function_name,
AppId_t app_id,
@@ -132,7 +88,7 @@ namespace steam_apps {
const Function<bool()>& original_function
) {
try {
const auto unlocked = config::is_dlc_unlocked(app_id, dlc_id, original_function);
const auto unlocked = smoke_api::config::is_dlc_unlocked(app_id, dlc_id, original_function);
LOG_INFO("{} -> {}DLC ID: {:>8}, Unlocked: {}", function_name, get_app_id_log(app_id), dlc_id, unlocked)
@@ -196,7 +152,7 @@ namespace steam_apps {
const auto inject_dlc = [&](const DLC& dlc) {
// Fill the output pointers
*pDlcId = dlc.get_id();
*pbAvailable = config::is_dlc_unlocked(
*pbAvailable = smoke_api::config::is_dlc_unlocked(
app_id, *pDlcId, [&]() {
return is_originally_unlocked(*pDlcId);
}
@@ -223,7 +179,9 @@ namespace steam_apps {
const auto success = original_function();
if (success) {
*pbAvailable = config::is_dlc_unlocked(app_id, *pDlcId, [&]() { return *pbAvailable; });
*pbAvailable = smoke_api::config::is_dlc_unlocked(
app_id, *pDlcId, [&]() { return *pbAvailable; }
);
print_dlc_info("original");
} else {
LOG_WARN("{} -> original call failed for index: {}", function_name, iDLC)

View File

@@ -1,7 +1,6 @@
#pragma once
#include <core/types.hpp>
#include <koalabox/core.hpp>
namespace steam_apps {

View File

@@ -1,6 +1,6 @@
#include <steam_impl/steam_client.hpp>
#include <steam_impl/steam_impl.hpp>
#include <koalabox/logger.hpp>
#include <steam_functions/steam_functions.hpp>
namespace steam_client {
@@ -13,7 +13,7 @@ namespace steam_client {
LOG_DEBUG("{} -> '{}' @ {}", function_name, interface_version, interface)
steam_functions::hook_virtuals(interface, interface_version);
steam_impl::hook_virtuals(interface, interface_version);
return interface;
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include <koalabox/core.hpp>
#include <core/types.hpp>
namespace steam_client {

View File

@@ -0,0 +1,280 @@
#include <steam_impl/steam_impl.hpp>
#include <steam_api_virtuals/steam_api_virtuals.hpp>
#include <steam_api_exports/steam_api_exports.hpp>
#include <core/globals.hpp>
#include <build_config.h>
#include <koalabox/hook.hpp>
#include <koalabox/win_util.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/util.hpp>
#include <polyhook2/Misc.hpp>
#if COMPILE_KOALAGEDDON
#include <koalageddon/steamclient/steamclient.hpp>
#endif
namespace steam_impl {
typedef Map<String, Map<int, int>> FunctionOrdinalMap;
FunctionOrdinalMap steam_client_ordinal_map = { // NOLINT(cert-err58-cpp)
{"ISteamClient_GetISteamApps",
{
{6, 16},
{7, 18},
{8, 15},
{9, 16},
{12, 15},
}
},
{"ISteamClient_GetISteamUser",
{
{6, 6},
{7, 5},
}
},
{"ISteamClient_GetISteamUtils",
{
{6, 12},
{7, 9},
}
},
{"ISteamClient_GetISteamGenericInterface",
{
{7, 14},
{8, 13},
{12, 12},
}
},
{"ISteamClient_GetISteamInventory",
{
{17, 34},
{18, 35},
}
}
};
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},
}}
};
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,
int min_version,
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 (const std::exception& ex) {
koalabox::util::panic("Failed to extract version number from: '{}'", version_string);
}
}
int get_ordinal(const FunctionOrdinalMap& ordinal_map, const String& function_name, int interface_version) {
const auto& map = ordinal_map.at(function_name);
for (auto it = map.rbegin(); it != map.rend(); it++) {
const auto [version, ordinal] = *it;
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( \
globals::address_map, \
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)) {
// This interface is already hooked. Skipping it.
return;
}
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, 20);
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, 21);
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)) {
#if COMPILE_KOALAGEDDON
koalageddon::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 (const Exception& ex) {
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

@@ -0,0 +1,11 @@
#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,5 +1,5 @@
#include <steam_impl/steam_inventory.hpp>
#include <core/config.hpp>
#include <smoke_api/config.hpp>
#include <koalabox/logger.hpp>
namespace steam_inventory {
@@ -53,11 +53,11 @@ namespace steam_inventory {
)
static uint32_t original_count = 0;
const auto injected_count = config::instance.extra_inventory_items.size();
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;
if (config::instance.auto_inject_inventory) {
if (smoke_api::config::instance.auto_inject_inventory) {
CALL_ONCE({
uint32_t count = 0;
if (get_item_definition_ids(nullptr, &count)) {
@@ -103,7 +103,7 @@ namespace steam_inventory {
for (int i = 0; i < injected_count; i++) {
auto& item = pOutItemsArray[original_count + auto_injected_count + i];
const auto item_def_id = config::instance.extra_inventory_items[i];
const auto item_def_id = smoke_api::config::instance.extra_inventory_items[i];
item = new_item(item_def_id);

View File

@@ -1,5 +1,5 @@
#include <steam_impl/steam_user.hpp>
#include <core/config.hpp>
#include <smoke_api/config.hpp>
#include <koalabox/logger.hpp>
namespace steam_user {
@@ -16,7 +16,7 @@ namespace steam_user {
return result;
}
const auto has_license = config::is_dlc_unlocked(
const auto has_license = smoke_api::config::is_dlc_unlocked(
0, appID, [&]() {
return result == k_EUserHasLicenseResultHasLicense;
}