new protocol system fixes

This commit is contained in:
Eduardo Bart 2012-05-14 21:04:04 -03:00
parent 7bca3de8eb
commit 946ec64e7f
8 changed files with 34 additions and 33 deletions

View File

@ -430,9 +430,14 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<InputMessage>("getU32", &InputMessage::getU32); g_lua.bindClassMemberFunction<InputMessage>("getU32", &InputMessage::getU32);
g_lua.bindClassMemberFunction<InputMessage>("getU64", &InputMessage::getU64); g_lua.bindClassMemberFunction<InputMessage>("getU64", &InputMessage::getU64);
g_lua.bindClassMemberFunction<InputMessage>("getString", &InputMessage::getString); g_lua.bindClassMemberFunction<InputMessage>("getString", &InputMessage::getString);
g_lua.bindClassMemberFunction<InputMessage>("peekU8", &InputMessage::peekU8);
g_lua.bindClassMemberFunction<InputMessage>("peekU16", &InputMessage::peekU16);
g_lua.bindClassMemberFunction<InputMessage>("peekU32", &InputMessage::peekU32);
g_lua.bindClassMemberFunction<InputMessage>("peekU64", &InputMessage::peekU64);
g_lua.bindClassMemberFunction<InputMessage>("decryptRSA", &InputMessage::decryptRSA); g_lua.bindClassMemberFunction<InputMessage>("decryptRSA", &InputMessage::decryptRSA);
g_lua.bindClassMemberFunction<InputMessage>("getReadSize", &InputMessage::getReadSize); g_lua.bindClassMemberFunction<InputMessage>("getReadSize", &InputMessage::getReadSize);
g_lua.bindClassMemberFunction<InputMessage>("getUnreadSize", &InputMessage::getUnreadSize); g_lua.bindClassMemberFunction<InputMessage>("getUnreadSize", &InputMessage::getUnreadSize);
g_lua.bindClassMemberFunction<InputMessage>("getMessageSize", &InputMessage::getMessageSize);
g_lua.bindClassMemberFunction<InputMessage>("eof", &InputMessage::eof); g_lua.bindClassMemberFunction<InputMessage>("eof", &InputMessage::eof);
// OutputMessage // OutputMessage
@ -447,6 +452,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<OutputMessage>("addString", &OutputMessage::addString); g_lua.bindClassMemberFunction<OutputMessage>("addString", &OutputMessage::addString);
g_lua.bindClassMemberFunction<OutputMessage>("addPaddingBytes", &OutputMessage::addPaddingBytes); g_lua.bindClassMemberFunction<OutputMessage>("addPaddingBytes", &OutputMessage::addPaddingBytes);
g_lua.bindClassMemberFunction<OutputMessage>("encryptRSA", &OutputMessage::encryptRSA); g_lua.bindClassMemberFunction<OutputMessage>("encryptRSA", &OutputMessage::encryptRSA);
g_lua.bindClassMemberFunction<OutputMessage>("getMessageSize", &OutputMessage::getMessageSize);
// Application // Application
g_lua.registerStaticClass("g_app"); g_lua.registerStaticClass("g_app");

View File

@ -30,6 +30,7 @@ InputMessage::InputMessage()
void InputMessage::reset() void InputMessage::reset()
{ {
m_messageSize = 0;
m_readPos = MAX_HEADER_SIZE; m_readPos = MAX_HEADER_SIZE;
m_headerPos = MAX_HEADER_SIZE; m_headerPos = MAX_HEADER_SIZE;
} }
@ -42,47 +43,35 @@ void InputMessage::setBuffer(const std::string& buffer)
m_messageSize = buffer.size(); m_messageSize = buffer.size();
} }
uint8 InputMessage::getU8(bool peek) uint8 InputMessage::getU8()
{ {
checkRead(1); checkRead(1);
uint8 v = m_buffer[m_readPos]; uint8 v = m_buffer[m_readPos];
if(!peek)
m_readPos += 1; m_readPos += 1;
return v; return v;
} }
uint16 InputMessage::getU16(bool peek) uint16 InputMessage::getU16()
{ {
checkRead(2); checkRead(2);
uint16 v = Fw::readLE16(m_buffer + m_readPos); uint16 v = Fw::readLE16(m_buffer + m_readPos);
if(!peek)
m_readPos += 2; m_readPos += 2;
return v; return v;
} }
uint32 InputMessage::getU32(bool peek) uint32 InputMessage::getU32()
{ {
checkRead(4); checkRead(4);
uint32 v = Fw::readLE32(m_buffer + m_readPos); uint32 v = Fw::readLE32(m_buffer + m_readPos);
if(!peek)
m_readPos += 4; m_readPos += 4;
return v; return v;
} }
uint64 InputMessage::getU64(bool peek) uint64 InputMessage::getU64()
{ {
checkRead(8); checkRead(8);
uint64 v = Fw::readLE64(m_buffer + m_readPos); uint64 v = Fw::readLE64(m_buffer + m_readPos);
if(!peek)
m_readPos += 8; m_readPos += 8;
return v; return v;
} }

