move events library implementation to core:internal/events & disable access to core:internal modules outside of stdlib
This commit is contained in:
parent
5785b9fdda
commit
3760fb86f7
@ -256,7 +256,7 @@ function open_file_in_editor(filename, line, mutable)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function on_open(mode)
|
function on_open(mode)
|
||||||
registry = require "core:internal/scripts_registry"
|
registry = __vc_scripts_registry
|
||||||
|
|
||||||
document.codePanel:setInterval(200, refresh_file_title)
|
document.codePanel:setInterval(200, refresh_file_title)
|
||||||
|
|
||||||
|
|||||||
@ -43,11 +43,8 @@ function build_files_list(filenames, highlighted_part)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_open(mode)
|
function on_open()
|
||||||
registry = require "core:internal/scripts_registry"
|
registry = __vc_scripts_registry
|
||||||
|
|
||||||
local files_list = document.filesList
|
|
||||||
|
|
||||||
filenames = registry.filenames
|
filenames = registry.filenames
|
||||||
table.sort(filenames)
|
table.sort(filenames)
|
||||||
build_files_list(filenames)
|
build_files_list(filenames)
|
||||||
|
|||||||
48
res/modules/internal/events.lua
Normal file
48
res/modules/internal/events.lua
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
local events = {
|
||||||
|
handlers = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function events.on(event, func)
|
||||||
|
if events.handlers[event] == nil then
|
||||||
|
events.handlers[event] = {}
|
||||||
|
end
|
||||||
|
table.insert(events.handlers[event], func)
|
||||||
|
end
|
||||||
|
|
||||||
|
function events.reset(event, func)
|
||||||
|
if func == nil then
|
||||||
|
events.handlers[event] = nil
|
||||||
|
else
|
||||||
|
events.handlers[event] = {func}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function events.remove_by_prefix(prefix)
|
||||||
|
for name, handlers in pairs(events.handlers) do
|
||||||
|
local actualname = name
|
||||||
|
if type(name) == 'table' then
|
||||||
|
actualname = name[1]
|
||||||
|
end
|
||||||
|
if actualname:sub(1, #prefix+1) == prefix..':' then
|
||||||
|
events.handlers[actualname] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function events.emit(event, ...)
|
||||||
|
local result = nil
|
||||||
|
local handlers = events.handlers[event]
|
||||||
|
if handlers == nil then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
for _, func in ipairs(handlers) do
|
||||||
|
local status, newres = xpcall(func, __vc__error, ...)
|
||||||
|
if not status then
|
||||||
|
debug.error("error in event ("..event..") handler: "..newres)
|
||||||
|
else
|
||||||
|
result = result or newres
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
return events
|
||||||
@ -62,5 +62,4 @@ end
|
|||||||
cache_names(block)
|
cache_names(block)
|
||||||
cache_names(item)
|
cache_names(item)
|
||||||
|
|
||||||
local scripts_registry = require "core:internal/scripts_registry"
|
__vc_scripts_registry.build_registry()
|
||||||
scripts_registry.build_registry()
|
|
||||||
|
|||||||
@ -169,61 +169,12 @@ function inventory.set_description(invid, slot, description)
|
|||||||
inventory.set_data(invid, slot, "description", description)
|
inventory.set_data(invid, slot, "description", description)
|
||||||
end
|
end
|
||||||
|
|
||||||
------------------------------------------------
|
events = require "core:internal/events"
|
||||||
------------------- Events ---------------------
|
|
||||||
------------------------------------------------
|
|
||||||
events = {
|
|
||||||
handlers = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
function events.on(event, func)
|
|
||||||
if events.handlers[event] == nil then
|
|
||||||
events.handlers[event] = {}
|
|
||||||
end
|
|
||||||
table.insert(events.handlers[event], func)
|
|
||||||
end
|
|
||||||
|
|
||||||
function events.reset(event, func)
|
|
||||||
if func == nil then
|
|
||||||
events.handlers[event] = nil
|
|
||||||
else
|
|
||||||
events.handlers[event] = {func}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function events.remove_by_prefix(prefix)
|
|
||||||
for name, handlers in pairs(events.handlers) do
|
|
||||||
local actualname = name
|
|
||||||
if type(name) == 'table' then
|
|
||||||
actualname = name[1]
|
|
||||||
end
|
|
||||||
if actualname:sub(1, #prefix+1) == prefix..':' then
|
|
||||||
events.handlers[actualname] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function pack.unload(prefix)
|
function pack.unload(prefix)
|
||||||
events.remove_by_prefix(prefix)
|
events.remove_by_prefix(prefix)
|
||||||
end
|
end
|
||||||
|
|
||||||
function events.emit(event, ...)
|
|
||||||
local result = nil
|
|
||||||
local handlers = events.handlers[event]
|
|
||||||
if handlers == nil then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
for _, func in ipairs(handlers) do
|
|
||||||
local status, newres = xpcall(func, __vc__error, ...)
|
|
||||||
if not status then
|
|
||||||
debug.error("error in event ("..event..") handler: "..newres)
|
|
||||||
else
|
|
||||||
result = result or newres
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
gui_util = require "core:internal/gui_util"
|
gui_util = require "core:internal/gui_util"
|
||||||
|
|
||||||
Document = gui_util.Document
|
Document = gui_util.Document
|
||||||
@ -319,11 +270,12 @@ entities.get_all = function(uids)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local bytearray = require "core:internal/bytearray"
|
local bytearray = require "core:internal/bytearray"
|
||||||
|
|
||||||
Bytearray = bytearray.FFIBytearray
|
Bytearray = bytearray.FFIBytearray
|
||||||
Bytearray_as_string = bytearray.FFIBytearray_as_string
|
Bytearray_as_string = bytearray.FFIBytearray_as_string
|
||||||
Bytearray_construct = function(...) return Bytearray(...) end
|
Bytearray_construct = function(...) return Bytearray(...) end
|
||||||
|
|
||||||
|
__vc_scripts_registry = require "core:internal/scripts_registry"
|
||||||
|
|
||||||
file.open = require "core:internal/stream_providers/file"
|
file.open = require "core:internal/stream_providers/file"
|
||||||
file.open_named_pipe = require "core:internal/stream_providers/named_pipe"
|
file.open_named_pipe = require "core:internal/stream_providers/named_pipe"
|
||||||
|
|
||||||
@ -342,6 +294,7 @@ else
|
|||||||
end
|
end
|
||||||
|
|
||||||
ffi = nil
|
ffi = nil
|
||||||
|
__vc_lock_internal_modules()
|
||||||
|
|
||||||
math.randomseed(time.uptime() * 1536227939)
|
math.randomseed(time.uptime() * 1536227939)
|
||||||
|
|
||||||
|
|||||||
@ -550,6 +550,8 @@ function reload_module(name)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local internal_locked = false
|
||||||
|
|
||||||
-- Load script with caching
|
-- Load script with caching
|
||||||
--
|
--
|
||||||
-- path - script path `contentpack:filename`.
|
-- path - script path `contentpack:filename`.
|
||||||
@ -559,6 +561,11 @@ end
|
|||||||
function __load_script(path, nocache)
|
function __load_script(path, nocache)
|
||||||
local packname, filename = parse_path(path)
|
local packname, filename = parse_path(path)
|
||||||
|
|
||||||
|
if internal_locked and (packname == "res" or packname == "core")
|
||||||
|
and filename:starts_with("modules/internal") then
|
||||||
|
error("access to core:internal modules outside of [core]")
|
||||||
|
end
|
||||||
|
|
||||||
-- __cached_scripts used in condition because cached result may be nil
|
-- __cached_scripts used in condition because cached result may be nil
|
||||||
if not nocache and __cached_scripts[path] ~= nil then
|
if not nocache and __cached_scripts[path] ~= nil then
|
||||||
return package.loaded[path]
|
return package.loaded[path]
|
||||||
@ -579,6 +586,10 @@ function __load_script(path, nocache)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function __vc_lock_internal_modules()
|
||||||
|
internal_locked = true
|
||||||
|
end
|
||||||
|
|
||||||
function require(path)
|
function require(path)
|
||||||
if not string.find(path, ':') then
|
if not string.find(path, ':') then
|
||||||
local prefix, _ = parse_path(_debug_getinfo(2).source)
|
local prefix, _ = parse_path(_debug_getinfo(2).source)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user