-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathTrading.cs
806 lines (678 loc) · 26.9 KB
/
Trading.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
using HarmonyLib;
using Multiplayer.API;
using RimWorld;
using RimWorld.Planet;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Verse;
using Verse.AI;
using Verse.AI.Group;
namespace Multiplayer.Client
{
public class MpTradeSession : ExposableSession, ISessionWithTransferables, ISessionWithCreationRestrictions, ITickingSession
{
public static MpTradeSession current;
public ITrader trader;
public Pawn playerNegotiator;
public bool giftMode;
public MpTradeDeal deal;
public bool giftsOnly;
public string Label
{
get
{
if (trader is Pawn pawn)
return pawn.Faction.Name;
return trader.TraderName;
}
}
public override Map Map => playerNegotiator.Map;
public override bool IsSessionValid => trader != null && playerNegotiator != null;
public MpTradeSession(Map _) : base(null) { }
private MpTradeSession(ITrader trader, Pawn playerNegotiator, bool giftMode) : base(null)
{
this.trader = trader;
this.playerNegotiator = playerNegotiator;
this.giftMode = giftMode;
giftsOnly = giftMode;
}
public bool CanExistWith(Session other)
{
if (other is not MpTradeSession otherTrade)
return true;
// todo show error messages?
if (otherTrade.trader == trader)
return false;
if (otherTrade.playerNegotiator == playerNegotiator)
return false;
return true;
}
public static MpTradeSession TryCreate(ITrader trader, Pawn playerNegotiator, bool giftMode)
{
MpTradeSession session = new MpTradeSession(trader, playerNegotiator, giftMode);
// Return null if there was a conflicting session
if (!Multiplayer.WorldComp.sessionManager.AddSession(session))
return null;
try
{
CancelTradeDealReset.cancel = true;
SetTradeSession(session);
session.deal = new MpTradeDeal(session);
Thing permSilver = ThingMaker.MakeThing(ThingDefOf.Silver, null);
permSilver.stackCount = 0;
session.deal.permanentSilver = permSilver;
session.deal.AddToTradeables(permSilver, Transactor.Trader);
session.deal.AddAllTradeables();
session.StartWaitingJobs();
}
finally
{
SetTradeSession(null);
CancelTradeDealReset.cancel = false;
}
return session;
}
// todo come back to it when the map doesn't get paused during trading
private void StartWaitingJobs()
{
}
public bool ShouldCancel()
{
if (!trader.CanTradeNow)
return true;
if (trader is Pawn traderPawn)
{
if (!traderPawn.Spawned || !playerNegotiator.Spawned)
return true;
return traderPawn.Position.DistanceToSquared(playerNegotiator.Position) > 2 * 2;
}
if (trader is Settlement traderBase)
{
var caravan = playerNegotiator.GetCaravan();
if (caravan == null)
return true;
if (CaravanVisitUtility.SettlementVisitedNow(caravan) != traderBase)
return true;
}
return false;
}
[SyncMethod]
public void TryExecute()
{
bool executed = false;
try
{
SetTradeSession(this);
deal.recacheColony = true;
deal.recacheTrader = true;
deal.Recache();
executed = deal.TryExecute(out bool traded);
}
finally
{
SetTradeSession(null);
}
if (executed)
Multiplayer.WorldComp.RemoveTradeSession(this);
}
[SyncMethod]
public void Reset()
{
deal.tradeables.ForEach(t => t.countToTransfer = 0);
deal.uiShouldReset = UIShouldReset.Silent;
}
[SyncMethod]
public void ToggleGiftMode()
{
giftMode = !giftMode;
deal.tradeables.ForEach(t => t.countToTransfer = 0);
deal.uiShouldReset = UIShouldReset.Silent;
}
public static void SetTradeSession(MpTradeSession session)
{
SyncSessionWithTransferablesMarker.DrawnSessionWithTransferables = session;
current = session;
TradeSession.trader = session?.trader;
TradeSession.playerNegotiator = session?.playerNegotiator;
TradeSession.giftMode = session?.giftMode ?? false;
TradeSession.deal = session?.deal;
}
public void OpenWindow(bool sound = true)
{
int tab = Multiplayer.WorldComp.trading.IndexOf(this);
if (Find.WindowStack.IsOpen<TradingWindow>())
{
Find.WindowStack.WindowOfType<TradingWindow>().selectedTab = tab;
}
else
{
TradingWindow window = new TradingWindow() { selectedTab = tab };
if (!sound)
window.soundAppear = null;
Find.WindowStack.Add(window);
}
}
public void CloseWindow(bool sound = true)
{
int tab = Multiplayer.WorldComp.trading.IndexOf(this);
if (Find.WindowStack.IsOpen<TradingWindow>())
{
Find.WindowStack.TryRemove(typeof(TradingWindow), doCloseSound: sound);
}
}
public void Tick()
{
if (playerNegotiator.Spawned) return;
if (ShouldCancel())
Multiplayer.WorldComp.sessionManager.RemoveSession(this);
}
public override void ExposeData()
{
base.ExposeData();
ILoadReferenceable trader = (ILoadReferenceable)this.trader;
Scribe_References.Look(ref trader, "trader");
this.trader = (ITrader)trader;
Scribe_References.Look(ref playerNegotiator, "playerNegotiator");
Scribe_Values.Look(ref giftMode, "giftMode");
Scribe_Values.Look(ref giftsOnly, "giftsOnly");
Scribe_Deep.Look(ref deal, "tradeDeal", this);
if (Scribe.mode == LoadSaveMode.PostLoadInit)
Multiplayer.WorldComp.trading.AddDistinct(this);
}
public Transferable GetTransferableByThingId(int thingId)
{
for (int i = 0; i < deal.tradeables.Count; i++)
{
Tradeable tr = deal.tradeables[i];
if (tr.FirstThingColony?.thingIDNumber == thingId)
return tr;
if (tr.FirstThingTrader?.thingIDNumber == thingId)
return tr;
}
return null;
}
public void Notify_CountChanged(Transferable tr)
{
deal.caravanDirty = true;
}
public override bool IsCurrentlyPausing(Map map) => map == Map;
public override FloatMenuOption GetBlockingWindowOptions(ColonistBar.Entry entry)
{
if (playerNegotiator?.Map != entry.map)
return null;
return new FloatMenuOption("MpTradingSession".Translate(), () =>
{
SwitchToMapOrWorld(entry.map);
CameraJumper.TryJumpAndSelect(playerNegotiator);
Find.WindowStack.Add(new TradingWindow()
{ selectedTab = Multiplayer.WorldComp.trading.IndexOf(this) });
});
}
public override void PostAddSession() => Multiplayer.WorldComp.trading.Add(this);
public override void PostRemoveSession()
{
var index = Multiplayer.WorldComp.trading.IndexOf(this);
Multiplayer.WorldComp.trading.RemoveAt(index);
Find.WindowStack?.WindowOfType<TradingWindow>()?.Notify_RemovedSession(index);
}
}
public class MpTradeDeal : TradeDeal, IExposable
{
public MpTradeSession session;
private static HashSet<Thing> newThings = new HashSet<Thing>();
private static HashSet<Thing> oldThings = new HashSet<Thing>();
public UIShouldReset uiShouldReset;
public HashSet<Thing> recacheThings = new HashSet<Thing>();
public bool recacheColony;
public bool recacheTrader;
public bool ShouldRecache => recacheColony || recacheTrader || recacheThings.Count > 0;
public bool caravanDirty;
public Thing permanentSilver;
public MpTradeDeal(MpTradeSession session)
{
this.session = session;
}
public void Recache()
{
if (recacheColony)
CheckAddRemoveColony();
if (recacheTrader)
CheckAddRemoveTrader();
if (recacheThings.Count > 0)
CheckReassign();
UpdateCurrencyCount();
uiShouldReset = UIShouldReset.Full;
recacheThings.Clear();
recacheColony = false;
recacheTrader = false;
}
private void CheckAddRemoveColony()
{
foreach (Thing t in session.trader.ColonyThingsWillingToBuy(session.playerNegotiator))
newThings.Add(t);
for (int i = tradeables.Count - 1; i >= 0; i--)
{
Tradeable tradeable = tradeables[i];
int toRemove = 0;
for (int j = tradeable.thingsColony.Count - 1; j >= 0; j--)
{
Thing thingColony = tradeable.thingsColony[j];
if (!newThings.Contains(thingColony))
toRemove++;
else
oldThings.Add(thingColony);
}
if (toRemove == 0) continue;
if (toRemove == tradeable.thingsColony.Count + tradeable.thingsTrader.Count)
tradeables.RemoveAt(i);
else
tradeable.thingsColony.RemoveAll(t => !newThings.Contains(t));
}
foreach (Thing newThing in newThings)
if (!oldThings.Contains(newThing))
AddToTradeables(newThing, Transactor.Colony);
newThings.Clear();
oldThings.Clear();
}
private void CheckAddRemoveTrader()
{
newThings.Add(permanentSilver);
foreach (Thing t in session.trader.Goods)
newThings.Add(t);
for (int i = tradeables.Count - 1; i >= 0; i--)
{
Tradeable tradeable = tradeables[i];
int toRemove = 0;
for (int j = tradeable.thingsTrader.Count - 1; j >= 0; j--)
{
Thing thingTrader = tradeable.thingsTrader[j];
if (!newThings.Contains(thingTrader))
toRemove++;
else
oldThings.Add(thingTrader);
}
if (toRemove == 0) continue;
if (toRemove == tradeable.thingsColony.Count + tradeable.thingsTrader.Count)
tradeables.RemoveAt(i);
else
tradeable.thingsTrader.RemoveAll(t => !newThings.Contains(t));
}
foreach (Thing newThing in newThings)
if (!oldThings.Contains(newThing))
AddToTradeables(newThing, Transactor.Trader);
newThings.Clear();
oldThings.Clear();
}
private void CheckReassign()
{
for (int i = tradeables.Count - 1; i >= 0; i--)
{
Tradeable tradeable = tradeables[i];
CheckReassign(tradeable, Transactor.Colony);
CheckReassign(tradeable, Transactor.Trader);
if (recacheThings.Count == 0) break;
}
}
private void CheckReassign(Tradeable tradeable, Transactor side)
{
List<Thing> things = side == Transactor.Colony ? tradeable.thingsColony : tradeable.thingsTrader;
for (int j = things.Count - 1; j >= 1; j--)
{
Thing thing = things[j];
TransferAsOneMode mode = tradeable.TraderWillTrade ? TransferAsOneMode.Normal : TransferAsOneMode.InactiveTradeable;
if (recacheThings.Contains(thing))
{
if (!TransferableUtility.TransferAsOne(tradeable.AnyThing, thing, mode))
things.RemoveAt(j);
else
AddToTradeables(thing, side);
}
}
}
public void ExposeData()
{
Scribe_Deep.Look(ref permanentSilver, "permanentSilver");
Scribe_Collections.Look(ref tradeables, "tradeables", LookMode.Deep);
}
}
public enum UIShouldReset
{
None,
Silent,
Full
}
[HarmonyPatch(typeof(TradeDeal), nameof(TradeDeal.Reset))]
static class CancelTradeDealReset
{
public static bool cancel;
static bool Prefix() => !cancel && Scribe.mode != LoadSaveMode.LoadingVars;
}
[HarmonyPatch(typeof(WindowStack), nameof(WindowStack.Add))]
static class CancelDialogTrade
{
static bool Prefix(Window window)
{
if (window is Dialog_Trade && (Multiplayer.ExecutingCmds || Multiplayer.Ticking))
return false;
return true;
}
}
[HarmonyPatch(typeof(Dialog_Trade), MethodType.Constructor)]
[HarmonyPatch(new[] { typeof(Pawn), typeof(ITrader), typeof(bool) })]
static class DialogTradeCtorPatch
{
static bool Prefix(Pawn playerNegotiator, ITrader trader, bool giftsOnly)
{
if (Multiplayer.ExecutingCmds || Multiplayer.Ticking)
{
MpTradeSession trade = MpTradeSession.TryCreate(trader, playerNegotiator, giftsOnly);
if (trade != null)
{
if (playerNegotiator.Map == Find.CurrentMap && playerNegotiator.CurJob.loadID == SyncMethods.tradeJobStartedByMe)
{
SyncMethods.tradeJobStartedByMe = -1;
trade.OpenWindow();
}
else if (trader is Settlement && Find.World.renderer.wantedMode == WorldRenderMode.Planet)
{
trade.OpenWindow();
}
}
return false;
}
return true;
}
}
[HarmonyPatch(typeof(IncidentWorker_TraderCaravanArrival), nameof(IncidentWorker_TraderCaravanArrival.TryExecuteWorker))]
static class ArriveAtCenter
{
static void Prefix(IncidentParms parms)
{
//if (MpVersion.IsDebug && Prefs.DevMode)
// parms.spawnCenter = (parms.target as Map).Center;
}
}
[HarmonyPatch(typeof(TradeDeal), nameof(TradeDeal.TryExecute))]
static class NullCheckDialogTrade
{
static IEnumerable<CodeInstruction> Transpiler(ILGenerator gen, IEnumerable<CodeInstruction> e)
{
List<CodeInstruction> insts = new List<CodeInstruction>(e);
LocalBuilder local = gen.DeclareLocal(typeof(Dialog_Trade));
for (int i = 0; i < insts.Count; i++)
{
CodeInstruction inst = insts[i];
yield return inst;
if (inst.opcode == OpCodes.Callvirt && ((MethodInfo)inst.operand).Name == nameof(WindowStack.WindowOfType))
{
Label label = gen.DefineLabel();
insts[i + 2].labels.Add(label);
yield return new CodeInstruction(OpCodes.Stloc, local);
yield return new CodeInstruction(OpCodes.Ldloc, local);
yield return new CodeInstruction(OpCodes.Brfalse, label);
yield return new CodeInstruction(OpCodes.Ldloc, local);
}
}
}
}
[HarmonyPatch(typeof(Reachability), nameof(Reachability.ClearCache))]
static class ReachabilityChanged
{
static void Postfix(Reachability __instance)
{
if (Multiplayer.Client != null)
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.map);
}
}
[HarmonyPatch(typeof(Area_Home), nameof(Area_Home.Set))]
static class AreaHomeChanged
{
static void Postfix(Area_Home __instance)
{
if (Multiplayer.Client != null)
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.Map);
}
}
[HarmonyPatch]
static class HaulDestinationChanged
{
static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(HaulDestinationManager), nameof(HaulDestinationManager.AddHaulDestination));
yield return AccessTools.Method(typeof(HaulDestinationManager), nameof(HaulDestinationManager.RemoveHaulDestination));
yield return AccessTools.Method(typeof(HaulDestinationManager), nameof(HaulDestinationManager.SetCellFor));
yield return AccessTools.Method(typeof(HaulDestinationManager), nameof(HaulDestinationManager.ClearCellFor));
}
static void Postfix(HaulDestinationManager __instance)
{
if (Multiplayer.Client != null)
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.map);
}
}
[HarmonyPatch(typeof(CompRottable), nameof(CompRottable.StageChanged))]
static class RottableStageChanged
{
static void Postfix(CompRottable __instance)
{
if (Multiplayer.Client == null) return;
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.parent.Map);
}
}
[HarmonyPatch]
static class ListerThingsChangedItem
{
static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(ListerThings), nameof(ListerThings.Add));
yield return AccessTools.Method(typeof(ListerThings), nameof(ListerThings.Remove));
}
static void Postfix(ListerThings __instance, Thing t)
{
if (Multiplayer.Client == null) return;
if (t.def.category == ThingCategory.Item && ListerThings.EverListable(t.def, __instance.use))
Multiplayer.WorldComp.DirtyColonyTradeForMap(t.Map);
}
}
[HarmonyPatch]
static class PawnDownedStateChanged
{
static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(Pawn_HealthTracker), nameof(Pawn_HealthTracker.MakeDowned));
yield return AccessTools.Method(typeof(Pawn_HealthTracker), nameof(Pawn_HealthTracker.MakeUndowned));
}
static void Postfix(Pawn_HealthTracker __instance)
{
if (Multiplayer.Client != null)
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.pawn.Map);
}
}
[HarmonyPatch(typeof(CompPowerTrader))]
[HarmonyPatch(nameof(CompPowerTrader.PowerOn), MethodType.Setter)]
static class OrbitalTradeBeaconPowerChanged
{
static void Postfix(CompPowerTrader __instance, bool value)
{
if (Multiplayer.Client == null) return;
if (__instance.parent is not Building_OrbitalTradeBeacon) return;
if (value == __instance.powerOnInt) return;
if (!Multiplayer.WorldComp.trading.Any(t => t.trader is TradeShip)) return;
// For trade ships
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.parent.Map);
}
}
[HarmonyPatch(typeof(Thing))]
[HarmonyPatch(nameof(Thing.HitPoints), MethodType.Setter)]
static class ThingHitPointsChanged
{
static void Prefix(Thing __instance, int value, ref bool __state)
{
if (Multiplayer.Client == null) return;
__state = __instance.def.category == ThingCategory.Item && value != __instance.hitPointsInt;
}
static void Postfix(Thing __instance, bool __state)
{
if (__state)
Multiplayer.WorldComp.DirtyTradeForSpawnedThing(__instance);
}
}
[HarmonyPatch]
static class ThingOwner_ChangedPatch
{
static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(ThingOwner), nameof(ThingOwner.NotifyAdded));
yield return AccessTools.Method(typeof(ThingOwner), nameof(ThingOwner.NotifyAddedAndMergedWith));
yield return AccessTools.Method(typeof(ThingOwner), nameof(ThingOwner.NotifyRemoved));
}
static void Postfix(ThingOwner __instance)
{
if (Multiplayer.Client == null) return;
if (__instance.owner is Pawn_InventoryTracker inv)
{
ITrader trader = null;
if (inv.pawn.GetLord()?.LordJob is LordJob_TradeWithColony lordJob)
// Carrier inventory changed
trader = lordJob.lord.ownedPawns.FirstOrDefault(p => p.GetTraderCaravanRole() == TraderCaravanRole.Trader);
else if (inv.pawn.trader != null)
// Trader inventory changed
trader = inv.pawn;
if (trader != null)
Multiplayer.WorldComp.DirtyTraderTradeForTrader(trader);
}
else if (__instance.owner is Settlement_TraderTracker trader)
{
Multiplayer.WorldComp.DirtyTraderTradeForTrader(trader.settlement);
}
else if (__instance.owner is TradeShip ship)
{
Multiplayer.WorldComp.DirtyTraderTradeForTrader(ship);
}
}
}
[HarmonyPatch]
static class Lord_TradeChanged
{
static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(Lord), nameof(Lord.AddPawn));
yield return AccessTools.Method(typeof(Lord), nameof(Lord.Notify_PawnLost));
}
static void Postfix(Lord __instance)
{
if (Multiplayer.Client == null) return;
if (__instance.LordJob is LordJob_TradeWithColony)
{
// Chattel changed
ITrader trader = __instance.ownedPawns.FirstOrDefault(p => p.GetTraderCaravanRole() == TraderCaravanRole.Trader);
Multiplayer.WorldComp.DirtyTraderTradeForTrader(trader);
}
else if (__instance.LordJob is LordJob_PrisonBreak)
{
// Prisoners in a break can't be sold
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.Map);
}
}
}
[HarmonyPatch]
static class MentalStateChanged
{
static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(MentalStateHandler), nameof(MentalStateHandler.TryStartMentalState));
yield return AccessTools.Method(typeof(MentalStateHandler), nameof(MentalStateHandler.ClearMentalStateDirect));
}
static void Postfix(MentalStateHandler __instance)
{
if (Multiplayer.Client == null) return;
// Pawns in a mental state can't be sold
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.pawn.Map);
}
}
[HarmonyPatch(typeof(JobDriver), nameof(JobDriver.Notify_Starting))]
static class JobExitMapStarted
{
static void Postfix(JobDriver __instance)
{
if (Multiplayer.Client == null) return;
if (__instance.job.exitMapOnArrival)
{
// Prisoners exiting the map can't be sold
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.pawn.Map);
}
}
}
[HarmonyPatch(typeof(Settlement_TraderTracker), nameof(Settlement_TraderTracker.TraderTrackerTick))]
static class DontDestroyStockWhileTrading
{
static bool Prefix(Settlement_TraderTracker __instance)
{
return Multiplayer.Client == null || Multiplayer.WorldComp.trading.All(s => s.trader != __instance.settlement);
}
}
[HarmonyPatch(typeof(MapPawns), nameof(MapPawns.DoListChangedNotifications))]
static class MapPawnsChanged
{
static void Postfix(MapPawns __instance)
{
if (Multiplayer.Client == null) return;
Multiplayer.WorldComp.DirtyColonyTradeForMap(__instance.map);
}
}
[HarmonyPatch(typeof(Pawn_AgeTracker), nameof(Pawn_AgeTracker.RecalculateLifeStageIndex))]
static class PawnLifeStageChanged
{
static void Postfix(Pawn_AgeTracker __instance)
{
if (Multiplayer.Client == null) return;
if (!__instance.pawn.Spawned) return;
Multiplayer.WorldComp.DirtyTradeForSpawnedThing(__instance.pawn);
}
}
[HarmonyPatch(typeof(Pawn_AgeTracker), nameof(Pawn_AgeTracker.AgeTick))]
static class PawnAgeChanged
{
static void Prefix(Pawn_AgeTracker __instance, ref int __state)
{
__state = __instance.AgeBiologicalYears;
}
static void Postfix(Pawn_AgeTracker __instance, int __state)
{
if (Multiplayer.Client == null) return;
if (__state == __instance.AgeBiologicalYears) return;
// todo?
}
}
[HarmonyPatch(typeof(TransferableUtility), nameof(TransferableUtility.TransferAsOne))]
static class TransferAsOneAgeCheck_Patch
{
static MethodInfo AgeBiologicalFloat = AccessTools.PropertyGetter(typeof(Pawn_AgeTracker), nameof(Pawn_AgeTracker.AgeBiologicalYearsFloat));
static MethodInfo AgeBiologicalInt = AccessTools.PropertyGetter(typeof(Pawn_AgeTracker), nameof(Pawn_AgeTracker.AgeBiologicalYears));
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> insts)
{
foreach (var inst in insts)
{
if (inst.operand == AgeBiologicalFloat)
{
yield return new CodeInstruction(OpCodes.Callvirt, AgeBiologicalInt);
yield return new CodeInstruction(OpCodes.Conv_R4);
continue;
}
yield return inst;
}
}
}
[HarmonyPatch(typeof(TradeDeal), nameof(TradeDeal.InSellablePosition))]
static class InSellablePositionPatch
{
// todo actually handle this
static void Postfix(Thing t, ref bool __result, ref string reason)
{
if (Multiplayer.Client == null) return;
//__result = t.Spawned;
//reason = null;
}
}
}