Add/get table functions implemented on ext charlist

This commit is contained in:
Henrique Santiago 2012-08-30 02:59:10 -03:00
parent d98f6f7673
commit c517f7b745
10 changed files with 120 additions and 8 deletions

View File

@ -182,9 +182,9 @@ function CharacterList.terminate()
CharacterList = nil
end
function CharacterList.create(characters, premDays)
function CharacterList.create(characters, account)
G.characters = characters
G.premDays = premDays
G.premDays = account.premDays
characterList:destroyChildren()
local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel')
@ -212,8 +212,8 @@ function CharacterList.create(characters, premDays)
characterList:focusChild(focusLabel, ActiveFocusReason)
if premDays > 0 then
accountStatusLabel:setText(tr("Account Status:\nPremium Account (%s) days left", premDays))
if account.premDays > 0 then
accountStatusLabel:setText(tr("Account Status:\nPremium Account (%s) days left", account.premDays))
else
accountStatusLabel:setText(tr('Account Status:\nFree Account'))
end

View File

@ -28,7 +28,7 @@ local function onMotd(protocol, motd)
motdButton:show()
end
local function onCharacterList(protocol, characters, premDays)
local function onCharacterList(protocol, characters, account)
if enterGame:getChildById('rememberPasswordBox'):isChecked() then
g_settings.set('account', g_crypt.encrypt(G.account))
g_settings.set('password', g_crypt.encrypt(G.password))
@ -40,7 +40,7 @@ local function onCharacterList(protocol, characters, premDays)
loadBox:destroy()
loadBox = nil
CharacterList.create(characters, premDays)
CharacterList.create(characters, account)
CharacterList.show()
local lastMotdNumber = g_settings.getNumber("motd")

View File

@ -295,3 +295,10 @@ KeyCodeDescs = {
[KeyNumpad8] = 'Numpad8',
[KeyNumpad9] = 'Numpad9'
}
NetworkMessageTypes = {
Boolean = 1,
Number = 2,
String = 3,
Table = 4
}

View File

@ -19,3 +19,6 @@ Module
dofile 'mouse'
dofiles 'ui'
dofile 'inputmessage'
dofile 'outputmessage'

View File

@ -0,0 +1,26 @@
function InputMessage:getData()
local dataType = self:getU8()
if dataType == NetworkMessageTypes.Boolean then
return numbertoboolean(self:getU8())
elseif dataType == NetworkMessageTypes.Number then
return self:getU64()
elseif dataType == NetworkMessageTypes.String then
return self:getString()
elseif dataType == NetworkMessageTypes.Table then
return self:getTable()
else
perror('Unknown data type ' .. dataType)
end
return nil
end
function InputMessage:getTable()
local ret = {}
local size = self:getU32()
for i=1,size do
local index = self:getData()
local value = self:getData()
ret[index] = value
end
return ret
end

View File

@ -0,0 +1,42 @@
function OutputMessage:addData(data)
if type(data) == 'boolean' then
self:addU8(NetworkMessageTypes.Boolean)
self:addU8(booleantonumber(data))
elseif type(data) == 'number' then
self:addU8(NetworkMessageTypes.Number)
self:addU64(data)
elseif type(data) == 'string' then
self:addU8(NetworkMessageTypes.String)
self:addString(data)
elseif type(data) == 'table' then
self:addU8(NetworkMessageTypes.Table)
self:addTable(data)
else
perror('Invalid data type ' .. type(data))
end
end
function OutputMessage:addTable(data)
local size = 0
-- reserve for size
local sizePos = self:getWritePos()
self:addU32(size)
local sizeSize = self:getWritePos() - sizePos
-- add values
for key,value in pairs(data) do
self:addData(key)
self:addData(value)
size = size + 1
end
-- write size
local currentPos = self:getWritePos()
self:setWritePos(sizePos)
self:addU32(size)
-- fix msg size and go back to end
self:setMessageSize(self:getMessageSize() - sizeSize)
self:setWritePos(currentPos)
end

View File

@ -244,6 +244,22 @@ function fromboolean(boolean)
end
end
function booleantonumber(boolean)
if boolean then
return 1
else
return 0
end
end
function numbertoboolean(number)
if number ~= 0 then
return true
else
return false
end
end
function signalcall(param, ...)
if type(param) == 'function' then
local status, ret = pcall(param, ...)

View File

@ -5,6 +5,7 @@ LoginServerError = 10
LoginServerMotd = 20
LoginServerUpdateNeeded = 30
LoginServerCharacterList = 100
LoginServerExtendedCharacterList = 101
function ProtocolLogin:login(host, port, accountName, accountPassword)
if string.len(host) == 0 or port == nil or port == 0 then
@ -83,6 +84,8 @@ function ProtocolLogin:onRecv(msg)
signalcall(self.onError, self, tr("Client needs update. Verify your spr/dat/pic versions."))
elseif opcode == LoginServerCharacterList then
self:parseCharacterList(msg)
elseif opcode == LoginServerExtendedCharacterList then
self:parseExtendedCharacterList(msg)
else
self:parseOpcode(opcode, msg)
end
@ -111,8 +114,16 @@ function ProtocolLogin:parseCharacterList(msg)
character.worldPort = msg:getU16()
characters[i] = character
end
local premDays = msg:getU16()
signalcall(self.onCharacterList, self, characters, premDays)
local account = {}
account.premDays = msg:getU16()
signalcall(self.onCharacterList, self, characters, account)
end
function ProtocolLogin:parseExtendedCharacterList(msg)
local characters = msg:getTable()
local account = msg:getTable()
signalcall(self.onCharacterList, self, characters, account)
end
function ProtocolLogin:parseOpcode(opcode, msg)

View File

@ -725,6 +725,9 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<OutputMessage>("addPaddingBytes", &OutputMessage::addPaddingBytes);
g_lua.bindClassMemberFunction<OutputMessage>("encryptRsa", &OutputMessage::encryptRsa);
g_lua.bindClassMemberFunction<OutputMessage>("getMessageSize", &OutputMessage::getMessageSize);
g_lua.bindClassMemberFunction<OutputMessage>("setMessageSize", &OutputMessage::setMessageSize);
g_lua.bindClassMemberFunction<OutputMessage>("getWritePos", &OutputMessage::getWritePos);
g_lua.bindClassMemberFunction<OutputMessage>("setWritePos", &OutputMessage::setWritePos);
#endif
#ifdef FW_SOUND

View File

@ -51,8 +51,12 @@ public:
void encryptRsa(int size);
uint16 getWritePos() { return m_writePos; }
uint16 getMessageSize() { return m_messageSize; }
void setWritePos(uint16 writePos) { m_writePos = writePos; }
void setMessageSize(uint16 messageSize) { m_messageSize = messageSize; }
protected:
uint8* getWriteBuffer() { return m_buffer + m_writePos; }
uint8* getHeaderBuffer() { return m_buffer + m_headerPos; }