Files

44 lines
2.4 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
#include "serializer.h"
#include "gateway/forever_serializer.hpp"
#include "gateway/social.hpp"
#include "gateway/who.hpp"
#include "gateway/pvp_statistics.hpp"
#include "gateway/auction_browse.hpp"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include "serializer_modes.hpp"
uint32_t gw_codec_abi(void) {return GW_CODEC_ABI;}
int gw_codec_serialize(uint32_t mode,const uint8_t* input,uint32_t size,uint32_t flags,
uint8_t* output,uint32_t capacity,uint32_t* written) {
if(!written)return GW_CODEC_ARGUMENT;
*written=0;
if(mode>=gw_codec_modes.size() || size>GW_CODEC_BOUND || capacity>GW_CODEC_BOUND ||
(size && !input) || (capacity && !output) || (flags&~GW_CODEC_TALENT_EXPERIMENT))return GW_CODEC_ARGUMENT;
try {
gateway::Bytes bytes;
if(size)bytes.assign(input,input+size);
auto result=mode==GW_CODEC_SOCIAL_PACKET?gateway::serialize_social_projection(bytes):
mode==GW_CODEC_CONTACT_NAMES?gateway::serialize_contact_names(bytes):
mode==GW_CODEC_WHO?gateway::serialize_who(bytes):
mode==GW_CODEC_PVP_STATISTICS?gateway::serialize_pvp_statistics(bytes):
mode==GW_CODEC_AUCTION_BUCKETS?gateway::serialize_auction_buckets(bytes):
mode==GW_CODEC_AUCTION_ITEMS?gateway::serialize_auction_items(bytes):
mode==GW_CODEC_AUCTION_PERSONAL?gateway::serialize_auction_personal(bytes):
mode==GW_CODEC_COMMODITY_QUOTE?gateway::serialize_commodity_quote(bytes):
gateway::source_forever_serialize(gw_codec_modes[mode],bytes,(flags&GW_CODEC_TALENT_EXPERIMENT)!=0);
if(result.size()>GW_CODEC_BOUND)return GW_CODEC_PACKET;
*written=uint32_t(result.size());
if(result.size()>capacity)return GW_CODEC_CAPACITY;
if(!result.empty())std::copy(result.begin(),result.end(),output);
return GW_CODEC_OK;
}catch(const std::exception& e) {
// Private diagnostics only: the rejection reason text (never packet bytes) when explicitly enabled.
if(const char* path=std::getenv("GW_PRIVATE_CODEC_WHAT");path && *path)
if(std::FILE* f=std::fopen(path,"a")){std::fprintf(f,"mode=%u what=%s\n",mode,e.what());std::fclose(f);}
return GW_CODEC_PACKET;
}catch(...) {return GW_CODEC_PACKET;}
}