Files
ReSkate/Extension/Settings/local_gameplay_settings.cpp
AyeZeeBB 0b4996e27a Fix settings reverting: saves never refreshed the settings caches
Changing a setting in the game's menu did not stick. The camera went
back to high on leaving the menu, grind assist and lean exit stayed at
20 / on, the compass and scoring ticker kept their old state, and the
Experimental tab's "reset to default" returned to whatever was set at
launch.
b763631 gave the settings hooks per-thread caches of saved values that
look a value up again only when profile::Store::changes() advances.
Only whole-profile commits advanced it. Every single save (a setting,
an option, an outfit) goes through Store::Update::commit(), which did
not, so the caches answered with launch-time values for the rest of
the session. native_setting_get also wrote the cached value back over
the new one when the two differed. The saves themselves reached the
database, so profiles are intact.
- Update::commit() advances the counter through Store::note_change(),
  after value_ holds the new values and before the lock is released.
  Store::commit() and the constructor use it too.
- A cache hit needs the whole key text, not the key's address and first
  8 bytes. Many keys share those bytes (egf_enable..., egf_exp_...,
  CRAS_HasSeen...Tab), so a reused address could answer for another
  setting. Still no heap string, store lock or map walk on a hit.
- An outfit that is not loaded or not saved is logged as a warning with
  its reason and preset (local_cosmetic_loadout_rejected). It was a
  debug event, so a player's log never said why an outfit was refused.
  Routine cases stay at debug.
- dingosdk_profile_change_tests (with the park editor tests): each kind
  of save advances changes(), an unchanged save does not, and saved
  values keep their types across a restart.
2026-10-03 16:35:12 -05:00

169 lines
8.0 KiB
C++

