Skip to content

Commit

Permalink
Merge pull request #30 from andreharv/BetterEducationToolbarFix
Browse files Browse the repository at this point in the history
Better education toolbar fix
  • Loading branch information
andreharv authored Sep 29, 2022
2 parents 71a11b2 + cb43c11 commit bdb05c2
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 118 deletions.
2 changes: 1 addition & 1 deletion NetworkExtensions2/NetworkExtensions2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
<Compile Include="Patching\AI\GetLengthSnapPatch.cs" />
<Compile Include="Patching\Patcher.cs" />
<Compile Include="Patching\UI\GetCategoryOrderPatch.cs" />
<Compile Include="Patching\UI\SpawnButtonEntryPatch.cs" />
<Compile Include="Patching\UI\CreateGroupItemPatch.cs" />
<Compile Include="Props\MediumAvenueSideLightBuilder.cs" />
<Compile Include="Props\LargeAvenueMedianLightBuilder.cs" />
<Compile Include="Redirection\RedirectionHelper.cs" />
Expand Down
18 changes: 3 additions & 15 deletions NetworkExtensions2/Patching/AI/CheckBuildPositionPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@

namespace NetworkExtensions2.Patching
{
[HarmonyPatch(typeof(NetAI))]
[HarmonyPatch(nameof(NetAI.CheckBuildPosition))]
[HarmonyPatch(typeof(NetAI), nameof(NetAI.CheckBuildPosition))]
public class CheckBuildPositionPatch
{
public static void Apply(Harmony harmony)
{
var postfix = typeof(CheckBuildPositionPatch).GetMethod(nameof(CheckBuildPositionPatch.Postfix), BindingFlags.NonPublic | BindingFlags.Static);
harmony.Patch(OriginalMethod, null, new HarmonyMethod(postfix));
}
public static void Revert(Harmony harmony)
{
harmony.Unpatch(OriginalMethod, HarmonyPatchType.Postfix);
}

static MethodInfo OriginalMethod => typeof(NetAI).GetMethod("CheckBuildPosition");

static void Postfix(ref ToolBase.ToolErrors __result)
[HarmonyPostfix]
public static void Postfix(ref ToolBase.ToolErrors __result)
{
if ((__result & ToolBase.ToolErrors.CannotUpgrade) == ToolBase.ToolErrors.CannotUpgrade)
{
Expand Down
22 changes: 5 additions & 17 deletions NetworkExtensions2/Patching/AI/CreateZoneBlocksPatch.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
using HarmonyLib;
using NetworkExtensions;
using System;
using System.Reflection;
using Transit.Framework.ExtensionPoints.AI;
using UnityEngine;

namespace NetworkExtensions2.Patching
{
[HarmonyPatch(typeof(RoadAI))]
[HarmonyPatch(nameof(RoadAI.CreateZoneBlocks))]
[HarmonyPatch(typeof(RoadAI), nameof(RoadAI.CreateZoneBlocks))]
internal static class CreateZoneBlocksPatch
{
public static void Apply(Harmony harmony)
{
var prefix = typeof(CreateZoneBlocksPatch).GetMethod(nameof(CreateZoneBlocksPatch.Prefix), BindingFlags.NonPublic | BindingFlags.Static);
harmony.Patch(OriginalMethod, new HarmonyMethod(prefix));
}
public static void Revert(Harmony harmony)
{
harmony.Unpatch(OriginalMethod, HarmonyPatchType.Prefix);
}

static MethodInfo OriginalMethod => typeof(RoadAI).GetMethod("CreateZoneBlocks");

private const float MIN_HALFWIDTH_DEFAULT = 8f;

static bool Prefix(RoadAI __instance, ushort segment, ref NetSegment data)
[HarmonyPrefix]
public static bool Prefix(RoadAI __instance, ushort segment, ref NetSegment data)
{
try
{
if (RoadZoneBlocksCreationManager.HasCustomCreator(__instance.m_info.name))
if (!Mod.FoundZoningAdjuster && RoadZoneBlocksCreationManager.HasCustomCreator(__instance.m_info.name))
{
RoadZoneBlocksCreationManager
.GetCustomCreator(__instance.m_info.name)
Expand Down
17 changes: 3 additions & 14 deletions NetworkExtensions2/Patching/AI/GetLengthSnapPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@

namespace NetworkExtensions2.Patching
{
[HarmonyPatch(typeof(NetAI))]
[HarmonyPatch(nameof(NetAI.GetLengthSnap))]
[HarmonyPatch(typeof(NetAI), nameof(NetAI.GetLengthSnap))]
internal static class GetLengthSnapPatch
{
public static void Apply(Harmony harmony)
{
var prefix = typeof(GetLengthSnapPatch).GetMethod(nameof(GetLengthSnapPatch.Prefix), BindingFlags.NonPublic | BindingFlags.Static);
harmony.Patch(OriginalMethod, new HarmonyMethod(prefix));
}
public static void Revert(Harmony harmony)
{
harmony.Unpatch(OriginalMethod, HarmonyPatchType.Prefix);
}

static MethodInfo OriginalMethod => typeof(NetAI).GetMethod(nameof(NetAI.GetLengthSnap));
static bool Prefix(NetAI __instance, ref float __result)
[HarmonyPrefix]
public static bool Prefix(NetAI __instance, ref float __result)
{
if (!(__instance is RoadAI))
{
Expand Down
39 changes: 28 additions & 11 deletions NetworkExtensions2/Patching/Patcher.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
using HarmonyLib;
using NetworkExtensions;
using System.Reflection;
using HarmonyLib;

namespace NetworkExtensions2.Patching
{
internal class Patcher
public static class Patcher
{
private static Harmony harmony => new Harmony("andreharv.CSL.NetworkExtensions2");
internal static void PatchAll()
private const string HarmonyId = "boformer.Harmony2Example";

private static bool patched = false;

public static void PatchAll()
{
CheckBuildPositionPatch.Apply(harmony);
GetLengthSnapPatch.Apply(harmony);
GetCategoryOrderPatch.Apply(harmony);
SpawnButtonEntryPatch.Apply(harmony);
if (patched) return;

UnityEngine.Debug.Log("Harmony 2 NExt2: Patching...");

patched = true;

// Apply your patches here!
// Harmony.DEBUG = true;
var harmony = new Harmony("andreharv.CSL.NetworkExtensions2");
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
internal static void UnpatchAll()

public static void UnpatchAll()
{
harmony.UnpatchAll();
if (!patched) return;

var harmony = new Harmony(HarmonyId);
harmony.UnpatchAll(HarmonyId);

patched = false;

UnityEngine.Debug.Log("Harmony 2 NExt2: Reverted...");
}
}
}
29 changes: 29 additions & 0 deletions NetworkExtensions2/Patching/UI/CreateGroupItemPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using ColossalFramework;
using ColossalFramework.UI;
using HarmonyLib;
using System;
using System.Reflection;
using Transit.Framework;
using UnityEngine;

namespace NetworkExtensions2.Patching
{
[HarmonyPatch(typeof(GeneratedGroupPanel), "CreateGroupItem")]
internal class CreateGroupItemPatch
{
private static MethodInfo m_spawnButtonEntry = typeof(GeneratedGroupPanel).GetMethod("SpawnButtonEntry", BindingFlags.Instance | BindingFlags.NonPublic, null , new Type[] { typeof(UITabstrip), typeof(string), typeof(string), typeof(bool), typeof(string), typeof(string), typeof(string), typeof(bool), typeof(bool) }, null);
[HarmonyPrefix]
public static bool Prefix(GeneratedGroupPanel __instance, GeneratedGroupPanel.GroupInfo info, string localeID, UITabstrip ___m_Strip)
{
var argName = info.subPanelType.IsNullOrWhiteSpace() ? __instance.service.Name() : info.subPanelType;
var category = info.name;
var button = (UIButton)m_spawnButtonEntry.Invoke(__instance, new object[] { ___m_Strip, argName, category, false, localeID, info.unlockText, "SubBar", info.isUnlocked, false });
var customAtlas = AtlasManager.instance.GetAtlas(category);
if (customAtlas != null)
{
button.atlas = customAtlas;
}
return false;
}
}
}
17 changes: 3 additions & 14 deletions NetworkExtensions2/Patching/UI/GetCategoryOrderPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@

namespace NetworkExtensions2.Patching
{
[HarmonyPatch(typeof(RoadsGroupPanel))]
[HarmonyPatch("GetCategoryOrder")]
[HarmonyPatch(typeof(RoadsGroupPanel), "GetCategoryOrder")]
internal static class GetCategoryOrderPatch
{
public static void Apply(Harmony harmony)
{
var prefix = typeof(GetCategoryOrderPatch).GetMethod(nameof(GetCategoryOrderPatch.Prefix), BindingFlags.NonPublic | BindingFlags.Static);
harmony.Patch(OriginalMethod, new HarmonyMethod(prefix));
}
public static void Revert(Harmony harmony)
{
harmony.Unpatch(OriginalMethod, HarmonyPatchType.Prefix);
}

static MethodInfo OriginalMethod => typeof(RoadsGroupPanel).GetMethod("GetCategoryOrder", BindingFlags.NonPublic | BindingFlags.Instance);
static bool Prefix(ref int __result, string name)
[HarmonyPrefix]
public static bool Prefix(ref int __result, string name)
{
int? order = RoadCategoryOrderManager.GetOrder(name);

Expand Down
35 changes: 0 additions & 35 deletions NetworkExtensions2/Patching/UI/SpawnButtonEntryPatch.cs

This file was deleted.

6 changes: 0 additions & 6 deletions NetworkExtensions2/RExModule.Install.Roads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Transit.Addon.RoadExtensions.Compatibility;
using Transit.Framework;
using Transit.Framework.Builders;
using Transit.Framework.Modularity;
using UnityEngine;
using System.Diagnostics;

#if DEBUG
using Debug = Transit.Framework.Debug;
#else
using Debug = UnityEngine.Debug;
#endif

namespace Transit.Addon.RoadExtensions
{
Expand Down
5 changes: 0 additions & 5 deletions NetworkExtensions2/ToolModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public override string Name
}

// Hack For FileManager, deprecated
public override void OnCreated(ILoading loading)
{
if (!Mod.FoundZoningAdjuster)
CreateZoneBlocksPatch.Apply(harmony);
}
public override void OnReleased()
{
harmony.UnpatchAll();
Expand Down

0 comments on commit bdb05c2

Please sign in to comment.