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

Sync building variations and road adjustments #327

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/basegame/CSM.BaseGame.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="Commands\Data\Buildings\BuildingSetHistoricalCommand.cs" />
<Compile Include="Commands\Data\Buildings\BuildingSetTollPriceCommand.cs" />
<Compile Include="Commands\Data\Buildings\BuildingSetTransferReasonCommand.cs" />
<Compile Include="Commands\Data\Buildings\BuildingSetVariationCommand.cs" />
<Compile Include="Commands\Data\Buildings\BuildingToolCreateCommand.cs" />
<Compile Include="Commands\Data\Buildings\BuildingUpgradeCommand.cs" />
<Compile Include="Commands\Data\Buildings\ServiceBuildingChangeVehicleCommand.cs" />
Expand Down Expand Up @@ -118,6 +119,7 @@
<Compile Include="Commands\Data\Props\PropCreateCommand.cs" />
<Compile Include="Commands\Data\Props\PropMoveCommand.cs" />
<Compile Include="Commands\Data\Props\PropReleaseCommand.cs" />
<Compile Include="Commands\Data\Roads\RoadAdjustCommand.cs" />
<Compile Include="Commands\Data\Roads\RoadSetPriorityCommand.cs" />
<Compile Include="Commands\Data\Roads\RoadSettingsCommand.cs" />
<Compile Include="Commands\Data\Terrain\SoilTradeCommand.cs" />
Expand Down Expand Up @@ -156,6 +158,7 @@
<Compile Include="Commands\Handler\Buildings\BuildingSetHistoricalHandler.cs" />
<Compile Include="Commands\Handler\Buildings\BuildingSetTollPriceHandler.cs" />
<Compile Include="Commands\Handler\Buildings\BuildingSetTransferReasonHandler.cs" />
<Compile Include="Commands\Handler\Buildings\BuildingSetVariationHandler.cs" />
<Compile Include="Commands\Handler\Buildings\BuildingToolCreateHandler.cs" />
<Compile Include="Commands\Handler\Buildings\BuildingUpgradeHandler.cs" />
<Compile Include="Commands\Handler\Buildings\TransportLineChangeVehicleHandler.cs" />
Expand Down Expand Up @@ -198,6 +201,7 @@
<Compile Include="Commands\Handler\Props\PropCreateHandler.cs" />
<Compile Include="Commands\Handler\Props\PropMoveHandler.cs" />
<Compile Include="Commands\Handler\Props\PropReleaseHandler.cs" />
<Compile Include="Commands\Handler\Roads\RoadAdjustHandler.cs" />
<Compile Include="Commands\Handler\Roads\RoadSetPriorityHandler.cs" />
<Compile Include="Commands\Handler\Roads\RoadSettingsHandler.cs" />
<Compile Include="Commands\Handler\Terrain\SoilTradeHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CSM.API.Commands;
using ProtoBuf;

namespace CSM.BaseGame.Commands.Data.Buildings
{
/// <summary>
/// Called when the building variation was changed.
/// </summary>
/// Sent by:
/// - BuildingHandler
[ProtoContract]
public class BuildingSetVariationCommand : CommandBase
{
/// <summary>
/// The id of the modified building.
/// </summary>
[ProtoMember(1)]
public ushort Building { get; set; }

/// <summary>
/// The new variation flags.
/// </summary>
[ProtoMember(2)]
public Building.Flags2 Variation { get; set; }
}
}
32 changes: 32 additions & 0 deletions src/basegame/Commands/Data/Roads/RoadAdjustCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using CSM.API.Commands;
using ProtoBuf;

