Simplify checking depend

This commit is contained in:
Rui 2016-11-05 15:17:08 +09:00 committed by GitHub
parent 556008b68f
commit ffb24d285f
4 changed files with 59 additions and 107 deletions

@ -1,3 +1,3 @@
# minetest-carpets # minetest-carpets
see https://forum.minetest.net/viewtopic.php?f=9&t=15459#p232311 See https://forum.minetest.net/viewtopic.php?t=15459

@ -1,4 +1,7 @@
local config = Settings(minetest.get_modpath("carpets").."/settings.txt") local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local config = Settings(modpath .. "/settings.txt")
local carpet_proto = { local carpet_proto = {
drawtype = "nodebox", drawtype = "nodebox",
@ -7,16 +10,14 @@ local carpet_proto = {
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -0.45, 0.5} fixed = {-0.5, -0.5, -0.5, 0.5, -0.45, 0.5}
}, }
} }
if config:get_bool("WoolFeeling") then if config:get_bool("WoolFeeling") then
local wool = minetest.registered_nodes["wool:white"] carpet_proto.sounds = default.node_sound_defaults()
carpet_proto.sounds = wool.sounds carpet_proto.groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, flammable = 3}
carpet_proto.groups = wool.groups
end end
function carpets.register(recipe, def) function carpets.register(recipe, def)
local node = {} local node = {}
@ -34,6 +35,7 @@ function carpets.register(recipe, def)
node.description = node.description or recipe_def.description .. " Carpet" node.description = node.description or recipe_def.description .. " Carpet"
node.tiles = node.tiles or recipe_def.tiles node.tiles = node.tiles or recipe_def.tiles
if node.tiles[6] then -- prefer "front" site for carpet if node.tiles[6] then -- prefer "front" site for carpet
node.tiles = {node.tiles[6]} node.tiles = {node.tiles[6]}
end end
@ -55,13 +57,15 @@ function carpets.register(recipe, def)
if config:get_bool("NoFlyCarpet") then if config:get_bool("NoFlyCarpet") then
local previous_on_place = node.on_place or minetest.item_place local previous_on_place = node.on_place or minetest.item_place
node.on_place = function(itemstack, placer, pointed_thing, ...) node.on_place = function(itemstack, placer, pointed_thing, ...)
if not pointed_thing then return end if not pointed_thing then
return
end
local above = pointed_thing.above local above = pointed_thing.above
local under = pointed_thing.under local under = pointed_thing.under
local under_node = minetest.get_node(under) local under_node = minetest.get_node(under)
if above.y == under.y + 1 and under_node.name == name then if above.y == (under.y + 1) and under_node.name == name then
return return
end end
return previous_on_place(itemstack, placer, pointed_thing, ...) return previous_on_place(itemstack, placer, pointed_thing, ...)
@ -72,7 +76,9 @@ function carpets.register(recipe, def)
minetest.register_craft({ minetest.register_craft({
output = name .. " 32", output = name .. " 32",
recipe = {{"group:wool", "group:wool", "group:wool"}, recipe = {
{"group:wool", recipe, "group:wool"}} {"group:wool", "group:wool", "group:wool"},
{"group:wool", recipe, "group:wool"}
}
}) })
end end

@ -1,14 +1,32 @@
carpets = {} carpets = {}
dofile(minetest.get_modpath("carpets").."/carpet_api.lua") local modname = minetest.get_current_modname()
dofile(minetest.get_modpath("carpets").."/modutils.lua") local modpath = minetest.get_modpath(modname)
depmod = carpets.modutils.get_depmod("carpets") dofile(modpath .. "/api.lua")
local depends = (function()
local file = io.open(modpath .. "/depends.txt")
if not file then
return
end
local function enabledfilter(name, def) local depends = {}
for line in file:lines() do
if line:sub(-1) == "?" then
line = line:sub(1, -2)
end
depends[line] = true
end
file:close()
return depends
end)()
local function filter(name, def)
-- disable carpets from loaded modules but not defined in dependency -- disable carpets from loaded modules but not defined in dependency
if depmod:check_depmod(name) == false then if not depends[def.mod_origin] then
return false return false
end end
@ -36,11 +54,9 @@ local function enabledfilter(name, def)
return true return true
end end
------------------------------------------------
-- main execution -- main execution
for name, def in pairs(minetest.registered_nodes) do for name, def in pairs(minetest.registered_nodes) do
if enabledfilter(name, def) then if filter(name, def) then
carpets.register(name) carpets.register(name)
end end
end end

@ -1,70 +0,0 @@
-- not modutils related, will be moved to a framework, if the utils framework will be developed :/
-- The envroot is accessable by global variable with module name as name (like if used in "carpets" mod, the modutils are at carpets.modutils available)
-- Different per module environment root allow the use of diffent versions of the same tool in different mods
local currentmod = minetest.get_current_modname()
local envroot = nil
if not currentmod or currentmod == "" then --not minetest or something hacky
envroot = _G --fallback for hacky calls, populate global
else
if not _G[currentmod] then
_G[currentmod] = {}
end
envroot = _G[currentmod]
end
-- framework stuff done. -- Now the tool
----------------------------------------
envroot.modutils = {}
-- Definition of returning object methods
local _check_depmod = function(this, checknode)
-- check if the node (checknode) is from dependent module
local delimpos = string.find(checknode, ":")
if delimpos then
local checkmodname = string.sub(checknode, 1, delimpos - 1)
for name, ref in pairs(this.deplist) do
if name == checkmodname then
return true
end
end
return false
end
end
function envroot.modutils.get_depmod(modname)
-- Definition of returning object attributes
local this = { }
this.modname = modname
this.deplist = {} -- depends.txt parsed
this.check_depmod = _check_depmod -- method
-- get module path
local modpath = minetest.get_modpath(modname)
if not modpath then
return nil -- module not found
end
-- read the depends file
local dependsfile = io.open(modpath.."/depends.txt")
if not dependsfile then
return nil
end
-- parse the depends file
for dependsline in dependsfile:lines() do
local depentry = {} -- Entry in deplist
local depmodname
if string.sub(dependsline, -1) == "?" then
depentry.required = false
depmodname = string.sub(dependsline, 1, -2)
else
depentry.required = true
depmodname = dependsline
end
this.deplist[depmodname] = depentry
end
return this
end