View File

@ -40,16 +40,22 @@ public:
void setBuffer(const std::string& buffer); void setBuffer(const std::string& buffer);
void skipBytes(uint16 bytes) { m_readPos += bytes; } void skipBytes(uint16 bytes) { m_readPos += bytes; }
uint8 getU8(bool peek = false); uint8 getU8();
uint16 getU16(bool peek = false); uint16 getU16();
uint32 getU32(bool peek = false); uint32 getU32();
uint64 getU64(bool peek = false); uint64 getU64();
std::string getString(); std::string getString();
uint8 peekU8() { uint8 v = getU8(); m_readPos-=1; return v; }
uint16 peekU16() { uint16 v = getU16(); m_readPos-=2; return v; }
uint32 peekU32() { uint32 v = getU32(); m_readPos-=4; return v; }
uint64 peekU64() { uint64 v = getU64(); m_readPos-=8; return v; }
void decryptRSA(int size, const std::string& p, const std::string& q, const std::string& d); void decryptRSA(int size, const std::string& p, const std::string& q, const std::string& d);
int getReadSize() { return m_readPos - m_headerPos; } int getReadSize() { return m_readPos - m_headerPos; }
int getUnreadSize() { return m_messageSize - (m_readPos - m_headerPos); } int getUnreadSize() { return m_messageSize - (m_readPos - m_headerPos); }
uint16 getMessageSize() { return m_messageSize; }
bool eof() { return (m_readPos - m_headerPos) >= m_messageSize; } bool eof() { return (m_readPos - m_headerPos) >= m_messageSize; }
@ -64,7 +70,6 @@ protected:
uint8* getHeaderBuffer() { return m_buffer + m_headerPos; } uint8* getHeaderBuffer() { return m_buffer + m_headerPos; }
uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; } uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; }
uint16 getHeaderSize() { return (MAX_HEADER_SIZE - m_headerPos); } uint16 getHeaderSize() { return (MAX_HEADER_SIZE - m_headerPos); }
uint16 getMessageSize() { return m_messageSize; }
uint16 readSize() { return getU16(); } uint16 readSize() { return getU16(); }
bool readChecksum(); bool readChecksum();

View File

@ -51,11 +51,12 @@ public:
void encryptRSA(int size, const std::string& key); void encryptRSA(int size, const std::string& key);
uint16 getMessageSize() { return m_messageSize; }
protected: protected:
uint8* getWriteBuffer() { return m_buffer + m_writePos; } uint8* getWriteBuffer() { return m_buffer + m_writePos; }
uint8* getHeaderBuffer() { return m_buffer + m_headerPos; } uint8* getHeaderBuffer() { return m_buffer + m_headerPos; }
uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; } uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; }
uint16 getMessageSize() { return m_messageSize; }
void writeChecksum(); void writeChecksum();
void writeMessageSize(); void writeMessageSize();

View File

