mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 19:44:54 +02:00
protocol login at lua
This commit is contained in:
@@ -46,6 +46,7 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindGlobalFunction("pointtostring", [](const Point& v) { return stdext::to_string(v); });
|
||||
g_lua.bindGlobalFunction("colortostring", [](const Color& v) { return stdext::to_string(v); });
|
||||
g_lua.bindGlobalFunction("sizetostring", [](const Size& v) { return stdext::to_string(v); });
|
||||
g_lua.bindGlobalFunction("iptostring", [](int v) { return stdext::ip_to_string(v); });
|
||||
|
||||
g_lua.registerStaticClass("g_crypt");
|
||||
g_lua.bindClassStaticFunction("g_crypt", "encrypt", Crypt::encrypt);
|
||||
@@ -397,11 +398,17 @@ void Application::registerLuaFunctions()
|
||||
|
||||
// Protocol
|
||||
g_lua.registerClass<Protocol>();
|
||||
//g_lua.bindClassMemberFunction<Protocol>("connect", &Protocol::connect);
|
||||
g_lua.bindClassStaticFunction<Protocol>("create", []{ return ProtocolPtr(new Protocol); });
|
||||
g_lua.bindClassMemberFunction<Protocol>("connect", &Protocol::connect);
|
||||
g_lua.bindClassMemberFunction<Protocol>("disconnect", &Protocol::disconnect);
|
||||
g_lua.bindClassMemberFunction<Protocol>("isConnected", &Protocol::isConnected);
|
||||
g_lua.bindClassMemberFunction<Protocol>("isConnecting", &Protocol::isConnecting);
|
||||
g_lua.bindClassMemberFunction<Protocol>("send", &Protocol::send);
|
||||
g_lua.bindClassMemberFunction<Protocol>("send", &Protocol::send); // must change to safeSend
|
||||
g_lua.bindClassMemberFunction<Protocol>("recv", &Protocol::recv);
|
||||
g_lua.bindClassMemberFunction<Protocol>("getXteaKey", &Protocol::getXteaKey);
|
||||
g_lua.bindClassMemberFunction<Protocol>("generateXteaKey", &Protocol::generateXteaKey);
|
||||
g_lua.bindClassMemberFunction<Protocol>("enableXteaEncryption", &Protocol::enableXteaEncryption);
|
||||
g_lua.bindClassMemberFunction<Protocol>("enableChecksum", &Protocol::enableChecksum);
|
||||
|
||||
// Module
|
||||
g_lua.registerClass<Module>();
|
||||
|
@@ -151,6 +151,15 @@ void Protocol::generateXteaKey()
|
||||
m_xteaKey[3] = unif(eng);
|
||||
}
|
||||
|
||||
std::vector<int> Protocol::getXteaKey()
|
||||
{
|
||||
std::vector<int> xteaKey;
|
||||
xteaKey.resize(4);
|
||||
for(int i = 0; i < 4; ++i)
|
||||
xteaKey[i] = m_xteaKey[i];
|
||||
return xteaKey;
|
||||
}
|
||||
|
||||
bool Protocol::xteaDecrypt(const InputMessagePtr& inputMessage)
|
||||
{
|
||||
uint16 encryptedSize = inputMessage->getUnreadSize();
|
||||
@@ -215,3 +224,8 @@ void Protocol::xteaEncrypt(const OutputMessagePtr& outputMessage)
|
||||
readPos = readPos + 2;
|
||||
}
|
||||
}
|
||||
|
||||
void Protocol::onConnect()
|
||||
{
|
||||
callLuaField("onConnect");
|
||||
}
|
||||
|
@@ -41,20 +41,21 @@ public:
|
||||
bool isConnected();
|
||||
bool isConnecting();
|
||||
|
||||
void generateXteaKey();
|
||||
std::vector<int> getXteaKey();
|
||||
void enableXteaEncryption() { m_xteaEncryptionEnabled = true; }
|
||||
|
||||
void enableChecksum() { m_checksumEnabled = true; }
|
||||
|
||||
virtual void send(const OutputMessagePtr& outputMessage);
|
||||
void recv();
|
||||
|
||||
ProtocolPtr asProtocol() { return std::static_pointer_cast<Protocol>(shared_from_this()); }
|
||||
|
||||
protected:
|
||||
void recv();
|
||||
|
||||
virtual void onConnect() = 0;
|
||||
virtual void onRecv(const InputMessagePtr& inputMessage) = 0;
|
||||
virtual void onError(const boost::system::error_code& err) = 0;
|
||||
|
||||
void enableChecksum() { m_checksumEnabled = true; }
|
||||
void enableXteaEncryption() { m_xteaEncryptionEnabled = true; }
|
||||
void generateXteaKey();
|
||||
virtual void onConnect();
|
||||
virtual void onRecv(const InputMessagePtr& inputMessage) {}
|
||||
virtual void onError(const boost::system::error_code& err) {}
|
||||
|
||||
uint32 m_xteaKey[4];
|
||||
|
||||
|
@@ -167,14 +167,11 @@ void OTClient::registerLuaFunctions()
|
||||
g_lua.bindClassStaticFunction("g_game", "getCharacterName", std::bind(&Game::getCharacterName, &g_game));
|
||||
g_lua.bindClassStaticFunction("g_game", "getWorldName", std::bind(&Game::getWorldName, &g_game));
|
||||
g_lua.bindClassStaticFunction("g_game", "getGMActions", std::bind(&Game::getGMActions, &g_game));
|
||||
g_lua.bindClassStaticFunction("g_game", "getClientVersion", std::bind(&Game::getClientVersion, &g_game));
|
||||
g_lua.bindClassStaticFunction("g_game", "getFeature", std::bind(&Game::getFeature, &g_game, std::placeholders::_1));
|
||||
|
||||
g_lua.bindGlobalFunction("getOufitColor", Outfit::getColor);
|
||||
|
||||
g_lua.registerClass<ProtocolLogin, Protocol>();
|
||||
g_lua.bindClassStaticFunction<ProtocolLogin>("create", &ProtocolLogin::create);
|
||||
g_lua.bindClassMemberFunction<ProtocolLogin>("login", &ProtocolLogin::login);
|
||||
g_lua.bindClassMemberFunction<ProtocolLogin>("cancelLogin", &ProtocolLogin::cancelLogin);
|
||||
|
||||
g_lua.registerClass<ProtocolGame, Protocol>();
|
||||
g_lua.bindClassStaticFunction<ProtocolGame>("create", []{ return ProtocolGamePtr(new ProtocolGame); });
|
||||
g_lua.bindClassMemberFunction<ProtocolGame>("login", &ProtocolGame::login);
|
||||
|
@@ -28,42 +28,20 @@
|
||||
#include <otclient/core/spritemanager.h>
|
||||
#include <otclient/core/game.h>
|
||||
|
||||
void ProtocolLogin::login(const std::string& host, int port, const std::string& accountName, const std::string& accountPassword)
|
||||
{
|
||||
if(accountName.empty() || accountPassword.empty()) {
|
||||
callLuaField("onError", "You must enter an account name and password.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_accountName = accountName;
|
||||
m_accountPassword = accountPassword;
|
||||
|
||||
connect(host, port);
|
||||
}
|
||||
|
||||
void ProtocolLogin::onConnect()
|
||||
{
|
||||
sendLoginPacket();
|
||||
}
|
||||
|
||||
void ProtocolLogin::onRecv(const InputMessagePtr& msg)
|
||||
{
|
||||
try {
|
||||
while(!msg->eof()) {
|
||||
int opcode = msg->getU8();
|
||||
|
||||
// try to parse in lua first
|
||||
if(callLuaField<bool>("onOpcode", opcode, msg))
|
||||
continue;
|
||||
|
||||
switch(opcode) {
|
||||
case Proto::LoginServerError:
|
||||
parseError(msg);
|
||||
break;
|
||||
case Proto::LoginServerMotd:
|
||||
parseMOTD(msg);
|
||||
break;
|
||||
case Proto::LoginServerUpdateNeeded:
|
||||
callLuaField("onError", "Client needs update.");
|
||||
break;
|
||||
case Proto::LoginServerCharacterList:
|
||||
parseCharacterList(msg);
|
||||
break;
|
||||
default:
|
||||
stdext::throw_exception(stdext::format("unknown opcode %d", opcode));
|
||||
break;
|
||||
@@ -126,34 +104,3 @@ void ProtocolLogin::sendLoginPacket()
|
||||
enableXteaEncryption();
|
||||
recv();
|
||||
}
|
||||
|
||||
void ProtocolLogin::parseError(const InputMessagePtr& msg)
|
||||
{
|
||||
std::string error = msg->getString();
|
||||
callLuaField("onError", error, false);
|
||||
}
|
||||
|
||||
void ProtocolLogin::parseMOTD(const InputMessagePtr& msg)
|
||||
{
|
||||
std::string motd = msg->getString();
|
||||
callLuaField("onMotd", motd);
|
||||
}
|
||||
|
||||
void ProtocolLogin::parseCharacterList(const InputMessagePtr& msg)
|
||||
{
|
||||
typedef std::tuple<std::string, std::string, std::string, int> CharacterInfo;
|
||||
typedef std::vector<CharacterInfo> CharaterList;
|
||||
CharaterList charList;
|
||||
|
||||
int numCharacters = msg->getU8();
|
||||
for(int i = 0; i < numCharacters; ++i) {
|
||||
std::string name = msg->getString();
|
||||
std::string world = msg->getString();
|
||||
uint32 ip = msg->getU32();
|
||||
uint16 port = msg->getU16();
|
||||
charList.push_back(CharacterInfo(name, world, stdext::ip_to_string(ip), port));
|
||||
}
|
||||
int premDays = msg->getU16();
|
||||
|
||||
callLuaField("onCharacterList", charList, premDays);
|
||||
}
|
||||
|
@@ -46,10 +46,6 @@ public:
|
||||
private:
|
||||
void sendLoginPacket();
|
||||
|
||||
void parseError(const InputMessagePtr& inputMessage);
|
||||
void parseMOTD(const InputMessagePtr& inputMessage);
|
||||
void parseCharacterList(const InputMessagePtr& inputMessage);
|
||||
|
||||
std::string m_accountName, m_accountPassword;
|
||||
|
||||
};
|
||||
|
Reference in New Issue
Block a user