So... I have been developing this mod on 2.0.7. Yeah I know, not the best idea to make mods for an old version.
Well, turns out that some recent version completely broke undo patching. And nobody told me!
To explain, in 2.0.7 the following things happened when a script quick replaced an entity with another:
- Add a new undo step
+ built-entity <- New Entity
+ removed-entity <- Old Entity that got removed
In 2.0.28 now this happens instead:
- Remove all undo steps that were referring to the old entity.
- Add a new undo step
+ built-entity <- New Entity
+ removed-entity <- Old Entity that got removed
So what I did in 2.0.7 was:
- Update all previous entries to refer to the new entity.
- Remove the two new entries.
Undo history remains intact, everyone is happy.
In 2.0.28 this is not possible. As soon as the entity stops existing, all the undo steps referencing the entity stop existing as well. So there are no undo steps to patch in the first place. If I remove the two new entries the entity simply ceases to be registered in the undo/redo stack. This happens regardless of how the entity was removed by a script (destroy or quick replace both yield the same result).
The API for interacting with the undo/redo stack is awful, you can only remove entries, not add any. I am not even sure if the patching mechanism itself hasn't stopped working as well as it might have been unintended.
It seems there is absolutely no way to make this mod work properly with Undo/Redo in the newer versions of Factorio.