Skip to content

Commit

Permalink
Refactor to be more future-proof
Browse files Browse the repository at this point in the history
  • Loading branch information
eth0net committed Jan 16, 2023
1 parent a88ac24 commit 1939ce6
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 117 deletions.
Binary file modified Assemblies/NamedSubcores.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Languages/English/Keyed/NamedSubcores.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LanguageData>
<Occupant>Occupant</Occupant>
<Pattern>Pattern</Pattern>
<Unknown>Unknown</Unknown>
</LanguageData>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<value>
<comps>
<li>
<compClass>NamedSubcores.NamedMechGestatorComp</compClass>
<compClass>NamedSubcores.MechGestatorComp</compClass>
</li>
</comps>
</value>
Expand All @@ -18,7 +18,7 @@
<xpath>/Defs/ThingDef[@Name="MechGestatorBase"]/comps</xpath>
<value>
<li>
<compClass>NamedSubcores.NamedMechGestatorComp</compClass>
<compClass>NamedSubcores.MechGestatorComp</compClass>
</li>
</value>
</match>
Expand Down
4 changes: 2 additions & 2 deletions Patches/NamedMechComp.xml → Patches/MechPatternComp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<value>
<comps>
<li>
<compClass>NamedSubcores.NamedMechComp</compClass>
<compClass>NamedSubcores.MechPatternComp</compClass>
</li>
</comps>
</value>
Expand All @@ -18,7 +18,7 @@
<xpath>/Defs/ThingDef[@Name="BaseMechanoid"]/comps</xpath>
<value>
<li>
<compClass>NamedSubcores.NamedMechComp</compClass>
<compClass>NamedSubcores.MechPatternComp</compClass>
</li>
</value>
</match>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<value>
<comps>
<li>
<compClass>NamedSubcores.NamedSubcoreComp</compClass>
<compClass>NamedSubcores.SubcorePatternComp</compClass>
</li>
</comps>
</value>
Expand All @@ -18,7 +18,7 @@
<xpath>/Defs/ThingDef[defName="SubcoreRegular" or defName="SubcoreHigh"]/comps</xpath>
<value>
<li>
<compClass>NamedSubcores.NamedSubcoreComp</compClass>
<compClass>NamedSubcores.SubcorePatternComp</compClass>
</li>
</value>
</match>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<value>
<comps>
<li>
<compClass>NamedSubcores.NamedSubcoreScannerComp</compClass>
<compClass>NamedSubcores.SubcoreScannerComp</compClass>
</li>
</comps>
</value>
Expand All @@ -18,7 +18,7 @@
<xpath>/Defs/ThingDef[defName="SubcoreSoftscanner" or defName="SubcoreRipscanner"]/comps</xpath>
<value>
<li>
<compClass>NamedSubcores.NamedSubcoreScannerComp</compClass>
<compClass>NamedSubcores.SubcoreScannerComp</compClass>
</li>
</value>
</match>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
namespace NamedSubcores
{
/// <summary>
/// NamedMechComp is added to mech pawns, allowing us to track the pawn that was scanned into the subcore.
/// BasePatternComp implements the common features for the subcore and mech components.
/// </summary>
public class NamedMechComp : ThingComp
public class BasePatternComp : ThingComp
{
/// <summary>
/// OccupantName tracks the name of the pawn scanned into the mech's subcore.
/// PatternName tracks the name of the pawn scanned.
/// </summary>
public Name OccupantName;
public Name PatternName = null;

/// <summary>
/// PostExposeData is used to save our component state.
/// </summary>
public override void PostExposeData()
{
Scribe_Deep.Look(ref OccupantName, "occupantName");
Scribe_Deep.Look(ref PatternName, "patternName");
base.PostExposeData();
}

Expand All @@ -27,7 +27,7 @@ public override void PostExposeData()
/// <returns></returns>
public override string CompInspectStringExtra()
{
return "Occupant".Translate() + ": " + (OccupantName?.ToStringShort ?? "Unknown".Translate());
return "Pattern".Translate() + ": " + (PatternName?.ToStringShort ?? "Unknown".Translate());
}
}
}
32 changes: 32 additions & 0 deletions Source/NamedSubcores/Comps/MechGestatorComp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Verse;

