From 972e9640a220c4dfc20791e82be99add64b60a65 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon <36712560+SokyranTheDragon@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:28:29 +0100 Subject: [PATCH] Compat for Automatic Parking (#494) --- Source/Mods/AutomaticParking.cs | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Source/Mods/AutomaticParking.cs diff --git a/Source/Mods/AutomaticParking.cs b/Source/Mods/AutomaticParking.cs new file mode 100644 index 0000000..76a1453 --- /dev/null +++ b/Source/Mods/AutomaticParking.cs @@ -0,0 +1,50 @@ +using System; +using HarmonyLib; +using Multiplayer.API; +using Verse; + +namespace Multiplayer.Compat; + +/// Automatic Parking by rabiosus +/// +[MpCompatFor("rabiosus.vfautoparking")] +public class AutomaticParking +{ + // CompParkingController + private static Type parkingControllerCompType; + private static AccessTools.FieldRef parkingControllerCompControllerField; + // ParkingController + private static AccessTools.FieldRef parkingControllerVehicleField; + + public AutomaticParking(ModContentPack mod) + { + MpCompatPatchLoader.LoadPatch(this); + + // CompParkingController + var type = parkingControllerCompType = AccessTools.TypeByName("VehicleAutoParking.Core.CompParkingController"); + parkingControllerCompControllerField = AccessTools.FieldRefAccess(type, "parkingController"); + // ParkingController + type = AccessTools.TypeByName("VehicleAutoParking.Core.ParkingController"); + parkingControllerVehicleField = AccessTools.FieldRefAccess(type, "vehicle"); + MP.RegisterSyncMethod(type, "SaveParkingPosition"); + MP.RegisterSyncMethod(type, "ResetParkingPosition"); + MP.RegisterSyncMethod(type, "MoveToParkingPosition"); + } + + [MpCompatSyncWorker("VehicleAutoParking.Core.ParkingController")] + private static void SyncParkingController(SyncWorker sync, ref object controller) + { + if (sync.isWriting) + { + sync.Write(parkingControllerVehicleField(controller)); + } + else + { + var comp = sync.Read().AllComps.FirstOrDefault(c => parkingControllerCompType.IsInstanceOfType(c)); + if (comp != null) + controller = parkingControllerCompControllerField(comp); + else + Log.Error("A vehicle is missing CompParkingController."); + } + } +} \ No newline at end of file