Skip to content

Commit dbecdb5

Browse files
committed
Compat for Quick Stockpile Creation
1 parent 3c85cca commit dbecdb5

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using HarmonyLib;
2+
using Multiplayer.API;
3+
using RimWorld;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Runtime.Serialization;
7+
using Verse;
8+
9+
namespace Multiplayer.Compat
10+
{
11+
/// <summary>Quick Stockpile Creation by Slofa</summary>
12+
/// <see href="https://steamcommunity.com/sharedfiles/filedetails/?id=1742151109"/>
13+
[MpCompatFor("longwater.smartstockpilecreation")]
14+
internal class SmartStockpileCreation
15+
{
16+
17+
static Type designatorType;
18+
static FastInvokeHandler allowedThingsGetter;
19+
static AccessTools.FieldRef<object, SpecialThingFilterDef> specialThingFilterDefLabel;
20+
static AccessTools.FieldRef<object, StoragePriority> priorityLabel;
21+
static AccessTools.FieldRef<object, string> contentsLabel;
22+
23+
static Type reallowGizmoType;
24+
static AccessTools.FieldRef<object, ThingDef> thingDefLabel;
25+
static AccessTools.FieldRef<object, Zone_Stockpile> zoneStockpileLabel;
26+
static AccessTools.FieldRef<object, ThingCategoryDef> categoryLabel;
27+
28+
static Type disallowGizmoType;
29+
static AccessTools.FieldRef<object, Thing> thingLabel;
30+
31+
public SmartStockpileCreation(ModContentPack mod)
32+
{
33+
// Create Stockpile from Thing
34+
{
35+
designatorType = AccessTools.TypeByName("SmartStockpileCreation.RimObjs.SmartStockpile.SmartStockpileDesignator");
36+
37+
allowedThingsGetter = MethodInvoker.GetHandler(AccessTools.PropertyGetter(designatorType, "AllowedThings"));
38+
specialThingFilterDefLabel = AccessTools.FieldRefAccess<SpecialThingFilterDef>(designatorType, "_specialThingFilterDef");
39+
priorityLabel = AccessTools.FieldRefAccess<StoragePriority>(designatorType, "_priority");
40+
contentsLabel = AccessTools.FieldRefAccess<string>(designatorType, "_stockpileContentsLabel");
41+
42+
MP.RegisterSyncWorker<Designator_ZoneAddStockpile>(SmartStockpileDesignatorWorker, designatorType);
43+
}
44+
45+
// Reallow Thing in Stockpile
46+
{
47+
reallowGizmoType = AccessTools.TypeByName("SmartStockpileCreation.RimObjs.DisallowInStockpile.ReallowInStockpileGizmo");
48+
49+
thingDefLabel = AccessTools.FieldRefAccess<ThingDef>(reallowGizmoType, "_thingDef");
50+
zoneStockpileLabel = AccessTools.FieldRefAccess<Zone_Stockpile>(reallowGizmoType, "_zoneStockpile");
51+
categoryLabel = AccessTools.FieldRefAccess<ThingCategoryDef>(reallowGizmoType, "_category");
52+
53+
MP.RegisterSyncMethod(AccessTools.Method(reallowGizmoType, "ProcessInput")).SetContext(SyncContext.MapSelected);
54+
MP.RegisterSyncWorker<Command>(ReallowInStockpileGizmoWorker, reallowGizmoType);
55+
}
56+
57+
// Disallow Thing in Stockpile
58+
{
59+
disallowGizmoType = AccessTools.TypeByName("SmartStockpileCreation.RimObjs.DisallowInStockpile.RemoveFromStockpileGizmo");
60+
61+
thingLabel = AccessTools.FieldRefAccess<Thing>(disallowGizmoType, "_thing");
62+
63+
MP.RegisterSyncMethod(AccessTools.Method(disallowGizmoType, "Process")).SetContext(SyncContext.MapSelected);
64+
MP.RegisterSyncWorker<Command>(RemoveFromStockpileGizmoWorker, disallowGizmoType);
65+
}
66+
}
67+
68+
void SmartStockpileDesignatorWorker(SyncWorker sync, ref Designator_ZoneAddStockpile obj) {
69+
if (sync.isWriting) {
70+
sync.Write(new List<ThingDef>((IEnumerable<ThingDef>) allowedThingsGetter.Invoke(obj)));
71+
sync.Write(specialThingFilterDefLabel(obj));
72+
sync.Write(priorityLabel(obj));
73+
sync.Write(contentsLabel(obj));
74+
} else {
75+
var allowedThings = sync.Read<List<ThingDef>>();
76+
var specialThingFilterDef = sync.Read<SpecialThingFilterDef>();
77+
var priority = sync.Read<StoragePriority>();
78+
var label = sync.Read<string>();
79+
obj = (Designator_ZoneAddStockpile) Activator.CreateInstance(designatorType, allowedThings, specialThingFilterDef, priority, label);
80+
}
81+
}
82+
83+
void ReallowInStockpileGizmoWorker(SyncWorker sync, ref Command obj) {
84+
if (sync.isWriting) {
85+
sync.Write(thingDefLabel(obj));
86+
sync.Write(zoneStockpileLabel(obj));
87+
sync.Write(categoryLabel(obj));
88+
} else {
89+
obj = (Command)FormatterServices.GetUninitializedObject(reallowGizmoType);
90+
thingDefLabel(obj) = sync.Read<ThingDef>();
91+
zoneStockpileLabel(obj) = sync.Read<Zone_Stockpile>();
92+
categoryLabel(obj) = sync.Read<ThingCategoryDef>();
93+
}
94+
}
95+
96+
void RemoveFromStockpileGizmoWorker(SyncWorker sync, ref Command obj) {
97+
if (sync.isWriting) {
98+
sync.Write(thingLabel(obj));
99+
} else {
100+
obj = (Command)FormatterServices.GetUninitializedObject(disallowGizmoType);
101+
thingLabel(obj) = sync.Read<Thing>();
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)