Skip to content

Commit

Permalink
various performance fixes + SOS2 compatibility fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rheirman committed May 1, 2020
1 parent 1e5540a commit e9b4c7a
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 66 deletions.
Binary file modified 1.1/Assemblies/WhatTheHack.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion 1.1/Patches/WhatTheHack.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<success>Always</success>
<operations>
<li Class="PatchOperationAdd">
<xpath>Defs/ThingDef[@ParentName="BaseMechanoid" or (race/fleshType = "Mechanoid" and not (@ParentName="BaseMechanoid") and not (@Name="BaseMechanoid"))]</xpath>
<xpath>Defs/ThingDef[@ParentName="BaseMechanoid" or (race/fleshType = "Mechanoid" and not (@ParentName="BaseMechanoid") and not (@Name="BaseMechanoid")) and not (recipes)]</xpath>
<value>
<recipes>
<li>WTH_HackMechanoid</li>
Expand Down
13 changes: 6 additions & 7 deletions 1.1/Source/WhatTheHack/Harmony/Building_TurretGun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(Building_TurretGun), "Tick")]
class Building_TurretGun_Tick
{
static void Postfix(Building_TurretGun __instance)
static void Postfix(Building_TurretGun __instance, TurretTop ___top)
{
if (__instance.GetComp<CompMountable>() is CompMountable comp && comp.Active)
{
TurretTop top = Traverse.Create(__instance).Field("top").GetValue<TurretTop>();
float curRotation = Traverse.Create(top).Property("CurRotation").GetValue<float>();
//TODO: look into performance of this. Try to avoid Traverse.
float curRotation = Traverse.Create(___top).Property("CurRotation").GetValue<float>();
if (__instance.Rotation != comp.mountedTo.Rotation)
{
Traverse.Create(top).Property("CurRotation").SetValue(comp.mountedTo.Rotation.AsAngle);
Traverse.Create(___top).Property("CurRotation").SetValue(comp.mountedTo.Rotation.AsAngle);
__instance.Rotation = comp.mountedTo.Rotation;
}
}
Expand All @@ -30,10 +30,9 @@ static void Postfix(Building_TurretGun __instance)
[HarmonyPatch(typeof(TurretTop), "TurretTopTick")]
class TurretTop_TurretTopTick
{
static bool Prefix(TurretTop __instance)
static bool Prefix(TurretTop __instance, Building_Turret ___parentTurret)
{
Building_Turret parentTurret = Traverse.Create(__instance).Field("parentTurret").GetValue<Building_Turret>();
if (parentTurret.GetComp<CompMountable>() is CompMountable comp && comp.Active && !parentTurret.CurrentTarget.IsValid)
if (___parentTurret.GetComp<CompMountable>() is CompMountable comp && comp.Active && !___parentTurret.CurrentTarget.IsValid)
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions 1.1/Source/WhatTheHack/Harmony/CompHibernatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(CompHibernatable), "Startup")]
class CompHibernatable_Startup
{
static void Postfix(CompHibernatable __instance)
static void Postfix(CompHibernatable __instance, ref int ___endStartupTick)
{
if(__instance.parent is Building_MechanoidBeacon beacon)
{
Traverse.Create(__instance).Field("endStartupTicks").SetValue(Traverse.Create(__instance).Field("endStartupTicks").GetValue<int>() + beacon.GetComp<CompHibernatable_MechanoidBeacon>().extraStartUpDays * GenDate.TicksPerDay);
___endStartupTick += beacon.GetComp<CompHibernatable_MechanoidBeacon>().extraStartUpDays * GenDate.TicksPerDay;
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions 1.1/Source/WhatTheHack/Harmony/CompLongRangeMineralScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@ public static class CompLongRangeMineralScanner_Foundminerals
private const int MaxDistance = 22;
private static readonly IntRange TimeoutDaysRange = new IntRange(min: 25, max: 50);

static bool Prefix(CompLongRangeMineralScanner __instance, Pawn worker)
static bool Prefix(CompLongRangeMineralScanner __instance, Pawn worker, ref ThingDef ___targetMineable)
{
if (__instance!=null)
{
ThingDef thing = Traverse.Create(__instance).Field("targetMineable").GetValue<ThingDef>();
if (thing == null)
if (___targetMineable == null)
{
return true;
}
if (thing == WTH_DefOf.WTH_MineableMechanoidParts)
if (___targetMineable == WTH_DefOf.WTH_MineableMechanoidParts)
{
// Traverse.Create(__instance).Field("daysWorkingSinceLastMinerals").SetValue(0f);
if (!TileFinder.TryFindNewSiteTile(out int tile, MinDistance, MaxDistance, true, false))
return false;

Slate slate = new Slate();
slate.Set<Map>("map", worker.Map, false);
slate.Set<ThingDef>("targetMineable", thing, false);
slate.Set<ThingDef>("targetMineable", ___targetMineable, false);
slate.Set<Pawn>("worker", worker, false);
if (!WTH_DefOf.WTH_LongRangeMineralScannerMechParts.CanRun(slate))
{
Expand Down
5 changes: 2 additions & 3 deletions 1.1/Source/WhatTheHack/Harmony/Dialog_LoadTransporters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(Dialog_LoadTransporters), "TryAccept")]
class Dialog_LoadTransporters_TryAccept
{
static void Postfix(Dialog_LoadTransporters __instance, bool __result)
static void Postfix(Dialog_LoadTransporters __instance, bool __result, ref List<TransferableOneWay> ___transferables)
{
if (__result)
{
List<TransferableOneWay> transferables = Traverse.Create(__instance).Field("transferables").GetValue<List<TransferableOneWay>>();
Traverse.Create(__instance).Field("transferables").SetValue(Utilities.LinkPortablePlatforms(transferables));
___transferables = Utilities.LinkPortablePlatforms(___transferables);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions 1.1/Source/WhatTheHack/Harmony/Dialog_SplitCaravan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(Dialog_SplitCaravan), "CountToTransferChanged")]
class Dialog_SplitCaravan_CountToTransferChanged
{
static void Postfix(Dialog_SplitCaravan __instance)
static void Postfix(Dialog_SplitCaravan __instance, ref List<TransferableOneWay> ___transferables)
{
List<TransferableOneWay> transferables = Traverse.Create(__instance).Field("transferables").GetValue<List<TransferableOneWay>>();
Utilities.CalcDaysOfFuel(transferables);
Utilities.CalcDaysOfFuel(___transferables);
}
}
}
5 changes: 2 additions & 3 deletions 1.1/Source/WhatTheHack/Harmony/Gizmo_CaravanInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(Gizmo_CaravanInfo), "GizmoOnGUI")]
class Gizmo_CaravanInfo_GizmoOnGUI
{
static void Postfix(Gizmo_CaravanInfo __instance)
static void Postfix(Gizmo_CaravanInfo __instance, ref Caravan ___caravan)
{
int numMechanoids = 0;
float fuelAmount = 0f;
float fuelConsumption = 0f;
int numPlatforms = 0;
float daysOfFuel = 0;
Caravan caravan = Traverse.Create(__instance).Field("caravan").GetValue<Caravan>();
StringBuilder daysOfFuelReason = new StringBuilder();

foreach (Thing thing in caravan.AllThings)
foreach (Thing thing in ___caravan.AllThings)
{
if (thing.def.race != null && thing.def.race.IsMechanoid && thing is Pawn pawn && pawn.IsHacked() && !pawn.health.hediffSet.HasHediff(WTH_DefOf.WTH_VanometricModule))
{
Expand Down
10 changes: 4 additions & 6 deletions 1.1/Source/WhatTheHack/Harmony/MapParent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(MapParent), "RecalculateHibernatableIncidentTargets")]
class MapParent_RecalculateHibernatableIncidentTargets
{
static void Postfix(MapParent __instance)
static void Postfix(MapParent __instance, ref HashSet<IncidentTargetTagDef> ___hibernatableIncidentTargets)
{
foreach (ThingWithComps current in __instance.Map.listerThings.ThingsOfDef(WTH_DefOf.WTH_MechanoidBeacon).OfType<ThingWithComps>())
{
CompHibernatable compHibernatable = current.TryGetComp<CompHibernatable>();
if (compHibernatable != null && compHibernatable.State == HibernatableStateDefOf.Starting && compHibernatable.Props.incidentTargetWhileStarting != null)
{
HashSet<IncidentTargetTagDef> hibernatableIncidentTargets = Traverse.Create(__instance).Field("hibernatableIncidentTargets").GetValue<HashSet<IncidentTargetTagDef>>();
if (hibernatableIncidentTargets == null)
if (___hibernatableIncidentTargets == null)
{
hibernatableIncidentTargets = new HashSet<IncidentTargetTagDef>();
___hibernatableIncidentTargets = new HashSet<IncidentTargetTagDef>();
}
hibernatableIncidentTargets.Add(compHibernatable.Props.incidentTargetWhileStarting);
Traverse.Create(__instance).Field("hibernatableIncidentTargets").SetValue(hibernatableIncidentTargets);
___hibernatableIncidentTargets.Add(compHibernatable.Props.incidentTargetWhileStarting);
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions 1.1/Source/WhatTheHack/Harmony/PawnUIOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(PawnUIOverlay), "DrawPawnGUIOverlay")]
class PawnUIOverlay_DrawPawnGUIOverlay
{
static void Postfix(PawnUIOverlay __instance)
static void Postfix(Pawn ___pawn)
{
Pawn pawn = Traverse.Create(__instance).Field("pawn").GetValue<Pawn>();
if(pawn.IsHacked() && pawn.Faction == Faction.OfPlayer && pawn.Name != null)
if(___pawn.IsHacked() && ___pawn.Faction == Faction.OfPlayer && ___pawn.Name != null)
{
Vector2 pos = GenMapUI.LabelDrawPosFor(pawn, -0.6f);
GenMapUI.DrawPawnLabel(pawn, pos, 1f, 9999f, null, GameFont.Tiny, true, true);
Vector2 pos = GenMapUI.LabelDrawPosFor(___pawn, -0.6f);
GenMapUI.DrawPawnLabel(___pawn, pos, 1f, 9999f, null, GameFont.Tiny, true, true);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions 1.1/Source/WhatTheHack/Harmony/Pawn_HealthTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ static void Postfix(Pawn_HealthTracker __instance)
[HarmonyPatch(typeof(Pawn_HealthTracker), "HasHediffsNeedingTend")]
static class Pawn_HealthTracker_HasHediffsNeedingTend
{
static bool Prefix(Pawn_HealthTracker __instance)
static bool Prefix(Pawn_HealthTracker __instance, ref Pawn ___pawn)
{
Pawn pawn = Traverse.Create(__instance).Field("pawn").GetValue<Pawn>();
if (pawn.RaceProps.IsMechanoid && pawn.IsHacked())
if (___pawn.RaceProps.IsMechanoid && ___pawn.IsHacked())
{
return false;
}
Expand Down
7 changes: 3 additions & 4 deletions 1.1/Source/WhatTheHack/Harmony/Pawn_JobTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(Pawn_JobTracker), "DetermineNextJob")]
static class Pawn_JobTracker_DetermineNextJob
{
static void Postfix(ref Pawn_JobTracker __instance, ref ThinkResult __result)
static void Postfix(ref Pawn_JobTracker __instance, ref ThinkResult __result, ref Pawn ___pawn)
{

Pawn pawn = Traverse.Create(__instance).Field("pawn").GetValue<Pawn>();
if(pawn.IsHacked() && pawn.IsActivated() && pawn.health.hediffSet.HasHediff(WTH_DefOf.WTH_TargetingHackedPoorly))
if(___pawn.IsHacked() && ___pawn.IsActivated() && ___pawn.health.hediffSet.HasHediff(WTH_DefOf.WTH_TargetingHackedPoorly))
{
HackedPoorlyEvent(pawn);
HackedPoorlyEvent(___pawn);
}
}

Expand Down
5 changes: 2 additions & 3 deletions 1.1/Source/WhatTheHack/Harmony/Pawn_SkillTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ namespace WhatTheHack.Harmony
[HarmonyPatch(typeof(Pawn_SkillTracker), "Learn")]
class Pawn_SkillTracker_Learn
{
static bool Prefix(Pawn_SkillTracker __instance)
static bool Prefix(Pawn_SkillTracker __instance, ref Pawn ___pawn)
{
Pawn pawn = Traverse.Create(__instance).Field("pawn").GetValue<Pawn>();
if (pawn.IsHacked())
if (___pawn.IsHacked())
{
return false;
}
Expand Down
85 changes: 64 additions & 21 deletions 1.1/Source/WhatTheHack/Harmony/StatWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace WhatTheHack.Harmony
{
//This makes sure that mechanoids without work modules for certain skills don't use their skill value for those skills, but use the noSkillOffset or noSkillFactor instead.

[HarmonyPatch(typeof(StatWorker), "GetExplanationUnfinalized")]
public static class StatWorker_GetExplanationUnfinalized
{
Expand All @@ -36,58 +36,101 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
}
}

//This makes sure that mechanoids without work modules for certain skills don't use their skill value for those skills, but use the noSkillOffset or noSkillFactor instead.
//The transpiler below is more correct, but its impact on performance is too high, so I replaced it with a simple postfix that just replaces calculated stat value with the noSkillOfset.
[HarmonyPatch(typeof(StatWorker), "GetValueUnfinalized")]
public static class StatWorker_GetValueUnfinalized
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
static void Postfix(StatWorker __instance, StatRequest req, ref StatDef ___stat, ref float __result)
{
var instructionsList = new List<CodeInstruction>(instructions);
foreach (CodeInstruction instruction in instructionsList)
Pawn pawn = req.Thing as Pawn;
if (pawn != null && pawn.IsHacked())
{
if (instruction.operand as MethodInfo == AccessTools.Field(typeof(Pawn), "skills"))
if (Utilities.ShouldGetStatValue(pawn, ___stat))
{
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StatWorker), "stat"));
yield return new CodeInstruction(OpCodes.Call, typeof(Utilities).GetMethod("ShouldGetStatValue"));
return;
}
else
{
yield return instruction;
__result = GetBaseValueFor(req, ___stat) + ___stat.noSkillOffset;
if (req.HasThing)
{
__result *= ___stat.noSkillOffset;
}
}
}
}
/*
public static bool ShouldGetStatValue(Pawn pawn)
private static float GetBaseValueFor(StatRequest request, StatDef stat)
{
return false;
float result = stat.defaultBaseValue;
if (request.StatBases != null)
{
for (int i = 0; i < request.StatBases.Count; i++)
{
if (request.StatBases[i].stat == stat)
{
result = request.StatBases[i].value;
break;
}
}
}
return result;
}
*/

}

////This makes sure that mechanoids without work modules for certain skills don't use their skill value for those skills, but use the noSkillOffset or noSkillFactor instead.
//[HarmonyPatch(typeof(StatWorker), "GetValueUnfinalized")]
//public static class StatWorker_GetValueUnfinalized
//{
// static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
// {
// var instructionsList = new List<CodeInstruction>(instructions);
// foreach (CodeInstruction instruction in instructionsList)
// {
// if (instruction.operand as MethodInfo == AccessTools.Field(typeof(Pawn), "skills"))
// {
// yield return new CodeInstruction(OpCodes.Ldarg_0);
// yield return new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StatWorker), "stat"));
// yield return new CodeInstruction(OpCodes.Call, typeof(Utilities).GetMethod("ShouldGetStatValue"));
// }
// else
// {
// yield return instruction;
// }
// }
// }
// /*
// public static bool ShouldGetStatValue(Pawn pawn)
// {
// return false;
// }

// */

//}

[HarmonyPatch(typeof(StatWorker), "ShouldShowFor")]
static class StatWorker_ShouldShowFor
{
static bool Prefix(StatWorker __instance, StatRequest req, ref bool __result)
static bool Prefix(StatWorker __instance, StatRequest req, ref bool __result, ref StatDef ___stat)
{
StatDef stat = Traverse.Create(__instance).Field("stat").GetValue<StatDef>();
if (stat.category == WTH_DefOf.WTH_StatCategory_HackedMechanoid && req.Thing is Pawn pawn)
if (___stat.category == WTH_DefOf.WTH_StatCategory_HackedMechanoid && req.Thing is Pawn pawn)
{
__result = pawn.IsHacked();
return false;
}
if(stat.category == WTH_DefOf.WTH_StatCategory_Colonist && req.Thing is Pawn pawn2)
if(___stat.category == WTH_DefOf.WTH_StatCategory_Colonist && req.Thing is Pawn pawn2)
{
__result = pawn2.IsColonistPlayerControlled;
return false;
}
if(stat.category == WTH_DefOf.WTH_StatCategory_Platform && req.Thing is Building_BaseMechanoidPlatform)
if(___stat.category == WTH_DefOf.WTH_StatCategory_Platform && req.Thing is Building_BaseMechanoidPlatform)
{
__result = true;
return false;
}
if(stat.category == WTH_DefOf.WTH_StatCategory_HackedMechanoid || stat.category == WTH_DefOf.WTH_StatCategory_Colonist || stat.category == WTH_DefOf.WTH_StatCategory_Platform)
if(___stat.category == WTH_DefOf.WTH_StatCategory_HackedMechanoid || ___stat.category == WTH_DefOf.WTH_StatCategory_Colonist || ___stat.category == WTH_DefOf.WTH_StatCategory_Platform)
{
__result = false;
return false;
Expand Down

0 comments on commit e9b4c7a

Please sign in to comment.