Fix elem.loadDefault handling element tools wrong

Firstly, it would not remove custom element tools when appropriate, reproduce with

	elem.allocate("CUSTOM", "FAKE")
	elem.element(elem.CUSTOM_PT_FAKE, elem.element(elem.DEFAULT_PT_SLCN))
	elem.loadDefault()
	elem.allocate("CUSTOM", "FAKE")
	elem.element(elem.CUSTOM_PT_FAKE, elem.element(elem.DEFAULT_PT_SLCN))

There will be two FAKE element tools, one of them in the Tools section, called NULL, and looking very default. Broken since ff4500620e, where I assumed that builtinElements's size reflected the amount of enabled built-in elements. This has not been the case at all since dd875987b9, where GetElements was made to return a PT_NUM-sized array instead of a vector with only built-in elements, and so its size increased to PT_NUM, including all possible element numbers. But, strictly speaking, even before that it had not been a correct assumption, because some entries (e.g. 146) of builtinElements could have been disabled, but were possible to allocate from Lua.

Secondly, it would not update built-in element tools when appropriate, reproduce with

	elem.property(elem.DEFAULT_PT_SLCN, "Name", "FAKE")
	elem.loadDefault()

SLCN's tool will still be called FAKE. Also broken since ff4500620e, where I neglected to deal with element tools whose elements got updated under them. At the time this would have been done by calling AllocElementTool on them again, but today the appropriate way is UpdateElementTool.
This commit is contained in:
Tamás Bálint Misius
2025-03-19 19:33:58 +01:00
parent 91e4b0d173
commit 9992a26e7d

View File

@@ -787,13 +787,22 @@ static int loadDefault(lua_State *L)
lua_settable(L, -3);
manageElementIdentifier(L, id, false);
if (id < (int)builtinElements.size())
auto oldEnabled = elements[id].Enabled;
if (id < (int)builtinElements.size() && builtinElements[id].Enabled)
{
elements[id] = builtinElements[id];
}
else
{
elements[id] = Element();
}
// TODO: somehow unify element and corresponding element tool management in a way that makes it hard to mess up
if (oldEnabled && elements[id].Enabled)
{
lsi->gameModel->UpdateElementTool(id);
}
else if (oldEnabled && !elements[id].Enabled)
{
lsi->gameModel->FreeTool(lsi->gameModel->GetToolFromIdentifier(identifier));
}
manageElementIdentifier(L, id, true);