Skip to content

Commit

Permalink
move ICommunicalbe impl to separate class file
Browse files Browse the repository at this point in the history
  • Loading branch information
Arakos committed Apr 29, 2022
1 parent 1771791 commit 243b6b0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 63 deletions.
1 change: 1 addition & 0 deletions Source/CalllATrader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="CallATrader.cs" />
<Compile Include="rimworld\ChoiceLetter_SelectTrader.cs" />
<Compile Include="Constants.cs" />
<Compile Include="rimworld\ICommunicable_OrbitalTradersHub.cs" />
<Compile Include="rimworld\IncidentWorker_OrbitalTraderVisitingOffer.cs" />
<Compile Include="Settings.cs" />
<Compile Include="integrations\HarmonyPatch.cs" />
Expand Down
65 changes: 2 additions & 63 deletions Source/integrations/HarmonyPatch.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Collections.Generic;
using HarmonyLib;
using RimWorld;
using UnityEngine;
using Verse;
using Verse.AI;

namespace Arakos.CallATrader
{

[HarmonyPatch(typeof(Building_CommsConsole), nameof(Building_CommsConsole.GetCommTargets))]
public static class Building_CommsConsole_Patch
{
private static readonly ICommunicable callTraderCommunicable = new CallATraderCommunicable();
private static readonly ICommunicable callTraderCommunicable = new ICommunicable_OrbitalTradersHub();

// injects additional option into the ICommunicable list on the comms console
[HarmonyPostfix]
Expand All @@ -23,61 +19,4 @@ public static void GetCommTargetsPatch(Pawn myPawn, ref IEnumerable<ICommunicabl
}
}


public class CallATraderCommunicable : ICommunicable
{
private readonly FloatMenuOption callTraderOption = new FloatMenuOption("", null, Textures.ORBITAL_TRADER_HUB_ICON, Color.white, MenuOptionPriority.Default);

private Building_CommsConsole cachedConsole;

private Pawn cachedPawn;

// this method gets invoked permanently while the comms console options menu is open - hence use caching
public FloatMenuOption CommFloatMenuOption(Building_CommsConsole console, Pawn negotiator)
{

bool cacheValid = System.Object.Equals(cachedConsole, console) && System.Object.Equals(cachedPawn, negotiator);
cachedPawn = negotiator;
cachedConsole = console;

// get ticks the call trader action is disabled for
int disabledForTicks = CallATrader.state.traderRequestActionDisabledUntil - Find.TickManager.TicksAbs;

// must be done this way because the impl of disabled in FloatMenuOption is retarded
if (disabledForTicks > 0)
{
callTraderOption.Disabled = true;
callTraderOption.Label = (Constants.MOD_PREFIX + ".console.label.disabled").Translate(GenDate.ToStringTicksToPeriod(disabledForTicks, shortForm: false));
}
else if (callTraderOption.Disabled || !cacheValid)
{
callTraderOption.Disabled = false;
callTraderOption.Label = (Constants.MOD_PREFIX + ".console.label").Translate();
callTraderOption.action = () => negotiator.jobs.TryTakeOrderedJob(JobMaker.MakeJob(DefDatabase<JobDef>.GetNamed(Constants.JOB_DEF_NAME, true), console), JobTag.MiscWork);
FloatMenuUtility.DecoratePrioritizedTask(callTraderOption, negotiator, console);
}
return callTraderOption;
}

public string GetCallLabel()
{
return "";
}

public Faction GetFaction()
{
Log.Message("getfactioncalled");
return null;
}

public string GetInfoText()
{
return "";
}

public void TryOpenComms(Pawn negotiator)
{
Log.Message("tryopencomms " + negotiator);
}
}
}
65 changes: 65 additions & 0 deletions Source/rimworld/ICommunicable_OrbitalTradersHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using RimWorld;
using UnityEngine;
using Verse;
using Verse.AI;

namespace Arakos.CallATrader
{

public class ICommunicable_OrbitalTradersHub : ICommunicable
{
private readonly FloatMenuOption callTraderOption = new FloatMenuOption("", null, Textures.ORBITAL_TRADER_HUB_ICON, Color.white, MenuOptionPriority.Default);

private Building_CommsConsole cachedConsole;

private Pawn cachedPawn;

// this method gets invoked permanently while the comms console options menu is open - hence use caching
public FloatMenuOption CommFloatMenuOption(Building_CommsConsole console, Pawn negotiator)
{

bool cacheValid = System.Object.Equals(cachedConsole, console) && System.Object.Equals(cachedPawn, negotiator);
cachedPawn = negotiator;
cachedConsole = console;

// get ticks the call trader action is disabled for
int disabledForTicks = CallATrader.state.traderRequestActionDisabledUntil - Find.TickManager.TicksAbs;

// must be done this way because the impl of disabled in FloatMenuOption is retarded
if (disabledForTicks > 0)
{
callTraderOption.Disabled = true;
callTraderOption.Label = (Constants.MOD_PREFIX + ".console.label.disabled").Translate(GenDate.ToStringTicksToPeriod(disabledForTicks, shortForm: false));
}
else if (callTraderOption.Disabled || !cacheValid)
{
callTraderOption.Disabled = false;
callTraderOption.Label = (Constants.MOD_PREFIX + ".console.label").Translate();
callTraderOption.action = () => negotiator.jobs.TryTakeOrderedJob(JobMaker.MakeJob(DefDatabase<JobDef>.GetNamed(Constants.JOB_DEF_NAME, true), console), JobTag.MiscWork);
FloatMenuUtility.DecoratePrioritizedTask(callTraderOption, negotiator, console);
}
return callTraderOption;
}

public string GetCallLabel()
{
return "";
}

public Faction GetFaction()
{
Log.Message("getfactioncalled");
return null;
}

public string GetInfoText()
{
return "";
}

public void TryOpenComms(Pawn negotiator)
{
Log.Message("tryopencomms " + negotiator);
}
}
}

0 comments on commit 243b6b0

Please sign in to comment.