Skip to content

Conversation

@Alex-Blanes
Copy link

I'm encountering an error when loading saved games with the PrisonLabor mod. The error occurs during PostLoadInit in PrisonLabor.Core.Trackers.CuffsTracker, throwing an ArgumentOutOfRangeException when accessing an out-of-range index in a list.

Error Log:

Could not do PostLoadInit on PrisonLabor.Core.Trackers.CuffsTracker: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
[Ref 2BA144C9]
 [0x0000c] in <51fded79cd284d4d911c5949aff4cb21>:0 
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <51fded79cd284d4d911c5949aff4cb21>:0 
  at PrisonLabor.Core.Other.ScribeUtils`2[T,S].Scribe (System.Collections.Generic.Dictionary`2[T,S]& dict, System.String name) [0x00098] in <41106d99db6e477e9c5ba392889be547>:0 
  at PrisonLabor.Core.Trackers.CuffsTracker.ExposeData () [0x00001] in <41106d99db6e477e9c5ba392889be547>:0 
  at Verse.PostLoadIniter.DoAllPostLoadInits () [0x00032] in <46372f5dadbf4af8939e608076251180>:0

Suggested Solution:

The error appears to originate in ScribeUtils.Scribe when trying to access an invalid index in a list during serialization. I've added bounds checking before accessing the list to prevent the exception. This fix prevents the crash and allows the game to continue loading.

I haven't included warning logging in this fix, but if you think it would be helpful for debugging cases where data might be corrupted or incomplete, a warning could be added.

This is my first contribution to a mod, so I'm open to feedback if this approach isn't helpful or could be improved. I'm here to learn and help make the mod better.

@Hazzer
Copy link
Collaborator

Hazzer commented Jan 19, 2026

Sorry for long time lag, got my computer broke. What exactly did you do, that this exception appear? Just load game or some changes to save was done meantime?

@Alex-Blanes
Copy link
Author

Sorry for long time lag, got my computer broke. What exactly did you do, that this exception appear? Just load game or some changes to save was done meantime?

No problem Hazzer we have busy lives, my desktop computer broked for me as well a month ago, my condolences. The origin also can be affected a little bit by having 300+ mods. I'll recheck it on this weekend but i think is a loading save game exception. Maybe can be related with the issue i publicated where my prisioners get handcuffed and bringed to bed, because this error affects to the cuffstracker. Checking issues for 'CuffsTracker' it also apperead on this 2024 issue anyways there appear a hundred of other exceptions.

{
dict = new Dictionary<T, S>();
for (var i = 0; i < tmpKeys.Count; i++)
int count = Math.Min(tmpKeys.Count, tmpVals.Count);
Copy link
Collaborator

@Hazzer Hazzer Jan 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can change whole class to:

 public class ScribeUtils
    {
        private List<Pawn> tmpKeys;
        private List<bool> tmpVals;

        public void Scribe(ref Dictionary<Pawn, bool> dict, string name)
        {
            if (Verse.Scribe.mode == LoadSaveMode.Saving)
            {
                tmpKeys = new List<Pawn>(dict.Keys);
                tmpVals = new List<bool>(dict.Values);
            }

            Scribe_Collections.Look(ref tmpKeys, $"{name}.keys", LookMode.Reference);
            Scribe_Collections.Look(ref tmpVals, $"{name}.vals", LookMode.Deep);

            if (Verse.Scribe.mode == LoadSaveMode.PostLoadInit)
            {
                dict = new Dictionary<Pawn, bool>();
                for (var i = 0; i < tmpKeys.Count; i++)
                    if (tmpKeys[i] != null)
                        dict[tmpKeys[i]] = tmpVals.ElementAtOrDefault(i);
            }
        }
    }

Generic here was overkill and with this code even if value is missing, we will put default (false) value :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants