mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 22:11:51 +00:00
add loaders (WIP)
This commit is contained in:
parent
4b234eb661
commit
9ca809ff94
6 changed files with 100 additions and 10 deletions
|
|
@ -1,3 +1,6 @@
|
||||||
|
local app = __vc_app
|
||||||
|
local internals = __vc_internals
|
||||||
|
|
||||||
local user_props = file.read_combined_object("config/user-props.toml")
|
local user_props = file.read_combined_object("config/user-props.toml")
|
||||||
local names = {
|
local names = {
|
||||||
"parent", "caption", "texture", "texture-faces", "model", "model-name",
|
"parent", "caption", "texture", "texture-faces", "model", "model-name",
|
||||||
|
|
@ -42,7 +45,7 @@ local function make_read_only(t)
|
||||||
__newindex = function()
|
__newindex = function()
|
||||||
error("table is read-only")
|
error("table is read-only")
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
make_read_only(block.properties)
|
make_read_only(block.properties)
|
||||||
|
|
@ -85,3 +88,33 @@ cache_names(block)
|
||||||
cache_names(item)
|
cache_names(item)
|
||||||
|
|
||||||
__vc_scripts_registry.build_registry()
|
__vc_scripts_registry.build_registry()
|
||||||
|
|
||||||
|
local packs = app.get_content()
|
||||||
|
table.insert(packs, 1, "core")
|
||||||
|
|
||||||
|
for i, packid in ipairs(packs) do
|
||||||
|
local loaders_dir = packid..":scripts/loaders"
|
||||||
|
if not file.isdir(loaders_dir) then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
local files = file.list(loaders_dir)
|
||||||
|
for _, path in ipairs(files) do
|
||||||
|
if file.ext(path) == "json" then
|
||||||
|
debug.log("registering loader "..path)
|
||||||
|
local dto = json.parse(file.read(path))
|
||||||
|
local module = setmetatable({}, {__index=internals.get_pack_env(packid)})
|
||||||
|
local module_path = file.join(file.parent(path), file.stem(path)..".lua")
|
||||||
|
if not file.exists(module_path) then
|
||||||
|
error("loader "..path.." missing module "..module_path)
|
||||||
|
end
|
||||||
|
load(file.read(module_path), module_path, "t", module)()
|
||||||
|
if rawget(module, "execute") == nil then
|
||||||
|
error("loader "..path.." missing function 'execute'")
|
||||||
|
end
|
||||||
|
if dto.type == "compiler" then
|
||||||
|
internals.register_compiler(packid, dto.extensions, module)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
::continue::
|
||||||
|
end
|
||||||
|
|
|
||||||
|
|
@ -392,6 +392,7 @@ end
|
||||||
|
|
||||||
ffi = nil
|
ffi = nil
|
||||||
__vc_app = nil
|
__vc_app = nil
|
||||||
|
__vc_internals = nil
|
||||||
__vc_lock_internal_modules()
|
__vc_lock_internal_modules()
|
||||||
__vc_lock_internal_modules = nil
|
__vc_lock_internal_modules = nil
|
||||||
__vc_update_coroutines = nil
|
__vc_update_coroutines = nil
|
||||||
|
|
|
||||||
|
|
@ -154,6 +154,20 @@ package = {
|
||||||
}
|
}
|
||||||
local __cached_scripts = {}
|
local __cached_scripts = {}
|
||||||
local __warnings_hidden = {}
|
local __warnings_hidden = {}
|
||||||
|
local __compilers = {}
|
||||||
|
|
||||||
|
function __vc_internals.register_compiler(sourcepack, extensions, module)
|
||||||
|
local compiler = {
|
||||||
|
packid = sourcepack,
|
||||||
|
extensions = extensions,
|
||||||
|
module = module,
|
||||||
|
}
|
||||||
|
for i, ext in ipairs(extensions) do
|
||||||
|
if not __compilers[ext] then
|
||||||
|
__compilers[ext] = compiler
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function on_deprecated_call(name, alternatives)
|
function on_deprecated_call(name, alternatives)
|
||||||
if __warnings_hidden[name] then
|
if __warnings_hidden[name] then
|
||||||
|
|
@ -200,6 +214,21 @@ end
|
||||||
|
|
||||||
local __internal_locked = false
|
local __internal_locked = false
|
||||||
|
|
||||||
|
local default_compiler = {
|
||||||
|
module = {
|
||||||
|
execute = function(code, path, env)
|
||||||
|
local script, err = load(code, path)
|
||||||
|
if script == nil then
|
||||||
|
error(err)
|
||||||
|
end
|
||||||
|
if env then
|
||||||
|
script = setfenv(script, env)
|
||||||
|
end
|
||||||
|
return script
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
-- Load script with caching
|
-- Load script with caching
|
||||||
--
|
--
|
||||||
-- path - script path `contentpack:filename`.
|
-- path - script path `contentpack:filename`.
|
||||||
|
|
@ -211,6 +240,9 @@ function __load_script(path, nocache, env)
|
||||||
local is_internal = (packname == "res" or packname == "core")
|
local is_internal = (packname == "res" or packname == "core")
|
||||||
and filename:find("modules/internal") == 1
|
and filename:find("modules/internal") == 1
|
||||||
|
|
||||||
|
local ext = path:match("%.([^:/\\]+)$")
|
||||||
|
local compiler = __compilers[ext] or default_compiler
|
||||||
|
|
||||||
nocache = nocache or is_internal
|
nocache = nocache or is_internal
|
||||||
|
|
||||||
if is_internal and __internal_locked then
|
if is_internal and __internal_locked then
|
||||||
|
|
@ -225,13 +257,7 @@ function __load_script(path, nocache, env)
|
||||||
error("script '"..filename.."' not found in '"..packname.."'")
|
error("script '"..filename.."' not found in '"..packname.."'")
|
||||||
end
|
end
|
||||||
|
|
||||||
local script, err = load(file.read(path), path)
|
local script = compiler.module.execute(file.read(path), path, env)
|
||||||
if script == nil then
|
|
||||||
error(err)
|
|
||||||
end
|
|
||||||
if env then
|
|
||||||
script = setfenv(script, env)
|
|
||||||
end
|
|
||||||
local result = script()
|
local result = script()
|
||||||
if not nocache then
|
if not nocache then
|
||||||
__cached_scripts[path] = script
|
__cached_scripts[path] = script
|
||||||
|
|
@ -245,6 +271,9 @@ function __vc_lock_internal_modules()
|
||||||
end
|
end
|
||||||
|
|
||||||
local __pack_envs = __vc__pack_envs
|
local __pack_envs = __vc__pack_envs
|
||||||
|
function __vc_internals.get_pack_env(packid)
|
||||||
|
return __pack_envs[packid]
|
||||||
|
end
|
||||||
__vc__pack_envs = nil
|
__vc__pack_envs = nil
|
||||||
|
|
||||||
function require(path)
|
function require(path)
|
||||||
|
|
@ -277,6 +306,13 @@ function __scripts_cleanup(non_reset_packs)
|
||||||
__pack_envs[packname] = nil
|
__pack_envs[packname] = nil
|
||||||
::continue::
|
::continue::
|
||||||
end
|
end
|
||||||
|
for ext, compiler in pairs(__compilers) do
|
||||||
|
if table.has(non_reset_packs, compiler.packid) then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
__compilers[ext] = nil
|
||||||
|
::continue::
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function __vc__error(msg, frame, n, lastn)
|
function __vc__error(msg, frame, n, lastn)
|
||||||
|
|
|
||||||
|
|
@ -220,7 +220,6 @@ void AssetsLoader::processPreloadList(AssetType tag, const dv::value& list) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetsLoader::processPreloadConfig(const io::path& file) {
|
void AssetsLoader::processPreloadConfig(const io::path& file) {
|
||||||
logger.debug() << "===== PRELOAD FILE: " << file.string();
|
|
||||||
auto root = io::read_json(file);
|
auto root = io::read_json(file);
|
||||||
processPreloadList(AssetType::ATLAS, root["atlases"]);
|
processPreloadList(AssetType::ATLAS, root["atlases"]);
|
||||||
processPreloadList(AssetType::FONT, root["fonts"]);
|
processPreloadList(AssetType::FONT, root["fonts"]);
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,11 @@ static void create_libs(State* L, StateType stateType) {
|
||||||
getglobal(L, "__vc_app");
|
getglobal(L, "__vc_app");
|
||||||
setregistry(L, "app");
|
setregistry(L, "app");
|
||||||
|
|
||||||
|
createtable(L, 0, 0);
|
||||||
|
pushvalue(L, -1);
|
||||||
|
setglobal(L, "__vc_internals");
|
||||||
|
setregistry(L, "internals");
|
||||||
|
|
||||||
if (stateType == StateType::SCRIPT) {
|
if (stateType == StateType::SCRIPT) {
|
||||||
getregistry(L, "app");
|
getregistry(L, "app");
|
||||||
setglobal(L, "app");
|
setglobal(L, "app");
|
||||||
|
|
|
||||||
|
|
@ -288,7 +288,23 @@ void scripting::on_content_load(Content* content) {
|
||||||
lua::setfield(L, "properties");
|
lua::setfield(L, "properties");
|
||||||
lua::pop(L);
|
lua::pop(L);
|
||||||
}
|
}
|
||||||
load_script("post_content.lua", true);
|
|
||||||
|
lua::getregistry(L, "app");
|
||||||
|
lua::setglobal(L, "__vc_app");
|
||||||
|
lua::getregistry(L, "internals");
|
||||||
|
lua::setglobal(L, "__vc_internals");
|
||||||
|
try {
|
||||||
|
load_script("post_content.lua", true);
|
||||||
|
} catch (const std::exception&) {
|
||||||
|
lua::pushnil(L);
|
||||||
|
lua::setglobal(L, "__vc_app");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
lua::pushnil(L);
|
||||||
|
lua::setglobal(L, "__vc_app");
|
||||||
|
lua::pushnil(L);
|
||||||
|
lua::setglobal(L, "__vc_internals");
|
||||||
|
|
||||||
load_script("stdcmd.lua", true);
|
load_script("stdcmd.lua", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue