Skip to content

Commit

Permalink
Merge pull request #1088 from CitiesSkylinesMods/master-to-testing
Browse files Browse the repository at this point in the history
Master to testing
  • Loading branch information
krzychu124 committed Apr 13, 2021
2 parents 9f7bb61 + a226e16 commit 54d3a8f
Show file tree
Hide file tree
Showing 64 changed files with 891 additions and 910 deletions.
41 changes: 41 additions & 0 deletions TLM/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,47 @@ max_line_length=100
insert_final_newline=true
trim_trailing_whitespace=true
indent_style=space
dotnet_diagnostic.RAS0001.severity = error
dotnet_diagnostic.SA1000.severity = none
dotnet_diagnostic.SA1005.severity = none
dotnet_diagnostic.SA1027.severity = none
dotnet_diagnostic.SA1028.severity = none
dotnet_diagnostic.SA1100.severity = none
dotnet_diagnostic.SA1101.severity = none
dotnet_diagnostic.SA1107.severity = none
dotnet_diagnostic.SA1115.severity = none
dotnet_diagnostic.SA1116.severity = none
dotnet_diagnostic.SA1118.severity = none
dotnet_diagnostic.SA1200.severity = none
dotnet_diagnostic.SA1208.severity = none
dotnet_diagnostic.SA1210.severity = none
dotnet_diagnostic.SA1211.severity = none
dotnet_diagnostic.SA1216.severity = none
dotnet_diagnostic.SA1217.severity = none
dotnet_diagnostic.SA1308.severity = suggestion
dotnet_diagnostic.SA1309.severity = silent
dotnet_diagnostic.SA1310.severity = none
dotnet_diagnostic.SA1500.severity = none
dotnet_diagnostic.SA1503.severity = suggestion
dotnet_diagnostic.SA1505.severity = silent
dotnet_diagnostic.SA1512.severity = none
dotnet_diagnostic.SA1513.severity = none
dotnet_diagnostic.SA1515.severity = none
dotnet_diagnostic.SA1516.severity = silent
dotnet_diagnostic.SA1600.severity = none
dotnet_diagnostic.SA1602.severity = none
dotnet_diagnostic.SA1611.severity = none
dotnet_diagnostic.SA1615.severity = none
dotnet_diagnostic.SA1623.severity = none
dotnet_diagnostic.SA1629.severity = none
dotnet_diagnostic.SA1633.severity = none
dotnet_diagnostic.SA1634.severity = none
dotnet_diagnostic.SA1635.severity = none
dotnet_diagnostic.SA1636.severity = none
dotnet_diagnostic.SA1637.severity = none
dotnet_diagnostic.SA1638.severity = none
dotnet_diagnostic.SA1640.severity = none
dotnet_diagnostic.SA1652.severity = none

