mirror of https://github.com/OpenMW/openmw.git
Lua console
parent
1363292fc9
commit
88d09c336c
@ -1,2 +1,5 @@
|
||||
PLAYER: scripts/omw/camera.lua
|
||||
NPC,CREATURE: scripts/omw/ai.lua
|
||||
PLAYER: scripts/omw/console/player.lua
|
||||
GLOBAL: scripts/omw/console/global.lua
|
||||
CUSTOM: scripts/omw/console/local.lua
|
||||
|
@ -0,0 +1,81 @@
|
||||
local util = require('openmw.util')
|
||||
|
||||
local player = nil
|
||||
|
||||
local function printToConsole(...)
|
||||
local strs = {}
|
||||
for i = 1, select('#', ...) do
|
||||
strs[i] = tostring(select(i, ...))
|
||||
end
|
||||
player:sendEvent('OMWConsolePrint', table.concat(strs, '\t'))
|
||||
end
|
||||
|
||||
local function printRes(...)
|
||||
if select('#', ...) >= 0 then
|
||||
printToConsole(...)
|
||||
end
|
||||
end
|
||||
|
||||
local env = {
|
||||
I = require('openmw.interfaces'),
|
||||
util = require('openmw.util'),
|
||||
storage = require('openmw.storage'),
|
||||
core = require('openmw.core'),
|
||||
types = require('openmw.types'),
|
||||
async = require('openmw.async'),
|
||||
world = require('openmw.world'),
|
||||
aux_util = require('openmw_aux.util'),
|
||||
view = require('openmw_aux.util').deepToString,
|
||||
print = printToConsole,
|
||||
exit = function() player:sendEvent('OMWConsoleExit') end,
|
||||
help = function() player:sendEvent('OMWConsoleHelp') end,
|
||||
}
|
||||
env._G = env
|
||||
setmetatable(env, {__index = _G, __metatable = false})
|
||||
_G = nil
|
||||
|
||||
local function executeLuaCode(code)
|
||||
local fn
|
||||
local ok, err = pcall(function() fn = util.loadCode('return ' .. code, env) end)
|
||||
if ok then
|
||||
ok, err = pcall(function() printRes(fn()) end)
|
||||
else
|
||||
ok, err = pcall(function() util.loadCode(code, env)() end)
|
||||
end
|
||||
if not ok then
|
||||
player:sendEvent('OMWConsoleError', err)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
eventHandlers = {
|
||||
OMWConsoleEval = function(data)
|
||||
player = data.player
|
||||
env.selected = data.selected
|
||||
executeLuaCode(data.code)
|
||||
if env.selected ~= data.selected then
|
||||
local ok, err = pcall(function() player:sendEvent('OMWConsoleSetSelected', env.selected) end)
|
||||
if not ok then player:sendEvent('OMWConsoleError', err) end
|
||||
end
|
||||
end,
|
||||
OMWConsoleStartLocal = function(data)
|
||||
player = data.player
|
||||
ok, err = pcall(function()
|
||||
if not data.selected:hasScript('scripts/omw/console/local.lua') then
|
||||
data.selected:addScript('scripts/omw/console/local.lua')
|
||||
end
|
||||
end)
|
||||
if ok then
|
||||
player:sendEvent('OMWConsoleSetContext', data.selected)
|
||||
else
|
||||
player:sendEvent('OMWConsoleError', err)
|
||||
end
|
||||
end,
|
||||
OMWConsoleStopLocal = function(obj)
|
||||
if obj:hasScript('scripts/omw/console/local.lua') then
|
||||
obj:removeScript('scripts/omw/console/local.lua')
|
||||
end
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
@ -0,0 +1,71 @@
|
||||
local util = require('openmw.util')
|
||||
local core = require('openmw.core')
|
||||
local self = require('openmw.self')
|
||||
|
||||
local player = nil
|
||||
|
||||
local function printToConsole(...)
|
||||
local strs = {}
|
||||
for i = 1, select('#', ...) do
|
||||
strs[i] = tostring(select(i, ...))
|
||||
end
|
||||
player:sendEvent('OMWConsolePrint', table.concat(strs, '\t'))
|
||||
end
|
||||
|
||||
local function printRes(...)
|
||||
if select('#', ...) >= 0 then
|
||||
printToConsole(...)
|
||||
end
|
||||
end
|
||||
|
||||
local env = {
|
||||
I = require('openmw.interfaces'),
|
||||
util = require('openmw.util'),
|
||||
storage = require('openmw.storage'),
|
||||
core = require('openmw.core'),
|
||||
types = require('openmw.types'),
|
||||
async = require('openmw.async'),
|
||||
nearby = require('openmw.nearby'),
|
||||
self = require('openmw.self'),
|
||||
aux_util = require('openmw_aux.util'),
|
||||
view = require('openmw_aux.util').deepToString,
|
||||
print = printToConsole,
|
||||
exit = function() player:sendEvent('OMWConsoleExit') end,
|
||||
help = function() player:sendEvent('OMWConsoleHelp') end,
|
||||
}
|
||||
env._G = env
|
||||
setmetatable(env, {__index = _G, __metatable = false})
|
||||
_G = nil
|
||||
|
||||
local function executeLuaCode(code)
|
||||
local fn
|
||||
local ok, err = pcall(function() fn = util.loadCode('return ' .. code, env) end)
|
||||
if ok then
|
||||
ok, err = pcall(function() printRes(fn()) end)
|
||||
else
|
||||
ok, err = pcall(function() util.loadCode(code, env)() end)
|
||||
end
|
||||
if not ok then
|
||||
player:sendEvent('OMWConsoleError', err)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
eventHandlers = {
|
||||
OMWConsoleEval = function(data)
|
||||
player = data.player
|
||||
env.selected = data.selected
|
||||
executeLuaCode(data.code)
|
||||
if env.selected ~= data.selected then
|
||||
local ok, err = pcall(function() player:sendEvent('OMWConsoleSetSelected', env.selected) end)
|
||||
if not ok then player:sendEvent('OMWConsoleError', err) end
|
||||
end
|
||||
end,
|
||||
},
|
||||
engineHandlers = {
|
||||
onLoad = function()
|
||||
core.sendGlobalEvent('OMWConsoleStopLocal', self.object)
|
||||
end,
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,158 @@
|
||||
local ui = require('openmw.ui')
|
||||
local util = require('openmw.util')
|
||||
local self = require('openmw.self')
|
||||
local core = require('openmw.core')
|
||||
|
||||
local function printHelp()
|
||||
local msg = [[
|
||||
This is the built-in Lua interpreter.
|
||||
help() - print this message
|
||||
exit() - exit Lua mode
|
||||
selected - currently selected object (click on any object to change)
|
||||
view(_G) - print content of the table `_G` (current environment)
|
||||
standard libraries (math, string, etc.) are loaded by default but not visible in `_G`
|
||||
view(types, 2) - print table `types` (i.e. `openmw.types`) and its subtables (2 - traversal depth)]]
|
||||
ui.printToConsole(msg, ui.CONSOLE_COLOR.Info)
|
||||
end
|
||||
|
||||
local function printToConsole(...)
|
||||
local strs = {}
|
||||
for i = 1, select('#', ...) do
|
||||
strs[i] = tostring(select(i, ...))
|
||||
end
|
||||
return ui.printToConsole(table.concat(strs, '\t'), ui.CONSOLE_COLOR.Info)
|
||||
end
|
||||
|
||||
local function printRes(...)
|
||||
if select('#', ...) >= 0 then
|
||||
printToConsole(...)
|
||||
end
|
||||
end
|
||||
|
||||
local currentSelf = nil
|
||||
local currentMode = ''
|
||||
|
||||
local function exitLuaMode()
|
||||
currentSelf = nil
|
||||
currentMode = ''
|
||||
ui.setConsoleMode('')
|
||||
ui.printToConsole('Lua mode OFF', ui.CONSOLE_COLOR.Success)
|
||||
end
|
||||
|
||||
local function setContext(obj)
|
||||
ui.printToConsole('Lua mode ON, use exit() to return, help() for more info', ui.CONSOLE_COLOR.Success)
|
||||
if obj == self then
|
||||
currentMode = 'Lua[Player]'
|
||||
ui.printToConsole('Context: Player', ui.CONSOLE_COLOR.Success)
|
||||
elseif obj then
|
||||
if not obj:isValid() then error('Object not available') end
|
||||
currentMode = 'Lua['..obj.recordId..']'
|
||||
ui.printToConsole('Context: Local['..tostring(obj)..']', ui.CONSOLE_COLOR.Success)
|
||||
else
|
||||
currentMode = 'Lua[Global]'
|
||||
ui.printToConsole('Context: Global', ui.CONSOLE_COLOR.Success)
|
||||
end
|
||||
currentSelf = obj
|
||||
ui.setConsoleMode(currentMode)
|
||||
end
|
||||
|
||||
local function setSelected(obj)
|
||||
local ok, err = pcall(function() ui.setConsoleSelectedObject(obj) end)
|
||||
if ok then
|
||||
ui.printToConsole('Selected object changed', ui.CONSOLE_COLOR.Success)
|
||||
else
|
||||
ui.printToConsole(err, ui.CONSOLE_COLOR.Error)
|
||||
end
|
||||
end
|
||||
|
||||
local env = {
|
||||
I = require('openmw.interfaces'),
|
||||
util = require('openmw.util'),
|
||||
storage = require('openmw.storage'),
|
||||
core = require('openmw.core'),
|
||||
types = require('openmw.types'),
|
||||
async = require('openmw.async'),
|
||||
nearby = require('openmw.nearby'),
|
||||
self = require('openmw.self'),
|
||||
input = require('openmw.input'),
|
||||
ui = require('openmw.ui'),
|
||||
camera = require('openmw.camera'),
|
||||
aux_util = require('openmw_aux.util'),
|
||||
view = require('openmw_aux.util').deepToString,
|
||||
print = printToConsole,
|
||||
exit = exitLuaMode,
|
||||
help = printHelp,
|
||||
}
|
||||
env._G = env
|
||||
setmetatable(env, {__index = _G, __metatable = false})
|
||||
_G = nil
|
||||
|
||||
local function executeLuaCode(code)
|
||||
local fn
|
||||
local ok, err = pcall(function() fn = util.loadCode('return ' .. code, env) end)
|
||||
if ok then
|
||||
ok, err = pcall(function() printRes(fn()) end)
|
||||
else
|
||||
ok, err = pcall(function() util.loadCode(code, env)() end)
|
||||
end
|
||||
if not ok then
|
||||
ui.printToConsole(err, ui.CONSOLE_COLOR.Error)
|
||||
end
|
||||
end
|
||||
|
||||
local function onConsoleCommand(mode, cmd, selectedObject)
|
||||
env.selected = selectedObject
|
||||
if mode == '' then
|
||||
cmd, arg = cmd:lower():match('(%w+) *(%w*)')
|
||||
if cmd == 'lua' then
|
||||
if arg == 'player' then
|
||||
cmd = 'luap'
|
||||
elseif arg == 'global' then
|
||||
cmd = 'luag'
|
||||
elseif arg == 'selected' then
|
||||
cmd = 'luas'
|
||||
else
|
||||
local msg = [[
|
||||
Usage: 'lua player' or 'luap' - enter player context
|
||||
'lua global' or 'luag' - enter global context
|
||||
'lua selected' or 'luas' - enter local context on the selected object]]
|
||||
ui.printToConsole(msg, ui.CONSOLE_COLOR.Info)
|
||||
end
|
||||
end
|
||||
if cmd == 'luap' or (cmd == 'luas' and selectedObject == self.object) then
|
||||
setContext(self)
|
||||
elseif cmd == 'luag' then
|
||||
setContext()
|
||||
elseif cmd == 'luas' then
|
||||
if selectedObject then
|
||||
core.sendGlobalEvent('OMWConsoleStartLocal', {player=self.object, selected=selectedObject})
|
||||
else
|
||||
ui.printToConsole('No selected object', ui.CONSOLE_COLOR.Error)
|
||||
end
|
||||
end
|
||||
elseif mode == currentMode then
|
||||
if cmd == 'exit()' then
|
||||
exitLuaMode()
|
||||
elseif currentSelf == self then
|
||||
executeLuaCode(cmd)
|
||||
if env.selected ~= selectedObject then setSelected(env.selected) end
|
||||
elseif currentSelf then
|
||||
currentSelf:sendEvent('OMWConsoleEval', {player=self.object, code=cmd, selected=selectedObject})
|
||||
else
|
||||
core.sendGlobalEvent('OMWConsoleEval', {player=self.object, code=cmd, selected=selectedObject})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
engineHandlers = {onConsoleCommand = onConsoleCommand},
|
||||
eventHandlers = {
|
||||
OMWConsolePrint = function(msg) ui.printToConsole(tostring(msg), ui.CONSOLE_COLOR.Info) end,
|
||||
OMWConsoleError = function(msg) ui.printToConsole(tostring(msg), ui.CONSOLE_COLOR.Error) end,
|
||||
OMWConsoleSetContext = setContext,
|
||||
OMWConsoleSetSelected = setSelected,
|
||||
OMWConsoleExit = exitLuaMode,
|
||||
OMWConsoleHelp = printHelp,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue