Hash collisions in MyStringHash cause confusion

Digi shared this bug 16 hours ago
Submitted

This is an inherent problem of the reliance on hash codes being unique for strings in a game where mods can create their own strings through pretty much any definition (sbc) subtypeId.


----


One cause happened naturally, but it was also made significantly harder to replicate, more on that after.


Steps to repro that particular case:

1. Add TSO - Large Grid Offset Light and Offset Spotlight and Vertical (wall) Farm Plot and Inset Farm Plot mods to a world.

2. In the world, place inset farm plot.

3. equip hand welder and rightclick the inset farmplot to add it to build planner, which will crash.


Expected stacktrace for the crash:

Thread:   1 ->  Exception: System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at VRage.Utils.MyStringHash.GetOrCompute(String str)
   at VRage.Game.MyDefinitionBase.Init(MyObjectBuilder_DefinitionBase builder)
   at Sandbox.Definitions.MyCompositeBlueprintDefinition.Init(MyObjectBuilder_DefinitionBase builder)
   at Sandbox.Game.Entities.Character.MyCharacter.MakeBlueprintFromBuildPlanItem(BuildPlanItem buildPlanItem)

There's nothing wrong with either mods individually, and the clash is absolute bad luck.

If you previously had the TSO mod and looked at the blocks in a way that they get added to ViewedContent.sbl, then the inset farmplot mod will crash alone but only for those players, because they have the clashing string in memory from that file.


We narrowed it down to this with the help of attaching a debugger to get the source string and a mod script to get the existing MyStringHash for it:

hashcode of 'BuildPlanItem_FunctionalBlock/InsetFarmPlot' is '-2016076724'; retrieved MyStringHash: 'TSOOffsetLightTOCO'


----


I understand that this might be impossible to fix, but the least I can ask is throw a custom error for this, which prints both the existing string in the dictionary and the new string, this will help identify the mods they came from and fix the problem with the help of the community.

It's really not practical to ask everyone that gets this problem to attach a debugger... especially if they're on a console, and this certainly will happen on consoles too considering there's zero scripts involved in causing it.

Leave a Comment
 
Attach a file