protocol via script

This commit is contained in:
Eduardo Bart 2011-05-30 22:55:34 -03:00
parent e239b0d611
commit 9b2d71f6d8
13 changed files with 132 additions and 16 deletions

View File

@ -43,6 +43,7 @@ window#enterGameWindow
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.bottom: 10 margin.bottom: 10
margin.right: 66 margin.right: 66
onClick: enterGame_onOkClicked()
button#cancelButton button#cancelButton
text: Cancel text: Cancel

View File

@ -12,6 +12,43 @@ function onApplicationClose()
App.exit() App.exit()
end end
function enterGame_onOkClicked()
local enterGameWindow = UI.getRootContainer():child("enterGameWindow")
enterGameWindow.visible = false
local loadMessageBox = messageBox("Please wait", "Connecting..")
loadMessageBox.onDestroy = function()
--TODO: cancel protocol
enterGameWindow.visible = true
protocolLogin = nil
end
protocolLogin = ProtocolLogin.new()
protocolLogin.onError = function(error)
loadMessageBox.onDestroy = nil
loadMessageBox:destroy()
local msgBox = messageBox("Login Error", error)
msgBox.onDestroy = function()
enterGameWindow.visible = true
end
protocolLogin = nil
end
protocolLogin.onMotd = function(motd)
loadMessageBox.onDestroy = nil
loadMessageBox:destroy()
local msgBox = messageBox("Message of the day", motd)
msgBox.onDestroy = function()
enterGameWindow.visible = true
end
protocolLogin = nil
end
local account = enterGameWindow:child("accountNameTextEdit").text
local password = enterGameWindow:child("passwordTextEdit").text
protocolLogin:login(account, password)
end
-- here is where everything starts -- here is where everything starts
if not initialStateLoaded then if not initialStateLoaded then
onEnterMenuState() onEnterMenuState()

View File

@ -1,11 +1,34 @@
function autoDestroyParent() MessageBox = {}
self.parent:destroy() MessageBox.__index = MessageBox
function MessageBox.create(title, text)
local msgBox = {}
setmetatable(msgBox, MessageBox)
local window = UI.load("messagebox.yml")
window.locked = true
window.title = title
window:child("textLabel").text = text
window:child("okButton").onClick = function()
self.parent:destroy()
end
window.onDestroy = function()
if msgBox.onDestroy then
msgBox.onDestroy()
end
end
msgBox.window = window
return msgBox
end
function MessageBox:destroy()
if self.window then
self.window:destroy()
self.window = nil
end
end end
function messageBox(title, text) function messageBox(title, text)
local msgBox = UI.load("messagebox.yml") return MessageBox.create(title, text)
msgBox.locked = true
msgBox.title = title
msgBox:child("textLabel").text = text
msgBox:child("okButton").onClick = autoDestroyParent
end end

View File

@ -28,6 +28,7 @@
#include <net/connection.h> #include <net/connection.h>
#include <net/inputmessage.h> #include <net/inputmessage.h>
#include <net/outputmessage.h> #include <net/outputmessage.h>
#include <script/scriptable.h>
#define CIPSOFT_PUBLIC_RSA "1321277432058722840622950990822933849527763264961655079678763618" \ #define CIPSOFT_PUBLIC_RSA "1321277432058722840622950990822933849527763264961655079678763618" \
"4334395343554449668205332383339435179772895415509701210392836078" \ "4334395343554449668205332383339435179772895415509701210392836078" \
@ -37,7 +38,7 @@
//#define RSA "109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110603907230888610072655818825358503429057592827629436413108566029093628212635953836686562675849720620786279431090218017681061521755056710823876476444260558147179707119674283982419152118103759076030616683978566631413" //#define RSA "109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110603907230888610072655818825358503429057592827629436413108566029093628212635953836686562675849720620786279431090218017681061521755056710823876476444260558147179707119674283982419152118103759076030616683978566631413"
class Protocol class Protocol : public Scriptable
{ {
public: public:
Protocol(); Protocol();
@ -48,6 +49,8 @@ public:
virtual void onRecv(InputMessage *inputMessage); virtual void onRecv(InputMessage *inputMessage);
virtual void onError(const boost::system::error_code& error); virtual void onError(const boost::system::error_code& error);
virtual const char *getScriptableName() const { return "Protocol"; }
protected: protected:
uint32 m_xteaKey[4]; uint32 m_xteaKey[4];
bool m_xteaEncryptionEnabled; bool m_xteaEncryptionEnabled;
@ -60,4 +63,6 @@ private:
ConnectionPtr m_connection; ConnectionPtr m_connection;
}; };
typedef boost::shared_ptr<Protocol> ProtocolPtr;
#endif #endif

