[XAM] Changes to settings handling
- Fixed incorrect X_USER_PROFILE_SETTING structure - Added X_USER_PROFILE_SETTING_HEADER based on entries from console - Removed X_USER_PROFILE_SETTING_DATA in favor of X_USER_DATA X_USER_DATA is used also in Properties in exactly the same way - Removed is_set in favor of X_USER_PROFILE_SETTING_SOURCE - Prevent setting from storing settings longer than 0x3E8 bytes. Some games try to write bigger value which causes them to crash
This commit is contained in:
committed by
Radosław Gliński
parent
7d740fb3c1
commit
321305cbf8
@@ -17,212 +17,141 @@
|
||||
#include <vector>
|
||||
|
||||
#include "xenia/base/byte_stream.h"
|
||||
#include "xenia/kernel/util/xuserdata.h"
|
||||
#include "xenia/xbox.h"
|
||||
|
||||
namespace xe {
|
||||
namespace kernel {
|
||||
namespace xam {
|
||||
|
||||
struct X_USER_PROFILE_SETTING_DATA {
|
||||
// UserProfile::Setting::Type. Appears to be 8-in-32 field, and the upper 24
|
||||
// are not always zeroed by the game.
|
||||
uint8_t type;
|
||||
uint8_t unk_1[3];
|
||||
xe::be<uint32_t> unk_4;
|
||||
// TODO(sabretooth): not sure if this is a union, but it seems likely.
|
||||
// Haven't run into cases other than "binary data" yet.
|
||||
constexpr uint32_t kMaxSettingSize = 0x03E8;
|
||||
|
||||
enum class X_USER_PROFILE_SETTING_SOURCE : uint32_t {
|
||||
NOT_SET = 0,
|
||||
DEFAULT = 1,
|
||||
TITLE = 2,
|
||||
UNKNOWN = 3,
|
||||
};
|
||||
|
||||
// Each setting contains 0x18 bytes long header
|
||||
struct X_USER_PROFILE_SETTING_HEADER {
|
||||
xe::be<uint32_t> setting_id;
|
||||
xe::be<uint32_t> unknown_1;
|
||||
xe::be<uint8_t> setting_type;
|
||||
char unknown_2[3];
|
||||
xe::be<uint32_t> unknown_3;
|
||||
|
||||
union {
|
||||
xe::be<int32_t> s32;
|
||||
xe::be<int64_t> s64;
|
||||
xe::be<uint32_t> u32;
|
||||
xe::be<double> f64;
|
||||
struct {
|
||||
xe::be<uint32_t> size;
|
||||
xe::be<uint32_t> ptr;
|
||||
} unicode;
|
||||
xe::be<float> f32;
|
||||
struct {
|
||||
xe::be<uint32_t> size;
|
||||
xe::be<uint32_t> ptr;
|
||||
} binary;
|
||||
xe::be<uint64_t> filetime;
|
||||
// Size is used only for types: CONTENT, WSTRING, BINARY
|
||||
be<uint32_t> size;
|
||||
// Raw values that can be written. They do not need to be serialized.
|
||||
be<int32_t> s32;
|
||||
be<int64_t> s64;
|
||||
be<uint32_t> u32;
|
||||
be<double> f64;
|
||||
be<float> f32;
|
||||
};
|
||||
};
|
||||
static_assert_size(X_USER_PROFILE_SETTING_DATA, 16);
|
||||
static_assert_size(X_USER_PROFILE_SETTING_HEADER, 0x18);
|
||||
|
||||
struct X_USER_PROFILE_SETTING {
|
||||
xe::be<uint32_t> from;
|
||||
xe::be<uint32_t> unk04;
|
||||
union {
|
||||
xe::be<uint32_t> user_index;
|
||||
xe::be<uint64_t> xuid;
|
||||
};
|
||||
xe::be<uint32_t> setting_id;
|
||||
xe::be<uint32_t> unk14;
|
||||
union {
|
||||
uint8_t data_bytes[sizeof(X_USER_PROFILE_SETTING_DATA)];
|
||||
X_USER_PROFILE_SETTING_DATA data;
|
||||
uint8_t data_bytes[sizeof(X_USER_DATA)];
|
||||
X_USER_DATA data;
|
||||
};
|
||||
};
|
||||
static_assert_size(X_USER_PROFILE_SETTING, 40);
|
||||
|
||||
class UserSetting {
|
||||
public:
|
||||
template <typename T>
|
||||
UserSetting(uint32_t setting_id, T data) {
|
||||
header_.setting_id = setting_id;
|
||||
|
||||
setting_id_.value = setting_id;
|
||||
CreateUserData(setting_id, data);
|
||||
}
|
||||
|
||||
static bool is_title_specific(uint32_t setting_id) {
|
||||
return (setting_id & 0x3F00) == 0x3F00;
|
||||
}
|
||||
|
||||
bool is_title_specific() const {
|
||||
return is_title_specific(setting_id_.value);
|
||||
}
|
||||
|
||||
const uint32_t GetSettingId() const { return setting_id_.value; }
|
||||
const X_USER_PROFILE_SETTING_SOURCE GetSettingSource() const {
|
||||
return created_by_;
|
||||
}
|
||||
const X_USER_PROFILE_SETTING_HEADER* GetSettingHeader() const {
|
||||
return &header_;
|
||||
}
|
||||
UserData* GetSettingData() { return user_data_.get(); }
|
||||
|
||||
void SetNewSettingSource(X_USER_PROFILE_SETTING_SOURCE new_source) {
|
||||
created_by_ = new_source;
|
||||
}
|
||||
|
||||
void SetNewSettingHeader(X_USER_PROFILE_SETTING_HEADER* header) {
|
||||
header_ = *header;
|
||||
}
|
||||
|
||||
private:
|
||||
void CreateUserData(uint32_t setting_id, uint32_t data) {
|
||||
header_.setting_type = static_cast<uint8_t>(X_USER_DATA_TYPE::INT32);
|
||||
header_.s64 = data;
|
||||
user_data_ = std::make_unique<Uint32UserData>(data);
|
||||
}
|
||||
void CreateUserData(uint32_t setting_id, int32_t data) {
|
||||
header_.setting_type = static_cast<uint8_t>(X_USER_DATA_TYPE::INT32);
|
||||
header_.s32 = data;
|
||||
user_data_ = std::make_unique<Int32UserData>(data);
|
||||
}
|
||||
void CreateUserData(uint32_t setting_id, float data) {
|
||||
header_.setting_type = static_cast<uint8_t>(X_USER_DATA_TYPE::FLOAT);
|
||||
header_.f32 = data;
|
||||
user_data_ = std::make_unique<FloatUserData>(data);
|
||||
}
|
||||
void CreateUserData(uint32_t setting_id, double data) {
|
||||
header_.setting_type = static_cast<uint8_t>(X_USER_DATA_TYPE::DOUBLE);
|
||||
header_.f64 = data;
|
||||
user_data_ = std::make_unique<DoubleUserData>(data);
|
||||
}
|
||||
void CreateUserData(uint32_t setting_id, int64_t data) {
|
||||
header_.setting_type = static_cast<uint8_t>(X_USER_DATA_TYPE::INT64);
|
||||
header_.s64 = data;
|
||||
user_data_ = std::make_unique<Int64UserData>(data);
|
||||
}
|
||||
void CreateUserData(uint32_t setting_id, const std::u16string& data) {
|
||||
header_.setting_type = static_cast<uint8_t>(X_USER_DATA_TYPE::WSTRING);
|
||||
header_.size =
|
||||
std::min(kMaxSettingSize, static_cast<uint32_t>((data.size() + 1) * 2));
|
||||
user_data_ = std::make_unique<UnicodeUserData>(data);
|
||||
}
|
||||
void CreateUserData(uint32_t setting_id, const std::vector<uint8_t>& data) {
|
||||
header_.setting_type = static_cast<uint8_t>(X_USER_DATA_TYPE::BINARY);
|
||||
header_.size =
|
||||
std::min(kMaxSettingSize, static_cast<uint32_t>(data.size()));
|
||||
user_data_ = std::make_unique<BinaryUserData>(data);
|
||||
}
|
||||
|
||||
X_USER_PROFILE_SETTING_SOURCE created_by_ =
|
||||
X_USER_PROFILE_SETTING_SOURCE::DEFAULT;
|
||||
|
||||
X_USER_PROFILE_SETTING_HEADER header_ = {};
|
||||
UserData::Key setting_id_ = {};
|
||||
std::unique_ptr<UserData> user_data_ = nullptr;
|
||||
};
|
||||
|
||||
class UserProfile {
|
||||
public:
|
||||
class SettingByteStream : public ByteStream {
|
||||
public:
|
||||
SettingByteStream(uint32_t ptr, uint8_t* data, size_t data_length,
|
||||
size_t offset = 0)
|
||||
: ByteStream(data, data_length, offset), ptr_(ptr) {}
|
||||
|
||||
uint32_t ptr() const { return static_cast<uint32_t>(ptr_ + offset()); }
|
||||
|
||||
private:
|
||||
uint32_t ptr_;
|
||||
};
|
||||
struct Setting {
|
||||
enum class Type {
|
||||
CONTENT = 0,
|
||||
INT32 = 1,
|
||||
INT64 = 2,
|
||||
DOUBLE = 3,
|
||||
WSTRING = 4,
|
||||
FLOAT = 5,
|
||||
BINARY = 6,
|
||||
DATETIME = 7,
|
||||
UNSET = 0xFF,
|
||||
};
|
||||
union Key {
|
||||
uint32_t value;
|
||||
struct {
|
||||
uint32_t id : 14;
|
||||
uint32_t unk : 2;
|
||||
uint32_t size : 12;
|
||||
uint32_t type : 4;
|
||||
};
|
||||
};
|
||||
uint32_t setting_id;
|
||||
Type type;
|
||||
size_t size;
|
||||
bool is_set;
|
||||
uint32_t loaded_title_id;
|
||||
Setting(uint32_t setting_id, Type type, size_t size, bool is_set)
|
||||
: setting_id(setting_id),
|
||||
type(type),
|
||||
size(size),
|
||||
is_set(is_set),
|
||||
loaded_title_id(0) {}
|
||||
virtual void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) {
|
||||
data->type = static_cast<uint8_t>(type);
|
||||
}
|
||||
virtual std::vector<uint8_t> Serialize() const {
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
virtual void Deserialize(std::vector<uint8_t>) {}
|
||||
bool is_title_specific() const { return (setting_id & 0x3F00) == 0x3F00; }
|
||||
};
|
||||
struct Int32Setting : public Setting {
|
||||
Int32Setting(uint32_t setting_id, int32_t value)
|
||||
: Setting(setting_id, Type::INT32, 4, true), value(value) {}
|
||||
int32_t value;
|
||||
void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) override {
|
||||
Setting::Append(data, stream);
|
||||
data->s32 = value;
|
||||
}
|
||||
};
|
||||
struct Int64Setting : public Setting {
|
||||
Int64Setting(uint32_t setting_id, int64_t value)
|
||||
: Setting(setting_id, Type::INT64, 8, true), value(value) {}
|
||||
int64_t value;
|
||||
void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) override {
|
||||
Setting::Append(data, stream);
|
||||
data->s64 = value;
|
||||
}
|
||||
};
|
||||
struct DoubleSetting : public Setting {
|
||||
DoubleSetting(uint32_t setting_id, double value)
|
||||
: Setting(setting_id, Type::DOUBLE, 8, true), value(value) {}
|
||||
double value;
|
||||
void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) override {
|
||||
Setting::Append(data, stream);
|
||||
data->f64 = value;
|
||||
}
|
||||
};
|
||||
struct UnicodeSetting : public Setting {
|
||||
UnicodeSetting(uint32_t setting_id, const std::u16string& value)
|
||||
: Setting(setting_id, Type::WSTRING, 8, true), value(value) {}
|
||||
std::u16string value;
|
||||
void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) override {
|
||||
Setting::Append(data, stream);
|
||||
if (value.empty()) {
|
||||
data->unicode.size = 0;
|
||||
data->unicode.ptr = 0;
|
||||
} else {
|
||||
size_t count = value.size() + 1;
|
||||
size_t size = 2 * count;
|
||||
assert_true(size <= std::numeric_limits<uint32_t>::max());
|
||||
data->unicode.size = static_cast<uint32_t>(size);
|
||||
data->unicode.ptr = stream->ptr();
|
||||
auto buffer =
|
||||
reinterpret_cast<uint16_t*>(&stream->data()[stream->offset()]);
|
||||
stream->Advance(size);
|
||||
xe::copy_and_swap(buffer, (uint16_t*)value.data(), count);
|
||||
}
|
||||
}
|
||||
};
|
||||
struct FloatSetting : public Setting {
|
||||
FloatSetting(uint32_t setting_id, float value)
|
||||
: Setting(setting_id, Type::FLOAT, 4, true), value(value) {}
|
||||
float value;
|
||||
void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) override {
|
||||
Setting::Append(data, stream);
|
||||
data->f32 = value;
|
||||
}
|
||||
};
|
||||
struct BinarySetting : public Setting {
|
||||
BinarySetting(uint32_t setting_id)
|
||||
: Setting(setting_id, Type::BINARY, 8, false), value() {}
|
||||
BinarySetting(uint32_t setting_id, const std::vector<uint8_t>& value)
|
||||
: Setting(setting_id, Type::BINARY, 8, true), value(value) {}
|
||||
std::vector<uint8_t> value;
|
||||
void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) override {
|
||||
Setting::Append(data, stream);
|
||||
if (value.empty()) {
|
||||
data->binary.size = 0;
|
||||
data->binary.ptr = 0;
|
||||
} else {
|
||||
size_t size = value.size();
|
||||
assert_true(size <= std::numeric_limits<uint32_t>::max());
|
||||
data->binary.size = static_cast<uint32_t>(size);
|
||||
data->binary.ptr = stream->ptr();
|
||||
stream->Write(value.data(), size);
|
||||
}
|
||||
}
|
||||
std::vector<uint8_t> Serialize() const override {
|
||||
return std::vector<uint8_t>(value.data(), value.data() + value.size());
|
||||
}
|
||||
void Deserialize(std::vector<uint8_t> data) override {
|
||||
value = data;
|
||||
is_set = true;
|
||||
}
|
||||
};
|
||||
struct DateTimeSetting : public Setting {
|
||||
DateTimeSetting(uint32_t setting_id, int64_t value)
|
||||
: Setting(setting_id, Type::DATETIME, 8, true), value(value) {}
|
||||
int64_t value;
|
||||
void Append(X_USER_PROFILE_SETTING_DATA* data,
|
||||
SettingByteStream* stream) override {
|
||||
Setting::Append(data, stream);
|
||||
data->filetime = value;
|
||||
}
|
||||
};
|
||||
|
||||
UserProfile(uint8_t index);
|
||||
|
||||
uint64_t xuid() const { return xuid_; }
|
||||
@@ -230,19 +159,19 @@ class UserProfile {
|
||||
uint32_t signin_state() const { return 1; }
|
||||
uint32_t type() const { return 1 | 2; /* local | online profile? */ }
|
||||
|
||||
void AddSetting(std::unique_ptr<Setting> setting);
|
||||
Setting* GetSetting(uint32_t setting_id);
|
||||
void AddSetting(std::unique_ptr<UserSetting> setting);
|
||||
UserSetting* GetSetting(uint32_t setting_id);
|
||||
|
||||
std::map<uint32_t, uint32_t> contexts_;
|
||||
|
||||
private:
|
||||
uint64_t xuid_;
|
||||
std::string name_;
|
||||
std::vector<std::unique_ptr<Setting>> setting_list_;
|
||||
std::unordered_map<uint32_t, Setting*> settings_;
|
||||
std::vector<std::unique_ptr<UserSetting>> setting_list_;
|
||||
std::unordered_map<uint32_t, UserSetting*> settings_;
|
||||
|
||||
void LoadSetting(UserProfile::Setting*);
|
||||
void SaveSetting(UserProfile::Setting*);
|
||||
void LoadSetting(UserSetting*);
|
||||
void SaveSetting(UserSetting*);
|
||||
};
|
||||
|
||||
} // namespace xam
|
||||
|
||||
Reference in New Issue
Block a user