namespace NamedSubcores
{
/// <summary>
/// MechGestatorComp is added to mech gestators and allows us to track the subcore component during gestation.
/// </summary>
public class MechGestatorComp : ThingComp
{
/// <summary>
/// SubcoreComp tracks the subcore component.
/// </summary>
public SubcorePatternComp SubcoreComp;

/// <summary>
/// PostExposeData is used to save our component state.
/// </summary>
public override void PostExposeData()
{
Scribe_Deep.Look(ref SubcoreComp, "subcoreComp");
base.PostExposeData();
}

/// <summary>
/// Reset allows the component to be reset for reuse.
/// </summary>
public void Reset()
{
SubcoreComp = null;
}
}
}
7 changes: 7 additions & 0 deletions Source/NamedSubcores/Comps/MechPatternComp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NamedSubcores
{
/// <summary>
/// MechPatternComp is added to mechanoids and is used to track the pawn scanned into the subcore.
/// </summary>
public class MechPatternComp : BasePatternComp { }
}
25 changes: 0 additions & 25 deletions Source/NamedSubcores/Comps/NamedMechGestatorComp.cs

This file was deleted.

34 changes: 0 additions & 34 deletions Source/NamedSubcores/Comps/NamedSubcoreComp.cs

This file was deleted.

7 changes: 7 additions & 0 deletions Source/NamedSubcores/Comps/SubcorePatternComp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NamedSubcores
{
/// <summary>
/// MechPatternComp is added to subcores and is used to track the pawn scanned into the subcore.
/// </summary>
public class SubcorePatternComp : BasePatternComp { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace NamedSubcores
{
/// <summary>
/// NamedSubcoreScannerComp is added to Building_SubcoreScanner,
/// allowing us to track when a subcore is ejected from the scanner.
/// SubcoreScannerComp is added to subcore scanners and allows us to track when a subcore is ejected.
/// </summary>
public class NamedSubcoreScannerComp : ThingComp
public class SubcoreScannerComp : ThingComp
{
/// <summary>
/// Ejected tracks whether a subcore has just been ejected.
/// </summary>
public bool Ejected = false;

/// <summary>
/// OccupantName tracks the name of the pawn in the scanner.
/// PatternName tracks the name of the pawn in the scanner.
/// </summary>
public Name OccupantName;

Expand All @@ -27,5 +26,14 @@ public override void PostExposeData()
Scribe_Deep.Look(ref OccupantName, "occupantName");
base.PostExposeData();
}

/// <summary>
/// Reset allows the component to be reset for reuse.
/// </summary>
public void Reset()
{
Ejected = false;
OccupantName = null;
}
}
}
16 changes: 8 additions & 8 deletions Source/NamedSubcores/Harmony/Harmony_Building_MechGestator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,34 @@ internal class Harmony_Building_MechGestator
internal static class Harmony_Building_MechGestator_Notify_AllGestationCyclesCompleted
{
/// <summary>
/// Prefix stores the named subcore component for later use.
/// Prefix stores the subcore component for later use.
/// </summary>
/// <param name="__instance"></param>
internal static void Prefix(Building_MechGestator __instance)
{
Func<Thing, bool> hasNamedSubcoreComp = (thing) => (thing?.TryGetComp<NamedSubcoreComp>() ?? null) != null;
static bool hasNamedSubcoreComp(Thing thing) => (thing?.TryGetComp<SubcorePatternComp>() ?? null) != null;
Thing subcore = __instance.innerContainer.FirstOrDefault(hasNamedSubcoreComp);
if (subcore == null) { return; }

NamedMechGestatorComp gestatorComp = __instance.GetComp<NamedMechGestatorComp>();
MechGestatorComp gestatorComp = __instance.GetComp<MechGestatorComp>();
if (gestatorComp == null) { return; }

gestatorComp.SubcoreOccupantName = subcore.TryGetComp<NamedSubcoreComp>()?.OccupantName;
gestatorComp.SubcoreComp = subcore.TryGetComp<SubcorePatternComp>();
}

/// <summary>
/// Postfix assigns the named subcore component to the new mech.
/// Postfix copies data from the subcore to the new mech.
/// </summary>
/// <param name="__instance"></param>
internal static void Postfix(Building_MechGestator __instance)
{
NamedMechGestatorComp gestatorComp = __instance.GetComp<NamedMechGestatorComp>();
MechGestatorComp gestatorComp = __instance.GetComp<MechGestatorComp>();
if (gestatorComp == null) { return; }

NamedMechComp mechComp = __instance.GestatingMech.GetComp<NamedMechComp>();
SubcorePatternComp mechComp = __instance.GestatingMech.GetComp<SubcorePatternComp>();
if (mechComp == null) { return; }

mechComp.OccupantName = gestatorComp.SubcoreOccupantName;
mechComp.PatternName = gestatorComp?.SubcoreComp?.PatternName;
}
}
}
Expand Down
50 changes: 23 additions & 27 deletions Source/NamedSubcores/Harmony/Harmony_Building_SubcoreScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static class Harmony_Building_SubcoreScanner_EjectContents
/// <param name="__instance"></param>
internal static void Postfix(Building_SubcoreScanner __instance)
{
__instance.GetComp<NamedSubcoreScannerComp>().Ejected = true;
__instance.GetComp<SubcoreScannerComp>().Ejected = true;
}
}

