Skip to content

Commit

Permalink
Fix GetGizmos Performance (#26)
Browse files Browse the repository at this point in the history
seems good to me
  • Loading branch information
KongMD-Steam authored Apr 25, 2021
1 parent c288f6e commit 560e74b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
71 changes: 26 additions & 45 deletions Source/intercepts/Intercepts_UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,61 +45,42 @@ public static void InterfaceDrop(ITab_Pawn_Gear __instance, Thing t)
public static class Pawn_GetGizmos_Postfix
{
[HarmonyPostfix]
public static void GetGizmos(Pawn __instance, ref IEnumerable<Gizmo> __result)
//public static void GetGizmos(Pawn __instance, ref IEnumerable<Gizmo> __result)
public static IEnumerable<Gizmo> Postfix(IEnumerable<Gizmo> __result, Pawn __instance)
{
if (!__instance.IsValidSidearmsCarrier())
return;
try
//This postfix inserts the SimpleSidearms gizmo before all other gizmos
if (__instance.IsValidSidearmsCarrier() && (__instance.IsColonistPlayerControlled
|| DebugSettings.godMode) && __instance.equipment != null && __instance.inventory != null
)
{
if ((__instance.IsColonistPlayerControlled)
|| DebugSettings.godMode)
{
if (__instance.equipment != null && __instance.inventory != null)
{
IEnumerable<ThingWithComps> carriedWeapons = __instance.getCarriedWeapons(includeTools: true);

CompSidearmMemory pawnMemory = CompSidearmMemory.GetMemoryCompForPawn(__instance);
if (pawnMemory == null)
return;
IEnumerable<ThingWithComps> carriedWeapons = __instance.getCarriedWeapons(includeTools: true);

//if (carriedWeapons.Count() > 0 || (pawnMemory != null && pawnMemory.RememberedWeapons.Count > 0))
{
List<ThingDefStuffDefPair> rangedWeaponMemories = new List<ThingDefStuffDefPair>();
List<ThingDefStuffDefPair> meleeWeaponMemories = new List<ThingDefStuffDefPair>();
CompSidearmMemory pawnMemory = CompSidearmMemory.GetMemoryCompForPawn(__instance);
if (pawnMemory != null)
{
List<ThingDefStuffDefPair> rangedWeaponMemories = new List<ThingDefStuffDefPair>();
List<ThingDefStuffDefPair> meleeWeaponMemories = new List<ThingDefStuffDefPair>();

if (pawnMemory != null)
{
foreach (ThingDefStuffDefPair weapon in pawnMemory.RememberedWeapons)
{
if (weapon.thing.IsMeleeWeapon)
meleeWeaponMemories.Add(weapon);
else if (weapon.thing.IsRangedWeapon)
rangedWeaponMemories.Add(weapon);
}
}
foreach (ThingDefStuffDefPair weapon in pawnMemory.RememberedWeapons)
{
if (weapon.thing.IsMeleeWeapon)
meleeWeaponMemories.Add(weapon);
else if (weapon.thing.IsRangedWeapon)
rangedWeaponMemories.Add(weapon);
}

Gizmo_SidearmsList advanced = new Gizmo_SidearmsList(__instance, carriedWeapons, pawnMemory.RememberedWeapons);
yield return new Gizmo_SidearmsList(__instance, carriedWeapons, pawnMemory.RememberedWeapons);

List<Gizmo> results = new List<Gizmo>();
results.Add(advanced);
if (DebugSettings.godMode)
{
Gizmo_Brainscope brainscope = new Gizmo_Brainscope(__instance);
results.Add(brainscope);
}
foreach (Gizmo gizmo in __result)
{
results.Add(gizmo);
};
__result = results;
}
if (DebugSettings.godMode)
{
yield return new Gizmo_Brainscope(__instance);
}
}

}
catch(Exception e)

foreach (var aGizmo in __result)
{
Log.Error("Exception during SimpleSidearms gizmo intercept. Cancelling intercept. Exception: " + e.ToString());
yield return aGizmo;
}
}
}
Expand Down
Binary file modified v1.2/Assemblies/SimpleSidearms.dll
Binary file not shown.

0 comments on commit 560e74b

Please sign in to comment.