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

debug switches UI #1696

Merged
merged 9 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
158 changes: 42 additions & 116 deletions TLM/TLM/State/ConfigData/DebugSettings.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,23 @@
namespace TrafficManager.State.ConfigData {
using ExtVehicleType = TrafficManager.Traffic.ExtVehicleType;
using JetBrains.Annotations;
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using CSUtil.Commons;
using System;
using TrafficManager.API.Traffic.Enums;
using TrafficManager.Traffic;
using ExtVehicleType = TrafficManager.Traffic.ExtVehicleType;

#if DEBUG
/// <summary>
/// DebugSettings is a part of GlobalConfig, enabled only in Debug mode
/// </summary>
public class DebugSettings {
/// <summary>
/// Do not use directly.
/// Use DebugSwitch.$EnumName$.Get() to access the switch values.
/// </summary>
[XmlArrayItem("Value")]
public DebugSwitchValue[] DebugSwitchValues = {
new (false, "0: Path-finding debug log"),
new (false, "1: Routing basic debug log"),
new (false, "2: Parking ai debug log (basic)"),
new (false, "3: Do not actually repair stuck vehicles/cims, just report"),
new (false, "4: Parking ai debug log (extended)"),
new (false, "5: Geometry debug log"),
new (false, "6: Debug parking AI distance issue"),
new (false, "7: Debug Timed Traffic Lights"),
new (false, "8: Debug routing"),
new (false, "9: Debug vehicle to segment end linking"),
new (false, "10: Prevent routing recalculation on global configuration reload"),
new (false, "11: Debug junction restrictions"),
new (false, "12: Debug pedestrian pathfinding"),
new (false, "13: Priority rules debug"),
new (false, "14: Disable GUI overlay of citizens having a valid path"),
new (false, "15: Disable checking of other vehicles for trams"),
new (false, "16: Debug TramBaseAI.SimulationStep (2)"),
new (false, "17: Debug alternative lane selection"),
new (false, "18: Transport line path-find debugging"),
new (false, "19: Enable obligation to drive on the right hand side of the road"),
new (false, "20: Debug realistic public transport"),
new (false, "21: Debug 'CalculateSegmentPosition'"),
new (false, "22: Parking ai debug log (vehicles)"),
new (false, "23: Debug lane connections"),
new (false, "24: Debug resource loading"),
new (false, "25: Debug turn-on-red"),
new (false, "26: Debug speed limits (also lists NetInfos skipped due to m_netAI in SpeedLimitsManager.cs)"),
new (false, "27: Allow U library UI and event debugging"),
// Added a new flag? Bump LATEST_VERSION in GlobalConfig!
};
private static DebugSwitch _debugSwitches;
public static DebugSwitch DebugSwitches {
get => _debugSwitches;
set {
_debugSwitches = value;
Log._Debug("DebugSettings.DebugSwitches set to " + DebugSwitches);
}
}

private int nodeId_ = 0;

Expand Down Expand Up @@ -94,91 +62,49 @@ public class DebugSettings {
/// This adds access to the new moved type from the compatible old field
/// </summary>
// Property will not be serialized, permit use of obsolete symbol
#pragma warning disable 612
[Obsolete]
public API.Traffic.Enums.ExtVehicleType ApiExtVehicleType
=> LegacyExtVehicleType.ToNew(ExtVehicleType);
#pragma warning restore 612

public ExtPathMode ExtPathMode = ExtPathMode.None;
}

/// <summary>
/// Indexes into Debug.Switches
/// </summary>
[Flags]
Copy link
Collaborator

@kvakvs kvakvs Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bit flags and not multiple boolean? Is there any need to pack all bits into 1 integer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it makes more sense this way. why to write extra code? why to have code duplication?
and we are not serializing anything anymore so there is no need for array.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason is that checking a processor-register wide boolean (64 bit) is faster than reading and masking a bit, with same result. Not important if all code for this is deleted in a Release build.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that code won't run in Release build, at least it shouldn't

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just trying to keep it simple.

public enum DebugSwitch {
PathFindingLog = 0,
RoutingBasicLog = 1,
BasicParkingAILog = 2,
[UsedImplicitly]
NoRepairStuckVehiclesCims = 3,
ExtendedParkingAILog = 4,
GeometryDebug = 5,
ParkingAIDistanceIssue = 6,
TimedTrafficLights = 7,
Routing = 8,
VehicleLinkingToSegmentEnd = 9,
NoRoutingRecalculationOnConfigReload = 10,
JunctionRestrictions = 11,
PedestrianPathfinding = 12,
PriorityRules = 13,
NoValidPathCitizensOverlay = 14,
[UsedImplicitly]
TramsNoOtherVehiclesChecking = 15,
TramBaseAISimulationStep = 16,
AlternativeLaneSelection = 17,
TransportLinePathfind = 18,
[UsedImplicitly]
ObligationToRHD = 19,
RealisticPublicTransport = 20,
CalculateSegmentPosition = 21,
VehicleParkingAILog = 22,
LaneConnections = 23,
ResourceLoading = 24,
TurnOnRed = 25,
SpeedLimits = 26,
ULibraryEvents = 27,
// Added a new flag? Bump LATEST_VERSION in GlobalConfig!
}

public class DebugSwitchValue : IXmlSerializable {
public bool Value = false;
public string Description = string.Empty;

[UsedImplicitly] //required for XmlSerializer
public DebugSwitchValue() { }

public DebugSwitchValue(bool value, string description) {
Value = value;
Description = description;
}
public XmlSchema GetSchema() {
return null;
}

public void ReadXml(XmlReader reader) {
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Text:
bool.TryParse(reader.Value, out Value);
break;
case XmlNodeType.Comment:
Description = reader.Value;
break;
case XmlNodeType.EndElement:
return;
}
}
}

public void WriteXml(XmlWriter writer) {
writer.WriteValue(Value);
writer.WriteComment(Description);
}
None = 0,
RoutingBasicLog = 1 << 0,
Routing = 1 << 1,
NoRoutingRecalculationOnConfigReload = 1 << 2, // Prevent routing recalculation on global configuration reload
PathFindingLog = 1 << 3,
PedestrianPathfinding = 1 << 4,
TransportLinePathfind = 1 << 5,
BasicParkingAILog = 1 << 6,
ExtendedParkingAILog = 1 << 7,
ParkingAIDistanceIssue = 1 << 8,
VehicleParkingAILog = 1 << 9,
NoRepairStuckVehiclesCims = 1 << 10, // Do not actually repair stuck vehicles/cims, just report
GeometryDebug = 1 << 11,
TimedTrafficLights = 1 << 12,
VehicleLinkingToSegmentEnd = 1 << 13,
JunctionRestrictions = 1 << 14,
PriorityRules = 1 << 15,
NoValidPathCitizensOverlay = 1 << 16, // Disable GUI overlay of citizens having a valid path
TramsNoOtherVehiclesChecking = 1 << 17, // Disable checking of other vehicles for trams
TramBaseAISimulationStep = 1 << 18,
AlternativeLaneSelection = 1 << 19,
ObligationToRHD = 1 << 19, // obligation to drive on the right hand side of the road
RealisticPublicTransport = 1 << 20,
CalculateSegmentPosition = 1 << 21,
LaneConnections = 1 << 23,
ResourceLoading = 1 << 24,
TurnOnRed = 1 << 25,
SpeedLimits = 1 << 26, // also lists NetInfos skipped due to m_netAI in SpeedLimitsManager.cs
ULibraryEvents = 1 << 27, // U library UI and event
}

