mirror of
https://github.com/edubart/otclient.git
synced 2025-10-15 03:54:54 +02:00
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:
60
modules/game_textmessage/protocol.lua
Normal file
60
modules/game_textmessage/protocol.lua
Normal 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
|
@@ -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
|
||||
|
@@ -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()
|
||||
|
Reference in New Issue
Block a user