From 6693a762c92ddece210b3cd9b4273efea49d57a3 Mon Sep 17 00:00:00 2001 From: RMROC451 Date: Sun, 23 Jun 2024 02:04:53 -0500 Subject: [PATCH 1/3] rather than wait for the next callout refresh ever 1s, call update if the inspector observers fire off. --- .../Patches/CarInspector_PopulateCarPanel_Patch.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs index acb418e..710d4cc 100644 --- a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs +++ b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs @@ -2,6 +2,7 @@ using Game.State; using HarmonyLib; using KeyValue.Runtime; +using Model.OpsNew; using Railloader; using RMROC451.TweaksAndThings.Enums; using RMROC451.TweaksAndThings.Extensions; @@ -11,6 +12,7 @@ using System.Collections.Generic; using System.Linq; using UI.Builder; using UI.CarInspector; +using UI.Tags; using static Model.Car; namespace RMROC451.TweaksAndThings.Patches; @@ -61,27 +63,29 @@ public class CarInspector_PopulateCarPanel_Patch private static UIPanelBuilder AddCarConsistRebuildObservers(UIPanelBuilder builder, IEnumerable consist) { + TagController tagController = UnityEngine.Object.FindFirstObjectByType(); foreach (Model.Car car in consist) { - builder = AddObserver(builder, car, PropertyChange.KeyForControl(PropertyChange.Control.Handbrake)); + builder = AddObserver(builder, car, PropertyChange.KeyForControl(PropertyChange.Control.Handbrake), tagController); foreach (LogicalEnd logicalEnd in ends) { - builder = AddObserver(builder, car, KeyValueKeyFor(EndGearStateKey.IsCoupled, car.LogicalToEnd(logicalEnd))); - builder = AddObserver(builder, car, KeyValueKeyFor(EndGearStateKey.IsAirConnected, car.LogicalToEnd(logicalEnd))); - builder = AddObserver(builder, car, KeyValueKeyFor(EndGearStateKey.Anglecock, car.LogicalToEnd(logicalEnd))); + builder = AddObserver(builder, car, KeyValueKeyFor(EndGearStateKey.IsCoupled, car.LogicalToEnd(logicalEnd)), tagController); + builder = AddObserver(builder, car, KeyValueKeyFor(EndGearStateKey.IsAirConnected, car.LogicalToEnd(logicalEnd)), tagController); + builder = AddObserver(builder, car, KeyValueKeyFor(EndGearStateKey.Anglecock, car.LogicalToEnd(logicalEnd)), tagController); } } return builder; } - private static UIPanelBuilder AddObserver(UIPanelBuilder builder, Model.Car car, string key) + private static UIPanelBuilder AddObserver(UIPanelBuilder builder, Model.Car car, string key, TagController tagController) { builder.AddObserver( car.KeyValueObject.Observe( key, delegate (Value value) { + tagController.UpdateTag(car, car.TagCallout, OpsController.Shared); builder.Rebuild(); }, false From b6edc93636e0f01b2051d737adfe97049bb5b146 Mon Sep 17 00:00:00 2001 From: RMROC451 Date: Sun, 23 Jun 2024 02:10:47 -0500 Subject: [PATCH 2/3] renaming main class to have plugin suffix to avoid namespace collision issues. --- .../Patches/CarInspector_PopulateCarPanel_Patch.cs | 3 ++- TweaksAndThings/Patches/EngineRosterRow_Refresh_Patch.cs | 2 +- TweaksAndThings/Patches/ExpandedConsole_Patch.cs | 2 +- TweaksAndThings/Patches/TagController_UpdateTag_Patch.cs | 3 ++- .../{TweaksAndThings.cs => TweaksAndThingsPlugin.cs} | 8 ++++---- 5 files changed, 10 insertions(+), 8 deletions(-) rename TweaksAndThings/{TweaksAndThings.cs => TweaksAndThingsPlugin.cs} (96%) diff --git a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs index 710d4cc..04ad7ed 100644 --- a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs +++ b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs @@ -4,6 +4,7 @@ using HarmonyLib; using KeyValue.Runtime; using Model.OpsNew; using Railloader; +using RMROC451.TweaksAndThings; using RMROC451.TweaksAndThings.Enums; using RMROC451.TweaksAndThings.Extensions; using Serilog; @@ -33,7 +34,7 @@ public class CarInspector_PopulateCarPanel_Patch private static bool Prefix(CarInspector __instance, UIPanelBuilder builder) { - TweaksAndThings tweaksAndThings = SingletonPluginBase.Shared; + TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase.Shared; if (!tweaksAndThings.IsEnabled) return true; var consist = __instance._car.EnumerateCoupled(LogicalEnd.A); diff --git a/TweaksAndThings/Patches/EngineRosterRow_Refresh_Patch.cs b/TweaksAndThings/Patches/EngineRosterRow_Refresh_Patch.cs index 6906bb7..d29cb5b 100644 --- a/TweaksAndThings/Patches/EngineRosterRow_Refresh_Patch.cs +++ b/TweaksAndThings/Patches/EngineRosterRow_Refresh_Patch.cs @@ -25,7 +25,7 @@ public class EngineRosterRow_Refresh_Patch { public static void Postfix(EngineRosterRow __instance) { - TweaksAndThings? tweaksAndThings = SingletonPluginBase.Shared; + TweaksAndThingsPlugin? tweaksAndThings = SingletonPluginBase.Shared; RosterFuelColumnSettings? rosterFuelColumnSettings = tweaksAndThings?.settings?.EngineRosterFuelColumnSettings; if (tweaksAndThings == null || diff --git a/TweaksAndThings/Patches/ExpandedConsole_Patch.cs b/TweaksAndThings/Patches/ExpandedConsole_Patch.cs index cc6740a..6c0f385 100644 --- a/TweaksAndThings/Patches/ExpandedConsole_Patch.cs +++ b/TweaksAndThings/Patches/ExpandedConsole_Patch.cs @@ -32,7 +32,7 @@ public class ExpandedConsole_Add_Patch { try { - TweaksAndThings tweaksAndThings = SingletonPluginBase.Shared; + TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase.Shared; StateManager shared = StateManager.Shared; GameStorage gameStorage = shared.Storage; WebhookSettings settings = tweaksAndThings?.settings?.WebhookSettingsList?.FirstOrDefault(ws => ws.RailroadMark == gameStorage.RailroadMark); diff --git a/TweaksAndThings/Patches/TagController_UpdateTag_Patch.cs b/TweaksAndThings/Patches/TagController_UpdateTag_Patch.cs index acd0eb4..495cd94 100644 --- a/TweaksAndThings/Patches/TagController_UpdateTag_Patch.cs +++ b/TweaksAndThings/Patches/TagController_UpdateTag_Patch.cs @@ -2,6 +2,7 @@ using Model; using Model.OpsNew; using Railloader; +using RMROC451.TweaksAndThings; using RMROC451.TweaksAndThings.Extensions; using UI; using UI.Tags; @@ -20,7 +21,7 @@ public class TagController_UpdateTag_Patch private static void Postfix(Car car, TagCallout tagCallout) { TagController tagController = UnityEngine.Object.FindObjectOfType(); - TweaksAndThings tweaksAndThings = SingletonPluginBase.Shared; + TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase.Shared; if (!tweaksAndThings.IsEnabled || !tweaksAndThings.settings.HandBrakeAndAirTagModifiers) { diff --git a/TweaksAndThings/TweaksAndThings.cs b/TweaksAndThings/TweaksAndThingsPlugin.cs similarity index 96% rename from TweaksAndThings/TweaksAndThings.cs rename to TweaksAndThings/TweaksAndThingsPlugin.cs index fe59771..e0e6970 100644 --- a/TweaksAndThings/TweaksAndThings.cs +++ b/TweaksAndThings/TweaksAndThingsPlugin.cs @@ -14,7 +14,7 @@ using RMROC451.TweaksAndThings.Enums; namespace RMROC451.TweaksAndThings; -public class TweaksAndThings : SingletonPluginBase, IUpdateHandler, IModTabHandler +public class TweaksAndThingsPlugin : SingletonPluginBase, IUpdateHandler, IModTabHandler { private HttpClient client; internal HttpClient Client @@ -28,16 +28,16 @@ public class TweaksAndThings : SingletonPluginBase, IUpdateHand } } internal Settings? settings { get; private set; } = null; - readonly ILogger logger = Log.ForContext(); + readonly ILogger logger = Log.ForContext(); IModdingContext moddingContext { get; set; } IModDefinition modDefinition { get; set; } - static TweaksAndThings() + static TweaksAndThingsPlugin() { Log.Information("Hello! Static Constructor was called!"); } - public TweaksAndThings(IModdingContext moddingContext, IModDefinition self) + public TweaksAndThingsPlugin(IModdingContext moddingContext, IModDefinition self) { this.modDefinition = self; From 2e4753602860d57c0ca4ba504765d95c534369f1 Mon Sep 17 00:00:00 2001 From: RMROC451 Date: Sun, 23 Jun 2024 02:30:13 -0500 Subject: [PATCH 3/3] added Bleed Consist button --- TweaksAndThings/Enums/MrocHelperType.cs | 3 +- TweaksAndThings/Extensions/Car_Extensions.cs | 2 ++ .../CarInspector_PopulateCarPanel_Patch.cs | 30 ++++++++++++++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/TweaksAndThings/Enums/MrocHelperType.cs b/TweaksAndThings/Enums/MrocHelperType.cs index 27bceb0..abd6b64 100644 --- a/TweaksAndThings/Enums/MrocHelperType.cs +++ b/TweaksAndThings/Enums/MrocHelperType.cs @@ -3,5 +3,6 @@ public enum MrocHelperType { Handbrake, - GladhandAndAnglecock + GladhandAndAnglecock, + BleedAirSystem } diff --git a/TweaksAndThings/Extensions/Car_Extensions.cs b/TweaksAndThings/Extensions/Car_Extensions.cs index 6415c92..d0ae00d 100644 --- a/TweaksAndThings/Extensions/Car_Extensions.cs +++ b/TweaksAndThings/Extensions/Car_Extensions.cs @@ -36,5 +36,7 @@ namespace RMROC451.TweaksAndThings.Extensions } return car; } + + public static bool NotMotivePower(this Car car) => car is not BaseLocomotive && car.Archetype != Model.Definition.CarArchetype.Tender; } } diff --git a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs index 04ad7ed..efd1e34 100644 --- a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs +++ b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs @@ -2,9 +2,9 @@ using Game.State; using HarmonyLib; using KeyValue.Runtime; +using Model; using Model.OpsNew; using Railloader; -using RMROC451.TweaksAndThings; using RMROC451.TweaksAndThings.Enums; using RMROC451.TweaksAndThings.Extensions; using Serilog; @@ -51,12 +51,18 @@ public class CarInspector_PopulateCarPanel_Patch if (consist.Any(c => c.EndAirSystemIssue())) { - hstack.AddButtonCompact("Connect Air Lines", delegate + hstack.AddButtonCompact("Connect Air", delegate { MrocConsistHelper(__instance._car, MrocHelperType.GladhandAndAnglecock); hstack.Rebuild(); - }).Tooltip("Connect Consist Air Lines", "Iterates over each car in this consist and connects gladhands and opens anglecocks."); + }).Tooltip("Connect Consist Air", "Iterates over each car in this consist and connects gladhands and opens anglecocks."); } + + hstack.AddButtonCompact("Bleed Consist", delegate + { + MrocConsistHelper(__instance._car, MrocHelperType.BleedAirSystem); + hstack.Rebuild(); + }).Tooltip("Bleed Air Lines", "Iterates over each car in this consist and bleeds the air out of the lines."); }); return true; @@ -111,16 +117,17 @@ public class CarInspector_PopulateCarPanel_Patch IEnumerable consist = car.EnumerateCoupled(LogicalEnd.A); Log.Information($"{car} => {mrocHelperType} => {string.Join("/", consist.Select(c => c.ToString()))}"); + TrainController tc = UnityEngine.Object.FindObjectOfType(); switch (mrocHelperType) { case MrocHelperType.Handbrake: if (consist.Any(c => c.HandbrakeApplied())) { consist.Do(c => c.SetHandbrake(false)); - } else + } + else { - TrainController tc = UnityEngine.Object.FindObjectOfType(); - consist = consist.Where(c => c.DetermineFuelCar(true) != null); + consist = consist.Where(c => c is not BaseLocomotive && c.Archetype != Model.Definition.CarArchetype.Tender); Log.Information($"{car} => {mrocHelperType} => {string.Join("/", consist.Select(c => c.ToString()))}"); //when ApplyHandbrakesAsNeeded is called, and the consist contains an engine, it stops applying brakes. tc.ApplyHandbrakesAsNeeded(consist.ToList(), PlaceTrainHandbrakes.Automatic); @@ -135,7 +142,7 @@ public class CarInspector_PopulateCarPanel_Patch StateManager.ApplyLocal( new PropertyChange( - c.id, + c.id, KeyValueKeyFor(EndGearStateKey.Anglecock, c.LogicalToEnd(end)), new FloatPropertyValue(endGear.IsCoupled ? 1f : 0f) ) @@ -149,6 +156,15 @@ public class CarInspector_PopulateCarPanel_Patch }) ); break; + + case MrocHelperType.BleedAirSystem: + consist = consist.Where(c => c.NotMotivePower()); + Log.Information($"{car} => {mrocHelperType} => {string.Join("/", consist.Select(c => c.ToString()))}"); + foreach (Model.Car bleed in consist) + { + StateManager.ApplyLocal(new PropertyChange(bleed.id, PropertyChange.Control.Bleed, 1)); + } + break; } } }