-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathCarUpdater.cs
105 lines (91 loc) · 3.54 KB
/
CarUpdater.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
using DV.LocoRestoration;
using DV.RemoteControls;
using DV.Simulation.Controllers;
using DV.ThingTypes;
using DV.Utils;
using HarmonyLib;
using System.Linq;
using UnityEngine;
namespace DvMod.RemoteDispatch
{
public static class CarUpdater
{
public static void ForceCarRefresh()
{
Sessions.AddTag("cars");
}
public static void MarkCarAsDirty(TrainCar car)
{
Sessions.AddTag($"carguid-{car.CarGUID}");
}
[HarmonyPatch(typeof(RemoteControllerModule), nameof(RemoteControllerModule.Init))]
public static class RemoteControllerModuleInitPatch
{
public static void Postfix(RemoteControllerModule __instance)
{
var trainCar = TrainCar.Resolve(__instance.gameObject);
var overrider = __instance.controlsOverrider;
var controls = new OverridableBaseControl[]
{
overrider.Brake,
overrider.IndependentBrake,
overrider.Reverser,
overrider.Throttle,
};
foreach (var control in controls.Where(c => c != null))
{
control.ControlUpdated += _ => MarkCarAsDirty(trainCar);
}
}
}
public static void MarkTrainsetAsDirty(Trainset trainset)
{
if (trainset.cars.Find(CarData.ShouldReturnTrainCar) != null)
Sessions.AddTag($"trainset-{trainset.id}");
}
[HarmonyPatch(typeof(TrainCar), nameof(TrainCar.Update))]
public static class UpdatePatch
{
public static void Postfix(TrainCar __instance)
{
if (__instance.logicCar == null || __instance.isStationary)
return;
MarkTrainsetAsDirty(__instance.trainset);
}
}
public static void Start()
{
CarSpawner carSpawner = SingletonBehaviour<CarSpawner>.Instance;
if (carSpawner == null)
{
Main.DebugLog(() => $"Tried to start {nameof(CarUpdater)} before {nameof(CarSpawner)} was initialized!");
return;
}
carSpawner.CarSpawned += OnCarsChanged;
carSpawner.CarAboutToBeDeleted += OnCarsChanged;
foreach (var controller in LocoRestorationController.allLocoRestorationControllers)
controller.StateChanged += OnRestorationStateChanged;
}
public static void Stop()
{
CarSpawner carSpawner = SingletonBehaviour<CarSpawner>.Instance;
if (carSpawner == null)
return;
carSpawner.CarSpawned -= OnCarsChanged;
carSpawner.CarAboutToBeDeleted -= OnCarsChanged;
foreach (var controller in LocoRestorationController.allLocoRestorationControllers)
controller.StateChanged -= OnRestorationStateChanged;
}
private static void OnCarsChanged(TrainCar trainCar)
{
Sessions.AddTag("cars");
}
private static void OnRestorationStateChanged(LocoRestorationController controller, TrainCarLivery livery, LocoRestorationController.RestorationState newState)
{
if (Main.settings.showUndiscoveredLocomotives)
return; // already sent to client during initialization
if (newState == LocoRestorationController.RestorationState.S3_RerailedCars)
OnCarsChanged(controller.loco);
}
}
}