193 lines
6.7 KiB
C++
193 lines
6.7 KiB
C++
#pragma once
|
|
|
|
#include "account_data_store.hpp"
|
|
#include <array>
|
|
#include <map>
|
|
#ifndef _WIN32
|
|
#include <sys/file.h>
|
|
#endif
|
|
|
|
namespace gateway::equipment_sets {
|
|
constexpr uint32_t Save = 0x3f00a8, Delete = 0x3f00a9, Use = 0x410001, Assign = 0x3e00c4;
|
|
constexpr uint32_t SetId = 0x47015e, Load = 0x4701ba, Result = 0x4701fa;
|
|
constexpr size_t Slots = 19, Limit = 20;
|
|
constexpr uint32_t SlotMask = (1u << Slots) - 1;
|
|
|
|
struct Set {
|
|
uint64_t guid = 0;
|
|
uint32_t index = 0, ignore = 0;
|
|
std::array<Guid128, Slots> pieces{};
|
|
std::optional<int32_t> spec;
|
|
std::string name, icon;
|
|
bool operator==(const Set&) const = default;
|
|
};
|
|
|
|
inline Guid128 piece(Reader& r) {
|
|
auto raw = unpack_guid(r, 16);
|
|
Guid128 id{};
|
|
std::copy(raw.begin(), raw.end(), id.begin());
|
|
return id;
|
|
}
|
|
inline std::string string(Reader& r, size_t size) {
|
|
auto bytes = r.take(size);
|
|
if (std::ranges::find(bytes, 0) != bytes.end()) throw Error("equipment string contains NUL");
|
|
return {bytes.begin(), bytes.end()};
|
|
}
|
|
inline Set read_set(Reader& r) {
|
|
Set s;
|
|
if (r.le(4) != 0) throw Error("unsupported equipment set type");
|
|
s.guid = r.le(8);
|
|
s.index = r.le(4);
|
|
s.ignore = r.le(4);
|
|
if (s.index >= Limit || (s.ignore & ~SlotMask)) throw Error("equipment set bounds");
|
|
for (auto& item : s.pieces) {
|
|
item = piece(r);
|
|
r.le(4); // Appearance is unused for equipment sets.
|
|
}
|
|
r.take(24); // Enchants and secondary appearances belong to transmog sets.
|
|
auto bits = (r.le(1) << 16);
|
|
bits |= r.le(1) << 8;
|
|
bits |= r.le(1);
|
|
if (bits & 0x800000) s.spec = int32_t(r.le(4));
|
|
s.name = string(r, (bits >> 15) & 255);
|
|
s.icon = string(r, (bits >> 6) & 511);
|
|
if (s.name.empty()) throw Error("equipment set has no name");
|
|
return s;
|
|
}
|
|
|
|
inline void write(Bytes& out, const Set& s) {
|
|
le(out, 0, 4);
|
|
le(out, s.guid, 8);
|
|
le(out, s.index, 4);
|
|
le(out, s.ignore, 4);
|
|
for (const auto& item : s.pieces) {
|
|
append(out, pack_guid(item));
|
|
le(out, 0, 4);
|
|
}
|
|
out.resize(out.size() + 24, 0);
|
|
auto bits = (unsigned(s.spec.has_value()) << 23) | (s.name.size() << 15) | (s.icon.size() << 6);
|
|
le(out, bits >> 16, 1);
|
|
le(out, bits >> 8, 1);
|
|
le(out, bits, 1);
|
|
if (s.spec) le(out, uint32_t(*s.spec), 4);
|
|
out.insert(out.end(), s.name.begin(), s.name.end());
|
|
out.insert(out.end(), s.icon.begin(), s.icon.end());
|
|
}
|
|
struct Catalogue {
|
|
uint64_t sequence = 0;
|
|
std::map<uint32_t, Set> sets;
|
|
Bytes packet() const {
|
|
Bytes out;
|
|
le(out, sets.size(), 4);
|
|
for (const auto& [index, s] : sets) write(out, s);
|
|
return out;
|
|
}
|
|
const Set* find(uint64_t guid) const {
|
|
for (const auto& [index, s] : sets) if (s.guid == guid) return &s;
|
|
return nullptr;
|
|
}
|
|
};
|
|
inline std::filesystem::path path(uint32_t account, const Guid128& character,
|
|
const std::filesystem::path& root) {
|
|
auto ref = account_data::file(account, account_data::CharacterEditMode, character, root);
|
|
if (ref.empty()) throw Error("equipment account or character unavailable");
|
|
return ref.parent_path() / ("equipment-" + ref.filename().string());
|
|
}
|
|
inline Catalogue load(const std::filesystem::path& path) {
|
|
Catalogue c;
|
|
std::error_code ec;
|
|
if (!std::filesystem::exists(path, ec) && !ec) return c;
|
|
auto length = std::filesystem::file_size(path, ec);
|
|
if (ec || length < 16 || length > 32768) throw Error("equipment store length");
|
|
std::ifstream file(path, std::ios::binary);
|
|
Bytes bytes(length);
|
|
if (!file.read(reinterpret_cast<char*>(bytes.data()), bytes.size()) || file.peek() != EOF)
|
|
throw Error("equipment store read");
|
|
Reader r(bytes);
|
|
if (r.le(4) != 0x31534547) throw Error("equipment store format");
|
|
c.sequence = r.le(8);
|
|
auto count = r.le(4);
|
|
if (count > Limit) throw Error("equipment store count");
|
|
for (size_t i = 0; i < count; ++i) {
|
|
auto s = read_set(r);
|
|
if (!s.guid || s.guid > c.sequence || c.find(s.guid) || !c.sets.emplace(s.index, s).second)
|
|
throw Error("equipment store identity");
|
|
}
|
|
r.end();
|
|
return c;
|
|
}
|
|
// Serialize the read/modify/replace across client processes. The OS releases the
|
|
// lock on process exit. Keep the sidecar name stable across atomic replacement.
|
|
class Lock {
|
|
#ifdef _WIN32
|
|
HANDLE handle;
|
|
#else
|
|
int fd;
|
|
#endif
|
|
public:
|
|
explicit Lock(const std::filesystem::path& file) {
|
|
std::filesystem::create_directories(file.parent_path());
|
|
auto guard = file;
|
|
guard += ".lock";
|
|
#ifdef _WIN32
|
|
handle = CreateFileW(guard.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr,
|
|
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (handle == INVALID_HANDLE_VALUE) throw Error("equipment store busy");
|
|
#else
|
|
fd = open(guard.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0600);
|
|
if (fd < 0) throw Error("equipment store lock");
|
|
if (flock(fd, LOCK_EX | LOCK_NB) != 0) { close(fd); throw Error("equipment store busy"); }
|
|
#endif
|
|
}
|
|
Lock(const Lock&) = delete;
|
|
~Lock() {
|
|
#ifdef _WIN32
|
|
CloseHandle(handle);
|
|
#else
|
|
close(fd);
|
|
#endif
|
|
}
|
|
};
|
|
inline void persist(const std::filesystem::path& file, const Catalogue& c) {
|
|
Bytes data;
|
|
le(data, 0x31534547, 4); // GES1
|
|
le(data, c.sequence, 8);
|
|
append(data, c.packet());
|
|
if (!account_data::replace_file(file, data)) throw Error("equipment store write");
|
|
}
|
|
inline Set save(const std::filesystem::path& file, Set s) {
|
|
Lock guard(file);
|
|
auto c = load(file);
|
|
auto found = c.sets.find(s.index);
|
|
if (found == c.sets.end()) {
|
|
if (s.guid || c.sequence == UINT64_MAX) throw Error("equipment set identity unavailable");
|
|
s.guid = ++c.sequence;
|
|
} else if (!s.guid) {
|
|
s.guid = found->second.guid;
|
|
if (s == found->second) return s; // A repeated create after a lost acknowledgement.
|
|
throw Error("equipment set index already in use");
|
|
} else if (s.guid != found->second.guid) throw Error("equipment set identity changed");
|
|
c.sets[s.index] = s;
|
|
persist(file, c);
|
|
return s;
|
|
}
|
|
inline Catalogue change(const std::filesystem::path& file, uint64_t guid,
|
|
bool erase, int32_t spec) {
|
|
Lock guard(file);
|
|
auto c = load(file);
|
|
auto s = c.find(guid);
|
|
if (!s) throw Error("unknown equipment set");
|
|
auto index = s->index;
|
|
if (erase) c.sets.erase(index);
|
|
else {
|
|
if (spec < -1 || spec >= 4) throw Error("invalid equipment specialization index");
|
|
for (auto& [key, set] : c.sets)
|
|
if (set.spec == spec) set.spec.reset();
|
|
if (spec >= 0) c.sets[index].spec = spec;
|
|
else c.sets[index].spec.reset();
|
|
}
|
|
persist(file, c);
|
|
return c;
|
|
}
|
|
} // namespace gateway::equipment_sets
|