internal static class DebugSwitchExtensions {
public static bool Get(this DebugSwitch sw) {
return GlobalConfig.Instance.Debug.DebugSwitchValues[(int)sw].Value;
return (DebugSettings.DebugSwitches & sw) != 0;
}
}
#endif
Expand Down
13 changes: 12 additions & 1 deletion TLM/TLM/State/OptionsTabs/MaintenanceTab_ConfigGroup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace TrafficManager.State {
using ICities;
using TrafficManager.UI;
using TrafficManager.UI.DebugSwitches;
using TrafficManager.UI.Helpers;

public static class MaintenanceTab_ConfigGroup {
Expand All @@ -14,12 +15,22 @@ public static class MaintenanceTab_ConfigGroup {
Handler = OnResetGlobalConfigClicked,
};

internal static void AddUI(UIHelperBase tab) {
#if DEBUG
public static ActionButton DebugSwiches = new() {
Translator = key => key,
Label = "Debug Switches",
Handler = DebugSwitchPanel.OpenModal,
krzychu124 marked this conversation as resolved.
Show resolved Hide resolved
};
#endif

internal static void AddUI(UIHelperBase tab) {
var group = tab.AddGroup(T("Group:Configuration"));

ReloadGlobalConfig.AddUI(group);
ResetGlobalConfig.AddUI(group);
#if DEBUG
DebugSwiches.AddUI(group);
#endif
}

private static string T(string key) => Translation.Options.Get(key);
Expand Down
4 changes: 3 additions & 1 deletion TLM/TLM/TLM.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" DefaultTargets="Build" 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 Expand Up @@ -182,6 +182,8 @@
<Compile Include="TrafficLight\Impl\ITrafficLightContainer.cs" />
<Compile Include="TrafficLight\Impl\TrafficLightSimulation.cs" />
<Compile Include="UI\AllowDespawn\AllowDespawnPanel.cs" />
<Compile Include="UI\DebugSwitches\DebugSwitchPanel.cs" />
<Compile Include="UI\DebugSwitches\DebugSwitchCheckboxOption.cs" />
<Compile Include="UI\Helpers\IValuePropagator.cs" />
<Compile Include="UI\Helpers\ActionButton.cs" />
<Compile Include="UI\Helpers\TriStateCheckboxOption.cs" />
Expand Down
26 changes: 26 additions & 0 deletions TLM/TLM/UI/DebugSwitches/DebugSwitchCheckboxOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if DEBUG
namespace TrafficManager.UI.DebugSwitches {
using ColossalFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrafficManager.State;
using TrafficManager.State.ConfigData;
using TrafficManager.UI.Helpers;

internal class DebugSwitchCheckboxOption : CheckboxOption {
internal DebugSwitch DebugSwitch;
public DebugSwitchCheckboxOption(DebugSwitch sw)
: base(sw.ToString(), Options.PersistTo.Global) {
krzychu124 marked this conversation as resolved.
Show resolved Hide resolved
DebugSwitch = sw;
Translator = val => val;
Handler = val => DebugSettings.DebugSwitches = DebugSettings.DebugSwitches.SetFlags(DebugSwitch, val);
Label = sw.ToString();
Refresh();
}

public void Refresh() => Value = DebugSwitch.Get();
}
}
#endif
Loading