32 lines
1.3 KiB
C++
32 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include "account_data_store.hpp"
|
|
|
|
namespace gateway::sdhd_preference {
|
|
|
|
constexpr uint32_t SetOpcode = 0x4401b1;
|
|
constexpr uint8_t SD = 0x80;
|
|
|
|
inline std::optional<bool> load(uint32_t account, const std::filesystem::path& root = account_data::storage_directory()) {
|
|
auto directory = account_data::account_directory(account, root);
|
|
if (directory.empty()) return std::nullopt;
|
|
std::ifstream in(directory / "sdhd.bin", std::ios::binary);
|
|
int value = in.get();
|
|
if ((value != 0 && value != SD) || in.peek() != std::char_traits<char>::eof() || in.bad())
|
|
return std::nullopt;
|
|
return value == SD;
|
|
}
|
|
|
|
// 70334 writes the choice as one MSB-first bit followed by seven zero padding bits.
|
|
inline bool set_from_client(uint32_t account, std::span<const uint8_t> body,
|
|
const std::filesystem::path& root = account_data::storage_directory()) {
|
|
if (body.size() != 1 || (body[0] != 0 && body[0] != SD)) return false;
|
|
auto directory = account_data::account_directory(account, root);
|
|
if (directory.empty()) return false;
|
|
std::error_code error;
|
|
std::filesystem::create_directories(directory, error);
|
|
return !error && account_data::replace_file(directory / "sdhd.bin", Bytes{body[0]});
|
|
}
|
|
|
|
} // namespace gateway::sdhd_preference
|