Skip to content

Commit

Permalink
fix: bug in scribe code for TradeShipData
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilyvion committed May 9, 2024
1 parent 7624243 commit 0965a22
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions Source/RealisticOrbitalTrade/Core/RealisticOrbitalTradeMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion Source/RealisticOrbitalTrade/Core/TradeShipData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<TradeShip, TradeShipData> _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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}

0 comments on commit 0965a22

Please sign in to comment.