Skip to content

Commit

Permalink
Rewrite to use Harmony
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardd committed Mar 8, 2020
1 parent 9d9d078 commit 4e5d20e
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 833 deletions.
28 changes: 28 additions & 0 deletions Crossings/CalculateNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HarmonyLib;
using UnityEngine;

namespace Crossings
{
[HarmonyPatch(typeof(NetNode))]
[HarmonyPatch(nameof(NetNode.CalculateNode))]
class CalculateNode
{
static void Postfix(ref NetNode __instance)
{
if ((__instance.m_flags & NetNode.Flags.Outside) != NetNode.Flags.None)
{
// Do nothing
}
else if ((__instance.m_flags & NetNode.Flags.Junction) != NetNode.Flags.None)
{
// This is already a junction - clear out any crossing flag we might have set
__instance.m_flags &= ~(NetNode.Flags)Crossings.CrossingFlag;
}
else if ((__instance.m_flags & (NetNode.Flags)Crossings.CrossingFlag) != NetNode.Flags.None)
{
__instance.m_flags |= NetNode.Flags.Junction;
__instance.m_flags &= ~(NetNode.Flags.Moveable | NetNode.Flags.Middle | NetNode.Flags.AsymForward | NetNode.Flags.AsymBackward | NetNode.Flags.Bend | NetNode.Flags.End);
}
}
}
}
15 changes: 8 additions & 7 deletions Crossings/CrossingTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ private void GetNetAtCursor()
if (RayCast(input, out output))
{
//Debug.Log("[Crossings] Found segment " + output.m_netSegment);
//Debug.Log("[Crossings] Found node " + output.m_netNode);
// Debug.Log("[Crossings] Found node " + output.m_netNode);
// Debug.Log("[Crossings] Found segment " + output.m_netSegment);
m_currentNodeID = output.m_netNode;
m_currentSegmentID = output.m_netSegment;
}
Expand Down Expand Up @@ -217,11 +218,11 @@ private IEnumerator CreateCrossing()
yield return null;
if (newNode == 0) {
NetTool.CreateNode (m_prefab, m_controlPoint, m_controlPoint, m_controlPoint, NetTool.m_nodePositionsSimulation, 0, false, false, true, false, false, false, 0, out newNode, out newSegment, out cost, out productionRate);
NetManager.instance.m_nodes.m_buffer [newNode].m_flags |= (NetNode.Flags)CrossingsNode.CrossingFlag;
NetManager.instance.m_nodes.m_buffer [newNode].m_flags |= (NetNode.Flags)Crossings.CrossingFlag;
// Debug.Log ("[Crossings] CreateNode real result: " + errors + " " + newNode + " " + newSegment + " " + cost + " " + productionRate);
} else {
NetManager.instance.m_nodes.m_buffer [newNode].m_flags |= (NetNode.Flags)CrossingsNode.CrossingFlag;
NetManager.instance.UpdateNode (newNode, 0, 0);
NetManager.instance.m_nodes.m_buffer [newNode].m_flags |= (NetNode.Flags)Crossings.CrossingFlag;
NetManager.instance.UpdateNode (newNode);
// Debug.Log ("[Crossings] Existing Node: " + newNode + " " + NetManager.instance.m_nodes.m_buffer [newNode].m_flags);
}
}
Expand All @@ -233,8 +234,8 @@ private IEnumerator CreateCrossing()
private IEnumerator RemoveCrossing()
{
if (m_currentNodeID != 0) {
NetManager.instance.m_nodes.m_buffer [m_currentNodeID].m_flags &= ~(NetNode.Flags)CrossingsNode.CrossingFlag;
NetManager.instance.UpdateNode (m_currentNodeID, 0, 0);
NetManager.instance.m_nodes.m_buffer [m_currentNodeID].m_flags &= ~(NetNode.Flags)Crossings.CrossingFlag;
NetManager.instance.UpdateNode(m_currentNodeID);
m_currentNodeID = 0;
}
yield return null;
Expand Down Expand Up @@ -275,7 +276,7 @@ private bool CanBuild(ushort segmentID, ushort nodeID)
return true; // No node means we can create one

NetNode.Flags flags = NetManager.instance.m_nodes.m_buffer [nodeID].m_flags;
if ((flags & (NetNode.Flags)CrossingsNode.CrossingFlag) != NetNode.Flags.None)
if ((flags & (NetNode.Flags)Crossings.CrossingFlag) != NetNode.Flags.None)
return true; // Already a crossing - can remove it

// Can't add crossing to the end, a junction (except a crossing one) or a bend
Expand Down
9 changes: 8 additions & 1 deletion Crossings/Crossings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using ICities;
using ColossalFramework.UI;
using HarmonyLib;
using UnityEngine;
using System;
using System.Reflection;


namespace Crossings {
public class Crossings {
public static NetNode.Flags CrossingFlag = NetNode.Flags.Sewage; // Hopefully this won't break anything *fingers crossed*
}


public class CrossingsInfo : IUserMod {
public string Name {
get { return "Pedestrian Crossings"; }
Expand All @@ -28,7 +34,8 @@ public override void OnCreated(ILoading loading)
// Debug.Log ("[Crossings] OnCreated()");
base.OnCreated (loading);

CrossingsNode.Hook ();
var harmony = new Harmony("org.guarana.citiesskylines.mod.crossings");
harmony.PatchAll();

ui.selectedToolModeChanged += (bool enabled) => {
SetToolEnabled(enabled);
Expand Down
15 changes: 12 additions & 3 deletions Crossings/Crossings.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -38,6 +38,9 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony, Version=2.0.0.8, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Lib.Harmony.2.0.0.8\lib\net35\0Harmony.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="ColossalManaged">
<HintPath>E:\Program Files %28x86%29\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\ColossalManaged.dll</HintPath>
Expand All @@ -57,8 +60,9 @@
<Compile Include="Crossings.cs" />
<Compile Include="CrossingsUIToggle.cs" />
<Compile Include="CrossingTool.cs" />
<Compile Include="Detours.cs" />
<Compile Include="CrossingsNode.cs" />
<Compile Include="CalculateNode.cs" />
<Compile Include="RefreshJunctionData.cs" />
<Compile Include="UpdateNodeFlags.cs" />
<Compile Include="UIUtils.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Expand All @@ -72,5 +76,10 @@
<None Include="..\README.md">
<Link>README.md</Link>
</None>
<None Include="packages.config" />
</ItemGroup>
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
6 changes: 6 additions & 0 deletions Crossings/Crossings.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ReferencePath>C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\</ReferencePath>
</PropertyGroup>
</Project>
Loading

0 comments on commit 4e5d20e

Please sign in to comment.