mirror of https://github.com/acidicoala/SmokeAPI
Refactor
parent
b1680fe3d7
commit
a2cbf55819
@ -1 +1 @@
|
||||
Subproject commit 15d5cfc2e515bc72e47da6c0c563820cff98551f
|
||||
Subproject commit b076544f304c91859a2260e195846aaaa99e6567
|
@ -1,82 +0,0 @@
|
||||
#include <core/cache.hpp>
|
||||
#include <core/paths.hpp>
|
||||
#include <koalabox/io.hpp>
|
||||
#include <koalabox/logger.hpp>
|
||||
|
||||
namespace cache {
|
||||
|
||||
|
||||
Cache read_cache_from_disk() {
|
||||
try {
|
||||
const auto cache_string = koalabox::io::read_file(paths::get_cache_path());
|
||||
|
||||
if (cache_string.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return nlohmann::json::parse(cache_string).get<Cache>();
|
||||
} catch (const Exception& e) {
|
||||
LOG_WARN("{} -> Failed to read cache from disk: {}", __func__, e.what())
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
void write_cache_to_disk(const Cache& cache) {
|
||||
try {
|
||||
const auto cache_string = nlohmann::json(cache).dump(2);
|
||||
|
||||
koalabox::io::write_file(paths::get_cache_path(), cache_string);
|
||||
} catch (const Exception& e) {
|
||||
LOG_ERROR("{} -> Failed to write cache to disk: {}", __func__, e.what())
|
||||
}
|
||||
}
|
||||
|
||||
Vector<AppId_t> get_dlc_ids(AppId_t app_id) {
|
||||
const auto cache = read_cache_from_disk();
|
||||
|
||||
const auto app_id_str = std::to_string(app_id);
|
||||
|
||||
if (cache.apps.contains(app_id_str)) {
|
||||
return cache.apps.at(app_id_str).dlc_ids;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<koalageddon::KoalageddonConfig> get_koalageddon_config() {
|
||||
const auto cache = read_cache_from_disk();
|
||||
|
||||
if (cache.koalageddon_config.is_null()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return cache.koalageddon_config.get<koalageddon::KoalageddonConfig>();
|
||||
}
|
||||
|
||||
void save_dlc_ids(AppId_t app_id, const Vector<AppId_t>& dlc_ids) {
|
||||
LOG_DEBUG("{} -> Caching DLC IDs for the app: {}", __func__, app_id)
|
||||
|
||||
auto cache = read_cache_from_disk();
|
||||
|
||||
const auto app_id_str = std::to_string(app_id);
|
||||
|
||||
if (not cache.apps.contains(app_id_str)) {
|
||||
cache.apps[app_id_str] = {};
|
||||
}
|
||||
|
||||
cache.apps[app_id_str].dlc_ids = dlc_ids;
|
||||
|
||||
write_cache_to_disk(cache);
|
||||
}
|
||||
|
||||
void save_koalageddon_config(const koalageddon::KoalageddonConfig& config) {
|
||||
LOG_DEBUG("{} -> Caching koalageddon config", __func__)
|
||||
|
||||
auto cache = read_cache_from_disk();
|
||||
|
||||
cache.koalageddon_config = config;
|
||||
|
||||
write_cache_to_disk(cache);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <koalabox/types.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <koalageddon/koalageddon.hpp>
|
||||
#include <core/steam_types.hpp>
|
||||
|
||||
/**
|
||||
* This namespace contains utility functions for reading from and writing to cache file on disk.
|
||||
* All functions are intended to be safe to call, i.e. they should not throw exceptions.
|
||||
*/
|
||||
namespace cache {
|
||||
|
||||
struct App {
|
||||
Vector<AppId_t> dlc_ids;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(App, dlc_ids) // NOLINT(misc-const-correctness)
|
||||
};
|
||||
|
||||
struct Cache {
|
||||
// Key represents App ID
|
||||
Map<String, App> apps;
|
||||
nlohmann::json koalageddon_config;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Cache, apps, koalageddon_config) // NOLINT(misc-const-correctness)
|
||||
};
|
||||
|
||||
Vector<AppId_t> get_dlc_ids(AppId_t app_id);
|
||||
|
||||
std::optional<koalageddon::KoalageddonConfig> get_koalageddon_config();
|
||||
|
||||
void save_dlc_ids(AppId_t app_id, const Vector<AppId_t>& dlc_ids);
|
||||
|
||||
void save_koalageddon_config(const koalageddon::KoalageddonConfig& config);
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
#include <core/globals.hpp>
|
||||
|
||||
namespace globals {
|
||||
|
||||
HMODULE smokeapi_handle = nullptr;
|
||||
HMODULE steamapi_module = nullptr;
|
||||
HMODULE vstdlib_module = nullptr;
|
||||
HMODULE steamclient_module = nullptr;
|
||||
Map<String, uintptr_t> address_map; // NOLINT(cert-err58-cpp)
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <koalabox/core.hpp>
|
||||
|
||||
#define COMPILE_KOALAGEDDON _WIN64
|
||||
|
||||
using AppId_t = uint32_t;
|
||||
using SteamInventoryResult_t = uint32_t;
|
@ -0,0 +1,33 @@
|
||||
#include <koalageddon/cache.hpp>
|
||||
#include <koalabox/cache.hpp>
|
||||
#include <koalabox/logger.hpp>
|
||||
|
||||
constexpr auto KEY_KG_CONFIG = "koalageddon_config";
|
||||
|
||||
namespace koalageddon::cache {
|
||||
|
||||
std::optional<KoalageddonConfig> get_koalageddon_config() {
|
||||
try {
|
||||
const auto cache = koalabox::cache::read_from_cache(KEY_KG_CONFIG).value();
|
||||
|
||||
return cache.at(KEY_KG_CONFIG).get<KoalageddonConfig>();
|
||||
} catch (const Exception& e) {
|
||||
LOG_ERROR("{} -> Failed to get cached koalageddon config: {}", __func__, e.what())
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
bool save_koalageddon_config(const KoalageddonConfig& config) {
|
||||
try {
|
||||
LOG_DEBUG("{} -> Caching koalageddon config", __func__)
|
||||
|
||||
return koalabox::cache::save_to_cache(KEY_KG_CONFIG, config);
|
||||
} catch (const Exception& e) {
|
||||
LOG_ERROR("{} -> Failed to cache koalageddon config: {}", __func__, e.what())
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <koalageddon/types.hpp>
|
||||
|
||||
namespace koalageddon::cache {
|
||||
|
||||
std::optional<KoalageddonConfig> get_koalageddon_config();
|
||||
|
||||
bool save_koalageddon_config(const KoalageddonConfig& config);
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <koalabox/core.hpp>
|
||||
|
||||
// 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.
|
||||
struct KoalageddonConfig {
|
||||
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 koalageddon config requires definition of all keys
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE(
|
||||
KoalageddonConfig, // 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
|
||||
)
|
||||
};
|
@ -0,0 +1,60 @@
|
||||
#include <smoke_api/app_cache.hpp>
|
||||
#include <core/paths.hpp>
|
||||
#include <koalabox/cache.hpp>
|
||||
#include <koalabox/logger.hpp>
|
||||
|
||||
struct App {
|
||||
Vector<AppId_t> dlc_ids;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(App, dlc_ids) // NOLINT(misc-const-correctness)
|
||||
};
|
||||
|
||||
using Apps = Map<String, App>;
|
||||
|
||||
constexpr auto KEY_APPS = "apps";
|
||||
|
||||
Apps get_cached_apps() {
|
||||
try {
|
||||
const auto cache = koalabox::cache::read_from_cache(KEY_APPS).value();
|
||||
|
||||
return cache.get<Apps>();
|
||||
} catch (const Exception& e) {
|
||||
LOG_WARN("{} -> Failed to get cached apps: {}", __func__, e.what())
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
namespace smoke_api::app_cache {
|
||||
|
||||
Vector<AppId_t> get_dlc_ids(AppId_t app_id) {
|
||||
try {
|
||||
LOG_DEBUG("{} -> Reading cached DLC IDs for the app: {}", __func__, app_id)
|
||||
|
||||
const auto app = get_cached_apps().at(std::to_string(app_id));
|
||||
|
||||
return app.dlc_ids;
|
||||
} catch (const Exception& e) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool save_dlc_ids(AppId_t app_id, const Vector<AppId_t>& dlc_ids) {
|
||||
try {
|
||||
LOG_DEBUG("{} -> Caching DLC IDs for the app: {}", __func__, app_id)
|
||||
|
||||
auto apps = get_cached_apps();
|
||||
|
||||
apps[std::to_string(app_id)] = {
|
||||
.dlc_ids = dlc_ids
|
||||
};
|
||||
|
||||
return koalabox::cache::save_to_cache(KEY_APPS, apps);
|
||||
} catch (const Exception& e) {
|
||||
LOG_ERROR("{} -> Failed to cache DLC IDs fro the app: {}", __func__, app_id)
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <core/types.hpp>
|
||||
|
||||
namespace smoke_api::app_cache {
|
||||
|
||||
Vector<AppId_t> get_dlc_ids(AppId_t app_id);
|
||||
|
||||
bool save_dlc_ids(AppId_t app_id, const Vector<AppId_t>& dlc_ids);
|
||||
|
||||
}
|
Loading…
Reference in New Issue