Skip to content

Commit

Permalink
Added compat for Vanilla Temperature Expanded (#436)
Browse files Browse the repository at this point in the history
The compat required a new sync worker for Vanilla Expanded Framework (for `PipeNet`) - specifically, the link/unlink interaction. It could technically be achieved differently (don't sync the pipe net but get it from the `Thing`), but this was the simplest solution and could come in handy if we ever need to sync `PipeNet` again in the future.

Likely the last, or one of the last 1.4 PRs from me.
  • Loading branch information
SokyranTheDragon authored Apr 6, 2024
1 parent 8f8aa1a commit dd62f75
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
104 changes: 104 additions & 0 deletions Source/Mods/VanillaExpandedFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,15 @@ private static IEnumerable<CodeInstruction> ReplaceTickWithConditionalTick(IEnum

#region Pipe System

// Deconstruct
private static Type deconstructPipeDesignatorType;
private static AccessTools.FieldRef<Designator_Deconstruct, Def> deconstructPipeDesignatorNetDefField;
// Pipe net manager
private static Type pipeNetManagerType;
private static AccessTools.FieldRef<MapComponent, IList> pipeNetManagerPipeNetsListField;
// Pipe net
private static AccessTools.FieldRef<object, Map> pipeNetMapField;
private static AccessTools.FieldRef<object, Def> pipeNetDefField;

private static void PatchPipeSystem()
{
Expand All @@ -1017,6 +1024,15 @@ private static void PatchPipeSystem()
var type = deconstructPipeDesignatorType = AccessTools.TypeByName("PipeSystem.Designator_DeconstructPipe");
deconstructPipeDesignatorNetDefField = AccessTools.FieldRefAccess<Def>(type, "pipeNetDef");
MP.RegisterSyncWorker<Designator_Deconstruct>(SyncDeconstructPipeDesignator, type);

// Pipe net
type = pipeNetManagerType = AccessTools.TypeByName("PipeSystem.PipeNetManager");
pipeNetManagerPipeNetsListField = AccessTools.FieldRefAccess<IList>(type, "pipeNets");

type = AccessTools.TypeByName("PipeSystem.PipeNet");
pipeNetMapField = AccessTools.FieldRefAccess<Map>(type, "map");
pipeNetDefField = AccessTools.FieldRefAccess<Def>(type, "def");
MP.RegisterSyncWorker<object>(SyncPipeNet, type, true);
}

private static void SyncDeconstructPipeDesignator(SyncWorker sync, ref Designator_Deconstruct designator)
Expand All @@ -1027,6 +1043,94 @@ private static void SyncDeconstructPipeDesignator(SyncWorker sync, ref Designato
designator = (Designator_Deconstruct)Activator.CreateInstance(deconstructPipeDesignatorType, sync.Read<Def>());
}

private static void SyncPipeNet(SyncWorker sync, ref object pipeNet)
{
if (sync.isWriting)
{
// Sync null net as -1
if (pipeNet == null)
{
sync.Write(-1);
return;
}

// No map, can't get manager - log error and treat as null
var map = pipeNetMapField(pipeNet);
if (map == null)
{
Log.Error($"Trying to sync a PipeNet with a null map. PipeNet={pipeNet}");
sync.Write(-1);
return;
}

// No manager for map, shouldn't happen - log error and treat as null
var manager = map.GetComponent(pipeNetManagerType);
if (manager == null)
{
Log.Error($"Trying to sync a PipeNet with a map that doesn't have PipeNetManager. PipeNet={pipeNet}, Map={map}");
sync.Write(-1);
return;
}

var def = pipeNetDefField(pipeNet);
var list = pipeNetManagerPipeNetsListField(manager);
var index = -1;
var found = false;

foreach (var currentPipeNet in list)
{
if (def == pipeNetDefField(currentPipeNet))
{
index++;
if (pipeNet == currentPipeNet)
{
found = true;
break;
}
}
}

if (!found)
{
// We did not find the pipe net - log error and treat as null
Log.Error($"Trying to sync a PipeNet, but it's not held by the manager. PipeNet={pipeNet}, map={map}, manager={manager}");
sync.Write(-1);
}
else
{
sync.Write(index);
sync.Write(def);
sync.Write(manager);
}
}
else
{
var index = sync.Read<int>();
// If negative - it's null
if (index < 0)
return;

var def = sync.Read<Def>();
var manager = sync.Read<MapComponent>();
var list = pipeNetManagerPipeNetsListField(manager);
var currentIndex = 0;

foreach (var currentPipeNet in list)
{
if (def == pipeNetDefField(currentPipeNet))
{
if (currentIndex == index)
{
pipeNet = currentPipeNet;
break;
}

currentIndex++;
}
}
}
}

#endregion

#region Faction Discovery
Expand Down
29 changes: 29 additions & 0 deletions Source/Mods/VanillaTemperatureExpanded.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HarmonyLib;
using Multiplayer.API;
using Verse;

namespace Multiplayer.Compat;

/// <summary>Vanilla Temperature Expanded by Oskar Potocki, xrushha, Arquebus, Taranchuk</summary>
/// <see href="https://github.com/Vanilla-Expanded/VanillaTemperatureExpanded"/>
/// <see href="https://steamcommunity.com/sharedfiles/filedetails/?id=3202046258"/>
[MpCompatFor("VanillaExpanded.Temperature")]
public class VanillaTemperatureExpanded
{
public VanillaTemperatureExpanded(ModContentPack mod)
{
LongEventHandler.ExecuteWhenFinished(LatePatch);

// Unlink/relink
MpCompat.RegisterLambdaDelegate("VanillaTemperatureExpanded.Comps.CompAcTempControl", nameof(ThingComp.CompGetGizmosExtra), 0);
}

private static void LatePatch()
{
var type = AccessTools.TypeByName("VanillaTemperatureExpanded.Buildings.Building_AcControlUnit");
// Change temperature by +/- 1/10
MP.RegisterSyncMethod(type, "InterfaceChangeTargetNetworkTemperature");
// Reset temperature
MP.RegisterSyncMethodLambda(type, nameof(Thing.GetGizmos), 2);
}
}

0 comments on commit dd62f75

Please sign in to comment.