@ -177,7 +177,7 @@ void OTClient::registerLuaFunctions()
g_lua.registerClass<ProtocolGame, Protocol>(); g_lua.registerClass<ProtocolGame, Protocol>();
g_lua.bindClassStaticFunction<ProtocolGame>("create", []{ return ProtocolGamePtr(new ProtocolGame); }); g_lua.bindClassStaticFunction<ProtocolGame>("create", []{ return ProtocolGamePtr(new ProtocolGame); });
g_lua.bindClassMemberFunction<ProtocolGame>("login", &ProtocolGame::login); g_lua.bindClassMemberFunction<ProtocolGame>("login", &ProtocolGame::login);
g_lua.bindClassMemberFunction<ProtocolGame>("send", &ProtocolGame::send); g_lua.bindClassMemberFunction<ProtocolGame>("safeSend", &ProtocolGame::safeSend);
g_lua.bindClassMemberFunction<ProtocolGame>("sendExtendedOpcode", &ProtocolGame::sendExtendedOpcode); g_lua.bindClassMemberFunction<ProtocolGame>("sendExtendedOpcode", &ProtocolGame::sendExtendedOpcode);
g_lua.bindClassMemberFunction<ProtocolGame>("addPosition", &ProtocolGame::addPosition); g_lua.bindClassMemberFunction<ProtocolGame>("addPosition", &ProtocolGame::addPosition);
g_lua.bindClassMemberFunction<ProtocolGame>("setMapDescription", &ProtocolGame::setMapDescription); g_lua.bindClassMemberFunction<ProtocolGame>("setMapDescription", &ProtocolGame::setMapDescription);

View File

@ -32,7 +32,7 @@ class ProtocolGame : public Protocol
{ {
public: public:
void login(const std::string& accountName, const std::string& accountPassword, const std::string& host, uint16 port, const std::string& characterName); void login(const std::string& accountName, const std::string& accountPassword, const std::string& host, uint16 port, const std::string& characterName);
void send(const OutputMessagePtr& outputMessage); void safeSend(const OutputMessagePtr& outputMessage);
void sendExtendedOpcode(uint8 opcode, const std::string& buffer); void sendExtendedOpcode(uint8 opcode, const std::string& buffer);
protected: protected:

View File

@ -431,7 +431,7 @@ void ProtocolGame::parseMapMoveWest(const InputMessagePtr& msg)
void ProtocolGame::parseUpdateTile(const InputMessagePtr& msg) void ProtocolGame::parseUpdateTile(const InputMessagePtr& msg)
{ {
Position tilePos = getPosition(msg); Position tilePos = getPosition(msg);
int thingId = msg->getU16(true); int thingId = msg->peekU16();
if(thingId == 0xFF01) { if(thingId == 0xFF01) {
msg->getU16(); msg->getU16();
} else { } else {
@ -1249,7 +1249,7 @@ int ProtocolGame::setFloorDescription(const InputMessagePtr& msg, int x, int y,
g_map.cleanTile(tilePos); g_map.cleanTile(tilePos);
if(skip == 0) { if(skip == 0) {
int tileOpt = msg->getU16(true); int tileOpt = msg->peekU16();
if(tileOpt >= 0xFF00) if(tileOpt >= 0xFF00)
skip = (msg->getU16() & 0xFF); skip = (msg->getU16() & 0xFF);
else { else {
@ -1274,7 +1274,7 @@ void ProtocolGame::setTileDescription(const InputMessagePtr& msg, Position posit
int stackPos = 0; int stackPos = 0;
while(true) { while(true) {
int inspectItemId = msg->getU16(true); int inspectItemId = msg->peekU16();
if(inspectItemId >= 0xFF00) { if(inspectItemId >= 0xFF00) {
return; return;
} }

View File

@ -23,7 +23,7 @@
#include "protocolgame.h" #include "protocolgame.h"
#include <otclient/core/game.h> #include <otclient/core/game.h>
void ProtocolGame::send(const OutputMessagePtr& outputMessage) void ProtocolGame::safeSend(const OutputMessagePtr& outputMessage)
{ {
// avoid usage of automated sends (bot modules) // avoid usage of automated sends (bot modules)
if(!g_game.checkBotProtection()) if(!g_game.checkBotProtection())
@ -37,7 +37,7 @@ void ProtocolGame::sendExtendedOpcode(uint8 opcode, const std::string& buffer)
msg->addU8(Proto::ClientExtendedOpcode); msg->addU8(Proto::ClientExtendedOpcode);
msg->addU8(opcode); msg->addU8(opcode);
msg->addString(buffer); msg->addString(buffer);
send(msg); safeSend(msg);
} }
void ProtocolGame::sendLoginPacket(uint challangeTimestamp, uint8 challangeRandom) void ProtocolGame::sendLoginPacket(uint challangeTimestamp, uint8 challangeRandom)