#include "Engine/Core/Log/logging.h"
#include "Extension/Profile/runtime_internal.h"
#include "Extension/Progression/local_entitlement_trigger_runtime.h"
#include "local_gameplay_settings.h"
#include "local_user_settings.h"
#include <optional>
#include <string_view>
#include <array>
namespace dingosdk::profile_runtime {
// Applied gameplay values live in an ECS cache even when the user-value
// service does not exist. Hook the applied cache, never the pending UI edits.
// See analysis/native-game-settings.md for the two distinct native boundaries.
GameplaySettingFunctions& gameplay_settings() { static GameplaySettingFunctions f; return f; }
// UI edits and repeated backend initialization share the same setters. Only
// the reviewed menu actions authorize a durable edit; initialization restores.
unsigned gameplay_expression_kind(std::uintptr_t vm) {
std::uintptr_t instance{}, resource{}, current{}; std::uint32_t key{};
std::array<std::uint32_t, 10> layout{};
if (!vm || !read(vm + 0x30, instance) || !read(instance, resource) ||
!read(vm + 0x38, current) || current != resource || !read(resource + 0x10, key) ||
!read(resource + 0x20, layout)) return 0;
if ((key == 0x6777763e && layout == std::array<std::uint32_t, 10>{112,2696,1010,2329,11,0,35,1638514,6881314,66563}) ||
(key == 0xff4a234e && layout == std::array<std::uint32_t, 10>{80,416,184,359,6,0,6,327698,2949131,1}) ||
(key == 0xff2662d5 && layout == std::array<std::uint32_t, 10>{96,2000,661,1338,6,0,23,983108,5046294,2}) ||
(key == 0x82c1331d && layout == std::array<std::uint32_t, 10>{64,496,202,472,9,0,5,28,1507339,66305}) ||
(key == 0x6e9f5108 && layout == std::array<std::uint32_t, 10>{96,1928,746,1444,11,0,14,1638495,7208982,17041665})) return 1;
if ((key == 0x7a55b1d7 && layout == std::array<std::uint32_t, 10>{224,5624,721,2953,19,0,17,2293843,6488140,66561}) ||
(key == 0x375c4ce9 && layout == std::array<std::uint32_t, 10>{112,1552,1014,2506,33,0,7,983209,8454179,264449})) return 2;
return 0;
}
bool gameplay_user_edit() {
if (gameplay_expression_kind(executing_expression) == 1) return true;
auto* scope = expression_scope;
for (unsigned depth = 0; scope && depth < 32; ++depth, scope = scope->parent)
if (scope->vm != executing_expression && gameplay_expression_kind(scope->vm) == 1) return true;
return false;
}
bool gameplay_setting_key(const void* reference, std::string& key) {
std::uintptr_t asset{};
if (!memory::peek(reinterpret_cast<std::uintptr_t>(reference), asset)) return false; // every script settings get
asset &= ~std::uintptr_t{4};
return asset && identifier(reinterpret_cast<const void*>(asset + 0x60), key);
}
// Every script settings get the game makes (several a frame from its expressions) asks this. One
// store lookup per setting asset and profile change, cached on the asking thread in a small
// direct-mapped table: a hit is two peeks, a copy of the name and compares, with no heap string,
// store lock or map walk (~0.5% of a multiplayer client frame before, profiled 2026-10-02). A hit
// needs the same asset and the same whole name, so a reused address cannot answer for another
// setting (many share their first bytes: egf_enable..., egf_exp_...).
const dingosdk::Json* saved_gameplay_json(const void* reference) {
auto& s = local_runtime();
if (!s.active.load(std::memory_order_acquire)) return nullptr;
std::uintptr_t asset{}, name{};
if (!memory::peek(reinterpret_cast<std::uintptr_t>(reference), asset)) return nullptr;
asset &= ~std::uintptr_t{4};
if (!asset || !memory::peek(asset + 0x60, name) || !name) return nullptr;
char text[256];
const auto length = memory::peek_cstring(name, text, sizeof(text));
if (length <= 0) return nullptr;
const std::string_view key(text, static_cast<std::size_t>(length));
struct Entry { std::uintptr_t asset{}; std::uint64_t changes{}; std::string key; std::optional<dingosdk::Json> value; };
thread_local std::array<Entry, 256> cache;
const auto changes = profile::Store::changes();
auto& entry = cache[((asset >> 4) ^ (asset >> 12)) & 255];
if (entry.asset != asset || entry.changes != changes || entry.key != key) {
for (const unsigned char c : key) if (c < 32 || c == 127) return nullptr;
entry.changes = 0;
entry.value = s.store->user_value(key);
entry.key = key;
entry.asset = asset;
entry.changes = changes;
}
return entry.value ? &*entry.value : nullptr;
}
void save_gameplay_setting(const void* reference, const dingosdk::Json& value) {
auto& s = local_runtime();
if (!s.active.load(std::memory_order_acquire)) return;
PreserveError preserve;
try {
std::string key;
if (!gameplay_setting_key(reference, key)) return;
if (s.store->user_value(key) == value) return;
s.store->set_user_value(key, value);
dingosdk::logging::event(dingosdk::logging::Channel::settings, dingosdk::Json{{"event", "local_gameplay_setting_saved"}, {"key", key}}.dump().c_str());
} catch (...) { dingosdk::logging::event(dingosdk::logging::Channel::settings, "{\"event\":\"local_gameplay_setting_save_failed\"}"); }
}
bool gameplay_get_bool(std::uintptr_t m, const void* a) {
if (const auto value = saved_gameplay_setting<bool>(a)) return *value;
return gameplay_settings().get_bool(m, a);
}
float gameplay_get_number(std::uintptr_t m, const void* a) {
if (const auto value = saved_gameplay_setting<float>(a)) return *value;
return gameplay_settings().get_number(m, a);
}
std::int32_t gameplay_get_integer(std::uintptr_t m, const void* a) {
if (const auto value = saved_gameplay_setting<std::int32_t>(a)) return *value;
return gameplay_settings().get_integer(m, a);
}
void* gameplay_get_string(std::uintptr_t m, void* out, const void* a) {
// The original constructs the output CString; assignment may then replace it.
auto* result = gameplay_settings().get_string(m, out, a);
if (const auto value = saved_gameplay_setting<std::string>(a))
user_values().assign_string(out, value->c_str(), static_cast<std::uint32_t>(value->size()));
return result;
}
void gameplay_set_bool(std::uintptr_t m, const void* a, bool v) {
if (local_runtime().active.load(std::memory_order_acquire)) {
PreserveError preserve; std::string key;
}
if (!gameplay_user_edit()) {
gameplay_settings().set_bool(m, a, saved_gameplay_setting<bool>(a).value_or(v)); return;
}
gameplay_settings().set_bool(m, a, v);
save_gameplay_setting(a, v);
}
void gameplay_set_number(const void* a, float v) {
if (!gameplay_user_edit()) {
gameplay_settings().set_number(a, saved_gameplay_setting<float>(a).value_or(v)); return;
}
gameplay_settings().set_number(a, v);
save_gameplay_setting(a, v);
}
void gameplay_set_integer(std::uintptr_t m, const void* a, std::int32_t v) {
if (!gameplay_user_edit()) {
gameplay_settings().set_integer(m, a, saved_gameplay_setting<std::int32_t>(a).value_or(v)); return;
}
gameplay_settings().set_integer(m, a, v);
save_gameplay_setting(a, v);
}
void gameplay_set_string(std::uintptr_t m, const void* a, const void* v) {
const bool edit = gameplay_user_edit();
if (!edit) {
if (const auto saved = saved_gameplay_setting<std::string>(a)) {
const char* text = saved->c_str(); gameplay_settings().set_string(m, a, &text); return;
}
}
gameplay_settings().set_string(m, a, v);
if (!edit || !local_runtime().active.load(std::memory_order_acquire)) return;
PreserveError preserve;
try {
std::uintptr_t text{}; std::string value;
if (!read(reinterpret_cast<std::uintptr_t>(v), text)) return;
for (std::size_t i = 0; i <= 4096; ++i) {
char c{}; if (!read(text + i, c)) return;
if (!c) { save_gameplay_setting(a, value); return; }
value += c;
}
} catch (...) { dingosdk::logging::event(dingosdk::logging::Channel::settings, "{\"event\":\"local_gameplay_setting_save_failed\"}"); }
}
}