[*]
charset=utf-8
Expand Down
2 changes: 1 addition & 1 deletion TLM/Benchmarks/Benchmarks.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 ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion TLM/CSUtil.CameraControl
2 changes: 1 addition & 1 deletion TLM/CSUtil.Commons/CSUtil.Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>..\TMPE.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion TLM/OptionsFramework
2 changes: 1 addition & 1 deletion TLM/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
// Minor Version
// Build Number
// Revision
[assembly: AssemblyVersion("11.5.3.*")]
[assembly: AssemblyVersion("11.5.4.*")]
2 changes: 1 addition & 1 deletion TLM/TLM/Custom/PathFinding/CustomPathFind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace TrafficManager.Custom.PathFinding {
#endif

/// <summary>
/// This replaces game PathFind class if PF_DIJKSTRA is defined
/// This replaces game PathFind class
/// This is ALL targets except Benchmark
/// </summary>
public class CustomPathFind : PathFind {
Expand Down
149 changes: 107 additions & 42 deletions TLM/TLM/Custom/PathFinding/CustomPathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ namespace TrafficManager.Custom.PathFinding {
using System;
using API.Traffic.Enums;
using TrafficManager.API.Traffic.Data;
#if !PF_DIJKSTRA
using CustomPathFind = CustomPathFind_Old;
#endif
using TrafficManager.Manager.Impl;
using TrafficManager.State;
using UnityEngine;
using ColossalFramework.UI;

public class CustomPathManager : PathManager {
/// <summary>
Expand All @@ -24,20 +25,58 @@ public class CustomPathManager : PathManager {

public static CustomPathManager _instance;

private PathManager stockPathManager_;

private static FastList<ISimulationManager> GetSimulationManagers() =>
typeof(SimulationManager)
.GetField("m_managers", BindingFlags.Static | BindingFlags.NonPublic)
?.GetValue(null)
as FastList<ISimulationManager>
?? throw new Exception("could not get SimulationManager.m_managers");

private static FieldInfo PathManagerInstance =>
typeof(Singleton<PathManager>)
.GetField(
"sInstance",
BindingFlags.Static | BindingFlags.NonPublic) ??
throw new Exception("pathManagerInstance is null");

#if QUEUEDSTATS
public static uint TotalQueuedPathFinds {
get; private set;
}
public static uint TotalQueuedPathFinds { get; private set; }
#endif

public static bool InitDone {
get; private set;
public static void OnLevelLoaded() {
try {
Log.Info("CustomPathManager.OnLevelLoaded() called.");
PathManager.instance.gameObject.AddComponent<CustomPathManager>();
} 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.";
Log.Error(error);
Log.Error($"Path manager replacement error: {ex}");
UIView.library.ShowModal<ExceptionPanel>("ExceptionPanel")
.SetMessage("TM:PE failed to load", error, true);
}
}

// On waking up, replace the stock pathfinders with the custom one
[UsedImplicitly]
public new virtual void Awake() {
protected override void Awake() {
// On waking up, replace the stock pathfinders with the custom one
// but retain the original version for future replace
// also suppress call to base class.
_instance = this;
stockPathManager_ = PathManager.instance
?? throw new Exception("stockPathManager is null");
Log._Debug($"Got stock PathManager instance {stockPathManager_?.GetName()}");
PathManagerInstance.SetValue(null, this);
Log._Debug("Should be custom: " + PathManager.instance.GetType());

UpdateWithPathManagerValues(stockPathManager_);

var simManagers = GetSimulationManagers();
simManagers.Remove(stockPathManager_);
simManagers.Add(this);
}

public void UpdateWithPathManagerValues(PathManager stockPathManager) {
Expand All @@ -51,8 +90,6 @@ public void UpdateWithPathManagerValues(PathManager stockPathManager) {
m_pathUnits = stockPathManager.m_pathUnits;
m_bufferLock = stockPathManager.m_bufferLock;

Log._Debug("Waking up CustomPathManager.");

QueueItems = new PathUnitQueueItem[MAX_PATHUNIT_COUNT];

PathFind[] stockPathFinds = GetComponents<PathFind>();
Expand All @@ -61,26 +98,18 @@ public void UpdateWithPathManagerValues(PathManager stockPathManager) {

Log._Debug("Creating " + numCustomPathFinds + " custom PathFind objects.");
_replacementPathFinds = new CustomPathFind[numCustomPathFinds];
FieldInfo f_pathfinds = typeof(PathManager).GetField(
"m_pathfinds",
BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new Exception("f_pathFinds is null");

lock(m_bufferLock) {
lock (m_bufferLock) {

for (int i = 0; i < numCustomPathFinds; i++) {
_replacementPathFinds[i] = gameObject.AddComponent<CustomPathFind>();
#if !PF_DIJKSTRA
_replacementPathFinds[i].pfId = i;
if (i == 0) {
_replacementPathFinds[i].IsMasterPathFind = true;
}
#endif
}

Log._Debug("Setting _replacementPathFinds");
FieldInfo fieldInfo = typeof(PathManager).GetField(
"m_pathfinds",
BindingFlags.NonPublic | BindingFlags.Instance);

Log._Debug("Setting m_pathfinds to custom collection");
fieldInfo?.SetValue(this, _replacementPathFinds);
f_pathfinds?.SetValue(this, _replacementPathFinds);

for (int i = 0; i < numOfStockPathFinds; i++) {
Log._Debug($"PF {i}: {stockPathFinds[i].m_queuedPathFindCount} queued path-finds");
Expand All @@ -90,8 +119,39 @@ public void UpdateWithPathManagerValues(PathManager stockPathManager) {
Destroy(stockPathFinds[i]);
}
}
}

public void UpdateOldPathManagerValues(PathManager stockPathManager) {
stockPathManager.m_drawCallData = m_drawCallData;
stockPathManager.m_pathUnitCount = m_pathUnitCount;
stockPathManager.m_renderPathGizmo = m_renderPathGizmo;

int n = _replacementPathFinds.Length;

Log._Debug("Creating " + n + " stock PathFind objects.");
PathFind[] stockPathFinds = new PathFind[n];

InitDone = true;
FieldInfo f_pathfinds = typeof(PathManager).GetField(
"m_pathfinds",
BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new Exception("f_pathFinds is null");

// both stcok and custom PathMangers use the same lock object
lock (m_bufferLock) {
for (int i = 0; i < n; i++) {
Log._Debug($"PF {i}: {_replacementPathFinds[i].m_queuedPathFindCount} queued path-finds");

// would cause deadlock since we have a lock on m_bufferLock
// customPathFinds[i].WaitForAllPaths();
Destroy(_replacementPathFinds[i]);
}

for (int i = 0; i < n; i++) {
stockPathFinds[i] = gameObject.AddComponent<PathFind>();
}

f_pathfinds?.SetValue(stockPathManager, stockPathFinds);
}
}

/// <summary>
Expand All @@ -110,7 +170,7 @@ internal void CustomReleasePath(uint unit) {
if (m_pathUnits.m_buffer[unit].m_simulationFlags == 0) {
return;
}
lock(m_bufferLock) {
lock (m_bufferLock) {

int numIters = 0;
while (unit != 0u) {
Expand Down Expand Up @@ -147,7 +207,7 @@ public bool CustomCreatePath(out uint unit,
ref Randomizer randomizer,
PathCreationArgs args) {
uint pathUnitId;
lock(m_bufferLock) {
lock (m_bufferLock) {

int numIters = 0;
while (true) {
Expand Down Expand Up @@ -249,18 +309,12 @@ public bool CustomCreatePath(out uint unit,
pathFind = pathFindCandidate;
}

#if PF_DIJKSTRA
if (pathFind != null && pathFind.CalculatePath(unit, args.skipQueue)) {
return true;
}
#else
if (pathFind != null && pathFind.ExtCalculatePath(unit, args.skipQueue)) {
return true;
}
#endif

// NON-STOCK CODE START
lock(m_bufferLock) {
lock (m_bufferLock) {

QueueItems[pathUnitId].queued = false;
// NON-STOCK CODE END
Expand Down Expand Up @@ -379,15 +433,26 @@ private static ExtVehicleType ConvertToExtVehicleType(VehicleInfo.VehicleType ve
return extVehicleType;
}

private void StopPathFinds() {
foreach (CustomPathFind pathFind in _replacementPathFinds) {
Destroy(pathFind);
}
public void OnLevelUnloading() {
Log.Info("CustomPathManager.OnLevelUnloading()");
DestroyImmediate(this);
}

protected virtual void OnDestroy() {
Log._Debug("CustomPathManager: OnDestroy");
StopPathFinds();
WaitForAllPaths();

PathManagerInstance.SetValue(null, stockPathManager_);
Log._Debug("Should be stock: " + PathManager.instance.GetType());

UpdateOldPathManagerValues(stockPathManager_);
var simManagers = GetSimulationManagers();

simManagers.Remove(this);

simManagers.Add(stockPathManager_);

_instance = null;
}
}
}
}
14 changes: 14 additions & 0 deletions TLM/TLM/Lifecycle/LoadingExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace TrafficManager.Lifecycle {
using ColossalFramework;
using CSUtil.Commons;
using ICities;
using JetBrains.Annotations;
using System.Collections.Generic;
using System.Reflection;

[UsedImplicitly]
public class LoadingExtension : LoadingExtensionBase {
public override void OnLevelLoaded(LoadMode mode) => TMPELifecycle.Instance.Load();
public override void OnLevelUnloading() => TMPELifecycle.Instance.Unload();
}
}
Loading

0 comments on commit 54d3a8f

Please sign in to comment.