Files
ReSkate/Extension/Settings/local_native_settings.cpp
T
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

148 lines
7.8 KiB
C++

#include "Engine/Core/Log/logging.h"
#include "Extension/Profile/runtime_internal.h"
#include "local_native_settings.h"
#include "Engine/Game/Build/addresses.h"
#include "Engine/Game/Build/20260929/engine.h"
#include "Engine/Game/Build/20260929/local_native_settings.h"
#include "Engine/Game/Build/20260929/named_settings.h"
#include "local_user_settings.h"
#include <optional>
#include <string_view>
#include <array>
namespace dingosdk::profile_runtime {
namespace native_types = addr::named_settings;
// Modern DingoUISettingAsset controls use DingoProfileSettingsManager's boxed
// option groups, independently of the legacy DelMar/ECS settings scripts.
NativeSettingsFunctions& native_settings() { static NativeSettingsFunctions f; return f; }
unsigned native_settings_location(std::uintptr_t group) {
auto& s = local_runtime();
if (!group || !s.active.load(std::memory_order_acquire)) return 0;
// Asked on every settings get the game's scripts make: peeked, no system call per read.
std::uintptr_t manager{}, begin{}, end{}, candidate{};
if (!memory::peek(s.base + addr::local_native_settings::profile_settings_manager, manager) || !manager ||
!memory::peek(manager, begin) || !memory::peek(manager + 8, end) ||
!begin || end < begin || end - begin != 3 * sizeof(std::uintptr_t)) return 0;
// LocalPerDevice (0) already has working native disk storage. Only the two
// cloud groups need the offline provider; retain their separate key spaces.
for (unsigned location : {1u, 2u})
if (memory::peek(begin + location * 8, candidate) && candidate == group) return location;
return 0;
}
// Runs on every settings get the game's scripts make: peeked, not read (one ReadProcessMemory
// call per read, ~0.15% of a multiplayer client frame, profiled 2026-10-02).
std::optional<dingosdk::Json> native_setting_json(const NativeSettingValue* input) {
NativeSettingValue value{};
if (!memory::peek(reinterpret_cast<std::uintptr_t>(input), value) || !value.data) return {};
const auto type = value.type - local_runtime().base;
switch (type) {
case native_types::native_bool: {
std::uint8_t byte{};
if (memory::peek(reinterpret_cast<std::uintptr_t>(value.data), byte) && byte <= 1) return byte != 0;
return {};
}
case native_types::native_uint32: return native_scalar<std::uint32_t>(value.data);
case native_types::native_int32: return native_scalar<std::int32_t>(value.data);
case native_types::native_float32: return native_scalar<float>(value.data);
case native_types::native_cstring: {
std::uintptr_t text{};
if (!memory::peek(reinterpret_cast<std::uintptr_t>(value.data), text)) return {};
char buffer[4097];
const auto length = memory::peek_cstring(text, buffer, sizeof(buffer));
if (length < 0) return {};
return std::string(buffer, static_cast<std::size_t>(length));
}
}
return {};
}
namespace {
// The saved option for a native setting key, or null: one store lookup per key and profile change,
// cached on the asking thread in a small direct-mapped table (valid until this thread's next call).
// A hit needs the same key address, location and whole key text (keys can share an address and
// their first bytes: CRAS_HasSeen...Tab), with no heap string, store lock or map walk. May throw.
const dingosdk::Json* saved_native_option(unsigned location, const char* key) {
const auto address = reinterpret_cast<std::uintptr_t>(key);
char text[256];
const auto length = memory::peek_cstring(address, text, sizeof(text));
if (length <= 0) return nullptr;
const std::string_view name(text, static_cast<std::size_t>(length));
struct Entry { std::uintptr_t key{}; unsigned location{}; std::uint64_t changes{}; std::string name; std::optional<dingosdk::Json> value; };
thread_local std::array<Entry, 256> cache;
const auto changes = profile::Store::changes();
auto& entry = cache[((address >> 3) ^ (address >> 11) ^ location) & 255];
if (entry.key != address || entry.location != location || entry.changes != changes || entry.name != name) {
for (const unsigned char c : name) if (c < 32 || c == 127) return nullptr;
entry.changes = 0;
entry.value = local_runtime().store->native_profile_option(location, name);
entry.name = name;
entry.key = address;
entry.location = location;
entry.changes = changes;
}
return entry.value ? &*entry.value : nullptr;
}
}
bool restore_native_setting(std::uintptr_t group, const char* key, std::uintptr_t type,
const dingosdk::Json& saved) {
const auto base = local_runtime().base;
switch (type - base) {
case native_types::native_bool: return restore_native_scalar<bool>(group, key, type, saved);
case native_types::native_uint32: return restore_native_scalar<std::uint32_t>(group, key, type, saved);
case native_types::native_int32: return restore_native_scalar<std::int32_t>(group, key, type, saved);
case native_types::native_float32: return restore_native_scalar<float>(group, key, type, saved);
case native_types::native_cstring: {
if (!saved.is_string()) return false;
const auto& value = saved.string();
// CString cloning needs engine-owned storage, not a std::string buffer.
const char* text = reinterpret_cast<const char*>(base + addr::engine::empty_cstring);
user_values().assign_string(&text, value.c_str(), static_cast<std::uint32_t>(value.size()));
struct Release { const char** text; ~Release() { local_runtime().destroy_string(text); } } release{&text};
const NativeSettingValue borrowed{type, &text};
return native_settings().set(group, key, type, &borrowed);
}
}
return false;
}
const NativeSettingValue* native_setting_get(std::uintptr_t group, const char* key) {
auto* result = native_settings().get(group, key);
PreserveError preserve;
try {
const auto location = native_settings_location(group);
const auto* saved = location ? saved_native_option(location, key) : nullptr;
if (!saved) return result;
const auto current = native_setting_json(result);
if (!current || *current == *saved) return result;
// Copied before the restore: a get it causes on this thread reuses the cache.
const auto wanted = *saved;
std::string name;
(void)identifier(&key, name);
NativeSettingValue native{};
if (memory::peek(reinterpret_cast<std::uintptr_t>(result), native) && restore_native_setting(group, key, native.type, wanted))
dingosdk::logging::event(dingosdk::logging::Channel::settings, dingosdk::Json{{"event","local_native_setting_restored"},{"location",location},{"key",name}}.dump().c_str());
} catch (...) { dingosdk::logging::event(dingosdk::logging::Channel::settings, "{\"event\":\"local_native_setting_restore_failed\"}"); }
return result;
}
bool native_setting_set(std::uintptr_t group, const char* key, std::uintptr_t type, const NativeSettingValue* value) {
const bool changed = native_settings().set(group, key, type, value);
PreserveError preserve;
try {
const auto location = native_settings_location(group); std::string id;
if (changed && location && identifier(&key, id)) {
// Read back the accepted native value, including type validation.
if (const auto saved = native_setting_json(native_settings().get(group, key))) {
local_runtime().store->set_native_profile_option(location, id, *saved);
dingosdk::logging::event(dingosdk::logging::Channel::settings, dingosdk::Json{{"event","local_native_setting_saved"},{"location",location},{"key",id}}.dump().c_str());
}
}
} catch (...) { dingosdk::logging::event(dingosdk::logging::Channel::settings, "{\"event\":\"local_native_setting_save_failed\"}"); }
return changed;
}
}