143 lines
7.1 KiB
C++
143 lines
7.1 KiB
C++
#pragma once
|
|
#include "gateway/chat.hpp"
|
|
#include <array>
|
|
#include <bitset>
|
|
#include <charconv>
|
|
#include <optional>
|
|
namespace gateway {
|
|
// Reserved complete Octo profile. CharTitles row identity is NOT the native
|
|
// title API/PlayerTitle namespace: those use MaskID. Content publication and
|
|
// native field/opcode admission remain separate qualification boundaries.
|
|
inline constexpr uint32_t source_title_count=94;
|
|
inline constexpr size_t known_title_word_count=5;
|
|
struct TitleIdentity {uint8_t source;uint32_t row;uint16_t mask;};
|
|
struct TitleHold : Error {using Error::Error;};
|
|
inline TitleIdentity title_identity(uint32_t source){
|
|
if(!source || source>source_title_count)throw TitleHold("Source title outside pinned catalogue");
|
|
return {uint8_t(source),20000+source,uint16_t(200+source)};
|
|
}
|
|
inline uint16_t source_chosen_title_mask(uint32_t player_bytes3){
|
|
// Actual Tortoise Player::ChangeTitle stores the title in byte2. Byte3
|
|
// is the separate honor rank and must not become a custom title.
|
|
const auto id=uint8_t(player_bytes3>>16);
|
|
return id?title_identity(id).mask:0;
|
|
}
|
|
inline uint8_t source_title_from_mask(int32_t mask){
|
|
if(mask==-1)return 0; // Original native SetCurrentTitle None sentinel.
|
|
if(mask<201 || mask>294)throw TitleHold("native title mask outside Source catalogue");
|
|
return uint8_t(mask-200);
|
|
}
|
|
inline int32_t native_title_mask(std::span<const uint8_t> body){
|
|
if(body.size()!=4)throw TitleHold("native title selection framing");
|
|
Reader r(body);auto mask=int32_t(r.le(4));
|
|
(void)source_title_from_mask(mask);return mask;
|
|
}
|
|
struct SourceTitleSnapshot {
|
|
std::bitset<source_title_count+1> earned;
|
|
uint8_t active=0;
|
|
};
|
|
struct SourceTitleReply {
|
|
std::optional<SourceTitleSnapshot> list;
|
|
std::optional<uint8_t> earned;
|
|
};
|
|
inline uint32_t title_decimal(std::string_view text){
|
|
uint32_t value=0;auto p=std::from_chars(text.data(),text.data()+text.size(),value);
|
|
if(text.empty() || (text.size()>1 && text.front()=='0') || p.ec!=std::errc{} || p.ptr!=text.data()+text.size() || value>source_title_count)
|
|
throw TitleHold("invalid Source title identifier");
|
|
return value;
|
|
}
|
|
inline SourceTitleReply source_title_reply(std::string_view text){
|
|
if(text.empty() || text.size()>1024)throw TitleHold("Source title reply bound");
|
|
constexpr std::string_view awarded="newTitle:",list="TW_AVAILABLE_TITLES:";
|
|
if(text.starts_with(awarded)){
|
|
auto id=title_decimal(text.substr(awarded.size()));
|
|
if(!id)throw TitleHold("Source none title cannot be awarded");
|
|
return {std::nullopt,uint8_t(id)};
|
|
}
|
|
if(!text.starts_with(list))throw TitleHold("unsupported Source title reply");
|
|
text.remove_prefix(list.size());SourceTitleSnapshot snapshot;
|
|
std::bitset<source_title_count+1> seen;unsigned rows=0,active_count=0;
|
|
while(!text.empty()){
|
|
auto end=text.find(';');auto row=text.substr(0,end);auto colon=row.find(':');
|
|
if(colon==row.npos)throw TitleHold("Source title row missing flag");
|
|
const auto id=title_decimal(row.substr(0,colon));auto active=row.substr(colon+1);
|
|
if((active!="0" && active!="1") || seen[id] || (!rows && id!=0))throw TitleHold("invalid or duplicate Source title row");
|
|
seen.set(id);++rows;if(rows>source_title_count+1)throw TitleHold("Source title row bound");
|
|
if(id)snapshot.earned.set(id);
|
|
if(active=="1"){snapshot.active=uint8_t(id);++active_count;}
|
|
if(end==text.npos)break;
|
|
text.remove_prefix(end+1);if(text.empty())throw TitleHold("Source title trailing row delimiter");
|
|
}
|
|
if(!seen[0] || active_count!=1)throw TitleHold("Source title active state inconsistent");
|
|
return {snapshot,std::nullopt};
|
|
}
|
|
struct TitleDelta {
|
|
std::bitset<source_title_count+1> earned,lost;
|
|
bool active_changed=false,list_received=false;
|
|
};
|
|
class SourceTitles {
|
|
uint64_t self_=0;
|
|
bool list_sent_=false,received_list_=false;
|
|
SourceTitleSnapshot state_;
|
|
std::optional<uint8_t> pending_;
|
|
public:
|
|
// Caller binds only at the actual authenticated self-world boundary.
|
|
// A different session must reset even when its character GUID is equal.
|
|
void begin_world(uint64_t self){
|
|
if(!self || self>UINT32_MAX)throw TitleHold("Source title self identity bound");
|
|
self_=self;list_sent_=false;received_list_=false;state_={};pending_.reset();
|
|
}
|
|
void clear(){self_=0;list_sent_=false;received_list_=false;state_={};pending_.reset();}
|
|
bool bound()const{return self_!=0;}
|
|
bool needs_list()const{return self_ && !list_sent_;}
|
|
SelfAddonMessage list_request()const{
|
|
if(!self_ || list_sent_)throw TitleHold("title list query outside one world request");
|
|
return {"TW_TITLES","Titles:List"};
|
|
}
|
|
void list_sent(){(void)list_request();list_sent_=true;}
|
|
const SourceTitleSnapshot& state()const{return state_;}
|
|
bool ready()const{return received_list_;}
|
|
std::optional<uint8_t> pending()const{return pending_;}
|
|
std::array<uint64_t,known_title_word_count> known_words()const{
|
|
std::array<uint64_t,known_title_word_count> words{};
|
|
for(uint32_t id=1;id<=source_title_count;++id)if(state_.earned[id]){
|
|
const auto mask=title_identity(id).mask;words[mask/64]|=uint64_t(1)<<(mask%64);
|
|
}
|
|
return words;
|
|
}
|
|
Bytes values_input(uint16_t realm,uint32_t map)const{
|
|
if(!self_ || !received_list_ || !realm || map>UINT16_MAX)
|
|
throw TitleHold("title values outside confirmed world snapshot");
|
|
Bytes out;le(out,realm,2);le(out,map,4);le(out,self_,8);
|
|
le(out,state_.active?title_identity(state_.active).mask:0,4);
|
|
for(auto word:known_words())le(out,word,8);
|
|
return out;
|
|
}
|
|
std::optional<TitleDelta> observe(const ServerAddonMessage& message){
|
|
if(!self_ || !list_sent_ || message.sender!=self_ || message.prefix!="TWT_TITLES")return std::nullopt;
|
|
// Parse completely before committing, including active consistency.
|
|
const auto reply=source_title_reply(message.text);auto next=state_;
|
|
if(reply.list)next=*reply.list;
|
|
else next.earned.set(*reply.earned); // No optimistic active-title change.
|
|
TitleDelta delta{next.earned&~state_.earned,state_.earned&~next.earned,next.active!=state_.active,bool(reply.list)};
|
|
state_=next;
|
|
if(reply.list){received_list_=true;pending_.reset();}
|
|
return delta;
|
|
}
|
|
SelfAddonMessage selection_request(int32_t mask)const{
|
|
if(!self_ || !received_list_)throw TitleHold("title selection before actual earned list");
|
|
auto id=source_title_from_mask(mask);
|
|
if(id && !state_.earned[id])throw TitleHold("title selection was not earned");
|
|
return {"TW_TITLES","ChangeTitle:"+std::to_string(id)};
|
|
}
|
|
SelfAddonMessage selection_request(std::span<const uint8_t> body)const{
|
|
return selection_request(native_title_mask(body));
|
|
}
|
|
void selection_sent(int32_t mask){
|
|
(void)selection_request(mask);pending_=source_title_from_mask(mask);
|
|
// Visible active state changes only on a subsequent Source reply.
|
|
}
|
|
void selection_sent(std::span<const uint8_t> body){selection_sent(native_title_mask(body));}
|
|
};
|
|
}
|