View File

@ -29,6 +29,7 @@
#include <core/resources.h> #include <core/resources.h>
#include <ui/ui.h> #include <ui/ui.h>
#include <core/dispatcher.h> #include <core/dispatcher.h>
#include "../../protocollogin.h"
void registerLuaFunctions() void registerLuaFunctions()
{ {
@ -65,10 +66,27 @@ void registerLuaFunctions()
// UITextEdit // UITextEdit
g_lua.registerClass("UITextEdit", "UIElement"); g_lua.registerClass("UITextEdit", "UIElement");
g_lua.registerMemberField("text", &lua_UITextEdit_getText, &lua_UITextEdit_setText);
// UIWindow // UIWindow
g_lua.registerClass("UIWindow", "UIContainer"); g_lua.registerClass("UIWindow", "UIContainer");
g_lua.registerMemberField("title", &lua_UIWindow_getTitle, &lua_UIWindow_setTitle); g_lua.registerMemberField("title", &lua_UIWindow_getTitle, &lua_UIWindow_setTitle);
// Protocol
g_lua.registerClass("ProtocolLogin");
g_lua.registerMemberFunction("new", []{
ProtocolLoginPtr protocolLogin(new ProtocolLogin);
g_lua.pushClassInstance(protocolLogin);
return 1;
});
g_lua.registerMemberFunction("login", []{
std::string accountPassword = g_lua.popString();
std::string accountName = g_lua.popString();
if(ProtocolLoginPtr protocolLogin = boost::dynamic_pointer_cast<ProtocolLogin>(g_lua.popClassInstance()))
protocolLogin->login(accountName, accountPassword);
return 0;
});
} }
@ -274,6 +292,27 @@ int lua_UILabel_getText()
return 1; return 1;
} }
////////////////////////////////////////////////////////////////////////////////
// UILabel
int lua_UITextEdit_setText()
{
std::string text = g_lua.popString();
if(UITextEditPtr textEdit = boost::dynamic_pointer_cast<UITextEdit>(g_lua.popClassInstance()))
textEdit->setText(text);
return 1;
}
int lua_UITextEdit_getText()
{
if(UITextEditPtr textEdit = boost::dynamic_pointer_cast<UITextEdit>(g_lua.popClassInstance()))
g_lua.pushString(textEdit->getText());
else
g_lua.pushNil();
return 1;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// UIWindow // UIWindow

View File

@ -61,6 +61,10 @@ int lua_UIContainer_getChildren();
int lua_UILabel_setText(); int lua_UILabel_setText();
int lua_UILabel_getText(); int lua_UILabel_getText();
// UITextEdit
int lua_UITextEdit_setText();
int lua_UITextEdit_getText();
// UIButton // UIButton
void lua_UIButton_onClick(); void lua_UIButton_onClick();

View File

@ -44,7 +44,7 @@ void Scriptable::releaseLuaTableRef()
} }
} }
void Scriptable::callLuaTableField(const std::string& field) void Scriptable::callLuaTableField(const std::string& field, int numArgs)
{ {
// set self // set self
g_lua.pushClassInstance(shared_from_this()); g_lua.pushClassInstance(shared_from_this());
@ -55,9 +55,11 @@ void Scriptable::callLuaTableField(const std::string& field)
// call it if its a function // call it if its a function
if(g_lua.isFunction()) { if(g_lua.isFunction()) {
g_lua.callFunction(); g_lua.insert(-numArgs-1);
g_lua.callFunction(numArgs);
// if its an array call each element // if its an array call each element
} else if(g_lua.isTable()) { } else if(g_lua.isTable()) {
//TODO: call here with arguments
g_lua.pushNil(); g_lua.pushNil();
while(g_lua.next()) { while(g_lua.next()) {
// call it if its a function // call it if its a function

View File

@ -36,7 +36,7 @@ public:
int getLuaTableRef(); int getLuaTableRef();
void releaseLuaTableRef(); void releaseLuaTableRef();
void callLuaTableField(const std::string& field); void callLuaTableField(const std::string& field, int numArgs = 0);
private: private:
int m_luaTableRef; int m_luaTableRef;

View File

@ -34,7 +34,7 @@ void UIButton::onInputEvent(const InputEvent& event)
} else if(event.type == EV_MOUSE_LUP && m_state == ButtonDown) { } else if(event.type == EV_MOUSE_LUP && m_state == ButtonDown) {
m_state = ButtonUp; m_state = ButtonUp;
if(getRect().contains(event.mousePos)) { if(getRect().contains(event.mousePos)) {
g_dispatcher.addTask(boost::bind(&Scriptable::callLuaTableField, shared_from_this(), "onClick")); g_dispatcher.addTask(boost::bind(&Scriptable::callLuaTableField, shared_from_this(), "onClick", 0));
} }
} else if(event.type == EV_MOUSE_MOVE && m_state != ButtonDown) { } else if(event.type == EV_MOUSE_MOVE && m_state != ButtonDown) {
if(isMouseOver()) if(isMouseOver())

View File

@ -115,7 +115,7 @@ void UIElement::setSkin(const UIElementSkinPtr& skin)
void UIElement::onLoad() void UIElement::onLoad()
{ {
g_dispatcher.addTask(boost::bind(&Scriptable::callLuaTableField, shared_from_this(), "onLoad")); g_dispatcher.addTask(boost::bind(&Scriptable::callLuaTableField, shared_from_this(), "onLoad", 0));
} }
void UIElement::render() void UIElement::render()

View File

@ -40,7 +40,7 @@ public:
void onRectUpdate(); void onRectUpdate();
void onFocusChange(); void onFocusChange();
void setText(const std::string& text); void setText(const std::string& text) { m_textArea.setText(text); }
std::string getText() const { return m_textArea.getText(); } std::string getText() const { return m_textArea.getText(); }
TextArea& getTextArea() { return m_textArea; } TextArea& getTextArea() { return m_textArea; }

View File

@ -24,6 +24,7 @@
#include "protocollogin.h" #include "protocollogin.h"
#include <net/outputmessage.h> #include <net/outputmessage.h>
#include <util/rsa.h> #include <util/rsa.h>
#include <script/luascript.h>
ProtocolLogin::ProtocolLogin() ProtocolLogin::ProtocolLogin()
{ {
@ -131,13 +132,15 @@ void ProtocolLogin::onRecv(InputMessage *inputMessage)
void ProtocolLogin::parseError(InputMessage *inputMessage) void ProtocolLogin::parseError(InputMessage *inputMessage)
{ {
std::string error = inputMessage->getString(); std::string error = inputMessage->getString();
logError(error.c_str()); g_lua.pushString(error);
callLuaTableField("onError", 1);
} }
void ProtocolLogin::parseMOTD(InputMessage *inputMessage) void ProtocolLogin::parseMOTD(InputMessage *inputMessage)
{ {
std::string motd = inputMessage->getString(); std::string motd = inputMessage->getString();
logError(motd.c_str()); g_lua.pushString(motd);
callLuaTableField("onMotd", 1);
} }
void ProtocolLogin::parseCharacterList(InputMessage *inputMessage) void ProtocolLogin::parseCharacterList(InputMessage *inputMessage)

View File

@ -39,6 +39,8 @@ public:
void sendPacket(); void sendPacket();
void onRecv(InputMessage *inputMessage); void onRecv(InputMessage *inputMessage);
const char *getScriptableName() const { return "ProtocolLogin"; }
private: private:
void parseError(InputMessage *inputMessage); void parseError(InputMessage *inputMessage);
void parseMOTD(InputMessage *inputMessage); void parseMOTD(InputMessage *inputMessage);