customnode/framework.lua
2017-04-20 19:32:21 +02:00

185 lines
4.6 KiB
Lua

----------------------------------------------------
-- API Call - register new variant
----------------------------------------------------
function customnode.register_variant(name, template)
local modname = minetest.get_current_modname()
if name:sub(1,1) == ":" then
name = name:sub(1)
elseif name:sub(1,modname:len()+1) ~= modname..":" then
error(name.." does not match naming conventions")
end
local def = setmetatable({}, customnode.variant_class)
def.__index = customnode.variant_class
-- set variant name
def.name = name
-- set template
if not template then
template = "default"
end
if type(template) == "string" then
assert(customnode._variants[template], "unknown template: "..template)
def.template = customnode._variants[template].template
def.tasks = customnode._variants[template].tasks
else
if name == "customnode:default" then
def.template = {}
def.tasks = {}
else
def.template = table.copy(customnode._variants["customnode:default"].template or {})
def.tasks = table.copy(customnode._variants["customnode:default"].tasks or {})
end
if template then
for k,v in pairs(template) do
if k == "tasks" then
for _, s in ipairs(v) do
def:add_task(s)
end
elseif k == "skip" then
def.skip = v
else
def.template[k] = v
end
end
end
end
customnode._variants[name] = def
return def
end
----------------------------------------------------
-- API Call - register task
----------------------------------------------------
function customnode.register_task(name, task)
if customnode._tasks[name] then
error(name.." is already registered task")
end
customnode._tasks[name] = task
end
----------------------------------------------------
-- read textures folder and get a generator list
----------------------------------------------------
function customnode.get_nodelist_by_textures(conf)
local tile_mapping = {
top = "top",
bottom = "bottom",
down = "bottom",
right = "right",
left = "left",
back = "back",
inner = "back",
front = "front",
side = "all",
normal = "all",
}
local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local files = minetest.get_dir_list(modpath.."/textures")
local customnode_list = {}
-- syntax is modename_[prefix_][name_][type_][tiletype].xyz
for _, v in ipairs(files) do
local index = 0
local skip = false
local basename
local variant
local variant_shortname
local tiletype
for part in v:gmatch("([^_]+)") do -- split filename by "_"
index = index + 1
-- remove file ending in last part
part = part:gsub('%....$','')
-- check modname namespace
if index == 1 then
if part ~= modname then
skip = true
break
end
-- check additional namespace
elseif index == 2 and conf.check_prefix then
if part ~= conf.check_prefix then
skip = true
break
end
-- process tile mapping
elseif tile_mapping[part] then
tiletype = tile_mapping[part]
else
if not variant and customnode._variants[modname..":"..part] then
variant = customnode._variants[modname..":"..part]
variant_shortname = part
elseif not variant and customnode._variants["customnode:"..part] then
variant = customnode._variants["customnode:"..part]
variant_shortname = part
end
if not basename then
basename = part
else
basename = basename.."_"..part
end
end
end
if variant and variant.skip then
skip = variant.skip
end
if not skip then
-- default tiletype
if not tiletype then
tiletype = "all"
end
if not basename then
basename = modname
end
-- default generator
if not variant then
variant = customnode._variants["customnode:default"]
variant_shortname = "default"
end
-- Add information to the customnode
if not customnode_list[basename] then
customnode_list[basename] = variant:new_generator({
modname = modname,
basename = basename,
variant_shortname = variant_shortname,
conf = conf,
})
end
customnode_list[basename].img_tiles[tiletype] = v
end
end
return customnode_list
end
----------------------------------------------------
-- API Call read textures folder and create all configured tasks
----------------------------------------------------
function customnode.add_nodes_from_textures(conf)
local generator_list = customnode.get_nodelist_by_textures(conf or {})
for name, generator in pairs(generator_list) do
local nodedef = generator:get_nodedef()
if nodedef then
for _, task in ipairs(generator.variant.tasks) do
task(nodedef, generator)
end
end
end
end