Module sandboxing system

Sandboxing makes module scripts run inside an isolated lua environments,
making more easier and secure to script

Move and rework TextMessage using the new sandbox system
This commit is contained in:
Eduardo Bart
2012-07-19 06:12:17 -03:00
parent e2921c6407
commit f289db3a9e
17 changed files with 287 additions and 263 deletions

View File

@@ -16,6 +16,5 @@ Module
dofile 'settings'
dofile 'keyboard'
dofile 'mouse'
dofile 'protocol'
dofiles 'ui'

View File

@@ -51,11 +51,13 @@ function g_settings.getString(key, default)
end
function g_settings.getInteger(key, default)
return tonumber(g_settings.get(key, default))
local v = tonumber(g_settings.get(key, default)) or 1
return v
end
function g_settings.getNumber(key, default)
return tonumber(g_settings.get(key, default))
local v = tonumber(g_settings.get(key, default)) or 1
return v
end
function g_settings.getBoolean(key, default)

View File

@@ -110,9 +110,34 @@ function extends(base)
return derived
end
function export(what, key)
if key ~= nil then
_G[key] = what
else
for k,v in pairs(what) do
_G[k] = v
end
end
end
function unexport(key)
if type(key) == 'table' then
for _k,v in pairs(key) do
_G[v] = nil
end
else
_G[key] = nil
end
end
function sandbox(what)
what = what or 2
setfenv(what, newenv())
end
function newenv()
local env = { }
setmetatable(env, { __index = _G} )
setmetatable(env, { __index = getfenv() } )
return env
end
@@ -157,13 +182,6 @@ function toboolean(str)
return false
end
local oldtonumber = tonumber
function tonumber(v)
if v == nil then return 0 end
return oldtonumber(v)
end
function signalcall(param, ...)
if type(param) == 'function' then
return param(...)

View File

@@ -8,7 +8,7 @@ local deathWindow
-- public functions
function PlayerDeath.init()
g_ui.importStyle('deathwindow.otui')
connect(g_game, { onDeath = PlayerDeath.display,
onGameEnd = PlayerDeath.reset })
end
@@ -22,7 +22,6 @@ function PlayerDeath.terminate()
end
function PlayerDeath.reset()
GameInterface.getMapPanel():recursiveGetChildById('centerAdvance'):hide()
if deathWindow then
deathWindow:destroy()
deathWindow = nil
@@ -37,9 +36,9 @@ end
function PlayerDeath.displayDeadMessage()
local advanceLabel = GameInterface.getMapPanel():recursiveGetChildById('centerAdvance')
if advanceLabel:isVisible() then
return
return
end
TextMessage.displayEventAdvance(tr('You are dead.'))
end
@@ -64,7 +63,7 @@ function PlayerDeath.openWindow()
deathWindow.onEnter = okFunc
deathWindow.onEscape = cancelFunc
okButton.onClick = okFunc
cancelButton.onClick = cancelFunc
end

View File

@@ -0,0 +1,60 @@
function getMessageTypes(version)
if version >= 960 then
perror("TODO: message types for 9.6")
return {}
elseif version >= 861 then
return {
[13] = 'ConsoleOrange',
[14] = 'ConsoleOrange',
[15] = 'Warning',
[16] = 'EventAdvance',
[17] = 'EventDefault',
[18] = 'StatusDefault',
[19] = 'Info',
[20] = 'StatusSmall',
[21] = 'ConsoleBlue',
[22] = 'ConsoleRed'
}
elseif version >= 854 then
return {
[18] = 'ConsoleRed',
[19] = 'ConsoleOrange',
[20] = 'ConsoleOrange',
[21] = 'Warning',
[22] = 'EventAdvance',
[23] = 'EventDefault',
[24] = 'StatusDefault',
[25] = 'Info',
[26] = 'StatusSmall',
[27] = 'ConsoleBlue'
}
else
return {
[18] = 'Warning',
[19] = 'EventAdvance',
[20] = 'EventDefault',
[21] = 'StatusDefault',
[22] = 'Info',
[23] = 'StatusSmall',
[24] = 'ConsoleBlue',
[25] = 'ConsoleRed',
[26] = 'ConsoleOrange',
[27] = 'ConsoleOrange',
}
end
end
function parseTextMessage(msg)
local msgtype = msg:getU8()
local text = msg:getString()
msgtype = getMessageTypes(g_game.getProtocolVersion())[msgtype]
signalcall(g_game.onTextMessage, msgtype, text)
end
function registerProtocol()
ProtocolGame.registerOpcode(GameServerOpcodes.GameServerTextMessage, parseTextMessage)
end
function unregisterProtocol()
ProtocolGame.unregisterOpcode(GameServerOpcodes.GameServerTextMessage)
end

View File

