Files
WoW-Hybrid-gateway/native/extended_action_bars.hpp

89 lines
3.6 KiB
C++

#pragma once
// Gitea #15: the Turtle server stores 120 action buttons (index8 + packed action24/type8). The 70334 client has
// 360 slots; the Gamepad UI and other extra bars use indices from 120 (C_GamepadUI first storage slot is 180).
// Slots 120..359 are kept client-locally per character, next to the account data store, and merged into the
// action button list at login. Nothing about them is sent to the source server.
#include <array>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <span>
#include <string>
#include "account_data_store.hpp"
#include "gateway/protocol.hpp"
namespace gateway::extended_action_bars {
constexpr uint32_t SetActionButtonOpcode = 0x440062;
constexpr unsigned SourceSlots = 120, NativeSlots = 360, Extra = NativeSlots - SourceSlots;
using Slots = std::array<uint64_t, Extra>;
inline bool extended(unsigned index) { return index >= SourceSlots && index < NativeSlots; }
inline std::filesystem::path path(uint32_t account, std::span<const uint8_t> guid,
const std::filesystem::path& root = account_data::storage_directory()) {
auto directory = account_data::account_directory(account, root);
if (directory.empty() || !account_data::valid_character(guid)) return {};
constexpr char digits[] = "0123456789abcdef";
std::string name = "actions-";
for (auto b : guid) {
name += digits[b >> 4];
name += digits[b & 15];
}
return directory / (name + ".bin");
}
// File: "GWAB" + uint32 version 1 + 240 x uint64 native packed action (action | type << 56). Any other content
// reads as empty bars; a damaged file never blocks login.
inline Slots load(const std::filesystem::path& file) {
Slots slots{};
if (file.empty()) return slots;
std::ifstream in(file, std::ios::binary);
char header[8];
if (!in.read(header, sizeof(header)) || std::string(header, 4) != "GWAB" || header[4] != 1 || header[5] || header[6] ||
header[7])
return slots;
uint8_t raw[Extra * 8];
if (!in.read(reinterpret_cast<char*>(raw), sizeof(raw)) || in.peek() != std::char_traits<char>::eof()) return Slots{};
Reader r{std::span<const uint8_t>(raw, sizeof(raw))};
for (auto& slot : slots) slot = r.le(8);
return slots;
}
inline bool save(const std::filesystem::path& file, const Slots& slots) {
if (file.empty()) return false;
std::error_code ec;
std::filesystem::create_directories(file.parent_path(), ec);
if (ec) return false;
Bytes content{'G', 'W', 'A', 'B', 1, 0, 0, 0};
for (auto slot : slots) le(content, slot, 8);
return account_data::replace_file(file, content);
}
// SetActionButton (70334 ActionPackets::SetActionButton::Read): uint64 packed action, uint16 index.
// Returns true when the request belonged to an extended slot and was stored locally.
inline bool store_request(const std::filesystem::path& file, std::span<const uint8_t> body) {
Reader r(body);
auto packed = r.le(8), index = r.le(2);
r.end();
if (!extended(unsigned(index))) return false;
auto slots = load(file);
slots[index - SourceSlots] = packed;
if (!save(file, slots)) throw Error("extended action bar store failed");
return true;
}
// forever_action_buttons output: 360 x uint64 + uint8 reason. Overlay the locally stored extended slots.
inline void merge(Bytes& buttons, const Slots& slots) {
if (buttons.size() != NativeSlots * 8 + 1) throw Error("native action button list size");
for (unsigned i = 0; i < Extra; ++i) {
auto value = slots[i];
for (unsigned b = 0; b < 8; ++b) buttons[(SourceSlots + i) * 8 + b] = uint8_t(value >> (8 * b));
}
}
} // namespace gateway::extended_action_bars