Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Harmony 2.x #866

Merged
merged 14 commits into from
May 24, 2020
359 changes: 71 additions & 288 deletions TLM/TLM/LoadingExtension.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion TLM/TLM/Manager/Impl/OptionsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TrafficManager.Manager.Impl {
namespace TrafficManager.Manager.Impl {
using CSUtil.Commons;
using System;
using TrafficManager.API.Manager;
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Manager/Impl/UtilityManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TrafficManager.Manager.Impl {
namespace TrafficManager.Manager.Impl {
using ColossalFramework;
using CSUtil.Commons;
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._CitizenManager {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;

[HarmonyPatch(typeof(CitizenManager), "ReleaseCitizenInstance")]
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_CitizenManager/ReleaseCitizenPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._CitizenManager {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;

[HarmonyPatch(typeof(CitizenManager), "ReleaseCitizen")]
Expand Down
23 changes: 19 additions & 4 deletions TLM/TLM/Patch/_CommonBuildingAI/SimulationStepPatch.cs
Original file line number Diff line number Diff line change
@@ -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 {
/// <summary>
/// Decreases parking space and public transport demand before each simulation step if the Parking AI is active.
/// </summary>
[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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_DefaultTool/OnToolGUIPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._DefaultTool {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.UI;
using UnityEngine;
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_DefaultTool/RenderOverlayPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._DefaultTool {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.UI;

Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_HumanAI/ArriveAtDestinationPatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TrafficManager.Patch._HumanAI {
using ColossalFramework;
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.State;

Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_InfoManager/SetModePatch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace TrafficManager.Patch._InfoManager
{
using ColossalFramework;
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.Util;
using TrafficManager.UI;
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_NetManager/FinalizeSegmentPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._NetManager {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;

[HarmonyPatch(typeof(NetManager), "FinalizeSegment")]
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_NetManager/UpdateSegmentPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._NetManager {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;

[HarmonyPatch(
Expand Down
3 changes: 1 addition & 2 deletions TLM/TLM/Patch/_NetSegment/CalculateSegmentPatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace TrafficManager.Patch._NetSegment {
using Harmony;
using JetBrains;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.Manager.Impl;

Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_RoadBaseAI/GetTrafficLightNodeStatePatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TrafficManager.Patch._RoadBaseAI {
using ColossalFramework;
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.State;
using UnityEngine;
Expand Down
21 changes: 16 additions & 5 deletions TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_RoadBaseAI/SetTrafficLightStatePatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._RoadBaseAI {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.State;
using static RoadBaseAI;
Expand Down
5 changes: 3 additions & 2 deletions TLM/TLM/Patch/_RoadBaseAI/TrafficLightSimulationStepPatch.cs
Original file line number Diff line number Diff line change
@@ -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 {
/// <summary>
/// Decides whether the stock simulation step for traffic lights should run.
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_RoadBaseAI/UpdateLanesPatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TrafficManager.Patch._RoadBaseAI {
using ColossalFramework;
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.State;

Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_RoadBaseAI/UpdateNodePatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TrafficManager.Patch._RoadBaseAI {
using ColossalFramework;
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;
using TrafficManager.Manager.Impl;
using TrafficManager.State;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace TrafficManager.Patch._RoadWorldInfoPanel
{
using ColossalFramework;
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;

[HarmonyPatch(typeof(RoadWorldInfoPanel), "OnAdjustRoadButton")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
/// <summary>
/// Decides whether the stock simulation step for traffic lights should run.
Expand Down
6 changes: 3 additions & 3 deletions TLM/TLM/Patch/_Vehicle/SpawnPatch.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using ColossalFramework;
using ColossalFramework;
using ColossalFramework.Math;
using CSUtil.Commons;
using Harmony;
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
// TODO [issue #864] replace custom AI with harmony patch when possible.

//[HarmonyPatch(typeof(Vehicle), "Spawn")]
//public static class SpawnPatch {
Expand Down
6 changes: 3 additions & 3 deletions TLM/TLM/Patch/_Vehicle/UnspawnPatch.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using ColossalFramework;
using ColossalFramework;
using ColossalFramework.Math;
using CSUtil.Commons;
using Harmony;
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
// TODO [issue #864] replace custom AI with harmony patch when possible.

//[HarmonyPatch(typeof(Vehicle), "Unspawn")]
//public static class UnspawnPatch {
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_VehicleManager/CreateVehiclePatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._VehicleManager {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;

[HarmonyPatch(typeof(VehicleManager), "CreateVehicle")]
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Patch/_VehicleManager/ReleaseVehiclePatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TrafficManager.Patch._VehicleManager {
using Harmony;
using HarmonyLib;
using JetBrains.Annotations;

[HarmonyPatch(typeof(VehicleManager), "ReleaseVehicle")]
Expand Down
92 changes: 92 additions & 0 deletions TLM/TLM/Patcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace TrafficManager {
using ColossalFramework;
using ColossalFramework.UI;
using CSUtil.Commons;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection;
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";
kianzarrin marked this conversation as resolved.
Show resolved Hide resolved

private bool initialized_ = false;

public void Install() {
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;
}

try {
Log.Info("Deploying attribute-driven detours");
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<SimulationManager>.instance.m_ThreadingWrapper.QueueMainThread(
() => {
UIView.library
.ShowModal<ExceptionPanel>("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(HARMONY_ID);

initialized_ = false;
Log.Info("Reverting detours finished.");
}

}
}
2 changes: 1 addition & 1 deletion TLM/TLM/RedirectionFramework
2 changes: 1 addition & 1 deletion TLM/TLM/State/GlobalConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TrafficManager.State {
namespace TrafficManager.State {
using CSUtil.Commons;
using JetBrains.Annotations;
using System.IO;
Expand Down
Loading