Advertisement
Jamestec

Fix IndexOutOfRangeException at TaleWorlds.CampaignSystem.Roster.TroopRoster.AddToCountsAtIndex

Jan 12th, 2023 (edited)
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. Bannerlord Fix IndexOutOfRangeException at TaleWorlds.CampaignSystem.Roster.TroopRoster.AddToCountsAtIndex
  2.  
  3. 1. Get dnSpy here:
  4. https://github.com/dnSpy/dnSpy/releases
  5.  
  6. 2. Extract and run dnSpy.exe.
  7.  
  8. 3. Go to
  9. C:\Program Files (x86)\Steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client
  10.  
  11. 4. Make a copy of TaleWorlds.CampaignSystem.dll just in case.
  12.  
  13. 5. Drag relevant libraries into dnSpy (left window thing), we want to edit TaleWorlds.CampaignSystem.dll so drag that in, might have to drag other ones in as well, I forgot.
  14.  
  15. 6. In the toolbar at the top, go Edit -> Search Assemblies. Search for AddToCountsAtIndex and double click the result.
  16.  
  17. 7. Right click on AddToCountsAtIndex and press Edit Method (C#)...
  18.  
  19. 8. Replace the text with this:
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Diagnostics;
  24. using System.Runtime.CompilerServices;
  25. using TaleWorlds.CampaignSystem.Party;
  26. using TaleWorlds.Core;
  27. using TaleWorlds.Library;
  28. using TaleWorlds.SaveSystem;
  29.  
  30. namespace TaleWorlds.CampaignSystem.Roster
  31. {
  32. // Token: 0x0200028C RID: 652
  33. public partial class TroopRoster : ISerializableObject
  34. {
  35. // Token: 0x06002222 RID: 8738
  36. public int AddToCountsAtIndex(int index, int countChange, int woundedCountChange = 0, int xpChange = 0, bool removeDepleted = true)
  37. {
  38. try
  39. {
  40. this.UpdateVersion();
  41. bool heroCountChanged = false;
  42. CharacterObject character = this.data[index].Character;
  43. bool isHero = character.IsHero;
  44. TroopRosterElement[] array = this.data;
  45. int num = index;
  46. array[num].Number = array[num].Number + countChange;
  47. int num2 = this.data[index].WoundedNumber + woundedCountChange;
  48. if (num2 > this.data[index].Number)
  49. {
  50. woundedCountChange += this.data[index].Number - num2;
  51. }
  52. else if (num2 < 0)
  53. {
  54. TaleWorlds.Library.Debug.FailedAssert("false", "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.CampaignSystem\\Roster\\TroopRoster.cs", "AddToCountsAtIndex", 426);
  55. }
  56. TroopRosterElement[] array2 = this.data;
  57. int num3 = index;
  58. array2[num3].WoundedNumber = array2[num3].WoundedNumber + woundedCountChange;
  59. if (xpChange != 0)
  60. {
  61. TroopRosterElement[] array3 = this.data;
  62. int num4 = index;
  63. array3[num4].Xp = array3[num4].Xp + xpChange;
  64. }
  65. if (this.IsPrisonRoster)
  66. {
  67. this.ClampConformity(index);
  68. }
  69. else
  70. {
  71. this.ClampXp(index);
  72. }
  73. if (isHero)
  74. {
  75. this._totalHeroes += countChange;
  76. if (character.HeroObject.IsWounded)
  77. {
  78. this._totalWoundedHeroes += countChange;
  79. }
  80. if (countChange != 0)
  81. {
  82. heroCountChanged = true;
  83. }
  84. }
  85. else
  86. {
  87. this._totalWoundedRegulars += woundedCountChange;
  88. this._totalRegulars += countChange;
  89. }
  90. if (removeDepleted && this.data[index].Number == 0)
  91. {
  92. this.RemoveRange(index, index + 1);
  93. index = -1;
  94. }
  95. if (this.OwnerParty != null && isHero)
  96. {
  97. if (countChange > 0)
  98. {
  99. if (!this.IsPrisonRoster)
  100. {
  101. this.OwnerParty.OnHeroAdded(character.HeroObject);
  102. }
  103. else
  104. {
  105. this.OwnerParty.OnHeroAddedAsPrisoner(character.HeroObject);
  106. }
  107. }
  108. else if (countChange < 0)
  109. {
  110. if (!this.IsPrisonRoster)
  111. {
  112. this.OwnerParty.OnHeroRemoved(character.HeroObject);
  113. }
  114. else
  115. {
  116. this.OwnerParty.OnHeroRemovedAsPrisoner(character.HeroObject);
  117. }
  118. }
  119. }
  120. if (countChange != 0 || woundedCountChange != 0)
  121. {
  122. this.OnNumberChanged(countChange != 0, woundedCountChange != 0, heroCountChanged);
  123. }
  124. }
  125. catch (IndexOutOfRangeException)
  126. {
  127. string troops = "Empty";
  128. int len = this.data.GetLength(0);
  129. if (len > 0)
  130. {
  131. troops = "";
  132. for (int i = 0; i < len; i++)
  133. {
  134. troops += string.Format("[{0}] = {1}, ", i, this.data[i].ToString());
  135. }
  136. }
  137. InformationManager.DisplayMessage(new InformationMessage(string.Format("Caught IndexOutOfRangeException, index = {0}, countChange = {1}, TroopRoster = {2}", index, countChange, troops)));
  138. }
  139. return index;
  140. }
  141. }
  142. }
  143.  
  144. 9. Press Compile.
  145.  
  146. 10. Go to File -> Save Module, make sure the Filename is
  147. C:\Program Files (x86)\Steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll
  148.  
  149. 11. Press Ok.
  150.  
  151. 11. Verify the dll is patched by looking Date Modified.
  152.  
  153. When the fix is triggered, it'll say something in the Combat Log (bottom left text thing) like:
  154. Caught IndexOutOfRange Exception, index = -1, countChange = -1, TroopRoster = [0] = 9 Looter, [1] = 0, [2] = 0, [3] = 0,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement