Skip to content

Commit

Permalink
another fix for doubled fixed equipment
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Dec 10, 2018
1 parent cef7dd2 commit 600d057
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 12 deletions.
3 changes: 2 additions & 1 deletion source/CCLight/Registry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#undef CCDEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down
4 changes: 2 additions & 2 deletions source/Control.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#undef CCDEBUG
#undef CCDEBUG

using Harmony;
using System;
Expand Down Expand Up @@ -54,7 +54,7 @@ public static void Init(string directory, string settingsJSON)
Registry.RegisterSimpleCustomComponents(Assembly.GetExecutingAssembly());
Validator.RegisterMechValidator(CategoryController.ValidateMech, CategoryController.ValidateMechCanBeFielded);

Logger.Log("Loaded CustomComponents v0.8.4.0 for bt 1.3");
Logger.Log("Loaded CustomComponents v0.8.4.2 for bt 1.3");
#if CCDEBUG
Logger.LogDebug("Loading Categories");
#endif
Expand Down
2 changes: 2 additions & 0 deletions source/CustomComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@
<Compile Include="ColorPatches\Salvage_RefreshItemColor.cs" />
<Compile Include="ColorPatches\Shop_RefreshColor.cs" />
<Compile Include="Components\TagColorComponent.cs" />
<Compile Include="DEBUG\MechDef_RefreshInventory.cs" />
<Compile Include="Fixes\MechDef_InsertFixedEquipmentIntoInventory.cs" />
<Compile Include="Fixes\SimGameState_CreateComponentInstallWorkOrder.cs" />
<Compile Include="Interfaces\IColorComponent.cs" />
<Compile Include="Components\RGBColorComponent.cs" />
<Compile Include="Components\Sorter.cs" />
<Compile Include="Interfaces\IClearInventory.cs" />
<Compile Include="Helpers\DEBUGTOOLS.cs" />
<Compile Include="TagRestrictions\TagRestrictions.cs" />
<Compile Include="TagRestrictions\TagRestrictionsHandler.cs" />
<Compile Include="DropResults\IChange.cs" />
Expand Down
32 changes: 29 additions & 3 deletions source/Fixes/MechDef_InsertFixedEquipmentIntoInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,58 @@ public static class MechDef_InsertFixedEquipmentIntoInventory
[HarmonyPrefix]
public static bool FIX(MechDef __instance, ref MechComponentRef[] ___inventory, DataManager ___dataManager)
{


if (__instance.Chassis == null)
return false;
#if CCDEBUG
if(DEBUGTOOLS.NEEDTOSHOW)
Control.Logger.LogDebug($"InsertFixedEquipmentIntoInventory for {__instance.Name}");
#endif
if (__instance.Chassis.FixedEquipment == null || __instance.Chassis.FixedEquipment.Length == 0)
{
#if CCDEBUG
if (DEBUGTOOLS.NEEDTOSHOW)
Control.Logger.LogDebug($"-- NO FIXED, return");
#endif
return false;

}
#if CCDEBUG
if (DEBUGTOOLS.NEEDTOSHOW)
Control.Logger.LogDebug($"-- start search for fixed");
#endif
int found = 0;
for (int i = 0; i < ___inventory.Length; i++)
{
if (!string.IsNullOrEmpty(___inventory[i].SimGameUID) && ___inventory[i].SimGameUID.Contains("FixedEquipment"))
{
#if CCDEBUG
if (DEBUGTOOLS.NEEDTOSHOW)
Control.Logger.LogDebug($"---- found {___inventory[i].MountedLocation} - {___inventory[i].ComponentDefID} - {___inventory[i].SimGameUID}");
#endif
___inventory[i].SetData(___inventory[i].HardpointSlot, ___inventory[i].DamageLevel, true);
found += 1;
}
}

if (found > 0)
return false;

#if CCDEBUG
if (DEBUGTOOLS.NEEDTOSHOW)
Control.Logger.LogDebug($"-- not found. inserting");
#endif
List<MechComponentRef> list = new List<MechComponentRef>();
for (int j = 0; j < __instance.Chassis.FixedEquipment.Length; j++)
{
MechComponentRef mechComponentRef = new MechComponentRef(__instance.Chassis.FixedEquipment[j], null);
mechComponentRef.DataManager = ___dataManager;
mechComponentRef.RefreshComponentDef();
mechComponentRef.SetSimGameUID(string.Format("FixedEquipment-{0}", Guid.NewGuid().ToString()));
mechComponentRef.SetSimGameUID($"FixedEquipment-{Guid.NewGuid().ToString()}");
list.Add(mechComponentRef);
#if CCDEBUG
if (DEBUGTOOLS.NEEDTOSHOW)
Control.Logger.LogDebug($"---- add {mechComponentRef.MountedLocation} - {mechComponentRef.ComponentDefID} - {mechComponentRef.SimGameUID}");
#endif
}
list.AddRange(___inventory);
___inventory = list.ToArray();
Expand Down
20 changes: 20 additions & 0 deletions source/Helpers/DEBUGTOOLS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using BattleTech;

namespace CustomComponents
{
internal class DEBUGTOOLS
{
public static bool NEEDTOSHOW = false;

public static void ShowInventory(MechDef mech)
{

Control.Logger.LogDebug($"SHOW INVENTORY FOR {mech.Name}");
foreach (var comp in mech.Inventory)
{
Control.Logger.LogDebug($" -- {comp.MountedLocation} -- {comp.ComponentDefID} -- {comp.SimGameUID}");
}
Control.Logger.LogDebug($"========== done ============");
}
}
}
19 changes: 13 additions & 6 deletions source/Helpers/DefaultHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#undef CCDEBUG
//#undef CCDEBUG

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -259,16 +259,18 @@ internal static void ForceItemDropStrip(this MechLabPanel mechlab, MechLabItemSl

internal static MechComponentRef[] ClearInventory(MechDef source, SimGameState state)
{
#if CCDEBUG
Control.Logger.LogDebug("Clearing Inventory");

#endif
var list = source.Inventory.ToList();

var result_list = list.Where(i => i.IsFixed).ToList();

for (int i = list.Count - 1; i >= 0; i--)
{

#if CCDEBUG
Control.Logger.LogDebug($"- {list[i].ComponentDefID} - {(list[i].Def == null ? "NULL" : list[i].SimGameUID)}");
#endif
if (list[i].Def == null)
{
list[i].RefreshComponentDef();
Expand All @@ -277,7 +279,9 @@ internal static MechComponentRef[] ClearInventory(MechDef source, SimGameState s

if (list[i].IsFixed)
{
#if CCDEBUG
Control.Logger.LogDebug("-- fixed - skipping");
#endif
continue;
}

Expand All @@ -289,14 +293,17 @@ internal static MechComponentRef[] ClearInventory(MechDef source, SimGameState s

foreach (var item in result_list)
{
item.SetSimGameUID(state.GenerateSimGameUID());
Control.Logger.LogDebug($"- {item.ComponentDefID} - {item.SimGameUID}");
if(string.IsNullOrEmpty(item.SimGameUID))
item.SetSimGameUID(state.GenerateSimGameUID());
#if CCDEBUG
Control.Logger.LogDebug($"- {item.ComponentDefID} - {item.SimGameUID}");
#endif
}

return result_list.ToArray();
}

#endregion
#endregion

}
}
12 changes: 12 additions & 0 deletions source/Patches/SimGameState_ReadyMech_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal class SimGameState_ReadyMech_Patch

public static void Prefix(SimGameState __instance)
{
#if CCDEBUG
DEBUGTOOLS.NEEDTOSHOW = true;
#endif
state = __instance;
}

Expand All @@ -35,10 +38,19 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

public static MechDef CreateMech(ChassisDef chassis, string simuid, MechDef original)
{
#if CCDEBUG
Control.Logger.LogDebug($"Createing Mech {chassis.Description.Id} - {original.Description.Id}");
#endif
var result = new MechDef(chassis, simuid, original);
#if CCDEBUG
DEBUGTOOLS.ShowInventory(result);
#endif
result.SetInventory(DefaultHelper.ClearInventory(original, state));
#if CCDEBUG
DEBUGTOOLS.ShowInventory(result);
#endif
return result;
}

}
}

0 comments on commit 600d057

Please sign in to comment.