@@ -1,41 +1,104 @@
TextMessage = {}
-- require styles
g_ui.importStyle('textmessage.otui')
-- private variables
local MessageTypes = {
consoleRed = { color = '#F55E5E', consoleTab = tr('Default') },
consoleOrange = { color = '#FE6500', consoleTab = tr('Default') },
consoleBlue = { color = '#9F9DFD', consoleTab = tr('Default') },
warning = { color = '#F55E5E', consoleTab = tr('Server Log'), labelId = 'centerWarning' },
infoDescription = { color = '#00EB00', consoleTab = tr('Server Log'), labelId = 'centerInfo', consoleOption = 'showInfoMessagesInConsole' },
eventAdvance = { color = '#FFFFFF', consoleTab = tr('Server Log'), labelId = 'centerAdvance', consoleOption = 'showEventMessagesInConsole' },
eventDefault = { color = '#FFFFFF', consoleTab = tr('Server Log'), labelId = 'bottomStatus', consoleOption = 'showEventMessagesInConsole' },
statusDefault = { color = '#FFFFFF', consoleTab = tr('Server Log'), labelId = 'bottomStatus', consoleOption = 'showStatusMessagesInConsole' },
statusSmall = { color = '#FFFFFF', labelId = 'bottomStatus' },
private = { color = '#5FF7F7', labelId = 'centerPrivate' }
MessageTypesConf = {
ConsoleRed = { color = '#F55E5E', consoleTab = tr('Default') },
ConsoleOrange = { color = '#FE6500', consoleTab = tr('Default') },
ConsoleBlue = { color = '#9F9DFD', consoleTab = tr('Default') },
Warning = { color = '#F55E5E', consoleTab = tr('Server Log'), labelId = 'warningLabel' },
Info = { color = '#00EB00', consoleTab = tr('Server Log'), labelId = 'infoLabel', consoleOption = 'showInfoMessagesInConsole' },
EventAdvance = { color = '#FFFFFF', consoleTab = tr('Server Log'), labelId = 'advanceLabel', consoleOption = 'showEventMessagesInConsole' },
EventDefault = { color = '#FFFFFF', consoleTab = tr('Server Log'), labelId = 'statusLabel', consoleOption = 'showEventMessagesInConsole' },
StatusDefault = { color = '#FFFFFF', consoleTab = tr('Server Log'), labelId = 'statusLabel', consoleOption = 'showStatusMessagesInConsole' },
StatusSmall = { color = '#FFFFFF', labelId = 'statusLabel' },
Private = { color = '#5FF7F7', labelId = 'privateLabel' }
}
local centerTextMessagePanel
local bottomStatusLabel
local privateLabel
centerTextMessagePanel = nil
statusLabel = nil
privateLabel = nil
warningLabel = nil
advanceLabel = nil
infoLabel = nil
-- private functions
local function displayMessage(msgtype, msg, time)
function init()
connect(g_game, {
onTextMessage = displayMessage,
onGameStart = clearMessages
})
registerProtocol()
g_ui.importStyle('textmessage.otui')
centerTextMessagePanel = g_ui.createWidget('Panel', GameInterface.getMapPanel())
centerTextMessagePanel:setId('centerTextMessagePanel')
local layout = UIVerticalLayout.create(centerTextMessagePanel)
layout:setFitChildren(true)
centerTextMessagePanel:setLayout(layout)
centerTextMessagePanel:setWidth(360)
centerTextMessagePanel:centerIn('parent')
warningLabel = createTextMessageLabel('warningLabel', centerTextMessagePanel, 'CenterLabel')
advanceLabel = createTextMessageLabel('advanceLabel', centerTextMessagePanel, 'CenterLabel')
infoLabel = createTextMessageLabel('infoLabel', centerTextMessagePanel, 'CenterLabel')
privateLabel = createTextMessageLabel('privateLabel', GameInterface.getMapPanel(), 'TopCenterLabel')
statusLabel = createTextMessageLabel('statusLabel', GameInterface.getMapPanel(), 'BottomLabel')
export({
clearMessages = clearMessages,
displayStatus = function() displayMessage(MessageTypes.StatusSmall, msg) end,
displayEventAdvance = function() displayMessage(MessageTypes.EventAdvance, msg, time) end,
displayPrivate = function() displayPrivate(msg, time) end
}, 'TextMessage')
end
function terminate()
disconnect(g_game, {
onTextMessage = display,
onGameStart = clearMessages
})
unregisterProtocol()
removeEvent(warningLabel.hideEvent)
removeEvent(advanceLabel.hideEvent)
removeEvent(infoLabel.hideEvent)
removeEvent(privateLabel.hideEvent)
removeEvent(statusLabel.hideEvent)
centerTextMessagePanel:destroy()
statusLabel:destroy()
privateLabel:destroy()
unexport('TextMessage')
end
function clearMessages()
warningLabel:hide()
advanceLabel:hide()
infoLabel:hide()
privateLabel:hide()
statusLabel:hide()
end
function createTextMessageLabel(id, parent, class)
local label = g_ui.createWidget(class, parent)
label:setFont('verdana-11px-rounded')
label:setId(id)
return label
end
function displayMessage(msgtype, msg, time)
if not g_game.isOnline() then return end
msgtypeconf = MessageTypesConf[msgtype]
if msgtype.consoleTab ~= nil then
if msgtype.consoleOption == nil or Options.getOption(msgtype.consoleOption) then
Console.addText(msg, msgtype, msgtype.consoleTab)
if msgtypeconf.consoleTab ~= nil then
if msgtypeconf.consoleOption == nil or Options.getOption(msgtypeconf.consoleOption) then
Console.addText(msg, msgtypeconf, msgtypeconf.consoleTab)
end
end
if msgtype.labelId then
local label = GameInterface.getMapPanel():recursiveGetChildById(msgtype.labelId)
if msgtypeconf.labelId then
local label = GameInterface.getMapPanel():recursiveGetChildById(msgtypeconf.labelId)
label:setText(msg)
label:setColor(msgtype.color)
label:setColor(msgtypeconf.color)
if not time then
time = math.max(#msg * 100, 4000)
@@ -47,77 +110,3 @@ local function displayMessage(msgtype, msg, time)
label.hideEvent = scheduleEvent(function() label:setVisible(false) end, time)
end
end
local function createTextMessageLabel(id, parent, class)
local label = g_ui.createWidget(class, parent)
label:setFont('verdana-11px-rounded')
label:setId(id)
return label
end
-- public functions
function TextMessage.init()
connect(g_game, { onTextMessage = TextMessage.display,
onGameStart = TextMessage.clearMessages })
centerTextMessagePanel = g_ui.createWidget('Panel', GameInterface.getMapPanel())
centerTextMessagePanel:setId('centerTextMessagePanel')
local layout = UIVerticalLayout.create(centerTextMessagePanel)
layout:setFitChildren(true)
centerTextMessagePanel:setLayout(layout)
centerTextMessagePanel:setWidth(360)
centerTextMessagePanel:centerIn('parent')
createTextMessageLabel('centerWarning', centerTextMessagePanel, 'CenterLabel')
createTextMessageLabel('centerAdvance', centerTextMessagePanel, 'CenterLabel')
createTextMessageLabel('centerInfo', centerTextMessagePanel, 'CenterLabel')
privateLabel = createTextMessageLabel('centerPrivate', GameInterface.getMapPanel(), 'TopCenterLabel')
bottomStatusLabel = createTextMessageLabel('bottomStatus', GameInterface.getMapPanel(), 'BottomLabel')
end
function TextMessage.terminate()
disconnect(g_game, { onDeath = TextMessage.displayDeadMessage,
onTextMessage = TextMessage.display,
onGameStart = TextMessage.clearMessages })
removeEvent(GameInterface.getMapPanel():recursiveGetChildById('centerWarning').hideEvent)
removeEvent(GameInterface.getMapPanel():recursiveGetChildById('centerAdvance').hideEvent)
removeEvent(GameInterface.getMapPanel():recursiveGetChildById('centerInfo').hideEvent)
removeEvent(GameInterface.getMapPanel():recursiveGetChildById('centerPrivate').hideEvent)
removeEvent(GameInterface.getMapPanel():recursiveGetChildById('bottomStatus').hideEvent)
centerTextMessagePanel:destroy()
bottomStatusLabel:destroy()
privateLabel:destroy()
centerTextMessagePanel = nil
bottomStatusLabel = nil
privateLabel = nil
TextMessage = nil
end
function TextMessage.clearMessages()
GameInterface.getMapPanel():recursiveGetChildById('centerWarning'):hide()
GameInterface.getMapPanel():recursiveGetChildById('centerAdvance'):hide()
GameInterface.getMapPanel():recursiveGetChildById('centerInfo'):hide()
GameInterface.getMapPanel():recursiveGetChildById('centerPrivate'):hide()
GameInterface.getMapPanel():recursiveGetChildById('bottomStatus'):hide()
end
function TextMessage.displayStatus(msg, time)
displayMessage(MessageTypes.statusSmall, msg)
end
function TextMessage.displayEventAdvance(msg, time)
displayMessage(MessageTypes.eventAdvance, msg, time)
end
function TextMessage.displayPrivate(msg, time)
displayMessage(MessageTypes.private, msg, time)
end
function TextMessage.display(msgtypedesc, msg)
local msgtype = MessageTypes[msgtypedesc]
if msgtype then
displayMessage(msgtype, msg)
end
end

View File

@@ -3,13 +3,14 @@ Module
description: Manage game text messages
author: edubart
website: www.otclient.info
sandboxed: true
dependencies:
- game_interface
@onLoad: |
dofile 'textmessage'
TextMessage.init()
scripts:
- protocol.lua
- textmessage.lua
@onUnload: |
TextMessage.terminate()
@onLoad: init()
@onUnload: terminate()

View File

@@ -10,7 +10,7 @@ Module
@onLoad: |
dofile 'const'
dofile 'protocol'
dofile 'protocollogin'
dofile 'protocolgame'
dofile 'game'