Fixed static order init fiasco

This commit is contained in:
acidicoala
2026-01-02 21:57:41 +05:00
parent 3b8635d16b
commit 9acf7312d3
8 changed files with 43 additions and 32 deletions

View File

@@ -1,14 +1,16 @@
#include <koalabox/config.hpp>
#include <koalabox/io.hpp>
#include <koalabox/logger.hpp>
#include "smoke_api/config.hpp"
namespace smoke_api::config {
Config instance; // NOLINT(cert-err58-cpp)
Config& get() noexcept {
static Config config;
return config;
}
std::vector<DLC> get_extra_dlcs(const uint32_t app_id) {
return DLC::get_dlcs_from_apps(instance.extra_dlcs, app_id);
return DLC::get_dlcs_from_apps(get().extra_dlcs, app_id);
}
bool is_dlc_unlocked(
@@ -16,16 +18,16 @@ namespace smoke_api::config {
const AppId_t dlc_id,
const std::function<bool()>& original_function
) {
auto status = instance.default_app_status;
auto status = get().default_app_status;
const auto app_id_str = std::to_string(app_id);
if(instance.override_app_status.contains(app_id_str)) {
status = instance.override_app_status[app_id_str];
if(get().override_app_status.contains(app_id_str)) {
status = get().override_app_status[app_id_str];
}
const auto dlc_id_str = std::to_string(dlc_id);
if(instance.override_dlc_status.contains(dlc_id_str)) {
status = instance.override_dlc_status[dlc_id_str];
if(get().override_dlc_status.contains(dlc_id_str)) {
status = get().override_dlc_status[dlc_id_str];
}
bool is_unlocked;

View File

@@ -43,7 +43,7 @@ namespace smoke_api::config {
)
};
extern Config instance;
Config& get() noexcept;
std::vector<DLC> get_extra_dlcs(AppId_t app_id);

View File

@@ -14,8 +14,15 @@ namespace {
/// This means we have to get extra DLC IDs from local config, remote config, or cache.
constexpr auto MAX_DLC = 64;
std::map<uint32_t, std::vector<DLC>> app_dlcs; // NOLINT(cert-err58-cpp)
std::set<uint32_t> fully_fetched; // NOLINT(cert-err58-cpp)
auto& get_fully_fetched_apps() {
static std::set<uint32_t> fully_fetched_apps;
return fully_fetched_apps;
}
auto& get_app_dlc_map() {
static std::map<uint32_t, std::vector<DLC>> app_dlc_map;
return app_dlc_map;
}
std::string get_app_id_log(const uint32_t app_id) {
return app_id ? std::format("App ID: {:>8}, ", app_id) : "";
@@ -31,13 +38,13 @@ namespace {
if(app_id == 0) {
LOG_ERROR("{} -> App ID is 0", __func__);
app_dlcs[app_id] = {}; // Dummy value to avoid checking for presence on each access
get_app_dlc_map()[app_id] = {}; // Dummy value to avoid checking for presence on each access
return;
}
// We want to fetch data only once. However, if any of the remote sources have failed
// previously, we want to attempt fetching again.
if(fully_fetched.contains(app_id)) {
if(get_fully_fetched_apps().contains(app_id)) {
return;
}
@@ -67,13 +74,13 @@ namespace {
}
if(github_dlcs_opt && steam_dlcs_opt) {
fully_fetched.insert(app_id);
get_fully_fetched_apps().insert(app_id);
} else {
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;
get_app_dlc_map()[app_id] = aggregated_dlcs;
smoke_api::cache::save_dlcs(app_id, aggregated_dlcs);
}
@@ -137,12 +144,12 @@ namespace smoke_api::steam_apps {
fetch_and_cache_dlcs(app_id);
if(app_dlcs.empty()) {
if(get_app_dlc_map().empty()) {
LOG_DEBUG("{} -> No cached DLCs, responding with original count", function_name);
return total_count(original_count);
}
return total_count(static_cast<int>(app_dlcs[app_id].size()));
return total_count(static_cast<int>(get_app_dlc_map()[app_id].size()));
} catch(const std::exception& e) {
LOG_ERROR("{} -> Uncaught exception: {}", function_name, e.what());
return 0;
@@ -188,8 +195,8 @@ namespace smoke_api::steam_apps {
pchName[bytes_to_copy] = '\0'; // Ensure null-termination
};
if(!app_dlcs.empty() && app_dlcs.contains(app_id)) {
const auto& dlcs = app_dlcs[app_id];
if(!get_app_dlc_map().empty() && get_app_dlc_map().contains(app_id)) {
const auto& dlcs = get_app_dlc_map()[app_id];
if(iDLC >= 0 && iDLC < dlcs.size()) {
output_dlc(dlcs[iDLC]);

View File

@@ -14,7 +14,7 @@ namespace smoke_api::steam_http {
try {
const auto result = original_function();
if(config::instance.log_steam_http) {
if(config::get().log_steam_http) {
const std::string_view buffer =
pBodyDataBuffer && unBufferSize
? std::string_view(
@@ -50,7 +50,7 @@ namespace smoke_api::steam_http {
try {
const auto result = original_function();
if(config::instance.log_steam_http) {
if(config::get().log_steam_http) {
const std::string_view buffer =
pBodyDataBuffer && unBufferSize
? std::string_view(
@@ -87,7 +87,7 @@ namespace smoke_api::steam_http {
try {
const auto result = original_function();
if(config::instance.log_steam_http) {
if(config::get().log_steam_http) {
const std::string_view content_type =
pchContentType ? pchContentType : "smoke_api::N/A";

View File

@@ -70,11 +70,11 @@ namespace smoke_api::steam_inventory {
);
static uint32_t original_count = 0;
const auto injected_count = config::instance.extra_inventory_items.size();
const auto injected_count = config::get().extra_inventory_items.size();
// Automatically get inventory items from steam
static std::vector<SteamItemDef_t> auto_inventory_items;
if(config::instance.auto_inject_inventory) {
if(config::get().auto_inject_inventory) {
static std::once_flag inventory_inject_flag;
std::call_once(
inventory_inject_flag,
@@ -126,7 +126,7 @@ namespace smoke_api::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 = config::get().extra_inventory_items[i];
item = new_item(item_def_id);