specific minetest -> generic engine namespace

master
Beha 2017-09-19 10:08:57 -04:00
parent 95eec98b8f
commit 3d562f1e06
9 changed files with 40 additions and 37 deletions

View File

@ -5,24 +5,24 @@ bgml.config = {}
-- somemod.config_table['some_index'] = true
--- somemod.config_table.some_index == true
-- somemod.config_table['some_index'] = true
-- [minetest.conf] somemodname.some_index = false
-- [engine.conf] somemodname.some_index = false
--- somemod.config_table.some_index == false
function bgml.config.config_factory()
local modname = minetest.get_current_modname()
local modname = engine.get_current_modname()
-- This function wraps around minetest.setting_get[bool] to check if the default should be overriden.
-- This function wraps around engine.setting_get[bool] to check if the default should be overriden.
local function get(setting, default)
if type(default) == "boolean" then
local read = minetest.settings:get_bool(modname.."."..setting)
local read = engine.settings:get_bool(modname.."."..setting)
if read == nil then
return default
else
return read
end
elseif type(default) == "string" then
return minetest.settings:get(modname.."."..setting) or default
return engine.settings:get(modname.."."..setting) or default
elseif type(default) == "number" then
return tonumber(minetest.settings:get(modname.."."..setting) or default)
return tonumber(engine.settings:get(modname.."."..setting) or default)
else
error(("Unknown format for configuration key '%s': %s"):format(setting, type(default)))
end

View File

@ -32,9 +32,9 @@ function bgml.logging.log_factory(modname)
-- Message should be whichever is last not nil.
message = self:message(b or a)
if level then
return minetest.log(level, message)
return engine.log(level, message)
else
return minetest.log(message)
return engine.log(message)
end
end,
}

View File

