From 243b6b0386437d7e05737f8604f75efafa9f2918 Mon Sep 17 00:00:00 2001 From: Lars Dehler Date: Fri, 29 Apr 2022 22:46:15 +0200 Subject: [PATCH] move ICommunicalbe impl to separate class file --- Source/CalllATrader.csproj | 1 + Source/integrations/HarmonyPatch.cs | 65 +------------------ .../ICommunicable_OrbitalTradersHub.cs | 65 +++++++++++++++++++ 3 files changed, 68 insertions(+), 63 deletions(-) create mode 100644 Source/rimworld/ICommunicable_OrbitalTradersHub.cs diff --git a/Source/CalllATrader.csproj b/Source/CalllATrader.csproj index ea1b685..a9e3b98 100644 --- a/Source/CalllATrader.csproj +++ b/Source/CalllATrader.csproj @@ -90,6 +90,7 @@ + diff --git a/Source/integrations/HarmonyPatch.cs b/Source/integrations/HarmonyPatch.cs index 3f89d26..56fbe6c 100644 --- a/Source/integrations/HarmonyPatch.cs +++ b/Source/integrations/HarmonyPatch.cs @@ -1,11 +1,7 @@ -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 { @@ -13,7 +9,7 @@ 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] @@ -23,61 +19,4 @@ public static void GetCommTargetsPatch(Pawn myPawn, ref IEnumerable 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.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); - } - } } diff --git a/Source/rimworld/ICommunicable_OrbitalTradersHub.cs b/Source/rimworld/ICommunicable_OrbitalTradersHub.cs new file mode 100644 index 0000000..ae83d80 --- /dev/null +++ b/Source/rimworld/ICommunicable_OrbitalTradersHub.cs @@ -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.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); + } + } +}