Files
2026-11-23 03:28:32 +02:00

54 lines
2.6 KiB
C++

#pragma once
// Genuine Source text -> established native system-chat writer.
#include "gateway/chat.hpp"
#include <optional>
namespace gateway {
struct TurtleSourceFeedback { uint32_t opcode=0,context=0; std::string text; };
inline std::optional<TurtleSourceFeedback> turtle_source_feedback(uint32_t opcode,std::span<const uint8_t> body){
if (opcode == 0x2fd) {
if (!body.empty()) return std::nullopt;
// 1.12 DisplayError(451) reads ERR_CHAT_RESTRICTED. The enUS patch-5
// string is a hardcore restriction; the 70334 chat reason 0 is a trial upsell.
return TurtleSourceFeedback{opcode, 0, "Hardcore characters cannot perform that action."};
}
if(opcode!=0x1cb && opcode!=0x291 && opcode!=0x2b8 && opcode!=0x33b)return std::nullopt;
Reader r(body);TurtleSourceFeedback result;result.opcode=opcode;
if(opcode==0x291 || opcode==0x33b)result.context=uint32_t(r.le(4));
uint64_t declared=0;if(opcode==0x2b8 || opcode==0x33b)declared=r.le(4);
result.text=r.cstring(4095);r.end();
if((opcode==0x2b8 || opcode==0x33b) && declared!=result.text.size()+1)
throw Error("Source feedback declared text length mismatch");
return result;
}
inline std::optional<Bytes> forever_timed_server_message(const TurtleSourceFeedback& message) {
if (message.opcode != 0x291) return std::nullopt;
// Both generations' ServerMessages rows 1/2 have one %s, 4/5 have none.
switch (message.context) {
case 1: case 2:
if (message.text.empty() || message.text.size() > 2047) return std::nullopt;
break;
case 4: case 5:
if (!message.text.empty()) return std::nullopt;
break;
default: return std::nullopt;
}
Bytes out;
le(out, message.context, 4);
const auto n = message.text.size();
out.push_back(uint8_t(n >> 3));
out.push_back(uint8_t((n & 7) << 5)); // SizedString::BitsSize<11>, MSB first.
out.insert(out.end(), message.text.begin(), message.text.end());
return out;
}
inline Bytes forever_source_system_feedback(const TurtleSourceFeedback& message,RosterRealm realm){
// Typed shutdown/restart messages need native ServerMessages localization;
// their payload may contain only a duration, or an empty cancellation text.
if(message.opcode==0x291 && message.context!=3)
throw Error("Typed Source server message requires original native handler");
if(message.text.size()>4095)throw Error("Source feedback text bound");
Bytes legacy{10};le(legacy,0,4);le(legacy,0,8);le(legacy,message.text.size()+1,4);
cstring(legacy,message.text);le(legacy,0,1);
return forever_chat(legacy,{},realm,0,true);
}
}