Expand All @@ -27,40 +27,39 @@ internal static void Postfix(Building_SubcoreScanner __instance)
internal static class Harmony_Building_SubcoreScanner_Tick
{
/// <summary>
/// Prefix stores the name of the current occupant for later use
/// Prefix stores the name of the current occupant for later use.
/// </summary>
/// <param name="__instance"></param>
internal static void Prefix(Building_SubcoreScanner __instance)
{
NamedSubcoreScannerComp scannerComp = __instance.GetComp<NamedSubcoreScannerComp>();
SubcoreScannerComp scannerComp = __instance.GetComp<SubcoreScannerComp>();
scannerComp.OccupantName = __instance?.Occupant?.Name ?? null;
}

/// <summary>
/// Postfix attempts to name the subcore if one was just ejected by the scanner
/// Postfix attempts to update the subcore if one was just ejected by the scanner.
/// </summary>
/// <param name="__instance"></param>
internal static void Postfix(Building_SubcoreScanner __instance)
{
NamedSubcoreScannerComp scannerComp = __instance.GetComp<NamedSubcoreScannerComp>();
if (scannerComp.Ejected)
SubcoreScannerComp scannerComp = __instance.GetComp<SubcoreScannerComp>();
if (!scannerComp.Ejected) { return; }

SubcorePatternComp subcoreComp = TryGetSubcoreComp(__instance);
if (subcoreComp != null)
{
NamedSubcoreComp subcoreComp = TryGetSubcoreComp(__instance);
if (subcoreComp != null)
{
subcoreComp.OccupantName = scannerComp.OccupantName;
scannerComp.OccupantName = null;
}
scannerComp.Ejected = false;
subcoreComp.PatternName = scannerComp.OccupantName;
}

scannerComp.Reset();
}

/// <summary>
/// Try to find the subcore ejected from the scanner and return the component for it
/// Try to find the subcore ejected from the scanner and return the component for it.
/// </summary>
/// <param name="scanner"></param>
/// <returns></returns>
static NamedSubcoreComp TryGetSubcoreComp(Building_SubcoreScanner scanner)
static SubcorePatternComp TryGetSubcoreComp(Building_SubcoreScanner scanner)
{
ThingDef subcoreDef = scanner.def.defName switch
{
Expand All @@ -69,21 +68,18 @@ static NamedSubcoreComp TryGetSubcoreComp(Building_SubcoreScanner scanner)
_ => null
};

if (subcoreDef != null)
{
static bool validator(Thing subcore)
{
NamedSubcoreComp comp = subcore.TryGetComp<NamedSubcoreComp>();
if (comp == null) { return false; }
return comp.OccupantName == null;
}

Thing subcore = GenClosest.ClosestThingReachable(scanner.InteractionCell, scanner.Map, ThingRequest.ForDef(subcoreDef), Verse.AI.PathEndMode.ClosestTouch, TraverseParms.For(TraverseMode.ByPawn), 9999, validator);
if (subcoreDef == null) { return null; }

return subcore?.TryGetComp<NamedSubcoreComp>() ?? null;
static bool validator(Thing subcore)
{
SubcorePatternComp comp = subcore.TryGetComp<SubcorePatternComp>();
if (comp == null) { return false; }
return comp.PatternName == null;
}

return null;
Thing subcore = GenClosest.ClosestThingReachable(scanner.InteractionCell, scanner.Map, ThingRequest.ForDef(subcoreDef), Verse.AI.PathEndMode.ClosestTouch, TraverseParms.For(TraverseMode.ByPawn), 9999, validator);

return subcore?.TryGetComp<SubcorePatternComp>() ?? null;
}
}
}
Loading

0 comments on commit 1939ce6

Please sign in to comment.