[1.0] Duplicate operations under "Add bill" (AllRecipes bug)

Started by SineSwiper, November 04, 2019, 12:53:57 PM

Previous topic - Next topic

SineSwiper

NOTE: This is a vanilla bug because it's within the core Verse's library.

When a mod dynamically adds recipe defs (or add recipes to pawns) to the game, RimWorld proper has a tendency to display duplicate recipes within Add Bills.  Here is an example.  Yes, that's a lot of mods, but here me out:

The Verse.ThingDef.AllRecipes method creates a cached version of recipes by looking through this.recipes, populating its cache object, and then looking through DefDatabase<RecipeDef> to find recipeUsers.  In other words, it's looking in both directions for what would likely be the same set of recipes, if the dynamic recipeDef mods are adding them in correctly.  For example, Humanoid Alien Races 2.0 adds its recipes in both spots.

The important bit is that AllRecipes does NOT call RemoveDuplicates against the cached object.  So, it ends up adding all of the recipes from this.recipes and adding a duplicate set of recipes from recipeUsers.  The RimWorld.HealthCardUtility.DrawMedOperationsTab method calls AllRecipes and doesn't account for duplicates, either.  In fact, this sort of bug might slow down certain operations because it's unnecessarily searching double the recipes.  Checking both sides is probably a good idea, but it should expect duplicates, too.

I could fix this with a Harmony patch, but given that this has farther-reaching effects than just my mod, I figured it best to report it here.

SineSwiper

I've confirmed that a Harmony postfix patch against the AllRecipes getter, which is just running RemoveDuplicates, does indeed fix the duplicates I was seeing.  However, since it's running that patch on each AllRecipes get, it's a bit slower than running it against the (private) cache once.  So, it's still worth fixing on the Verse library.

ison

Thanks for reporting. However, why would a mod add a RecipeDef to both "recipes" and "recipeUsers"? I think they are exclusive recipe sources. So it looks like a bug with this mod.

LWM

One obvious answer presents itself to me:  Mod creators don't know they should only have one source?