From 0965a222f6985fa9a67a7144c89539deec4d327d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Kriv=C3=A1cs=20Schr=C3=B8der?= Date: Thu, 9 May 2024 11:55:44 +0200 Subject: [PATCH] fix: bug in scribe code for TradeShipData Which meant that if you loaded a game while there was an orbital trader active with a grace time set, "time until comms close" got reset, and you could once again make trades with the trader even during the grace period. Similarly, you could initiate a second trade with a trader while another trade was already ongoing under the same conditions. --- CHANGELOG.md | 4 ++++ .../Core/RealisticOrbitalTradeMod.cs | 8 +++++++ .../Core/TradeShipData.cs | 24 ++++++++++++++++++- .../Patch/Rimworld_TradeShip_ExposeData.cs | 2 +- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb51533..fa92f6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- There was a bug in the code that loaded trade information associated with trade ships, which meant that if you loaded a game while there was an orbital trader active with a grace time set, "time until comms close" got reset, and you could once again make trades with the trader even during the grace period. Similarly, you could initiate a second trade with a trader while another trade was already ongoing under the same conditions. + ## [0.2.0] - 2024-05-04 ### Added diff --git a/Source/RealisticOrbitalTrade/Core/RealisticOrbitalTradeMod.cs b/Source/RealisticOrbitalTrade/Core/RealisticOrbitalTradeMod.cs index 2bf6610..8211387 100644 --- a/Source/RealisticOrbitalTrade/Core/RealisticOrbitalTradeMod.cs +++ b/Source/RealisticOrbitalTrade/Core/RealisticOrbitalTradeMod.cs @@ -43,6 +43,14 @@ public static void Message(string msg) Log.Message("[Realistic Orbital Trade] " + msg); } + public static void Dev(string msg) + { + if (Prefs.DevMode) + { + Log.Message("[Realistic Orbital Trade][DEV] " + msg); + } + } + public static void Warning(string msg) { Log.Warning("[Realistic Orbital Trade] " + msg); diff --git a/Source/RealisticOrbitalTrade/Core/TradeShipData.cs b/Source/RealisticOrbitalTrade/Core/TradeShipData.cs index efb90c5..20830f9 100644 --- a/Source/RealisticOrbitalTrade/Core/TradeShipData.cs +++ b/Source/RealisticOrbitalTrade/Core/TradeShipData.cs @@ -12,6 +12,15 @@ internal class TradeShipData : IExposable public int ticksUntilCommsClosed = -1; public TradeAgreement? activeTradeAgreement; + public TradeShipData(TradeShip tradeShip, bool getting) + { + RealisticOrbitalTradeMod.Dev($"Instantiating new TradeShipData instance for {tradeShip} (getting? {getting})"); + if (!getting) + { + tradeShip.SetData(this); + } + } + public void ExposeData() { Scribe_Values.Look(ref ticksUntilCommsClosed, "ticksUntilCommsClosed", 0); @@ -27,10 +36,23 @@ public override string ToString() internal static class TradeShipExtensions { + + internal static void SetData(this TradeShip tradeShip, TradeShipData tradeShipData) + { + if (_tradeShipExtra.TryGetValue(tradeShip, out _)) + { + _tradeShipExtra.Remove(tradeShip); + } + _tradeShipExtra.Add(tradeShip, tradeShipData); + } private static ConditionalWeakTable _tradeShipExtra = new(); public static TradeShipData GetData(this TradeShip tradeShip) { - return _tradeShipExtra.GetValue(tradeShip, (tradeShip) => new()); + return _tradeShipExtra.GetValue(tradeShip, (tradeShip) => + { + RealisticOrbitalTradeMod.Dev($"Generating new TradeShipData value for {tradeShip} ({tradeShip.GetHashCode()} -- {tradeShip.GetUniqueLoadID()})"); + return new(tradeShip, true); + }); } } diff --git a/Source/RealisticOrbitalTrade/Patch/Rimworld_TradeShip_ExposeData.cs b/Source/RealisticOrbitalTrade/Patch/Rimworld_TradeShip_ExposeData.cs index 6177acd..ee4a08e 100644 --- a/Source/RealisticOrbitalTrade/Patch/Rimworld_TradeShip_ExposeData.cs +++ b/Source/RealisticOrbitalTrade/Patch/Rimworld_TradeShip_ExposeData.cs @@ -11,6 +11,6 @@ internal static class Rimworld_TradeShip_ExposeData private static void Postfix(TradeShip __instance) { var extra = __instance.GetData(); - Scribe_Deep.Look(ref extra, "realisticOrbitalTradeData"); + Scribe_Deep.Look(ref extra, "realisticOrbitalTradeData", new object[] { __instance, false }); } }