raiguard / RecipeBook

Recipe Book for Factorio.
MIT License
20 stars 9 forks source link

Ignore invalid prototypes during build phase #81

Closed OmarTawfik closed 2 years ago

OmarTawfik commented 2 years ago

Thanks for the awesome mod!

I recently encountered these errors while loading my save file:

From the official API:

Is this object valid? This Lua object holds a reference to an object within the game engine. It is possible that the game-engine object is removed whilst a mod still holds the corresponding Lua object. If that happens, the object becomes invalid, i.e. this attribute will be false. Mods are advised to check for object validity if any change to the game state might have occurred between the creation of the Lua object and its access.

This PR adds the validity checks in two places they were missing. I tested it by modifying my local copy of the mod and reloading. Errors are gone. In any case, ignoring an invalid entity is significantly better than crashing during load.

raiguard commented 2 years ago

So, the reason why the mod doesn't do any checking like this it's because there is exactly one circumstance where this error can happen: when a mod's content changes without its version changing.

The reason why adding checks like this doesn't work is because everything in the recipe book is interconnected. If you don't process a fluid because it's invalid, a recipe will have that fluid as an ingredient and that creates inconsistent state in the recipe book data. The way I did it now was an intentional design trade-off, because otherwise you would have to add a boatload of checks absolutely everywhere in the code to catch all of those situations. And now you have recipes that are missing some ingredients, because you removed them since they were invalid. It just breaks the whole thing.

Did you come across a crash like this in a normal playthrough? If you did, I would very much like to know, since that should never happen. The only way this should ever happen is if you are a mod developer who is actively developing a content mod.

OmarTawfik commented 2 years ago

Thank you for clarifying! That was not clear to me reading the docs. In my case, I was indeed working on a mod, but it has been stable for a few reloads before that crash happened. I'll close this for now, and will reopen if I get a repro in a clean state.

raiguard commented 2 years ago

I can probably add a mode for mod devs to avoid this crash. I'll let you know if I get something. It will significantly increase the size of the mod's global table, but will prevent it from crashing when stuff changes.

OmarTawfik commented 2 years ago

I think if it only a local (dev-time) error, maybe it won't justify the cost of implementing this on your end. In case we run into this again, can you please provide a workaround? is there a certain cache/data file to clear?

raiguard commented 2 years ago

I do not believe there is a way to avoid this without simply disabling the mod. A proper fix would require code changes. It is a shame, because this mod is very useful for verifying that content mods are working as expected.