namespace CSM.BaseGame.Commands.Data.Roads
{
/// <summary>
/// Sent when the player adjusts the extents of a road through the route menu.
/// </summary>
/// Sent by: RoadHandler
[ProtoContract]
public class RoadAdjustCommand : CommandBase
{
/// <summary>
/// The segments previously belonging to the edited road.
/// </summary>
[ProtoMember(1)]
public HashSet<ushort> OriginalSegments { get; set; }

/// <summary>
/// The segments now belonging to the edited road.
/// </summary>
[ProtoMember(2)]
public HashSet<ushort> IncludedSegments { get; set; }

/// <summary>
/// The selected road segment instance.
/// </summary>
[ProtoMember(3)]
public InstanceID LastInstance { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using ColossalFramework;
using CSM.API.Commands;
using CSM.API.Helpers;
using CSM.BaseGame.Commands.Data.Buildings;

namespace CSM.BaseGame.Commands.Handler.Buildings
{
public class BuildingSetVariationHandler : CommandHandler<BuildingSetVariationCommand>
{
protected override void Handle(BuildingSetVariationCommand command)
{
IgnoreHelper.Instance.StartIgnore();

CommonBuildingAI building_ai = Singleton<BuildingManager>.instance.m_buildings.m_buffer[command.Building].Info.m_buildingAI as CommonBuildingAI;
if (building_ai != null)
building_ai.ReplaceVariation(command.Building, command.Variation);

IgnoreHelper.Instance.EndIgnore();
}
}
}
42 changes: 42 additions & 0 deletions src/basegame/Commands/Handler/Roads/RoadAdjustHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using CSM.API.Commands;
using CSM.API.Helpers;
using CSM.BaseGame.Commands.Data.Roads;

namespace CSM.BaseGame.Commands.Handler.Roads
{
public class RoadAdjustHandler : CommandHandler<RoadAdjustCommand>
{
private NetAdjust _adjustDummy;

protected override void Handle(RoadAdjustCommand command)
{
IgnoreHelper.Instance.StartIgnore();

if (_adjustDummy == null)
{
SetupAdjustDummy();
}

ReflectionHelper.SetAttr(_adjustDummy, "m_originalSegments", command.OriginalSegments);
ReflectionHelper.SetAttr(_adjustDummy, "m_includedSegments", command.IncludedSegments);
ReflectionHelper.SetAttr(_adjustDummy, "m_lastInstance", command.LastInstance);
ReflectionHelper.SetAttr(_adjustDummy, "m_tempAdjustmentIndex", 0);

_adjustDummy.ApplyModification(0);

IgnoreHelper.Instance.EndIgnore();
}

private void SetupAdjustDummy()
{
_adjustDummy = new NetAdjust();
ReflectionHelper.SetAttr(_adjustDummy, "m_pathVisible", true);
ReflectionHelper.SetAttr(_adjustDummy, "m_startPath", new FastList<ushort>());
ReflectionHelper.SetAttr(_adjustDummy, "m_endPath", new FastList<ushort>());
ReflectionHelper.SetAttr(_adjustDummy, "m_tempPath", new FastList<ushort>());
ReflectionHelper.SetAttr(_adjustDummy, "m_segmentQueue", new FastList<ushort>());
ReflectionHelper.SetAttr(_adjustDummy, "m_segmentData", new Dictionary<ushort, NetAdjust.SegmentData>());
}
}
}
24 changes: 24 additions & 0 deletions src/basegame/Injections/BuildingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,28 @@ public static void Postfix(ushort buildingId, VehicleInfo info)
});
}
}

[HarmonyPatch(typeof(CommonBuildingAI))]
[HarmonyPatch("ReplaceVariation")]
public class ReplaceVariation
{
public static void Prefix(ushort buildingID, Building.Flags2 variation)
{
if (IgnoreHelper.Instance.IsIgnored())
{
return;
}

if ((variation & Building.Flags2.SubmeshVariation) == Building.Flags2.None)
{
return;
}

Command.SendToAll(new BuildingSetVariationCommand
{
Building = buildingID,
Variation = variation,
});
}
}
}
23 changes: 23 additions & 0 deletions src/basegame/Injections/RoadHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,27 @@ public static MethodBase TargetMethod()
return ReflectionHelper.GetIteratorTargetMethod(typeof(NetManager), "<SetPriorityRoad>c__Iterator3", out Type _);
}
}

[HarmonyPatch(typeof(NetAdjust))]
[HarmonyPatch("ApplyModification")]
public class ApplyModification
{
public static void Prefix(int index, bool ___m_pathVisible, int ___m_tempAdjustmentIndex,
HashSet<ushort> ___m_originalSegments, HashSet<ushort> ___m_includedSegments,
InstanceID ___m_lastInstance)
{
if (IgnoreHelper.Instance.IsIgnored())
return;

if (!___m_pathVisible || ___m_tempAdjustmentIndex != index)
return;

Command.SendToAll(new RoadAdjustCommand()
{
OriginalSegments = ___m_originalSegments,
IncludedSegments = ___m_includedSegments,
LastInstance = ___m_lastInstance,
});
}
}
}