allow usage of network messages in extended opcode

This commit is contained in:
Eduardo Bart 2012-05-14 20:13:48 -03:00
parent 2478809945
commit 7bca3de8eb
6 changed files with 42 additions and 26 deletions

View File

@ -423,6 +423,7 @@ void Application::registerLuaFunctions()
// InputMessage
g_lua.registerClass<InputMessage>();
g_lua.bindClassStaticFunction<InputMessage>("create", []{ return InputMessagePtr(new InputMessage); });
g_lua.bindClassMemberFunction<InputMessage>("setBuffer", &InputMessage::setBuffer);
g_lua.bindClassMemberFunction<InputMessage>("skipBytes", &InputMessage::skipBytes);
g_lua.bindClassMemberFunction<InputMessage>("getU8", &InputMessage::getU8);
g_lua.bindClassMemberFunction<InputMessage>("getU16", &InputMessage::getU16);
@ -437,6 +438,7 @@ void Application::registerLuaFunctions()
// OutputMessage
g_lua.registerClass<OutputMessage>();
g_lua.bindClassStaticFunction<OutputMessage>("create", []{ return OutputMessagePtr(new OutputMessage); });
g_lua.bindClassMemberFunction<OutputMessage>("getBuffer", &OutputMessage::getBuffer);
g_lua.bindClassMemberFunction<OutputMessage>("reset", &OutputMessage::reset);
g_lua.bindClassMemberFunction<OutputMessage>("addU8", &OutputMessage::addU8);
g_lua.bindClassMemberFunction<OutputMessage>("addU16", &OutputMessage::addU16);

View File

@ -30,28 +30,16 @@ InputMessage::InputMessage()
void InputMessage::reset()
{
m_messageSize = 0;
m_readPos = MAX_HEADER_SIZE;
m_headerPos = MAX_HEADER_SIZE;
}
void InputMessage::setHeaderSize(uint16 size)
void InputMessage::setBuffer(const std::string& buffer)
{
m_headerPos = MAX_HEADER_SIZE - size;
m_readPos = m_headerPos;
}
void InputMessage::fillBuffer(uint8 *buffer, uint16 size)
{
memcpy(m_buffer + m_readPos, buffer, size);
m_messageSize += size;
}
bool InputMessage::readChecksum()
{
uint32_t receivedCheck = getU32();
uint32 checksum = Fw::getAdlerChecksum(m_buffer + m_readPos, getUnreadSize());
return receivedCheck == checksum;
memcpy(m_buffer + MAX_HEADER_SIZE, buffer.c_str(), buffer.size());
m_readPos = MAX_HEADER_SIZE;
m_headerPos = MAX_HEADER_SIZE;
m_messageSize = buffer.size();
}
uint8 InputMessage::getU8(bool peek)
@ -112,6 +100,25 @@ void InputMessage::decryptRSA(int size, const std::string& p, const std::string&
RSA::decrypt((char*)m_buffer + m_readPos, size, p.c_str(), q.c_str(), d.c_str());
}
void InputMessage::fillBuffer(uint8 *buffer, uint16 size)
{
memcpy(m_buffer + m_readPos, buffer, size);
m_messageSize += size;
}
void InputMessage::setHeaderSize(uint16 size)
{
m_headerPos = MAX_HEADER_SIZE - size;
m_readPos = m_headerPos;
}
bool InputMessage::readChecksum()
{
uint32_t receivedCheck = getU32();
uint32 checksum = Fw::getAdlerChecksum(m_buffer + m_readPos, getUnreadSize());
return receivedCheck == checksum;
}
bool InputMessage::canRead(int bytes)
{
if((m_readPos - m_headerPos + bytes > m_messageSize) || (m_readPos + bytes > BUFFER_MAXSIZE))

View File

@ -37,6 +37,8 @@ public:
InputMessage();
void setBuffer(const std::string& buffer);
void skipBytes(uint16 bytes) { m_readPos += bytes; }
uint8 getU8(bool peek = false);
uint16 getU16(bool peek = false);
@ -53,7 +55,6 @@ public:
protected:
void reset();
void fillBuffer(uint8 *buffer, uint16 size);
void setHeaderSize(uint16 size);

View File

@ -40,6 +40,8 @@ public:
void reset();
std::string getBuffer() { return std::string((char*)m_buffer + m_headerPos, m_messageSize); }
void addU8(uint8 value);
void addU16(uint16 value);
void addU32(uint32 value);

View File

@ -108,12 +108,12 @@ namespace Proto {
// all in game opcodes must be equal or greater than 50
GameServerFirstGameOpcode = 50,
// otclient ONLY
GameServerExtendedOpcode = 50,
// NOTE: add any custom opcodes in this range
// 51 - 99
// otclient ONLY
GameServerExtendedOpcode = 99,
// original tibia ONLY
GameServerFullMap = 100,
GameServerMapTopRow = 101,
@ -203,11 +203,11 @@ namespace Proto {
// all in game opcodes must be equal or greater than 50
ClientFirstGameOpcode = 50,
// NOTE: add any custom opcodes in this range
// 50 - 98
// otclient ONLY
ClientExtendedOpcode = 99,
ClientExtendedOpcode = 50,
// NOTE: add any custom opcodes in this range
// 51 - 99
// original tibia ONLY
ClientAutoWalk = 100,

View File

@ -1212,7 +1212,11 @@ void ProtocolGame::parseExtendedOpcode(const InputMessagePtr& msg)
int opcode = msg->getU8();
std::string buffer = msg->getString();
callLuaField("onExtendedOpcode", opcode, buffer);
try {
callLuaField("onExtendedOpcode", opcode, buffer);
} catch(Exception& e) {
logError("Network exception in extended opcode ", opcode, ": ", e.what());
}
}
void ProtocolGame::setMapDescription(const InputMessagePtr& msg, int x, int y, int z, int width, int height)