atrooney-online-2/src/aopacket.cpp
Skye Deving 5d9309b1b3
Handle packet decoding separately for SC packet (#433)
Should fix special (escaped) characters not being parsed correctly in character list.
2021-01-24 18:26:39 -06:00

46 lines
952 B
C++

#include "aopacket.h"
AOPacket::AOPacket(QString p_packet_string)
{
QStringList packet_contents = p_packet_string.split("#");
m_header = packet_contents.first();
m_contents = packet_contents.mid(1, packet_contents.size()-2); // trims %
}
QString AOPacket::to_string(bool encoded)
{
QStringList contents = m_contents;
if (encoded)
escape(contents);
return m_header + "#" + contents.join("#") + "#%";
}
void AOPacket::net_encode()
{
escape(m_contents);
}
void AOPacket::net_decode()
{
unescape(m_contents);
}
void AOPacket::escape(QStringList &contents)
{
contents.replaceInStrings("#", "<num>")
.replaceInStrings("%", "<percent>")
.replaceInStrings("$", "<dollar>")
.replaceInStrings("&", "<and>");
}
void AOPacket::unescape(QStringList &contents)
{
contents.replaceInStrings("<num>", "#")
.replaceInStrings("<percent>", "%")
.replaceInStrings("<dollar>", "$")
.replaceInStrings("<and>", "&");
}