From d586bb5e93d08c5b4a4e76619c5af3458d048195 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Fri, 11 May 2012 16:25:29 -0300 Subject: [PATCH] some protocol changes for more compatibility --- src/otclient/core/creature.cpp | 2 +- src/otclient/core/game.cpp | 28 ++----------------------- src/otclient/core/game.h | 7 ------- src/otclient/net/protocolcodes.h | 21 ++++++++++++------- src/otclient/net/protocolgame.cpp | 4 ++++ src/otclient/net/protocolgame.h | 1 + src/otclient/net/protocolgameparse.cpp | 29 ++++++++++++++++++-------- src/otclient/net/protocolgamesend.cpp | 8 +------ src/otclient/net/protocollogin.cpp | 6 +----- 9 files changed, 44 insertions(+), 62 deletions(-) diff --git a/src/otclient/core/creature.cpp b/src/otclient/core/creature.cpp index bb14b453..86ec8f42 100644 --- a/src/otclient/core/creature.cpp +++ b/src/otclient/core/creature.cpp @@ -41,7 +41,7 @@ Creature::Creature() : Thing() { - m_id = 0; + m_id = 1; m_healthPercent = 100; m_speed = 200; m_direction = Otc::South; diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index f5e980dc..dda0bdd5 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -49,6 +49,7 @@ void Game::resetGameStates() m_safeFight = true; m_followingCreature = nullptr; m_attackingCreature = nullptr; + m_localPlayer = nullptr; for(auto& it : m_containers) { const ContainerPtr& container = it.second; @@ -76,11 +77,8 @@ void Game::processConnectionError(const boost::system::error_code& error) void Game::processDisconnect() { - if(isOnline()) { - m_localPlayer = nullptr; - + if(isOnline()) processGameEnd(); - } if(m_protocolGame) { m_protocolGame->disconnect(); @@ -144,28 +142,6 @@ void Game::processGMActions(const std::vector& actions) g_lua.callGlobalField("g_game", "onGMActions", actions); } -void Game::processPlayerStats(double health, double maxHealth, - double freeCapacity, double experience, - double level, double levelPercent, - double mana, double maxMana, - double magicLevel, double magicLevelPercent, - double soul, double stamina) -{ - if(!m_localPlayer) { - logTraceError("there is no local player"); - return; - } - - m_localPlayer->setHealth(health, maxHealth); - m_localPlayer->setFreeCapacity(freeCapacity); - m_localPlayer->setExperience(experience); - m_localPlayer->setLevel(level, levelPercent); - m_localPlayer->setMana(mana, maxMana); - m_localPlayer->setMagicLevel(magicLevel, magicLevelPercent); - m_localPlayer->setStamina(stamina); - m_localPlayer->setSoul(soul); -} - void Game::processPing() { g_lua.callGlobalField("g_game", "onPing"); diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 3f22d1ef..efdb3b49 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -53,13 +53,6 @@ protected: void processDeath(int penality); void processGMActions(const std::vector& actions); - void processPlayerStats(double health, double maxHealth, - double freeCapacity, double experience, - double level, double levelPercent, - double mana, double maxMana, - double magicLevel, double magicLevelPercent, - double soul, double stamina); - void processInventoryChange(int slot, const ItemPtr& item); void processCreatureMove(const CreaturePtr& creature, const Position& oldPos, const Position& newPos); void processCreatureTeleport(const CreaturePtr& creature); diff --git a/src/otclient/net/protocolcodes.h b/src/otclient/net/protocolcodes.h index 2a87b721..ba5cacaf 100644 --- a/src/otclient/net/protocolcodes.h +++ b/src/otclient/net/protocolcodes.h @@ -52,6 +52,20 @@ namespace Proto { constexpr int ClientVersion = PROTOCOL; constexpr int PicSignature = 0x4E119CBF; + enum OsTypes { + OsWindows = 1, + OsLinux = 2, + OsMac = 3 + }; + +#ifdef OSTYPE + constexpr int ClientOs = OSTYPE; +#elif defined WIN32 + constexpr int ClientOs = OsWindows; +#else + constexpr int ClientOs = OsLinux; +#endif + #if PROTOCOL>=860 constexpr int NumViolationReasons = 20; #elif PROTOCOL>=854 @@ -60,13 +74,6 @@ namespace Proto { constexpr int NumViolationReasons = 32; #endif - enum OsTypes { - OsWindows = 1, - OsLinux = 2, - OsMac = 3, - OsBrowser = 4 - }; - enum LoginServerOpts { LoginServerError = 10, LoginServerMotd = 20, diff --git a/src/otclient/net/protocolgame.cpp b/src/otclient/net/protocolgame.cpp index 19cc47cd..07107af4 100644 --- a/src/otclient/net/protocolgame.cpp +++ b/src/otclient/net/protocolgame.cpp @@ -24,6 +24,7 @@ #include #include #include +#include void ProtocolGame::login(const std::string& accountName, const std::string& accountPassword, const std::string& host, uint16 port, const std::string& characterName) { @@ -43,6 +44,9 @@ void ProtocolGame::onConnect() { recv(); + // must create local player before parsing anything + m_localPlayer = LocalPlayerPtr(new LocalPlayer); + #if PROTOCOL>=854 m_waitingLoginPacket = true; #else diff --git a/src/otclient/net/protocolgame.h b/src/otclient/net/protocolgame.h index 86a2985a..3424af2a 100644 --- a/src/otclient/net/protocolgame.h +++ b/src/otclient/net/protocolgame.h @@ -196,6 +196,7 @@ private: private: Boolean m_waitingLoginPacket; + Boolean m_firstPacket; std::string m_accountName; std::string m_accountPassword; std::string m_characterName; diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index 6038ab33..db4050a9 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -42,6 +42,12 @@ void ProtocolGame::parseMessage(InputMessage& msg) while(!msg.eof()) { opcode = msg.getU8(); + if(m_firstPacket) { + if(opcode != Proto::GameServerInitGame) + logWarning("first server network opcode is not GameServerInitGame"); + m_firstPacket = false; + } + switch(opcode) { case Proto::GameServerInitGame: parseInitGame(msg); @@ -271,7 +277,7 @@ void ProtocolGame::parseMessage(InputMessage& msg) parseExtendedOpcode(msg); break; default: - Fw::throwException("unknown opcode ", opcode, ", previous opcode is ", prevOpcode); + Fw::throwException("unknown opcode"); break; } prevOpcode = opcode; @@ -287,9 +293,7 @@ void ProtocolGame::parseInitGame(InputMessage& msg) int serverBeat = msg.getU16(); bool canReportBugs = msg.getU8(); - m_localPlayer = LocalPlayerPtr(new LocalPlayer); m_localPlayer->setId(playerId); - g_game.processGameStart(m_localPlayer, serverBeat, canReportBugs); } @@ -595,7 +599,12 @@ void ProtocolGame::parseWorldLight(InputMessage& msg) void ProtocolGame::parseMagicEffect(InputMessage& msg) { Position pos = parsePosition(msg); +#if PROTOCOL>=854 + // newer tibia decreased the max effects number, why??? int effectId = msg.getU8(); +#else + int effectId = msg.getU16(); +#endif EffectPtr effect = EffectPtr(new Effect()); effect->setId(effectId); @@ -781,12 +790,14 @@ void ProtocolGame::parsePlayerStats(InputMessage& msg) double soul = msg.getU8(); double stamina = msg.getU16(); - g_game.processPlayerStats(health, maxHealth, - freeCapacity, experience, - level, levelPercent, - mana, maxMana, - magicLevel, magicLevelPercent, - soul, stamina); + m_localPlayer->setHealth(health, maxHealth); + m_localPlayer->setFreeCapacity(freeCapacity); + m_localPlayer->setExperience(experience); + m_localPlayer->setLevel(level, levelPercent); + m_localPlayer->setMana(mana, maxMana); + m_localPlayer->setMagicLevel(magicLevel, magicLevelPercent); + m_localPlayer->setStamina(stamina); + m_localPlayer->setSoul(soul); } void ProtocolGame::parsePlayerSkills(InputMessage& msg) diff --git a/src/otclient/net/protocolgamesend.cpp b/src/otclient/net/protocolgamesend.cpp index dadcad31..697bdad6 100644 --- a/src/otclient/net/protocolgamesend.cpp +++ b/src/otclient/net/protocolgamesend.cpp @@ -28,13 +28,7 @@ void ProtocolGame::sendLoginPacket(uint timestamp, uint8 unknown) OutputMessage msg; msg.addU8(Proto::ClientEnterGame); - -#ifdef WIN32 - msg.addU16(Proto::OsWindows); -#else - msg.addU16(Proto::OsLinux); -#endif - + msg.addU16(Proto::ClientOs); msg.addU16(Proto::ClientVersion); int paddingBytes = 128; diff --git a/src/otclient/net/protocollogin.cpp b/src/otclient/net/protocollogin.cpp index f0a2b9f2..9409dd66 100644 --- a/src/otclient/net/protocollogin.cpp +++ b/src/otclient/net/protocollogin.cpp @@ -86,11 +86,7 @@ void ProtocolLogin::sendLoginPacket() OutputMessage msg; msg.addU8(Proto::ClientEnterAccount); -#ifdef WIN32 - msg.addU16(Proto::OsWindows); -#else - msg.addU16(Proto::OsLinux); -#endif + msg.addU16(Proto::ClientOs); msg.addU16(Proto::ClientVersion); msg.addU32(g_thingsType.getSignature()); // data signature