From 44bf23f5be233008e8c09d33c3489d1ce7ab9277 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Sat, 25 Apr 2020 17:12:59 +0300 Subject: [PATCH 01/13] Harmony2 - manual patch --- TLM/TLM/LoadingExtension.cs | 100 +----------------- .../ReleaseCitizenInstancePatch.cs | 2 +- .../_CitizenManager/ReleaseCitizenPatch.cs | 2 +- TLM/TLM/Patch/_DefaultTool/OnToolGUIPatch.cs | 2 +- .../Patch/_DefaultTool/RenderOverlayPatch.cs | 2 +- .../_HumanAI/ArriveAtDestinationPatch.cs | 2 +- TLM/TLM/Patch/_InfoManager/SetModePatch.cs | 2 +- .../Patch/_NetManager/FinalizeSegmentPatch.cs | 2 +- .../Patch/_NetManager/UpdateSegmentPatch.cs | 2 +- .../GetTrafficLightNodeStatePatch.cs | 2 +- .../_RoadBaseAI/SetTrafficLightStatePatch.cs | 2 +- TLM/TLM/Patch/_RoadBaseAI/UpdateLanesPatch.cs | 2 +- TLM/TLM/Patch/_RoadBaseAI/UpdateNodePatch.cs | 2 +- .../OnAdjustRoadButtonPatch.cs | 2 +- TLM/TLM/Patch/_Vehicle/SpawnPatch.cs | 2 +- TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs | 2 +- .../_VehicleManager/CreateVehiclePatch.cs | 2 +- .../_VehicleManager/ReleaseVehiclePatch.cs | 2 +- TLM/TLM/ThreadingExtension.cs | 15 --- 19 files changed, 22 insertions(+), 127 deletions(-) diff --git a/TLM/TLM/LoadingExtension.cs b/TLM/TLM/LoadingExtension.cs index a54c50d25..8ef03f9a6 100644 --- a/TLM/TLM/LoadingExtension.cs +++ b/TLM/TLM/LoadingExtension.cs @@ -2,7 +2,7 @@ namespace TrafficManager { using ColossalFramework.UI; using ColossalFramework; using CSUtil.Commons; - using Harmony; + using HarmonyLib; using ICities; using JetBrains.Annotations; using UnityEngine; @@ -16,8 +16,6 @@ namespace TrafficManager { using TrafficManager.RedirectionFramework; using TrafficManager.State; using TrafficManager.UI; - using static TrafficManager.Util.Shortcuts; - using UnityEngine; [UsedImplicitly] public class LoadingExtension : LoadingExtensionBase { @@ -40,20 +38,11 @@ public Detour(MethodInfo originalMethod, MethodInfo customMethod) { } } - public class ManualHarmonyPatch { - public MethodInfo method; - public HarmonyMethod prefix; - public HarmonyMethod transpiler = null; - public HarmonyMethod postfix = null; - } - public static CustomPathManager CustomPathManager { get; set; } public static bool DetourInited { get; set; } - public static List Detours { get; set; } - - public static HarmonyInstance HarmonyInst { get; private set; } + public static Harmony HarmonyInst { get; private set; } /// /// Contains loaded languages and lookup functions for text translations @@ -70,55 +59,6 @@ public static bool IsPathManagerReplaced { get; private set; } - /// - /// Manually deployed Harmony patches - /// - public static IList ManualHarmonyPatches { get; } = - new List { - new ManualHarmonyPatch() { - method = typeof(CommonBuildingAI).GetMethod( - "SimulationStep", - BindingFlags.Public | BindingFlags.Instance, - null, - new[] { typeof(ushort), typeof(Building).MakeByRefType() }, - null), - prefix = new HarmonyMethod( - typeof(Patch._CommonBuildingAI.SimulationStepPatch).GetMethod("Prefix")) - }, - new ManualHarmonyPatch() { - method = typeof(RoadBaseAI).GetMethod( - "TrafficLightSimulationStep", - BindingFlags.Public | BindingFlags.Static, - null, - new[] { typeof(ushort), typeof(NetNode).MakeByRefType() }, - null), - prefix = new HarmonyMethod( - typeof(Patch._RoadBaseAI.TrafficLightSimulationStepPatch).GetMethod( - "Prefix")) - }, - new ManualHarmonyPatch() { - method = typeof(TrainTrackBaseAI).GetMethod( - "LevelCrossingSimulationStep", - BindingFlags.Public | BindingFlags.Static, - null, - new[] { typeof(ushort), typeof(NetNode).MakeByRefType() }, - null), - prefix = new HarmonyMethod( - typeof(Patch._TrainTrackBase.LevelCrossingSimulationStepPatch).GetMethod( - "Prefix")) - }, - new ManualHarmonyPatch() { - method = typeof(RoadBaseAI).GetMethod( - "SimulationStep", - BindingFlags.Public | BindingFlags.Instance, - null, - new[] { typeof(ushort), typeof(NetSegment).MakeByRefType() }, - null), - prefix = new HarmonyMethod( - typeof(Patch._RoadBaseAI.SegmentSimulationStepPatch).GetMethod("Prefix")) - } - }; - /// /// Method redirection states for Harmony-driven patches /// @@ -146,14 +86,6 @@ public void RevertDetours() { return; } - Log.Info("Reverting manual detours"); - Detours.Reverse(); - foreach (Detour d in Detours) { - RedirectionHelper.RevertRedirect(d.OriginalMethod, d.Redirect); - } - - Detours.Clear(); - Log.Info("Reverting attribute-driven detours"); AssemblyRedirector.Revert(); @@ -178,7 +110,7 @@ private void InitDetours() { try { Log.Info("Deploying Harmony patches"); #if DEBUG - HarmonyInstance.DEBUG = true; + Harmony.DEBUG = true; #endif Assembly assembly = Assembly.GetExecutingAssembly(); @@ -186,7 +118,7 @@ private void InitDetours() { // Harmony attribute-driven patching Log.Info($"Performing Harmony attribute-driven patching"); - HarmonyInst = HarmonyInstance.Create(HARMONY_ID); + HarmonyInst = new Harmony(HARMONY_ID); HarmonyInst.PatchAll(assembly); foreach (Type type in assembly.GetTypes()) { @@ -203,30 +135,9 @@ private void InitDetours() { HarmonyMethodStates[info] = state; } } - - // Harmony manual patching - Log.Info($"Performing Harmony manual patching"); - - foreach (ManualHarmonyPatch manualPatch in ManualHarmonyPatches) { - Log.InfoFormat( - "Manually patching method {0}.{1}. Prefix: {2}, Postfix: {3}, Transpiler: {4}", - manualPatch.method.DeclaringType.FullName, - manualPatch.method.Name, manualPatch.prefix?.method, - manualPatch.postfix?.method, manualPatch.transpiler?.method); - - HarmonyInst.Patch( - manualPatch.method, - manualPatch.prefix, - manualPatch.postfix, - manualPatch.transpiler); - - IntPtr ptr = manualPatch.method.MethodHandle.GetFunctionPointer(); - RedirectCallsState state = RedirectionHelper.GetState(ptr); - HarmonyMethodStates[manualPatch.method] = state; - } } catch (Exception e) { Log.Error("Could not deploy Harmony patches"); - Log.Info(e.ToString()); + Log.Info(e.Message); Log.Info(e.StackTrace); detourFailed = true; } @@ -272,7 +183,6 @@ public override void OnCreated(ILoading loading) { return; } - Detours = new List(); RegisteredManagers = new List(); DetourInited = false; CustomPathManager = new CustomPathManager(); diff --git a/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenInstancePatch.cs b/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenInstancePatch.cs index 2843b7895..75e9d635e 100644 --- a/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenInstancePatch.cs +++ b/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenInstancePatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._CitizenManager { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; [HarmonyPatch(typeof(CitizenManager), "ReleaseCitizenInstance")] diff --git a/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenPatch.cs b/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenPatch.cs index 28f82b118..49870d0f3 100644 --- a/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenPatch.cs +++ b/TLM/TLM/Patch/_CitizenManager/ReleaseCitizenPatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._CitizenManager { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; [HarmonyPatch(typeof(CitizenManager), "ReleaseCitizen")] diff --git a/TLM/TLM/Patch/_DefaultTool/OnToolGUIPatch.cs b/TLM/TLM/Patch/_DefaultTool/OnToolGUIPatch.cs index 5f7e42e0e..3e8aceba4 100644 --- a/TLM/TLM/Patch/_DefaultTool/OnToolGUIPatch.cs +++ b/TLM/TLM/Patch/_DefaultTool/OnToolGUIPatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._DefaultTool { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.UI; using UnityEngine; diff --git a/TLM/TLM/Patch/_DefaultTool/RenderOverlayPatch.cs b/TLM/TLM/Patch/_DefaultTool/RenderOverlayPatch.cs index 8362cc9a1..ed84ddef8 100644 --- a/TLM/TLM/Patch/_DefaultTool/RenderOverlayPatch.cs +++ b/TLM/TLM/Patch/_DefaultTool/RenderOverlayPatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._DefaultTool { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.UI; using static TrafficManager.UI.SubTools.PrioritySignsTool; diff --git a/TLM/TLM/Patch/_HumanAI/ArriveAtDestinationPatch.cs b/TLM/TLM/Patch/_HumanAI/ArriveAtDestinationPatch.cs index 2da447f8f..ad879d80e 100644 --- a/TLM/TLM/Patch/_HumanAI/ArriveAtDestinationPatch.cs +++ b/TLM/TLM/Patch/_HumanAI/ArriveAtDestinationPatch.cs @@ -1,6 +1,6 @@ namespace TrafficManager.Patch._HumanAI { using ColossalFramework; - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.State; diff --git a/TLM/TLM/Patch/_InfoManager/SetModePatch.cs b/TLM/TLM/Patch/_InfoManager/SetModePatch.cs index 11264167b..aa4ded5e9 100644 --- a/TLM/TLM/Patch/_InfoManager/SetModePatch.cs +++ b/TLM/TLM/Patch/_InfoManager/SetModePatch.cs @@ -1,7 +1,7 @@ namespace TrafficManager.Patch._InfoManager { using ColossalFramework; - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.Util; using TrafficManager.UI; diff --git a/TLM/TLM/Patch/_NetManager/FinalizeSegmentPatch.cs b/TLM/TLM/Patch/_NetManager/FinalizeSegmentPatch.cs index c949ecc66..aa12f999e 100644 --- a/TLM/TLM/Patch/_NetManager/FinalizeSegmentPatch.cs +++ b/TLM/TLM/Patch/_NetManager/FinalizeSegmentPatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._NetManager { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; [HarmonyPatch(typeof(NetManager), "FinalizeSegment")] diff --git a/TLM/TLM/Patch/_NetManager/UpdateSegmentPatch.cs b/TLM/TLM/Patch/_NetManager/UpdateSegmentPatch.cs index c82830cf0..f9efcb16b 100644 --- a/TLM/TLM/Patch/_NetManager/UpdateSegmentPatch.cs +++ b/TLM/TLM/Patch/_NetManager/UpdateSegmentPatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._NetManager { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; [HarmonyPatch( diff --git a/TLM/TLM/Patch/_RoadBaseAI/GetTrafficLightNodeStatePatch.cs b/TLM/TLM/Patch/_RoadBaseAI/GetTrafficLightNodeStatePatch.cs index b0d603a40..d6492030a 100644 --- a/TLM/TLM/Patch/_RoadBaseAI/GetTrafficLightNodeStatePatch.cs +++ b/TLM/TLM/Patch/_RoadBaseAI/GetTrafficLightNodeStatePatch.cs @@ -1,6 +1,6 @@ namespace TrafficManager.Patch._RoadBaseAI { using ColossalFramework; - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.State; using UnityEngine; diff --git a/TLM/TLM/Patch/_RoadBaseAI/SetTrafficLightStatePatch.cs b/TLM/TLM/Patch/_RoadBaseAI/SetTrafficLightStatePatch.cs index 71e2de631..e879523aa 100644 --- a/TLM/TLM/Patch/_RoadBaseAI/SetTrafficLightStatePatch.cs +++ b/TLM/TLM/Patch/_RoadBaseAI/SetTrafficLightStatePatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._RoadBaseAI { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.State; using static RoadBaseAI; diff --git a/TLM/TLM/Patch/_RoadBaseAI/UpdateLanesPatch.cs b/TLM/TLM/Patch/_RoadBaseAI/UpdateLanesPatch.cs index 790ea3a4f..4e7e58a03 100644 --- a/TLM/TLM/Patch/_RoadBaseAI/UpdateLanesPatch.cs +++ b/TLM/TLM/Patch/_RoadBaseAI/UpdateLanesPatch.cs @@ -1,6 +1,6 @@ namespace TrafficManager.Patch._RoadBaseAI { using ColossalFramework; - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.State; diff --git a/TLM/TLM/Patch/_RoadBaseAI/UpdateNodePatch.cs b/TLM/TLM/Patch/_RoadBaseAI/UpdateNodePatch.cs index ad00f6a2b..aaf5ec254 100644 --- a/TLM/TLM/Patch/_RoadBaseAI/UpdateNodePatch.cs +++ b/TLM/TLM/Patch/_RoadBaseAI/UpdateNodePatch.cs @@ -1,6 +1,6 @@ namespace TrafficManager.Patch._RoadBaseAI { using ColossalFramework; - using Harmony; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.Manager.Impl; using TrafficManager.State; diff --git a/TLM/TLM/Patch/_RoadWorldInfoPanel/OnAdjustRoadButtonPatch.cs b/TLM/TLM/Patch/_RoadWorldInfoPanel/OnAdjustRoadButtonPatch.cs index 7c97ce993..a2746ff7b 100644 --- a/TLM/TLM/Patch/_RoadWorldInfoPanel/OnAdjustRoadButtonPatch.cs +++ b/TLM/TLM/Patch/_RoadWorldInfoPanel/OnAdjustRoadButtonPatch.cs @@ -1,7 +1,7 @@ namespace TrafficManager.Patch._RoadWorldInfoPanel { using ColossalFramework; - using Harmony; + using HarmonyLib; using JetBrains.Annotations; [HarmonyPatch(typeof(RoadWorldInfoPanel), "OnAdjustRoadButton")] diff --git a/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs b/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs index dae8047d2..95e7f7b79 100644 --- a/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs +++ b/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs @@ -1,7 +1,7 @@ using ColossalFramework; using ColossalFramework.Math; using CSUtil.Commons; -using Harmony; +using HarmonyLib; using System; using System.Collections.Generic; using System.Linq; diff --git a/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs b/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs index 3cdc5682c..dbe41983c 100644 --- a/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs +++ b/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs @@ -1,7 +1,7 @@ using ColossalFramework; using ColossalFramework.Math; using CSUtil.Commons; -using Harmony; +using HarmonyLib; using System; using System.Collections.Generic; using System.Linq; diff --git a/TLM/TLM/Patch/_VehicleManager/CreateVehiclePatch.cs b/TLM/TLM/Patch/_VehicleManager/CreateVehiclePatch.cs index e60303080..1566d0d63 100644 --- a/TLM/TLM/Patch/_VehicleManager/CreateVehiclePatch.cs +++ b/TLM/TLM/Patch/_VehicleManager/CreateVehiclePatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._VehicleManager { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; [HarmonyPatch(typeof(VehicleManager), "CreateVehicle")] diff --git a/TLM/TLM/Patch/_VehicleManager/ReleaseVehiclePatch.cs b/TLM/TLM/Patch/_VehicleManager/ReleaseVehiclePatch.cs index 3172fc299..6b9f5e4a6 100644 --- a/TLM/TLM/Patch/_VehicleManager/ReleaseVehiclePatch.cs +++ b/TLM/TLM/Patch/_VehicleManager/ReleaseVehiclePatch.cs @@ -1,5 +1,5 @@ namespace TrafficManager.Patch._VehicleManager { - using Harmony; + using HarmonyLib; using JetBrains.Annotations; [HarmonyPatch(typeof(VehicleManager), "ReleaseVehicle")] diff --git a/TLM/TLM/ThreadingExtension.cs b/TLM/TLM/ThreadingExtension.cs index 6a9a267a4..8f41ce3d1 100644 --- a/TLM/TLM/ThreadingExtension.cs +++ b/TLM/TLM/ThreadingExtension.cs @@ -49,21 +49,6 @@ public override void OnBeforeSimulationFrame() { List missingDetours = new List(); - foreach (Detour detour in Detours) { - if (!RedirectionHelper.IsRedirected( - detour.OriginalMethod, - detour.CustomMethod)) - { - missingDetours.Add( - string.Format( - " {0}.{1} with {2} parameters ({3})", - detour.OriginalMethod.DeclaringType.Name, - detour.OriginalMethod.Name, - detour.OriginalMethod.GetParameters().Length, - detour.OriginalMethod.DeclaringType.AssemblyQualifiedName)); - } - } - foreach (KeyValuePair entry in HarmonyMethodStates) { MethodBase method = entry.Key; RedirectCallsState oldState = entry.Value; From 51294ca9e16621c6baaf8a683ee414ab3fa0b6c5 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Sat, 25 Apr 2020 20:48:03 +0300 Subject: [PATCH 02/13] Harmony2 working --- TLM/TLM/LoadingExtension.cs | 51 +++++-------------- .../_CommonBuildingAI/SimulationStepPatch.cs | 23 +++++++-- .../_RoadBaseAI/SegmentSimulationStepPatch.cs | 21 ++++++-- .../TrafficLightSimulationStepPatch.cs | 5 +- .../LevelCrossingSimulationStepPatch.cs | 5 +- TLM/TLM/RedirectionFramework | 2 +- TLM/TLM/ThreadingExtension.cs | 17 ------- 7 files changed, 54 insertions(+), 70 deletions(-) diff --git a/TLM/TLM/LoadingExtension.cs b/TLM/TLM/LoadingExtension.cs index 8ef03f9a6..3d3e35da4 100644 --- a/TLM/TLM/LoadingExtension.cs +++ b/TLM/TLM/LoadingExtension.cs @@ -16,6 +16,8 @@ namespace TrafficManager { using TrafficManager.RedirectionFramework; using TrafficManager.State; using TrafficManager.UI; + using TrafficManager.UI.MainMenu.OSD; + using TrafficManager.Util; [UsedImplicitly] public class LoadingExtension : LoadingExtensionBase { @@ -42,8 +44,6 @@ public Detour(MethodInfo originalMethod, MethodInfo customMethod) { public static bool DetourInited { get; set; } - public static Harmony HarmonyInst { get; private set; } - /// /// Contains loaded languages and lookup functions for text translations /// @@ -59,13 +59,6 @@ public static bool IsPathManagerReplaced { get; private set; } - /// - /// Method redirection states for Harmony-driven patches - /// - public static IDictionary HarmonyMethodStates { - get; - } = new Dictionary(); - /// /// Method redirection states for attribute-driven detours /// @@ -86,13 +79,9 @@ public void RevertDetours() { return; } - Log.Info("Reverting attribute-driven detours"); - AssemblyRedirector.Revert(); - - Log.Info("Reverting Harmony detours"); - foreach (MethodBase m in HarmonyMethodStates.Keys) { - HarmonyInst.Unpatch(m, HarmonyPatchType.All, HARMONY_ID); - } + var harmony = new Harmony(HARMONY_ID); + Shortcuts.Assert(harmony != null, "HarmonyInst!=null"); + harmony.UnpatchAll(); DetourInited = false; Log.Info("Reverting detours finished."); @@ -108,38 +97,22 @@ private void InitDetours() { bool detourFailed = false; try { - Log.Info("Deploying Harmony patches"); #if DEBUG Harmony.DEBUG = true; #endif - Assembly assembly = Assembly.GetExecutingAssembly(); - - HarmonyMethodStates.Clear(); - // Harmony attribute-driven patching Log.Info($"Performing Harmony attribute-driven patching"); - HarmonyInst = new Harmony(HARMONY_ID); - HarmonyInst.PatchAll(assembly); - - foreach (Type type in assembly.GetTypes()) { - object[] attributes = type.GetCustomAttributes(typeof(HarmonyPatch), true); - if (attributes.Length <= 0) { - continue; - } - - foreach (object attr in attributes) { - HarmonyPatch harmonyPatchAttr = (HarmonyPatch)attr; - MethodBase info = HarmonyUtil.GetOriginalMethod(harmonyPatchAttr.info); - IntPtr ptr = info.MethodHandle.GetFunctionPointer(); - RedirectCallsState state = RedirectionHelper.GetState(ptr); - HarmonyMethodStates[info] = state; - } - } - } catch (Exception e) { + var harmony = new Harmony(HARMONY_ID); + Shortcuts.Assert(harmony!=null, "HarmonyInst!=null"); + harmony.PatchAll(); + Log.Info($"Harmony attribute-driven patching successfull!"); + } + catch (Exception e) { Log.Error("Could not deploy Harmony patches"); Log.Info(e.Message); Log.Info(e.StackTrace); detourFailed = true; + throw e; } try { diff --git a/TLM/TLM/Patch/_CommonBuildingAI/SimulationStepPatch.cs b/TLM/TLM/Patch/_CommonBuildingAI/SimulationStepPatch.cs index 076eefda1..1432bbbc5 100644 --- a/TLM/TLM/Patch/_CommonBuildingAI/SimulationStepPatch.cs +++ b/TLM/TLM/Patch/_CommonBuildingAI/SimulationStepPatch.cs @@ -1,13 +1,28 @@ -namespace TrafficManager.Patch._CommonBuildingAI { +namespace TrafficManager.Patch._CommonBuildingAI { + using HarmonyLib; using JetBrains.Annotations; + using System.Reflection; + + [HarmonyPatch] + [UsedImplicitly] + public class SimulationStepPatch + { + [UsedImplicitly] + public static MethodBase TargetMethod() + { + return HarmonyLib.AccessTools.DeclaredMethod( + typeof(CommonBuildingAI), + "SimulationStep", + new[] { typeof(ushort), typeof(Building).MakeByRefType() }) ?? + throw new System.Exception("_CommonBuildingAI.SimulationStepPatch failed to find TargetMethod"); + } - // [Harmony] Manually patched because struct references are used - public class SimulationStepPatch { /// /// Decreases parking space and public transport demand before each simulation step if the Parking AI is active. /// [UsedImplicitly] - public static void Prefix(ushort buildingID, ref Building data) { + public static void Prefix(ushort buildingID, ref Building data) + { Constants.ManagerFactory.ExtBuildingManager.OnBeforeSimulationStep(buildingID, ref data); } } diff --git a/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs b/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs index aebb6158d..4b7c5700c 100644 --- a/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs +++ b/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs @@ -1,13 +1,24 @@ -namespace TrafficManager.Patch._RoadBaseAI { +namespace TrafficManager.Patch._RoadBaseAI { using API.Traffic.Enums; using ColossalFramework; + using HarmonyLib; using JetBrains.Annotations; + using System.Reflection; using TrafficManager.State; - using CSUtil.Commons.Benchmark; - - // [Harmony] Manually patched because struct references are used - public class SegmentSimulationStepPatch { + [HarmonyPatch] + [UsedImplicitly] + public class SegmentSimulationStepPatch + { + [UsedImplicitly] + public static MethodBase TargetMethod() + { + return HarmonyLib.AccessTools.DeclaredMethod( + typeof(RoadBaseAI), + "SimulationStep", + new[] { typeof(ushort), typeof(NetSegment).MakeByRefType() }) ?? + throw new System.Exception("SegmentSimulationStepPatch failed to find TargetMethod"); + } private static ushort lastSimulatedSegmentId = 0; private static byte trafficMeasurementMod = 0; diff --git a/TLM/TLM/Patch/_RoadBaseAI/TrafficLightSimulationStepPatch.cs b/TLM/TLM/Patch/_RoadBaseAI/TrafficLightSimulationStepPatch.cs index 11d4ef6d7..29eff0b8b 100644 --- a/TLM/TLM/Patch/_RoadBaseAI/TrafficLightSimulationStepPatch.cs +++ b/TLM/TLM/Patch/_RoadBaseAI/TrafficLightSimulationStepPatch.cs @@ -1,8 +1,9 @@ -namespace TrafficManager.Patch._RoadBaseAI { +namespace TrafficManager.Patch._RoadBaseAI { + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.State; - // [Harmony] Manually patched because struct references are used + [HarmonyPatch(typeof(TrainTrackBaseAI), nameof(TrainTrackBaseAI.LevelCrossingSimulationStep))] public class TrafficLightSimulationStepPatch { /// /// Decides whether the stock simulation step for traffic lights should run. diff --git a/TLM/TLM/Patch/_TrainTrackBaseAI/LevelCrossingSimulationStepPatch.cs b/TLM/TLM/Patch/_TrainTrackBaseAI/LevelCrossingSimulationStepPatch.cs index 18e609a7c..33fb557dc 100644 --- a/TLM/TLM/Patch/_TrainTrackBaseAI/LevelCrossingSimulationStepPatch.cs +++ b/TLM/TLM/Patch/_TrainTrackBaseAI/LevelCrossingSimulationStepPatch.cs @@ -1,8 +1,9 @@ -namespace TrafficManager.Patch._TrainTrackBase { +namespace TrafficManager.Patch._TrainTrackBase { + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.State; - // [Harmony] Manually patched because struct references are used + [HarmonyPatch(typeof(RoadBaseAI), nameof(RoadBaseAI.TrafficLightSimulationStep))] public class LevelCrossingSimulationStepPatch { /// /// Decides whether the stock simulation step for traffic lights should run. diff --git a/TLM/TLM/RedirectionFramework b/TLM/TLM/RedirectionFramework index 68a47a187..790597211 160000 --- a/TLM/TLM/RedirectionFramework +++ b/TLM/TLM/RedirectionFramework @@ -1 +1 @@ -Subproject commit 68a47a18757ea50269498d7db8a5f3e70318aa88 +Subproject commit 7905972117f9470693fb707e78b5f49eb5e75370 diff --git a/TLM/TLM/ThreadingExtension.cs b/TLM/TLM/ThreadingExtension.cs index 8f41ce3d1..c5a75a362 100644 --- a/TLM/TLM/ThreadingExtension.cs +++ b/TLM/TLM/ThreadingExtension.cs @@ -49,23 +49,6 @@ public override void OnBeforeSimulationFrame() { List missingDetours = new List(); - foreach (KeyValuePair entry in HarmonyMethodStates) { - MethodBase method = entry.Key; - RedirectCallsState oldState = entry.Value; - RedirectCallsState newState = - RedirectionHelper.GetState(method.MethodHandle.GetFunctionPointer()); - - if (!oldState.Equals(newState)) { - missingDetours.Add( - string.Format( - " {0}.{1} with {2} parameters ({3})", - method.DeclaringType.Name, - method.Name, - method.GetParameters().Length, - method.DeclaringType.AssemblyQualifiedName)); - } - } - Log.Info($"ThreadingExtension.OnBeforeSimulationFrame: First frame detected. " + $"Detours checked. Result: {missingDetours.Count} missing detours"); From 2eabc1b8d7f2ed3fc8b74c9fba84650e9cb7bc5d Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Sat, 25 Apr 2020 21:01:05 +0300 Subject: [PATCH 03/13] Cities Harmony is woring. --- TLM/TLM/RedirectionFramework | 2 +- TLM/TLM/TLM.csproj | 11 ++++++++--- TLM/TLM/TrafficManagerMod.cs | 3 +++ TLM/TLM/packages.config | 2 ++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/TLM/TLM/RedirectionFramework b/TLM/TLM/RedirectionFramework index 790597211..89b0ff1d9 160000 --- a/TLM/TLM/RedirectionFramework +++ b/TLM/TLM/RedirectionFramework @@ -1 +1 @@ -Subproject commit 7905972117f9470693fb707e78b5f49eb5e75370 +Subproject commit 89b0ff1d99c5b25c488bc5408d4bb0586519ee2c diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 254c3930d..9f36ea1e2 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -87,13 +87,19 @@ ..\dependencies - - RedirectionFramework\lib\0TMPE.Harmony.dll + + ..\packages\Lib.Harmony.2.0.0.9\lib\net35\0Harmony.dll + False + False $(MangedDLLPath)\Assembly-CSharp.dll False + + ..\packages\CitiesHarmony.API.1.0.4\lib\net35\CitiesHarmony.API.dll + False + $(MangedDLLPath)\ColossalManaged.dll False @@ -176,7 +182,6 @@ - diff --git a/TLM/TLM/TrafficManagerMod.cs b/TLM/TLM/TrafficManagerMod.cs index 513e507dc..99f35f958 100644 --- a/TLM/TLM/TrafficManagerMod.cs +++ b/TLM/TLM/TrafficManagerMod.cs @@ -12,6 +12,7 @@ namespace TrafficManager { using static TrafficManager.Util.Shortcuts; using ColossalFramework; using UnityEngine.SceneManagement; + using CitiesHarmony.API; public class TrafficManagerMod : IUserMod { #if LABS @@ -85,6 +86,8 @@ public void OnEnabled() { Instance = this; InGameHotReload = InGame(); + + HarmonyHelper.EnsureHarmonyInstalled(); } [UsedImplicitly] diff --git a/TLM/TLM/packages.config b/TLM/TLM/packages.config index 5011e1991..595a8c302 100644 --- a/TLM/TLM/packages.config +++ b/TLM/TLM/packages.config @@ -1,5 +1,7 @@  + + \ No newline at end of file From 206f3069cffe22213d93d53c613122dd93cc58ec Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Sat, 25 Apr 2020 21:30:57 +0300 Subject: [PATCH 04/13] new LifeCycle directory. removed redundant/commented out code. --- TLM/TLM/{ => LifeCycle}/LoadingExtension.cs | 246 ++++++------------ TLM/TLM/LifeCycle/Patcher.cs | 113 ++++++++ .../SerializableDataExtension.cs | 3 +- TLM/TLM/{ => LifeCycle}/ThreadingExtension.cs | 48 +--- TLM/TLM/{ => LifeCycle}/TrafficManagerMod.cs | 2 +- TLM/TLM/Manager/Impl/OptionsManager.cs | 3 +- TLM/TLM/Manager/Impl/UtilityManager.cs | 4 +- TLM/TLM/Patch/_Vehicle/SpawnPatch.cs | 24 -- TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs | 24 -- TLM/TLM/State/GlobalConfig.cs | 3 +- TLM/TLM/State/Options.cs | 1 + TLM/TLM/TLM.csproj | 11 +- TLM/TLM/UI/Helpers/GuideHandler.cs | 3 +- TLM/TLM/UI/IncompatibleModsPanel.cs | 1 + TLM/TLM/UI/Localization/Translation.cs | 1 + TLM/TLM/UI/MainMenu/DebugMenu.cs | 1 + TLM/TLM/UI/MainMenu/MainMenuWindow.cs | 1 + TLM/TLM/UI/ModUI.cs | 1 + 18 files changed, 210 insertions(+), 280 deletions(-) rename TLM/TLM/{ => LifeCycle}/LoadingExtension.cs (63%) create mode 100644 TLM/TLM/LifeCycle/Patcher.cs rename TLM/TLM/{State => LifeCycle}/SerializableDataExtension.cs (99%) rename TLM/TLM/{ => LifeCycle}/ThreadingExtension.cs (54%) rename TLM/TLM/{ => LifeCycle}/TrafficManagerMod.cs (99%) delete mode 100644 TLM/TLM/Patch/_Vehicle/SpawnPatch.cs delete mode 100644 TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs diff --git a/TLM/TLM/LoadingExtension.cs b/TLM/TLM/LifeCycle/LoadingExtension.cs similarity index 63% rename from TLM/TLM/LoadingExtension.cs rename to TLM/TLM/LifeCycle/LoadingExtension.cs index 3d3e35da4..5b3bc3ad4 100644 --- a/TLM/TLM/LoadingExtension.cs +++ b/TLM/TLM/LifeCycle/LoadingExtension.cs @@ -1,49 +1,30 @@ -namespace TrafficManager { - using ColossalFramework.UI; +namespace TrafficManager.LifeCycle { using ColossalFramework; + using ColossalFramework.UI; using CSUtil.Commons; - using HarmonyLib; using ICities; using JetBrains.Annotations; - using UnityEngine; - using Object = UnityEngine.Object; + using System; using System.Collections.Generic; using System.Reflection; - using System; using TrafficManager.API.Manager; using TrafficManager.Custom.PathFinding; using TrafficManager.Manager.Impl; - using TrafficManager.RedirectionFramework; using TrafficManager.State; using TrafficManager.UI; - using TrafficManager.UI.MainMenu.OSD; - using TrafficManager.Util; + using UnityEngine; + using Object = UnityEngine.Object; [UsedImplicitly] public class LoadingExtension : LoadingExtensionBase { - private const string HARMONY_ID = "de.viathinksoft.tmpe"; internal static LoadingExtension Instance = null; FastList simManager => typeof(SimulationManager).GetField("m_managers", BindingFlags.Static | BindingFlags.NonPublic) ?.GetValue(null) as FastList; - public class Detour { - public MethodInfo OriginalMethod; - public MethodInfo CustomMethod; - public RedirectCallsState Redirect; - - public Detour(MethodInfo originalMethod, MethodInfo customMethod) { - OriginalMethod = originalMethod; - CustomMethod = customMethod; - Redirect = RedirectionHelper.RedirectCalls(originalMethod, customMethod); - } - } - public static CustomPathManager CustomPathManager { get; set; } - public static bool DetourInited { get; set; } - /// /// Contains loaded languages and lookup functions for text translations /// @@ -59,14 +40,6 @@ public static bool IsPathManagerReplaced { get; private set; } - /// - /// Method redirection states for attribute-driven detours - /// - public static IDictionary DetouredMethodStates { - get; - private set; - } = new Dictionary(); - static LoadingExtension() { TranslationDatabase.LoadAllTranslations(); } @@ -74,90 +47,18 @@ static LoadingExtension() { public LoadingExtension() { } - public void RevertDetours() { - if (!DetourInited) { - return; - } - - var harmony = new Harmony(HARMONY_ID); - Shortcuts.Assert(harmony != null, "HarmonyInst!=null"); - harmony.UnpatchAll(); - - DetourInited = false; - Log.Info("Reverting detours finished."); - } - - private void InitDetours() { - // TODO realize detouring with annotations - if (DetourInited) { - return; - } - - Log.Info("Init detours"); - bool detourFailed = false; - - try { -#if DEBUG - Harmony.DEBUG = true; -#endif - // Harmony attribute-driven patching - Log.Info($"Performing Harmony attribute-driven patching"); - var harmony = new Harmony(HARMONY_ID); - Shortcuts.Assert(harmony!=null, "HarmonyInst!=null"); - harmony.PatchAll(); - Log.Info($"Harmony attribute-driven patching successfull!"); - } - catch (Exception e) { - Log.Error("Could not deploy Harmony patches"); - Log.Info(e.Message); - Log.Info(e.StackTrace); - detourFailed = true; - throw e; - } - - try { - Log.Info("Deploying attribute-driven detours"); - DetouredMethodStates = AssemblyRedirector.Deploy(); - } catch (Exception e) { - Log.Error("Could not deploy attribute-driven detours"); - Log.Info(e.ToString()); - Log.Info(e.StackTrace); - detourFailed = true; - } - - if (detourFailed) { - Log.Info("Detours failed"); - Singleton.instance.m_ThreadingWrapper.QueueMainThread( - () => { - UIView.library - .ShowModal("ExceptionPanel") - .SetMessage( - "TM:PE failed to load", - "Traffic Manager: President Edition failed to load. You can " + - "continue playing but it's NOT recommended. Traffic Manager will " + - "not work as expected.", - true); - }); - } else { - Log.Info("Detours successful"); - } - - DetourInited = true; - } - public override void OnCreated(ILoading loading) { Log._Debug("LoadingExtension.OnCreated() called"); // SelfDestruct.DestructOldInstances(this); base.OnCreated(loading); - if(IsGameLoaded) { + if (IsGameLoaded) { // When another mod is detected, OnCreated is called again for god - or CS team - knows what reason! Log._Debug("Hot reload of another mod detected. Skipping LoadingExtension.OnCreated() ..."); return; } RegisteredManagers = new List(); - DetourInited = false; CustomPathManager = new CustomPathManager(); RegisterCustomManagers(); @@ -223,8 +124,7 @@ public override void OnLevelUnloading() { var gameObject = UIView.GetAView().gameObject; - void Destroy() where T : MonoBehaviour - { + void Destroy() where T : MonoBehaviour { Object obj = (Object)gameObject.GetComponent(); if (obj != null) { Object.Destroy(obj); @@ -259,7 +159,7 @@ void Destroy() where T : MonoBehaviour // ignored - prevents collision with other mods } - RevertDetours(); + Patcher.Instance?.Uninstall(); IsGameLoaded = false; } @@ -276,81 +176,80 @@ public override void OnLevelLoaded(LoadMode mode) { case SimulationManager.UpdateMode.NewGameFromMap: case SimulationManager.UpdateMode.NewGameFromScenario: case SimulationManager.UpdateMode.LoadGame: { - if (BuildConfig.applicationVersion != BuildConfig.VersionToString( - TrafficManagerMod.GAME_VERSION, - false)) - { - string[] majorVersionElms = BuildConfig.applicationVersion.Split('-'); - string[] versionElms = majorVersionElms[0].Split('.'); - uint versionA = Convert.ToUInt32(versionElms[0]); - uint versionB = Convert.ToUInt32(versionElms[1]); - uint versionC = Convert.ToUInt32(versionElms[2]); - - Log.Info($"Detected game version v{BuildConfig.applicationVersion}"); - - bool isModTooOld = TrafficManagerMod.GAME_VERSION_A < versionA || - (TrafficManagerMod.GAME_VERSION_A == versionA && - TrafficManagerMod.GAME_VERSION_B < versionB); + if (BuildConfig.applicationVersion != BuildConfig.VersionToString( + TrafficManagerMod.GAME_VERSION, + false)) { + string[] majorVersionElms = BuildConfig.applicationVersion.Split('-'); + string[] versionElms = majorVersionElms[0].Split('.'); + uint versionA = Convert.ToUInt32(versionElms[0]); + uint versionB = Convert.ToUInt32(versionElms[1]); + uint versionC = Convert.ToUInt32(versionElms[2]); + + Log.Info($"Detected game version v{BuildConfig.applicationVersion}"); + + bool isModTooOld = TrafficManagerMod.GAME_VERSION_A < versionA || + (TrafficManagerMod.GAME_VERSION_A == versionA && + TrafficManagerMod.GAME_VERSION_B < versionB); // || (TrafficManagerMod.GameVersionA == versionA // && TrafficManagerMod.GameVersionB == versionB // && TrafficManagerMod.GameVersionC < versionC); - bool isModNewer = TrafficManagerMod.GAME_VERSION_A < versionA || - (TrafficManagerMod.GAME_VERSION_A == versionA && - TrafficManagerMod.GAME_VERSION_B > versionB); + bool isModNewer = TrafficManagerMod.GAME_VERSION_A < versionA || + (TrafficManagerMod.GAME_VERSION_A == versionA && + TrafficManagerMod.GAME_VERSION_B > versionB); // || (TrafficManagerMod.GameVersionA == versionA // && TrafficManagerMod.GameVersionB == versionB // && TrafficManagerMod.GameVersionC > versionC); - if (isModTooOld) { - string msg = string.Format( - "Traffic Manager: President Edition detected that you are running " + - "a newer game version ({0}) than TM:PE has been built for ({1}). " + - "Please be aware that TM:PE has not been updated for the newest game " + - "version yet and thus it is very likely it will not work as expected.", - BuildConfig.applicationVersion, - BuildConfig.VersionToString(TrafficManagerMod.GAME_VERSION, false)); - - Log.Error(msg); - Singleton.instance.m_ThreadingWrapper.QueueMainThread( - () => { - UIView.library - .ShowModal("ExceptionPanel") - .SetMessage( - "TM:PE has not been updated yet", - msg, - false); - }); - } else if (isModNewer) { - string msg = string.Format( - "Traffic Manager: President Edition has been built for game version {0}. " + - "You are running game version {1}. Some features of TM:PE will not " + - "work with older game versions. Please let Steam update your game.", - BuildConfig.VersionToString(TrafficManagerMod.GAME_VERSION, false), - BuildConfig.applicationVersion); - - Log.Error(msg); - Singleton - .instance.m_ThreadingWrapper.QueueMainThread( - () => { - UIView.library - .ShowModal("ExceptionPanel") - .SetMessage( - "Your game should be updated", - msg, - false); - }); + if (isModTooOld) { + string msg = string.Format( + "Traffic Manager: President Edition detected that you are running " + + "a newer game version ({0}) than TM:PE has been built for ({1}). " + + "Please be aware that TM:PE has not been updated for the newest game " + + "version yet and thus it is very likely it will not work as expected.", + BuildConfig.applicationVersion, + BuildConfig.VersionToString(TrafficManagerMod.GAME_VERSION, false)); + + Log.Error(msg); + Singleton.instance.m_ThreadingWrapper.QueueMainThread( + () => { + UIView.library + .ShowModal("ExceptionPanel") + .SetMessage( + "TM:PE has not been updated yet", + msg, + false); + }); + } else if (isModNewer) { + string msg = string.Format( + "Traffic Manager: President Edition has been built for game version {0}. " + + "You are running game version {1}. Some features of TM:PE will not " + + "work with older game versions. Please let Steam update your game.", + BuildConfig.VersionToString(TrafficManagerMod.GAME_VERSION, false), + BuildConfig.applicationVersion); + + Log.Error(msg); + Singleton + .instance.m_ThreadingWrapper.QueueMainThread( + () => { + UIView.library + .ShowModal("ExceptionPanel") + .SetMessage( + "Your game should be updated", + msg, + false); + }); + } } - } - IsGameLoaded = true; - break; - } + IsGameLoaded = true; + break; + } default: { - Log.Info($"OnLevelLoaded: Unsupported game mode {mode}"); - return; - } + Log.Info($"OnLevelLoaded: Unsupported game mode {mode}"); + return; + } } //it will replace stock PathManager or already Replaced before HotReload @@ -403,7 +302,8 @@ public override void OnLevelLoaded(LoadMode mode) { Log._Debug("Should be custom: " + Singleton.instance.GetType()); IsPathManagerReplaced = true; - } catch (Exception ex) { + } + catch (Exception ex) { string error = "Traffic Manager: President Edition failed to load. You can continue " + "playing but it's NOT recommended. Traffic Manager will not work as expected."; @@ -439,7 +339,7 @@ public override void OnLevelLoaded(LoadMode mode) { UIView.GetAView().gameObject.AddComponent(); - InitDetours(); + Patcher.Create().Install(); // Log.Info("Fixing non-created nodes with problems..."); // FixNonCreatedNodeProblems(); diff --git a/TLM/TLM/LifeCycle/Patcher.cs b/TLM/TLM/LifeCycle/Patcher.cs new file mode 100644 index 000000000..6c7dda195 --- /dev/null +++ b/TLM/TLM/LifeCycle/Patcher.cs @@ -0,0 +1,113 @@ +namespace TrafficManager.LifeCycle { + using ColossalFramework.UI; + using ColossalFramework; + using CSUtil.Commons; + using HarmonyLib; + using System.Collections.Generic; + using System.Reflection; + using System; + + using TrafficManager.RedirectionFramework; + + using TrafficManager.Util; + + public class Patcher { + public static Patcher Instance { get; private set; } + public static Patcher Create() => Instance = new Patcher(); + + private const string HARMONY_ID = "de.viathinksoft.tmpe"; + + public class Detour { + public MethodInfo OriginalMethod; + public MethodInfo CustomMethod; + public RedirectCallsState Redirect; + + public Detour(MethodInfo originalMethod, MethodInfo customMethod) { + OriginalMethod = originalMethod; + CustomMethod = customMethod; + Redirect = RedirectionHelper.RedirectCalls(originalMethod, customMethod); + } + } + + public bool initialized_ { get; private set; } = false; + + /// + /// Method redirection states for attribute-driven detours + /// + public IDictionary DetouredMethodStates { get; private set; } = + new Dictionary(); + + public void Install() { + // TODO realize detouring with annotations + if (initialized_) { + return; + } + + Log.Info("Init detours"); + bool fail = false; + + try { +#if DEBUG + Harmony.DEBUG = true; +#endif + // Harmony attribute-driven patching + Log.Info($"Performing Harmony attribute-driven patching"); + var harmony = new Harmony(HARMONY_ID); + Shortcuts.Assert(harmony != null, "HarmonyInst!=null"); + harmony.PatchAll(); + Log.Info($"Harmony attribute-driven patching successfull!"); + } + catch (Exception e) { + Log.Error("Could not deploy Harmony patches"); + Log.Info(e.Message); + Log.Info(e.StackTrace); + fail = true; + throw e; + } + + try { + Log.Info("Deploying attribute-driven detours"); + DetouredMethodStates = AssemblyRedirector.Deploy(); + } + catch (Exception e) { + Log.Error("Could not deploy attribute-driven detours"); + Log.Info(e.ToString()); + Log.Info(e.StackTrace); + fail = true; + } + + if (fail) { + Log.Info("Detours failed"); + Singleton.instance.m_ThreadingWrapper.QueueMainThread( + () => { + UIView.library + .ShowModal("ExceptionPanel") + .SetMessage( + "TM:PE failed to load", + "Traffic Manager: President Edition failed to load. You can " + + "continue playing but it's NOT recommended. Traffic Manager will " + + "not work as expected.", + true); + }); + } else { + Log.Info("Detours successful"); + } + + initialized_ = true; + } + + public void Uninstall() { + if (!initialized_) { + return; + } + + var harmony = new Harmony(HARMONY_ID); + Shortcuts.Assert(harmony != null, "HarmonyInst!=null"); + harmony.UnpatchAll(); + + initialized_ = false; + Log.Info("Reverting detours finished."); + } + + } +} diff --git a/TLM/TLM/State/SerializableDataExtension.cs b/TLM/TLM/LifeCycle/SerializableDataExtension.cs similarity index 99% rename from TLM/TLM/State/SerializableDataExtension.cs rename to TLM/TLM/LifeCycle/SerializableDataExtension.cs index d8747dce7..72cfa0de9 100644 --- a/TLM/TLM/State/SerializableDataExtension.cs +++ b/TLM/TLM/LifeCycle/SerializableDataExtension.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.State { +namespace TrafficManager.LifeCycle { using CSUtil.Commons; using ICities; using JetBrains.Annotations; @@ -8,6 +8,7 @@ namespace TrafficManager.State { using System; using TrafficManager.API.Manager; using TrafficManager.Manager.Impl; + using State; [UsedImplicitly] public class SerializableDataExtension diff --git a/TLM/TLM/ThreadingExtension.cs b/TLM/TLM/LifeCycle/ThreadingExtension.cs similarity index 54% rename from TLM/TLM/ThreadingExtension.cs rename to TLM/TLM/LifeCycle/ThreadingExtension.cs index c5a75a362..86019487c 100644 --- a/TLM/TLM/ThreadingExtension.cs +++ b/TLM/TLM/LifeCycle/ThreadingExtension.cs @@ -1,4 +1,4 @@ -namespace TrafficManager { +namespace TrafficManager.LifeCycle { using ColossalFramework.UI; using ColossalFramework; using CSUtil.Commons.Benchmark; @@ -25,8 +25,6 @@ public sealed class ThreadingExtension : ThreadingExtensionBase { IRoutingManager routeMan = Constants.ManagerFactory.RoutingManager; IUtilityManager utilMan = Constants.ManagerFactory.UtilityManager; - bool firstFrame = true; - public override void OnCreated(IThreading threading) { base.OnCreated(threading); @@ -43,55 +41,11 @@ public override void OnBeforeSimulationTick() { public override void OnBeforeSimulationFrame() { base.OnBeforeSimulationFrame(); - if (firstFrame) { - firstFrame = false; - Log.Info("ThreadingExtension.OnBeforeSimulationFrame: First frame detected. Checking detours."); - - List missingDetours = new List(); - - Log.Info($"ThreadingExtension.OnBeforeSimulationFrame: First frame detected. " + - $"Detours checked. Result: {missingDetours.Count} missing detours"); - - if (missingDetours.Count > 0) { - string error = - "Traffic Manager: President Edition detected an incompatibility with another " + - "mod! You can continue playing but it's NOT recommended. Traffic Manager will " + - "not work as expected. See TMPE.log for technical details."; - Log.Error(error); - string log = "The following methods were overriden by another mod:"; - - foreach (string missingDetour in missingDetours) { - log += $"\n\t{missingDetour}"; - } - - Log.Info(log); - - if (GlobalConfig.Instance.Main.ShowCompatibilityCheckErrorMessage) { - Prompt.Error("TM:PE Incompatibility Issue", error); - } - } - } - if (Options.timedLightsEnabled) { tlsMan.SimulationStep(); } } - // public override void OnAfterSimulationFrame() { - // base.OnAfterSimulationFrame(); - // - // routeMan.SimulationStep(); - // - // ++ticksSinceLastMinuteUpdate; - // if (ticksSinceLastMinuteUpdate > 60 * 60) { - // ticksSinceLastMinuteUpdate = 0; - // GlobalConfig.Instance.SimulationStep(); - // #if DEBUG - // DebugMenuPanel.PrintTransportStats(); - // #endif - // } - // } - public override void OnUpdate(float realTimeDelta, float simulationTimeDelta) { base.OnUpdate(realTimeDelta, simulationTimeDelta); diff --git a/TLM/TLM/TrafficManagerMod.cs b/TLM/TLM/LifeCycle/TrafficManagerMod.cs similarity index 99% rename from TLM/TLM/TrafficManagerMod.cs rename to TLM/TLM/LifeCycle/TrafficManagerMod.cs index 99f35f958..9a0db0979 100644 --- a/TLM/TLM/TrafficManagerMod.cs +++ b/TLM/TLM/LifeCycle/TrafficManagerMod.cs @@ -1,4 +1,4 @@ -namespace TrafficManager { +namespace TrafficManager.LifeCycle { using ColossalFramework.Globalization; using ColossalFramework.UI; using CSUtil.Commons; diff --git a/TLM/TLM/Manager/Impl/OptionsManager.cs b/TLM/TLM/Manager/Impl/OptionsManager.cs index 7de7ad7f7..c155052f0 100644 --- a/TLM/TLM/Manager/Impl/OptionsManager.cs +++ b/TLM/TLM/Manager/Impl/OptionsManager.cs @@ -1,10 +1,11 @@ -namespace TrafficManager.Manager.Impl { +namespace TrafficManager.Manager.Impl { using CSUtil.Commons; using System; using TrafficManager.API.Manager; using TrafficManager.API.Traffic.Enums; using TrafficManager.State; using TrafficManager.UI.Helpers; + using TrafficManager.LifeCycle; public class OptionsManager : AbstractCustomManager, diff --git a/TLM/TLM/Manager/Impl/UtilityManager.cs b/TLM/TLM/Manager/Impl/UtilityManager.cs index 6bb7164f0..7d11e9514 100644 --- a/TLM/TLM/Manager/Impl/UtilityManager.cs +++ b/TLM/TLM/Manager/Impl/UtilityManager.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.Manager.Impl { +namespace TrafficManager.Manager.Impl { using ColossalFramework; using CSUtil.Commons; using System.Threading; @@ -7,6 +7,8 @@ using TrafficManager.API.Manager; using TrafficManager.State; using UnityEngine; + using TrafficManager.LifeCycle; + public class UtilityManager : AbstractCustomManager, IUtilityManager { public static UtilityManager Instance { get; } diff --git a/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs b/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs deleted file mode 100644 index 95e7f7b79..000000000 --- a/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs +++ /dev/null @@ -1,24 +0,0 @@ -using ColossalFramework; -using ColossalFramework.Math; -using CSUtil.Commons; -using HarmonyLib; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEngine; - -namespace TrafficManager.Patch._Vehicle { - // TODO this does currently not work with Harmony v1.1 - - //[HarmonyPatch(typeof(Vehicle), "Spawn")] - //public static class SpawnPatch { - // /// - // /// Notifies the vehicle state manager about a spawned vehicle. - // /// - // [HarmonyPostfix] - // public static void Postfix(ushort vehicleID) { - // Constants.ManagerFactory.VehicleStateManager.OnSpawnVehicle(vehicleID, ref Singleton.instance.m_vehicles.m_buffer[vehicleID]); - // } - //} -} \ No newline at end of file diff --git a/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs b/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs deleted file mode 100644 index dbe41983c..000000000 --- a/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs +++ /dev/null @@ -1,24 +0,0 @@ -using ColossalFramework; -using ColossalFramework.Math; -using CSUtil.Commons; -using HarmonyLib; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEngine; - -namespace TrafficManager.Patch._Vehicle { - // TODO this does currently not work with Harmony v1.1 - - //[HarmonyPatch(typeof(Vehicle), "Unspawn")] - //public static class UnspawnPatch { - // /// - // /// Notifies the vehicle state manager about a despawned vehicle. - // /// - // //[HarmonyPrefix] - // public static void Prefix(ushort vehicleID) { - // Constants.ManagerFactory.VehicleStateManager.OnDespawnVehicle(vehicleID, ref Singleton.instance.m_vehicles.m_buffer[vehicleID]); - // } - //} -} \ No newline at end of file diff --git a/TLM/TLM/State/GlobalConfig.cs b/TLM/TLM/State/GlobalConfig.cs index fe2fbe404..ca9c4834e 100644 --- a/TLM/TLM/State/GlobalConfig.cs +++ b/TLM/TLM/State/GlobalConfig.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.State { +namespace TrafficManager.State { using CSUtil.Commons; using JetBrains.Annotations; using System.IO; @@ -6,6 +6,7 @@ using System; using TrafficManager.State.ConfigData; using TrafficManager.Util; + using TrafficManager.LifeCycle; [XmlRootAttribute("GlobalConfig", Namespace = "http://www.viathinksoft.de/tmpe", IsNullable = false)] public class GlobalConfig : GenericObservable { diff --git a/TLM/TLM/State/Options.cs b/TLM/TLM/State/Options.cs index 4562fe995..18ab23416 100644 --- a/TLM/TLM/State/Options.cs +++ b/TLM/TLM/State/Options.cs @@ -8,6 +8,7 @@ namespace TrafficManager.State { using TrafficManager.UI.Helpers; using TrafficManager.UI; using UnityEngine; + using TrafficManager.LifeCycle; public class Options : MonoBehaviour { #if DEBUG diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 9f36ea1e2..ce787f4a1 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -150,6 +150,7 @@ + @@ -211,8 +212,6 @@ - - @@ -247,7 +246,7 @@ - + @@ -334,8 +333,8 @@ - - + + @@ -344,7 +343,7 @@ - + diff --git a/TLM/TLM/UI/Helpers/GuideHandler.cs b/TLM/TLM/UI/Helpers/GuideHandler.cs index 552b2f4e9..711c20366 100644 --- a/TLM/TLM/UI/Helpers/GuideHandler.cs +++ b/TLM/TLM/UI/Helpers/GuideHandler.cs @@ -1,8 +1,9 @@ +namespace TrafficManager.UI.Helpers { using ColossalFramework; using CSUtil.Commons; using System.Collections.Generic; + using TrafficManager.LifeCycle; -namespace TrafficManager.UI.Helpers { public class GuideHandler { private Dictionary GuideTable = new Dictionary(); diff --git a/TLM/TLM/UI/IncompatibleModsPanel.cs b/TLM/TLM/UI/IncompatibleModsPanel.cs index 96e093728..1b0f6b43d 100644 --- a/TLM/TLM/UI/IncompatibleModsPanel.cs +++ b/TLM/TLM/UI/IncompatibleModsPanel.cs @@ -9,6 +9,7 @@ namespace TrafficManager.UI { using System; using TrafficManager.State; using UnityEngine; + using LifeCycle; public class IncompatibleModsPanel : UIPanel { private const ulong LOCAL_MOD = ulong.MaxValue; diff --git a/TLM/TLM/UI/Localization/Translation.cs b/TLM/TLM/UI/Localization/Translation.cs index ba72f8c9a..ef1dece6e 100644 --- a/TLM/TLM/UI/Localization/Translation.cs +++ b/TLM/TLM/UI/Localization/Translation.cs @@ -7,6 +7,7 @@ namespace TrafficManager.UI { using ColossalFramework.Globalization; using CSUtil.Commons; using TrafficManager.State; + using TrafficManager.LifeCycle; /// /// Adding a new language step by step: diff --git a/TLM/TLM/UI/MainMenu/DebugMenu.cs b/TLM/TLM/UI/MainMenu/DebugMenu.cs index 9b07d6382..c60c70836 100644 --- a/TLM/TLM/UI/MainMenu/DebugMenu.cs +++ b/TLM/TLM/UI/MainMenu/DebugMenu.cs @@ -12,6 +12,7 @@ namespace TrafficManager.UI.MainMenu { using global::TrafficManager.State; using JetBrains.Annotations; using UnityEngine; + using TrafficManager.LifeCycle; #if DEBUG // whole class coverage public class DebugMenuPanel : UIPanel diff --git a/TLM/TLM/UI/MainMenu/MainMenuWindow.cs b/TLM/TLM/UI/MainMenu/MainMenuWindow.cs index 29df961ba..83707445d 100644 --- a/TLM/TLM/UI/MainMenu/MainMenuWindow.cs +++ b/TLM/TLM/UI/MainMenu/MainMenuWindow.cs @@ -16,6 +16,7 @@ namespace TrafficManager.UI.MainMenu { using TrafficManager.U.Panel; using TrafficManager.UI.MainMenu.OSD; using UnityEngine; + using TrafficManager.LifeCycle; public class MainMenuWindow : U.Panel.BaseUWindowPanel, diff --git a/TLM/TLM/UI/ModUI.cs b/TLM/TLM/UI/ModUI.cs index 20ad58d46..1d11a76e3 100644 --- a/TLM/TLM/UI/ModUI.cs +++ b/TLM/TLM/UI/ModUI.cs @@ -5,6 +5,7 @@ namespace TrafficManager.UI { using TrafficManager.UI.MainMenu; using TrafficManager.Util; using UnityEngine; + using TrafficManager.LifeCycle; /// /// Globally available UI manager class which contains the main menu button and the panel. From 85f9d95c4e311f0a6b665db81177781fb9aea665 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Sat, 25 Apr 2020 22:08:03 +0300 Subject: [PATCH 05/13] --- TLM/TLM/Patch/_Vehicle/SpawnPatch.cs | 24 ++++++++++++++++++++++++ TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs | 24 ++++++++++++++++++++++++ TLM/TLM/TLM.csproj | 2 ++ 3 files changed, 50 insertions(+) create mode 100644 TLM/TLM/Patch/_Vehicle/SpawnPatch.cs create mode 100644 TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs diff --git a/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs b/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs new file mode 100644 index 000000000..2cada799f --- /dev/null +++ b/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs @@ -0,0 +1,24 @@ +using ColossalFramework; +using ColossalFramework.Math; +using CSUtil.Commons; +using Harmony; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; + +namespace TrafficManager.Patch._Vehicle { + // TODO [issue #864] replace custom AI with harmony patch when possible. + + //[HarmonyPatch(typeof(Vehicle), "Spawn")] + //public static class SpawnPatch { + // /// + // /// Notifies the vehicle state manager about a spawned vehicle. + // /// + // [HarmonyPostfix] + // public static void Postfix(ushort vehicleID) { + // Constants.ManagerFactory.VehicleStateManager.OnSpawnVehicle(vehicleID, ref Singleton.instance.m_vehicles.m_buffer[vehicleID]); + // } + //} +} \ No newline at end of file diff --git a/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs b/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs new file mode 100644 index 000000000..1cf3bcf23 --- /dev/null +++ b/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs @@ -0,0 +1,24 @@ +using ColossalFramework; +using ColossalFramework.Math; +using CSUtil.Commons; +using Harmony; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; + +namespace TrafficManager.Patch._Vehicle { + // TODO [issue #864] replace custom AI with harmony patch when possible. + + //[HarmonyPatch(typeof(Vehicle), "Unspawn")] + //public static class UnspawnPatch { + // /// + // /// Notifies the vehicle state manager about a despawned vehicle. + // /// + // //[HarmonyPrefix] + // public static void Prefix(ushort vehicleID) { + // Constants.ManagerFactory.VehicleStateManager.OnDespawnVehicle(vehicleID, ref Singleton.instance.m_vehicles.m_buffer[vehicleID]); + // } + //} +} \ No newline at end of file diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index ce787f4a1..bec383f4d 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -161,6 +161,8 @@ + + From def7b65babb196907fac9bf659c1bad45b57bbc8 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Sat, 25 Apr 2020 22:29:09 +0300 Subject: [PATCH 06/13] revived vehcileAI commented out code with TODO issue number. --- TLM/TLM/Patch/_Vehicle/SpawnPatch.cs | 2 +- TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs b/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs index 2cada799f..1b2b8f7cf 100644 --- a/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs +++ b/TLM/TLM/Patch/_Vehicle/SpawnPatch.cs @@ -1,7 +1,7 @@ using ColossalFramework; using ColossalFramework.Math; using CSUtil.Commons; -using Harmony; +using HarmonyLib; using System; using System.Collections.Generic; using System.Linq; diff --git a/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs b/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs index 1cf3bcf23..16e528c66 100644 --- a/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs +++ b/TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs @@ -1,7 +1,7 @@ using ColossalFramework; using ColossalFramework.Math; using CSUtil.Commons; -using Harmony; +using HarmonyLib; using System; using System.Collections.Generic; using System.Linq; From 008c8d6c76e2fe96034487ee50b3b6db2df3ec30 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Sat, 25 Apr 2020 22:45:47 +0300 Subject: [PATCH 07/13] fixed post build script. --- TLM/TLM/TLM.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index bec383f4d..7f0f7027d 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -680,7 +680,7 @@ xcopy /y "$(TargetDir)TMPE.API.dll" "%25DEPLOYDIR%25" xcopy /y "$(TargetDir)CSUtil.CameraControl.dll" "%25DEPLOYDIR%25" xcopy /y "$(TargetDir)TMPE.RedirectionFramework.dll" "%25DEPLOYDIR%25" xcopy /y "$(TargetDir)CSUtil.Commons.dll" "%25DEPLOYDIR%25" -xcopy /y "$(TargetDir)0TMPE.Harmony.dll" "%25DEPLOYDIR%25" +xcopy /y "$(TargetDir)CitiesHarmony.API.dll" "%25DEPLOYDIR%25" rem To avoid double hot reload, TrafficManager.dll must be replaced last and fast. rem Once TrafficManager.dll is re-loaded, all other dlls will be reloaded as well From 4bdc0ed2f72429b828e56508b5fdcfc98f9c7d31 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Mon, 18 May 2020 21:20:41 +0300 Subject: [PATCH 08/13] Undid move to LifeCycle to reduce the size of PR --- TLM/TLM/{LifeCycle => }/LoadingExtension.cs | 2 +- TLM/TLM/Manager/Impl/OptionsManager.cs | 1 - TLM/TLM/Manager/Impl/UtilityManager.cs | 2 -- TLM/TLM/{LifeCycle => }/Patcher.cs | 2 +- TLM/TLM/State/GlobalConfig.cs | 1 - TLM/TLM/State/Options.cs | 1 - .../{LifeCycle => State}/SerializableDataExtension.cs | 2 +- TLM/TLM/TLM.csproj | 10 +++++----- TLM/TLM/{LifeCycle => }/ThreadingExtension.cs | 2 +- TLM/TLM/{LifeCycle => }/TrafficManagerMod.cs | 2 +- TLM/TLM/UI/Helpers/GuideHandler.cs | 1 - TLM/TLM/UI/IncompatibleModsPanel.cs | 1 - TLM/TLM/UI/Localization/Translation.cs | 1 - TLM/TLM/UI/MainMenu/DebugMenu.cs | 1 - TLM/TLM/UI/MainMenu/MainMenuWindow.cs | 1 - TLM/TLM/UI/ModUI.cs | 1 - 16 files changed, 10 insertions(+), 21 deletions(-) rename TLM/TLM/{LifeCycle => }/LoadingExtension.cs (99%) rename TLM/TLM/{LifeCycle => }/Patcher.cs (99%) rename TLM/TLM/{LifeCycle => State}/SerializableDataExtension.cs (99%) rename TLM/TLM/{LifeCycle => }/ThreadingExtension.cs (98%) rename TLM/TLM/{LifeCycle => }/TrafficManagerMod.cs (99%) diff --git a/TLM/TLM/LifeCycle/LoadingExtension.cs b/TLM/TLM/LoadingExtension.cs similarity index 99% rename from TLM/TLM/LifeCycle/LoadingExtension.cs rename to TLM/TLM/LoadingExtension.cs index 5b3bc3ad4..e4a2af7f7 100644 --- a/TLM/TLM/LifeCycle/LoadingExtension.cs +++ b/TLM/TLM/LoadingExtension.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.LifeCycle { +namespace TrafficManager { using ColossalFramework; using ColossalFramework.UI; using CSUtil.Commons; diff --git a/TLM/TLM/Manager/Impl/OptionsManager.cs b/TLM/TLM/Manager/Impl/OptionsManager.cs index c155052f0..76bbbbb1a 100644 --- a/TLM/TLM/Manager/Impl/OptionsManager.cs +++ b/TLM/TLM/Manager/Impl/OptionsManager.cs @@ -5,7 +5,6 @@ namespace TrafficManager.Manager.Impl { using TrafficManager.API.Traffic.Enums; using TrafficManager.State; using TrafficManager.UI.Helpers; - using TrafficManager.LifeCycle; public class OptionsManager : AbstractCustomManager, diff --git a/TLM/TLM/Manager/Impl/UtilityManager.cs b/TLM/TLM/Manager/Impl/UtilityManager.cs index 7d11e9514..bb5102e62 100644 --- a/TLM/TLM/Manager/Impl/UtilityManager.cs +++ b/TLM/TLM/Manager/Impl/UtilityManager.cs @@ -7,8 +7,6 @@ namespace TrafficManager.Manager.Impl { using TrafficManager.API.Manager; using TrafficManager.State; using UnityEngine; - using TrafficManager.LifeCycle; - public class UtilityManager : AbstractCustomManager, IUtilityManager { public static UtilityManager Instance { get; } diff --git a/TLM/TLM/LifeCycle/Patcher.cs b/TLM/TLM/Patcher.cs similarity index 99% rename from TLM/TLM/LifeCycle/Patcher.cs rename to TLM/TLM/Patcher.cs index 6c7dda195..6fdda9df2 100644 --- a/TLM/TLM/LifeCycle/Patcher.cs +++ b/TLM/TLM/Patcher.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.LifeCycle { +namespace TrafficManager { using ColossalFramework.UI; using ColossalFramework; using CSUtil.Commons; diff --git a/TLM/TLM/State/GlobalConfig.cs b/TLM/TLM/State/GlobalConfig.cs index ca9c4834e..bf99e7a77 100644 --- a/TLM/TLM/State/GlobalConfig.cs +++ b/TLM/TLM/State/GlobalConfig.cs @@ -6,7 +6,6 @@ namespace TrafficManager.State { using System; using TrafficManager.State.ConfigData; using TrafficManager.Util; - using TrafficManager.LifeCycle; [XmlRootAttribute("GlobalConfig", Namespace = "http://www.viathinksoft.de/tmpe", IsNullable = false)] public class GlobalConfig : GenericObservable { diff --git a/TLM/TLM/State/Options.cs b/TLM/TLM/State/Options.cs index 18ab23416..4562fe995 100644 --- a/TLM/TLM/State/Options.cs +++ b/TLM/TLM/State/Options.cs @@ -8,7 +8,6 @@ namespace TrafficManager.State { using TrafficManager.UI.Helpers; using TrafficManager.UI; using UnityEngine; - using TrafficManager.LifeCycle; public class Options : MonoBehaviour { #if DEBUG diff --git a/TLM/TLM/LifeCycle/SerializableDataExtension.cs b/TLM/TLM/State/SerializableDataExtension.cs similarity index 99% rename from TLM/TLM/LifeCycle/SerializableDataExtension.cs rename to TLM/TLM/State/SerializableDataExtension.cs index 72cfa0de9..0b5fb7fe5 100644 --- a/TLM/TLM/LifeCycle/SerializableDataExtension.cs +++ b/TLM/TLM/State/SerializableDataExtension.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.LifeCycle { +namespace TrafficManager { using CSUtil.Commons; using ICities; using JetBrains.Annotations; diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 7f0f7027d..e4728c5e3 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -150,7 +150,7 @@ - + @@ -248,7 +248,7 @@ - + @@ -335,8 +335,8 @@ - - + + @@ -345,7 +345,7 @@ - + diff --git a/TLM/TLM/LifeCycle/ThreadingExtension.cs b/TLM/TLM/ThreadingExtension.cs similarity index 98% rename from TLM/TLM/LifeCycle/ThreadingExtension.cs rename to TLM/TLM/ThreadingExtension.cs index 86019487c..cc9e8d97f 100644 --- a/TLM/TLM/LifeCycle/ThreadingExtension.cs +++ b/TLM/TLM/ThreadingExtension.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.LifeCycle { +namespace TrafficManager { using ColossalFramework.UI; using ColossalFramework; using CSUtil.Commons.Benchmark; diff --git a/TLM/TLM/LifeCycle/TrafficManagerMod.cs b/TLM/TLM/TrafficManagerMod.cs similarity index 99% rename from TLM/TLM/LifeCycle/TrafficManagerMod.cs rename to TLM/TLM/TrafficManagerMod.cs index 9a0db0979..99f35f958 100644 --- a/TLM/TLM/LifeCycle/TrafficManagerMod.cs +++ b/TLM/TLM/TrafficManagerMod.cs @@ -1,4 +1,4 @@ -namespace TrafficManager.LifeCycle { +namespace TrafficManager { using ColossalFramework.Globalization; using ColossalFramework.UI; using CSUtil.Commons; diff --git a/TLM/TLM/UI/Helpers/GuideHandler.cs b/TLM/TLM/UI/Helpers/GuideHandler.cs index 711c20366..76aaad576 100644 --- a/TLM/TLM/UI/Helpers/GuideHandler.cs +++ b/TLM/TLM/UI/Helpers/GuideHandler.cs @@ -2,7 +2,6 @@ namespace TrafficManager.UI.Helpers { using ColossalFramework; using CSUtil.Commons; using System.Collections.Generic; - using TrafficManager.LifeCycle; public class GuideHandler { private Dictionary GuideTable = new Dictionary(); diff --git a/TLM/TLM/UI/IncompatibleModsPanel.cs b/TLM/TLM/UI/IncompatibleModsPanel.cs index 1b0f6b43d..96e093728 100644 --- a/TLM/TLM/UI/IncompatibleModsPanel.cs +++ b/TLM/TLM/UI/IncompatibleModsPanel.cs @@ -9,7 +9,6 @@ namespace TrafficManager.UI { using System; using TrafficManager.State; using UnityEngine; - using LifeCycle; public class IncompatibleModsPanel : UIPanel { private const ulong LOCAL_MOD = ulong.MaxValue; diff --git a/TLM/TLM/UI/Localization/Translation.cs b/TLM/TLM/UI/Localization/Translation.cs index ef1dece6e..ba72f8c9a 100644 --- a/TLM/TLM/UI/Localization/Translation.cs +++ b/TLM/TLM/UI/Localization/Translation.cs @@ -7,7 +7,6 @@ namespace TrafficManager.UI { using ColossalFramework.Globalization; using CSUtil.Commons; using TrafficManager.State; - using TrafficManager.LifeCycle; /// /// Adding a new language step by step: diff --git a/TLM/TLM/UI/MainMenu/DebugMenu.cs b/TLM/TLM/UI/MainMenu/DebugMenu.cs index c60c70836..9b07d6382 100644 --- a/TLM/TLM/UI/MainMenu/DebugMenu.cs +++ b/TLM/TLM/UI/MainMenu/DebugMenu.cs @@ -12,7 +12,6 @@ namespace TrafficManager.UI.MainMenu { using global::TrafficManager.State; using JetBrains.Annotations; using UnityEngine; - using TrafficManager.LifeCycle; #if DEBUG // whole class coverage public class DebugMenuPanel : UIPanel diff --git a/TLM/TLM/UI/MainMenu/MainMenuWindow.cs b/TLM/TLM/UI/MainMenu/MainMenuWindow.cs index 83707445d..29df961ba 100644 --- a/TLM/TLM/UI/MainMenu/MainMenuWindow.cs +++ b/TLM/TLM/UI/MainMenu/MainMenuWindow.cs @@ -16,7 +16,6 @@ namespace TrafficManager.UI.MainMenu { using TrafficManager.U.Panel; using TrafficManager.UI.MainMenu.OSD; using UnityEngine; - using TrafficManager.LifeCycle; public class MainMenuWindow : U.Panel.BaseUWindowPanel, diff --git a/TLM/TLM/UI/ModUI.cs b/TLM/TLM/UI/ModUI.cs index 1d11a76e3..20ad58d46 100644 --- a/TLM/TLM/UI/ModUI.cs +++ b/TLM/TLM/UI/ModUI.cs @@ -5,7 +5,6 @@ namespace TrafficManager.UI { using TrafficManager.UI.MainMenu; using TrafficManager.Util; using UnityEngine; - using TrafficManager.LifeCycle; /// /// Globally available UI manager class which contains the main menu button and the panel. From f8d2cc037f218f8a353d76f71790946fff5bc888 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Mon, 18 May 2020 21:26:50 +0300 Subject: [PATCH 09/13] --- TLM/TLM/Patcher.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/TLM/TLM/Patcher.cs b/TLM/TLM/Patcher.cs index 6fdda9df2..7b344ca69 100644 --- a/TLM/TLM/Patcher.cs +++ b/TLM/TLM/Patcher.cs @@ -1,14 +1,12 @@ namespace TrafficManager { - using ColossalFramework.UI; using ColossalFramework; + using ColossalFramework.UI; using CSUtil.Commons; using HarmonyLib; + using System; using System.Collections.Generic; using System.Reflection; - using System; - using TrafficManager.RedirectionFramework; - using TrafficManager.Util; public class Patcher { From 331cccf782878f33ce665badbe26030f86a0a729 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Mon, 18 May 2020 21:36:15 +0300 Subject: [PATCH 10/13] --- TLM/TLM/TLM.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index e4728c5e3..396a7b332 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -1,4 +1,4 @@ - + @@ -161,8 +161,6 @@ - - @@ -214,6 +212,8 @@ + + From 187da37026e97966bc0ba915939eff679a6b9973 Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Mon, 18 May 2020 22:10:05 +0300 Subject: [PATCH 11/13] merged with trunk --- TLM/TLM/Patch/_NetSegment/CalculateSegmentPatch.cs | 3 +-- TLM/TLM/State/SerializableDataExtension.cs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/TLM/TLM/Patch/_NetSegment/CalculateSegmentPatch.cs b/TLM/TLM/Patch/_NetSegment/CalculateSegmentPatch.cs index 5190c3bc6..6dbe45d1a 100644 --- a/TLM/TLM/Patch/_NetSegment/CalculateSegmentPatch.cs +++ b/TLM/TLM/Patch/_NetSegment/CalculateSegmentPatch.cs @@ -1,6 +1,5 @@ namespace TrafficManager.Patch._NetSegment { - using Harmony; - using JetBrains; + using HarmonyLib; using JetBrains.Annotations; using TrafficManager.Manager.Impl; diff --git a/TLM/TLM/State/SerializableDataExtension.cs b/TLM/TLM/State/SerializableDataExtension.cs index 0b5fb7fe5..d8747dce7 100644 --- a/TLM/TLM/State/SerializableDataExtension.cs +++ b/TLM/TLM/State/SerializableDataExtension.cs @@ -1,4 +1,4 @@ -namespace TrafficManager { +namespace TrafficManager.State { using CSUtil.Commons; using ICities; using JetBrains.Annotations; @@ -8,7 +8,6 @@ namespace TrafficManager { using System; using TrafficManager.API.Manager; using TrafficManager.Manager.Impl; - using State; [UsedImplicitly] public class SerializableDataExtension From 8df57a05735fb9509982bf2e005774a5bc58bf2c Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Tue, 19 May 2020 09:03:56 +0300 Subject: [PATCH 12/13] added harmony_Id to unpatchall(). removed redundant code. --- TLM/TLM/Patcher.cs | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/TLM/TLM/Patcher.cs b/TLM/TLM/Patcher.cs index 7b344ca69..4de0282f0 100644 --- a/TLM/TLM/Patcher.cs +++ b/TLM/TLM/Patcher.cs @@ -11,32 +11,14 @@ namespace TrafficManager { public class Patcher { public static Patcher Instance { get; private set; } + public static Patcher Create() => Instance = new Patcher(); private const string HARMONY_ID = "de.viathinksoft.tmpe"; - public class Detour { - public MethodInfo OriginalMethod; - public MethodInfo CustomMethod; - public RedirectCallsState Redirect; - - public Detour(MethodInfo originalMethod, MethodInfo customMethod) { - OriginalMethod = originalMethod; - CustomMethod = customMethod; - Redirect = RedirectionHelper.RedirectCalls(originalMethod, customMethod); - } - } - - public bool initialized_ { get; private set; } = false; - - /// - /// Method redirection states for attribute-driven detours - /// - public IDictionary DetouredMethodStates { get; private set; } = - new Dictionary(); + private bool initialized_ = false; public void Install() { - // TODO realize detouring with annotations if (initialized_) { return; } @@ -65,7 +47,7 @@ public void Install() { try { Log.Info("Deploying attribute-driven detours"); - DetouredMethodStates = AssemblyRedirector.Deploy(); + AssemblyRedirector.Deploy(); } catch (Exception e) { Log.Error("Could not deploy attribute-driven detours"); @@ -101,7 +83,7 @@ public void Uninstall() { var harmony = new Harmony(HARMONY_ID); Shortcuts.Assert(harmony != null, "HarmonyInst!=null"); - harmony.UnpatchAll(); + harmony.UnpatchAll(HARMONY_ID); initialized_ = false; Log.Info("Reverting detours finished."); From c331055ceecfd4bddd48820b1e3705dfb833c4da Mon Sep 17 00:00:00 2001 From: "kian.zarrin" Date: Tue, 19 May 2020 19:21:52 +0300 Subject: [PATCH 13/13] pr fix --- TLM/TLM/Patcher.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TLM/TLM/Patcher.cs b/TLM/TLM/Patcher.cs index 4de0282f0..d75f693d3 100644 --- a/TLM/TLM/Patcher.cs +++ b/TLM/TLM/Patcher.cs @@ -42,7 +42,6 @@ public void Install() { Log.Info(e.Message); Log.Info(e.StackTrace); fail = true; - throw e; } try {