Skip to content

Commit

Permalink
Implement mod settings
Browse files Browse the repository at this point in the history
  • Loading branch information
eth0net committed Jan 30, 2023
1 parent 502efc2 commit d7ccc58
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 93 deletions.
Binary file modified Assemblies/SubcoreInfo.dll
Binary file not shown.
1 change: 1 addition & 0 deletions Languages/English/Keyed/SubcoreInfo.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LanguageData>
<SubcoreInfo>SubcoreInfo</SubcoreInfo>
<Pattern>Pattern</Pattern>
<Unknown>Unknown</Unknown>
</LanguageData>
3 changes: 2 additions & 1 deletion Source/SubcoreInfo/Comps/CompMechInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace SubcoreInfo.Comps
/// <summary>
/// CompMechInfo is added to mechanoids and is used to track the pawn scanned into the subcore.
/// </summary>
public class CompMechInfo : CompPatternInfo {
public class CompMechInfo : CompPatternInfo
{
/// <summary>
/// Disassembling tracks when a mech is being disassembled.
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion Source/SubcoreInfo/Comps/CompSubcoreInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class CompSubcoreInfo : CompPatternInfo
/// <returns></returns>
public override bool AllowStackWith(Thing other)
{
if (base.AllowStackWith(other) == false) { return false; };
if (!base.AllowStackWith(other)) { return false; }

if (!SubcoreInfoSettings.separatePatternStacks) { return true; }

CompSubcoreInfo otherComp = other?.TryGetComp<CompSubcoreInfo>();
if (otherComp == null) { return false; }
Expand All @@ -33,6 +35,8 @@ public override void PostPostGeneratedForTrader(TraderKindDef trader, int forTil
{
base.PostPostGeneratedForTrader(trader, forTile, forFaction);

if (!SubcoreInfoSettings.randomTraderPatterns) { return; }

Gender gender = Rand.Bool ? Gender.Male : Gender.Female;
PawnKindDef pawnKind = forFaction.RandomPawnKind();
RulePackDef pawnKindNameMaker = pawnKind.GetNameMaker(gender);
Expand Down
56 changes: 27 additions & 29 deletions Source/SubcoreInfo/Harmony/Harmony_Building_MechGestator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,41 @@

namespace SubcoreInfo.Harmony
{
internal class Harmony_Building_MechGestator
/// <summary>
/// Harmony_Building_MechGestator_Notify_AllGestationCyclesCompleted patches mech gestators to use our component on completion.
/// </summary>
[HarmonyPatch(typeof(Building_MechGestator), nameof(Building_MechGestator.Notify_AllGestationCyclesCompleted))]
internal static class Harmony_Building_MechGestator_Notify_AllGestationCyclesCompleted
{
/// <summary>
/// Harmony_Building_MechGestator_Notify_AllGestationCyclesCompleted patches mech gestators to use our component on completion.
/// Prefix stores the subcore pattern for later use.
/// </summary>
[HarmonyPatch(typeof(Building_MechGestator), nameof(Building_MechGestator.Notify_AllGestationCyclesCompleted))]
internal static class Harmony_Building_MechGestator_Notify_AllGestationCyclesCompleted
/// <param name="__instance"></param>
/// <param name="__state"></param>
internal static void Prefix(Building_MechGestator __instance, ref Name __state)
{
/// <summary>
/// Prefix stores the subcore pattern for later use.
/// </summary>
/// <param name="__instance"></param>
/// <param name="__state"></param>
internal static void Prefix(Building_MechGestator __instance, ref Name __state)
{
static bool hasNamedSubcoreComp(Thing thing) => (thing?.TryGetComp<CompSubcoreInfo>() ?? null) != null;
Thing subcore = __instance.innerContainer.FirstOrDefault(hasNamedSubcoreComp);
if (subcore == null) { return; }
Thing subcore = __instance.innerContainer.FirstOrDefault(HasCompSubcoreInfo);
if (subcore == null) { return; }

CompSubcoreInfo subcoreComp = subcore.TryGetComp<CompSubcoreInfo>();
if (subcoreComp == null) { return; }
CompSubcoreInfo subcoreComp = subcore.TryGetComp<CompSubcoreInfo>();
if (subcoreComp == null) { return; }

__state = subcoreComp.PatternName;
}
__state = subcoreComp.PatternName;
}

/// <summary>
/// Postfix copies data from the subcore to the new mech.
/// </summary>
/// <param name="__instance"></param>
/// <param name="__state"></param>
internal static void Postfix(Building_MechGestator __instance, Name __state)
{
CompMechInfo mechComp = __instance.GestatingMech.GetComp<CompMechInfo>();
if (mechComp == null) { return; }
/// <summary>
/// Postfix copies data from the subcore to the new mech.
/// </summary>
/// <param name="__instance"></param>
/// <param name="__state"></param>
internal static void Postfix(Building_MechGestator __instance, Name __state)
{
CompMechInfo mechComp = __instance.GestatingMech.GetComp<CompMechInfo>();
if (mechComp == null) { return; }

mechComp.PatternName = __state;
}
mechComp.PatternName = __state;
}

static bool HasCompSubcoreInfo(Thing thing) => thing?.TryGetComp<CompSubcoreInfo>() != null;
}
}
8 changes: 4 additions & 4 deletions Source/SubcoreInfo/Harmony/Harmony_Building_SubcoreScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static class Harmony_Building_SubcoreScanner_Tick
/// <param name="__state"></param>
internal static void Prefix(Building_SubcoreScanner __instance, ref Name __state)
{
__state = __instance?.Occupant?.Name ?? null;
__state = __instance?.Occupant?.Name;
}

/// <summary>
Expand All @@ -45,13 +45,13 @@ internal static void Prefix(Building_SubcoreScanner __instance, ref Name __state
internal static void Postfix(Building_SubcoreScanner __instance, Name __state)
{
CompSubcoreInfo comp = TryGetSubcoreComp(__instance);
if (comp == null) return;
if (comp == null) { return; }

comp.PatternName = __state;
}

/// <summary>
/// Try to find kekethe 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>
Expand All @@ -74,7 +74,7 @@ static bool validator(Thing subcore)

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

return subcore?.TryGetComp<CompSubcoreInfo>() ?? null;
return subcore?.TryGetComp<CompSubcoreInfo>();
}
}
}
29 changes: 13 additions & 16 deletions Source/SubcoreInfo/Harmony/Harmony_JobDriver_DisassembleMech.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@

namespace SubcoreInfo.Harmony
{
internal class Harmony_JobDriver_DisassembleMech
/// <summary>
/// Harmony_JobDriver_DisassembleMech patches mech disassembling to use our component.
/// </summary>
[HarmonyPatch(typeof(JobDriver_DisassembleMech), "MakeNewToils")]
internal static class Harmony_JobDriver_DisassembleMech_MakeNewToils
{
/// <summary>
/// Harmony_JobDriver_DisassembleMech patches mech disassembling to use our component.
/// Prefix is called before the original method.
/// </summary>
[HarmonyPatch(typeof(JobDriver_DisassembleMech), "MakeNewToils")]
internal static class Harmony_JobDriver_DisassembleMech_MakeNewToils
/// <param name="__instance"></param>
internal static void Prefix(JobDriver_DisassembleMech __instance)
{
/// <summary>
/// Prefix is called before the original method.
/// </summary>
/// <param name="__instance"></param>
internal static void Prefix(JobDriver_DisassembleMech __instance)
{
Pawn mech = (Pawn)__instance.job.GetTarget(TargetIndex.A).Thing;
if (mech == null) { return; }
Pawn mech = (Pawn)__instance.job.GetTarget(TargetIndex.A).Thing;
if (mech == null) { return; }

CompMechInfo mechComp = mech.GetComp<CompMechInfo>();
if (mechComp == null) { return; }
CompMechInfo mechComp = mech.GetComp<CompMechInfo>();
if (mechComp == null) { return; }

mechComp.Disassembling = true;
}
mechComp.Disassembling = true;
}
}
}
69 changes: 33 additions & 36 deletions Source/SubcoreInfo/Harmony/Harmony_Pawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,50 @@

namespace SubcoreInfo.Harmony
{
internal class Harmony_Pawn
/// <summary>
/// Harmony_JobDriver_DisassembleMech patches mech disassembling to use our component.
/// </summary>
[HarmonyPatch(typeof(Pawn), nameof(Pawn.Kill))]
internal static class Harmony_Pawn_Kill
{
/// <summary>
/// Harmony_JobDriver_DisassembleMech patches mech disassembling to use our component.
/// Prefix is called before the original method.
/// </summary>
[HarmonyPatch(typeof(Pawn), nameof(Pawn.Kill))]
internal static class Harmony_Pawn_Kill
/// <param name="__instance"></param>
internal static void Prefix(Pawn __instance)
{
/// <summary>
/// Prefix is called before the original method.
/// </summary>
/// <param name="__instance"></param>
internal static void Prefix(Pawn __instance)
{
if (__instance == null || !__instance.IsColonyMech) { return; }
if (__instance == null || !__instance.IsColonyMech) { return; }

CompMechInfo mechComp = __instance.GetComp<CompMechInfo>();
if (mechComp == null || !mechComp.Disassembling) { return; }
CompMechInfo mechComp = __instance.GetComp<CompMechInfo>();
if (mechComp == null || !mechComp.Disassembling) { return; }

CompSubcoreInfo subcoreComp = TryGetSubcoreComp(__instance);
if (subcoreComp == null) { return; }
CompSubcoreInfo subcoreComp = TryGetSubcoreComp(__instance);
if (subcoreComp == null) { return; }

subcoreComp.PatternName = mechComp.PatternName;
mechComp.Disassembling = false;
}
subcoreComp.PatternName = mechComp.PatternName;
mechComp.Disassembling = false;
}

/// <summary>
/// Try to find the subcore dropped during disassembly and return the component for it.
/// </summary>
/// <param name="scanner"></param>
/// <returns></returns>
static CompSubcoreInfo TryGetSubcoreComp(Pawn mech)
{
ThingDefCountClass subcoreClass = MechanitorUtility.IngredientsFromDisassembly(mech.def).FirstOrDefault((ThingDefCountClass thing) => thing.thingDef.defName == "SubcoreRegular" || thing.thingDef.defName == "SubcoreHigh");
if (subcoreClass == null) { return null; }
/// <summary>
/// Try to find the subcore dropped during disassembly and return the component for it.
/// </summary>
/// <param name="scanner"></param>
/// <returns></returns>
static CompSubcoreInfo TryGetSubcoreComp(Pawn mech)
{
ThingDefCountClass subcoreClass = MechanitorUtility.IngredientsFromDisassembly(mech.def).FirstOrDefault((ThingDefCountClass thing) => thing.thingDef.defName == "SubcoreRegular" || thing.thingDef.defName == "SubcoreHigh");
if (subcoreClass == null) { return null; }

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

Thing subcore = GenClosest.ClosestThingReachable(mech.Position, mech.Map, ThingRequest.ForDef(subcoreClass.thingDef), PathEndMode.ClosestTouch, TraverseParms.For(TraverseMode.ByPawn), 9999, validator);
Thing subcore = GenClosest.ClosestThingReachable(mech.Position, mech.Map, ThingRequest.ForDef(subcoreClass.thingDef), PathEndMode.ClosestTouch, TraverseParms.For(TraverseMode.ByPawn), 9999, validator);

return subcore?.TryGetComp<CompSubcoreInfo>() ?? null;
}
return subcore?.TryGetComp<CompSubcoreInfo>() ?? null;
}
}
}
45 changes: 39 additions & 6 deletions Source/SubcoreInfo/SubcoreInfo.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
using Verse;
using RimWorld;
using UnityEngine;
using Verse;

namespace SubcoreInfo
{
/// <summary>
/// SubcoreInfo static class to load up the mod and initialise everything.
/// </summary>
[StaticConstructorOnStartup]
public static class SubcoreInfo
public class SubcoreInfo : Mod
{
/// <summary>
/// SubcoreInfo settings reference.
/// </summary>
internal static SubcoreInfoSettings settings;

/// <summary>
/// SubcoreInfo constructor to patch things using harmony.
/// </summary>
static SubcoreInfo()
public SubcoreInfo(ModContentPack content) : base(content)
{
settings = GetSettings<SubcoreInfoSettings>();
new HarmonyLib.Harmony("eth0net.SubcoreInfo").PatchAll();
}

/// <summary>
/// DoSettingsWindowContents configures the settings window.
/// </summary>
/// <param name="inRect"></param>
public override void DoSettingsWindowContents(Rect inRect)
{
Listing_Standard listing = new();

listing.Begin(inRect);

listing.CheckboxLabeled("Separate subcore stacks by pattern", ref SubcoreInfoSettings.separatePatternStacks);
listing.CheckboxLabeled("Random patterns on trader subcores", ref SubcoreInfoSettings.randomTraderPatterns);

listing.End();

base.DoSettingsWindowContents(inRect);
}

/// <summary>
/// SettingsCategory returns the name of the settings category.
/// </summary>
/// <returns></returns>
public override string SettingsCategory()
{
var harmony = new HarmonyLib.Harmony("eth0net.SubcoreInfo.harmony");
harmony.PatchAll();
return "SubcoreInfo".Translate();
}
}
}
1 change: 1 addition & 0 deletions Source/SubcoreInfo/SubcoreInfo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Harmony\Harmony_JobDriver_DisassembleMech.cs" />
<Compile Include="Harmony\Harmony_Pawn.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SubcoreInfoSettings.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
27 changes: 27 additions & 0 deletions Source/SubcoreInfo/SubcoreInfoSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Verse;

namespace SubcoreInfo
{
internal class SubcoreInfoSettings : ModSettings
{
/// <summary>
/// Separate subcore stacks by pattern.
/// </summary>
public static bool separatePatternStacks = true;

/// <summary>
/// Generate random patterns for trader subcores.
/// </summary>
public static bool randomTraderPatterns = true;

/// <summary>
/// ExposeData saves and loads the settings.
/// </summary>
public override void ExposeData()
{
Scribe_Values.Look(ref separatePatternStacks, "separatePatternStacks", true, true);
Scribe_Values.Look(ref randomTraderPatterns, "randomTraderPatterns", true, true);
base.ExposeData();
}
}
}

0 comments on commit d7ccc58

Please sign in to comment.