@ -2,7 +2,7 @@ bgml.require("core/hooks")
bgml.require("core/logging")
-- Calls the mod beginning hooks, returns a fresh mod table.
function bgml.mod.begin()
local modname = minetest.get_current_modname()
local modname = engine.get_current_modname()
local modtable = {
require = bgml.mod.require_factory(),
ready = bgml.mod.ready_mod_factory(),
@ -19,7 +19,7 @@ end
-- Returns a function that calls the mod ready hooks.
function bgml.mod.ready_mod_factory()
local modname = minetest.get_current_modname()
local modname = engine.get_current_modname()
return function()
bgml.hooks.global:call("mod_ready", modname)
bgml.hooks.global:call("mod_ready:"..modname)

View File

@ -7,13 +7,13 @@ local c = bgml.internal.config
--- bgml.log_config must be the first default, so that other config defaults can output when set.
c.log_config = false
-- Log mod_ready hooks. Defaults to the somewhat common log_mods setting.
c.log_mods = minetest.settings:get_bool("log_mods") or false
c.log_mods = engine.settings:get_bool("log_mods") or false
-- Databases
-- Path in which to save database serializations.
c.db_path = minetest.get_worldpath() .. DIR_DELIM .. "bgml" .. DIR_DELIM .. "db"
c.db_path = engine.get_worldpath() .. DIR_DELIM .. "bgml" .. DIR_DELIM .. "db"
-- Default database save internal.
c.save_interval = 10
-- How many database files can be written per globalstep?

View File

@ -10,6 +10,9 @@ bgml = {
mod = {},
}
-- Engine alias namespace to whatever engine we're running on.
engine = minetest
local MAX_REQUIRE_ITERS = 10
-- First we need a way to load the rest of BGML. Thus the dofile wrapper system is in init.lua
@ -17,10 +20,10 @@ local MAX_REQUIRE_ITERS = 10
-- Generate the `require` function.
function bgml.mod.require_factory()
-- Attempt to locate our directory.
local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local modname = engine.get_current_modname()
local modpath = engine.get_modpath(modname)
if modpath == nil then
error("minetest.get_modpath for "..modname.." returned nil.")
error("engine.get_modpath for "..modname.." returned nil.")
end
-- Directory found, build the function.
@ -58,7 +61,7 @@ function bgml.mod.require_factory()
bgml["_require_"..modname](init_path)
end
-- Loop through everything in this directory and call the function again on it.
for _,n in ipairs(minetest.get_dir_list(full_path)) do
for _,n in ipairs(engine.get_dir_list(full_path)) do
if n ~= "init.lua" then
bgml["_require_"..modname](path .. DIR_DELIM .. n, iter + 1)
end
@ -68,7 +71,7 @@ function bgml.mod.require_factory()
end
-- The fsutils must be loaded manually in order to use BGML's require.
local fsutils_path = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELIM .. "utils" .. DIR_DELIM .. "fs.lua"
local fsutils_path = engine.get_modpath(engine.get_current_modname()) .. DIR_DELIM .. "utils" .. DIR_DELIM .. "fs.lua"
bgml._loaded[fsutils_path] = true
dofile(fsutils_path)

View File

@ -2,7 +2,7 @@ bgml.entities = {
loaded = {},
}
minetest.register_globalstep(function(dtime)
engine.register_globalstep(function(dtime)
local to_unload = {}
for id,v in pairs(bgml.entities.loaded) do
-- If the object is inaccessible, and therefore unloaded:
@ -22,7 +22,7 @@ minetest.register_globalstep(function(dtime)
end)
function bgml.register_entity(name, def)
minetest.register_entity(name, bgml.lutils.combine(def, {
engine.register_entity(name, bgml.lutils.combine(def, {
permanent = false,
autosave = true,
@ -37,16 +37,16 @@ function bgml.register_entity(name, def)
-- If an entity has no static data then destroy it.
if #staticdata <= 0 then
bgml.internal.log.error("[entities] Removed entity "..name.." due to empty staticdata at "..minetest.pos_to_string(self.object:getpos()))
bgml.internal.log.error("[entities] Removed entity "..name.." due to empty staticdata at "..engine.pos_to_string(self.object:getpos()))
self.object:remove()
return
end
-- Build extra/external properties from staticdata.
self.ext = minetest.deserialize(staticdata)
self.ext = engine.deserialize(staticdata)
if type(self.ext) ~= "table" or not self.ext.id then
bgml.internal.log.error("[entities] Removed entity "..name.." due to invalid ext table at "..minetest.pos_to_string(self.object:getpos()))
bgml.internal.log.error("[entities] Removed entity "..name.." due to invalid ext table at "..engine.pos_to_string(self.object:getpos()))
self.object:remove()
return
end
@ -116,21 +116,21 @@ function bgml.register_entity(name, def)
end
-- Return the serialized staticdata, excluding unwanted parts.
return minetest.serialize(bgml.lutils.exclude(self.ext, {"param"}))
return engine.serialize(bgml.lutils.exclude(self.ext, {"param"}))
end,
}))
end
function bgml.add_entity(pos, name, param)
-- Build the parameters
local def = minetest.registered_entities[name]
local def = engine.registered_entities[name]
local ext = {
id = bgml.utils.uid(),
}
local staticdata = minetest.serialize(ext)
local staticdata = engine.serialize(ext)
-- Create the object. The staticdata will be passed to on_activate.
local obj = minetest.add_entity(pos, name, staticdata)
local obj = engine.add_entity(pos, name, staticdata)
-- If it was successful:
if obj then
-- Set the param, which can be accessed from on_creation or on_load.

View File

@ -2,7 +2,7 @@ local BLOCK_SIZE = 16
-- Get node, even if unloaded. Will load node's block.
function bgml.get_node(pos)
local node = minetest.get_node_or_nil(pos)
local node = engine.get_node_or_nil(pos)
if node then
return node
end
@ -19,13 +19,13 @@ end
-- Search for nodes in a box.
-- ...(origin, fromlen, names)
function bgml.get_nodes_around(...)
return get_nodes_x(minetest.find_nodes_in_area, ...)
return get_nodes_x(engine.find_nodes_in_area, ...)
end
-- Search for nodes under air in a box.
-- ...(origin, fromlen, names)
function bgml.get_nodes_around_under_air(...)
return get_nodes_x(minetest.find_nodes_in_area_under_air, ...)
return get_nodes_x(engine.find_nodes_in_area_under_air, ...)
end
-- Get the mapblock a node belongs to.

View File

@ -31,7 +31,7 @@ function dmt:save()
if not f then
error("Unable to save database to: "..self.tmppath)
end
f:write(minetest.serialize(self.data))
f:write(engine.serialize(self.data))
f:close()
if not os.rename(self.tmppath, self.path) then
error("Unable to rename temporary database to: "..self.path)
@ -68,7 +68,7 @@ end
setmetatable(bgml.db, {__call = function(self, ...) return bgml.db.new(...) end, __index = dmt})
minetest.register_globalstep(function(dtime)
engine.register_globalstep(function(dtime)
-- Check if any databases need added to the save queue.
for name,t in pairs(bgml.db.tables) do
if not bgml.db.save_pending[name] and (os.time() - t.last_save) > bgml.db.save_registry[name] then
@ -87,7 +87,7 @@ minetest.register_globalstep(function(dtime)
end)
-- Save all databases at shutdown time.
minetest.register_on_shutdown(function()
engine.register_on_shutdown(function()
bgml.hooks.global:call("db_shutdown_begin")
for name,t in pairs(bgml.db.tables) do
t:save()
@ -97,7 +97,7 @@ end)
if bgml.internal.config.db_cleaner then
bgml.hooks.global:add("db_shutdown_end", "bgml:db_cleaner", function()
for _,name in ipairs(minetest.get_dir_list(bgml.internal.config.db_path), false) do
for _,name in ipairs(engine.get_dir_list(bgml.internal.config.db_path), false) do
if not bgml.db.tables[name] then
if not os.remove(basepath(name)) then
error("Could not remove database: "..name)
@ -112,14 +112,14 @@ bgml.hooks.global:add("db_shutdown_end", "bgml:db_logger", function()
bgml.internal.log.info("[db] All databases saved due to shutdown.")
end)
minetest.mkdir(bgml.internal.config.db_path)
engine.mkdir(bgml.internal.config.db_path)
-- Preload all databases into the holding table.
local num = 0
for _,name in ipairs(minetest.get_dir_list(bgml.internal.config.db_path), false) do
for _,name in ipairs(engine.get_dir_list(bgml.internal.config.db_path), false) do
local f = io.open(basepath(name), "r")
if f then
bgml.db.load_hold[name] = minetest.deserialize(f:read("*all"))
bgml.db.load_hold[name] = engine.deserialize(f:read("*all"))
f:close()
else
error("Unreadable database in the db path: "..name)

View File

@ -24,7 +24,7 @@ end
local function is_x(path, type)
local dir, name = bgml.fsutils.split_path(path)
for _,n in ipairs(minetest.get_dir_list(dir, type)) do
for _,n in ipairs(engine.get_dir_list(dir, type)) do
if n == name then
return true
end