aboutsummaryrefslogtreecommitdiff
path: root/advtrains/wagons.lua
diff options
context:
space:
mode:
authorY. Wang <y5nw@protonmail.com>2024-09-19 18:39:19 +0000
committerorwell <orwell@bleipb.de>2024-11-09 22:09:00 +0100
commit66868e2eefe97b7a6d35fa3fecc895b9c7bbc4cc (patch)
tree5fdad135e06648875d9a050852c98e215192531d /advtrains/wagons.lua
parent86e80e4cfb0d7017ff962e40ab99a4ab6addf8f2 (diff)
downloadadvtrains-66868e2eefe97b7a6d35fa3fecc895b9c7bbc4cc.tar.gz
advtrains-66868e2eefe97b7a6d35fa3fecc895b9c7bbc4cc.tar.bz2
advtrains-66868e2eefe97b7a6d35fa3fecc895b9c7bbc4cc.zip
Address wagon aliasing issues
As it turns out, not fully testing new features is not necessarily a good idea ... This patch follows up 1F616EMO's patch by * Making get_wagon_prototype return the resolved alias, * Handling recursive wagon alises (in particular, loops), and * Adding (partial) unittest for the wagon aliasing system. [v2]: The testcases are complemented a bit more to cover situations where the alias resolution system should return nil. [v2]: This patch should hopefully also warn about not spawning wagons. Note that this only warns about the missing wagon entity and does _not_ actually fix the issue. How to test: * In a world with both advtrains_train_subway and advtrains_train_japan enabled, place a subway wagon, a Japanese engine, and a regular Japanese wagon. * Add the test mod to the world; do NOT remove advtrains_train_japan. * Restart the world. Notice that the Japanese wagons still appear as Japanese wagons despite being aliased to subway wagons. * Restart the world without the advtrains_train_japan mod. Notice that the engine appears as the subway wagon while the regular Japanese wagon appears as the wagon placeholder. [v2]: Also note that the warning message about the missing wagon prototype still mentions the regular Japanese wagon. * Restart the world again with the advtrains_train_japan mod. Notice that both type of Japanese wagons reappear as Japanese wagons. * Observe that unittests work. Test mod: advtrains.register_wagon_alias("advtrains:engine_japan", "advtrains:subway_wagon") advtrains.register_wagon_alias("advtrains:wagon_japan", "advtrains:wagon_japan")
Diffstat (limited to 'advtrains/wagons.lua')
-rw-r--r--advtrains/wagons.lua35
1 files changed, 23 insertions, 12 deletions
diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua
index 50ce4ab..8bb12ee 100644
--- a/advtrains/wagons.lua
+++ b/advtrains/wagons.lua
@@ -16,15 +16,8 @@ advtrains.wagons = {}
advtrains.wagon_alias = {}
advtrains.wagon_prototypes = setmetatable({}, {
__index = function(t, k)
- local rtn_val = rawget(t, k)
- if rtn_val ~= nil then
- return rtn_val
- end
- local alias = advtrains.wagon_alias[k]
- if alias then
- return rawget(t, alias)
- end
- return nil
+ local _, proto = advtrains.resolve_wagon_alias(k)
+ return proto
end
})
advtrains.wagon_objects = {}
@@ -1338,17 +1331,35 @@ function advtrains.get_wagon_prototype(data)
data.type = data.entity_name
data.entity_name = nil
end
- if not wt or not advtrains.wagon_prototypes[wt] then
+ local rt, proto = advtrains.resolve_wagon_alias(wt)
+ if not rt then
atwarn("Unable to load wagon type",wt,", using placeholder")
- wt="advtrains:wagon_placeholder"
+ rt = "advtrains:wagon_placeholder"
+ proto = advtrains.wagon_prototypes[rt]
end
- return wt, advtrains.wagon_prototypes[wt]
+ return rt, proto
end
function advtrains.register_wagon_alias(src, dst)
advtrains.wagon_alias[src] = dst
end
+local function recursive_resolve_alias(name, seen)
+ local prototype = rawget(advtrains.wagon_prototypes, name)
+ if prototype then
+ return name, prototype
+ end
+ local resolved = advtrains.wagon_alias[name]
+ if resolved and not seen[resolved] then
+ seen[name] = true
+ return recursive_resolve_alias(resolved, seen)
+ end
+end
+
+function advtrains.resolve_wagon_alias(name)
+ return recursive_resolve_alias(name, {})
+end
+
function advtrains.standard_inventory_formspec(self, pname, invname)
--[[minetest.chat_send_player(pname, string.format("self=%s, pname=%s, invname=%s", self, pname, invname))
for k,v in pairs(self) do