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
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 = {
drawtype = "nodebox",
@ -7,16 +10,14 @@ local carpet_proto = {
node_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -0.45, 0.5}
},
}
}
if config:get_bool("WoolFeeling") then
local wool = minetest.registered_nodes["wool:white"]
carpet_proto.sounds = wool.sounds
carpet_proto.groups = wool.groups
carpet_proto.sounds = default.node_sound_defaults()
carpet_proto.groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, flammable = 3}
end
function carpets.register(recipe, def)
local node = {}
@ -34,6 +35,7 @@ function carpets.register(recipe, def)
node.description = node.description or recipe_def.description .. " Carpet"
node.tiles = node.tiles or recipe_def.tiles
if node.tiles[6] then -- prefer "front" site for carpet
node.tiles = {node.tiles[6]}
end
@ -55,13 +57,15 @@ function carpets.register(recipe, def)
if config:get_bool("NoFlyCarpet") then
local previous_on_place = node.on_place or minetest.item_place
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 under = pointed_thing.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
end
return previous_on_place(itemstack, placer, pointed_thing, ...)
@ -72,7 +76,9 @@ function carpets.register(recipe, def)
minetest.register_craft({
output = name .. " 32",
recipe = {{"group:wool", "group:wool", "group:wool"},
{"group:wool", recipe, "group:wool"}}
recipe = {
{"group:wool", "group:wool", "group:wool"},
{"group:wool", recipe, "group:wool"}
}
})
end

@ -1,14 +1,32 @@
carpets = {}
dofile(minetest.get_modpath("carpets").."/carpet_api.lua")
dofile(minetest.get_modpath("carpets").."/modutils.lua")
local modname = minetest.get_current_modname()
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
if depmod:check_depmod(name) == false then
if not depends[def.mod_origin] then
return false
end
@ -36,11 +54,9 @@ local function enabledfilter(name, def)
return true
end
------------------------------------------------
-- main execution
for name, def in pairs(minetest.registered_nodes) do
if enabledfilter(name, def) then
if filter(name, def) then
carpets.register(name)
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