Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
added more debug messages, and more refactoring around the save syste…
Browse files Browse the repository at this point in the history
…m. Trying to solve some crashing and null reference problems caused from loading bad save data from the old XML format.
  • Loading branch information
seiggy committed Aug 4, 2015
1 parent a6dbd9c commit 57c6de1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 49 deletions.
5 changes: 3 additions & 2 deletions TLM/TLM/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ public static Configuration LoadConfigurationFromFile(string filename)
{
using (var reader = new StreamReader(filename))
{
Log.Message("Deserializing Configuration Object");
var config = (Configuration)serializer.Deserialize(reader);
return config;
}
}
catch
catch (Exception e)
{
// ignored
Log.Error($"Error deserializing Configuration Object {e.Message}");
}

return null;
Expand Down
4 changes: 3 additions & 1 deletion TLM/TLM/CustomPathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using ColossalFramework;
using ColossalFramework.Math;
using JetBrains.Annotations;
using UnityEngine;

// ReSharper disable InconsistentNaming
Expand All @@ -17,7 +18,8 @@ public class CustomPathManager : PathManager
public static CustomPathFind PathFindInstance;

//On waking up, replace the stock pathfinders with the custom one
protected new virtual void Awake()
[UsedImplicitly]
public new virtual void Awake()
{
Log.Message("Waking up CustomPathManager.");
var stockPathFinds = GetComponents<PathFind>();
Expand Down
33 changes: 27 additions & 6 deletions TLM/TLM/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,43 @@ public static class Log

public static void Message(object s)
{
if(InGameDebug)
DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Message, Prefix + s.ToString());
try
{
if (InGameDebug)
DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Message, Prefix + s.ToString());
}
catch (Exception)
{
// cross thread issue?
}
Debug.Log(Prefix + s.ToString());
}

public static void Error(object s)
{
if (InGameDebug)
DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Error, Prefix + s.ToString());
try
{
if (InGameDebug)
DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Error, Prefix + s.ToString());
}
catch (Exception)
{
// cross thread issue?
}
Debug.LogError(Prefix + s.ToString());
}

public static void Warning(object s)
{
if (InGameDebug)
DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Warning, Prefix + s.ToString());
try
{
if (InGameDebug)
DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Warning, Prefix + s.ToString());
}
catch (Exception)
{
// cross thread issue?
}
Debug.LogWarning(Prefix + s.ToString());
}

Expand Down
61 changes: 31 additions & 30 deletions TLM/TLM/SerializableDataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SerializableDataExtension : SerializableDataExtensionBase
public static ISerializableData SerializableData;
private static Configuration _configuration;
public static bool ConfigLoaded = false;
public static bool StateLoaded = false;

public override void OnCreated(ISerializableData serializableData)
{
Expand Down Expand Up @@ -109,10 +110,22 @@ private void DeserializeData(byte[] data)
_configuration = (Configuration) binaryFormatter.Deserialize(memoryStream);
}
ConfigLoaded = true;

Log.Message("Setting timer to load data.");
var timer = new Timer(5000);
timer.Elapsed += (sender, args) =>
{
if (!ConfigLoaded || StateLoaded) return;
Log.Message("Loading State Data from Save.");
LoadDataState();
StateLoaded = true;
};
timer.Start();
}

public static void LoadDataState()
{
Log.Message("Loading State from Config");
foreach (
var segment in
_configuration.PrioritySegments.Where(segment => !TrafficPriority.IsPrioritySegment((ushort) segment[0],
Expand All @@ -128,6 +141,7 @@ var segment in
var node in _configuration.NodeDictionary.Where(node => CustomRoadAI.GetNodeSimulation((ushort) node[0]) == null)
)
{
Log.Message($"Adding Node do Simulation {node[0]}");
try
{
CustomRoadAI.AddNodeToSimulation((ushort)node[0]);
Expand All @@ -149,6 +163,7 @@ var segmentData in
_configuration.ManualSegments.Where(
segmentData => !TrafficLightsManual.IsSegmentLight((ushort) segmentData[0], segmentData[1])))
{
Log.Message($"Adding Light to Segment {segmentData[0]}");
try
{
TrafficLightsManual.AddSegmentLight((ushort)segmentData[0], segmentData[1],
Expand All @@ -175,6 +190,7 @@ var segmentData in

for (var i = 0; i < _configuration.TimedNodes.Count; i++)
{
Log.Message($"Adding Timed Node {i}");
try
{
var nodeid = (ushort)_configuration.TimedNodes[i][0];
Expand Down Expand Up @@ -228,60 +244,45 @@ var segmentData in
}
}

var j1 = 0;
for (var i1 = 0; i1 < 32768; i1++)
Log.Message($"Config Nodes: {_configuration.NodeTrafficLights.Length}\nLevel Nodes: {Singleton<NetManager>.instance.m_nodes.m_buffer.Length}");
for (var i = 0; i < _configuration.NodeTrafficLights.Length; i++)
{
//Log.Message($"Adding NodeTrafficLights iteration: {i1}");
try
{
if (Singleton<NetManager>.instance.m_nodes.m_buffer[i1].Info.m_class.m_service != ItemClass.Service.Road ||
Singleton<NetManager>.instance.m_nodes.m_buffer[i1].m_flags == 0)
if (Singleton<NetManager>.instance.m_nodes.m_buffer[i].Info.m_class.m_service != ItemClass.Service.Road ||
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags == 0)
continue;
var trafficLight = _configuration.NodeTrafficLights[j1];

var trafficLight = _configuration.NodeTrafficLights[i];

if (trafficLight == '1')
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i1].m_flags |= NetNode.Flags.TrafficLights;
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.TrafficLights;
}
else
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i1].m_flags &= ~NetNode.Flags.TrafficLights;
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.TrafficLights;
}
}
catch (Exception e)
{
// ignore as it's probably bad save data.
Log.Warning("Error setting the NodeTrafficLights: " + e.Message);
}
j1++;
}

var j2 = 0;
for (var i2 = 0; i2 < 32768; i2++)
{
try
{
if (Singleton<NetManager>.instance.m_nodes.m_buffer[i2].Info.m_class.m_service != ItemClass.Service.Road ||
Singleton<NetManager>.instance.m_nodes.m_buffer[i2].m_flags == 0)
continue;
var crossWalk = _configuration.NodeCrosswalk[j2];
var crossWalk = _configuration.NodeCrosswalk[i];

if (crossWalk == '1')
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i2].m_flags |= NetNode.Flags.Junction;
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.Junction;
}
else
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i2].m_flags &= ~NetNode.Flags.Junction;
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.Junction;
}
}
catch (Exception e)
{
// bad save data. ignore
Log.Warning("Error loading data from the NodeCrosswalk: " + e.Message);
// ignore as it's probably bad save data.
//Log.Warning("Error setting the NodeTrafficLights: " + e.Message);
}
j2++;
}

Log.Message($"LaneFlags: {_configuration.LaneFlags}");
var lanes = _configuration.LaneFlags.Split(',');

Expand Down
12 changes: 6 additions & 6 deletions TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\Assembly-CSharp.dll</HintPath>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="ColossalManaged">
<HintPath>D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\ColossalManaged.dll</HintPath>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\ColossalManaged.dll</HintPath>
</Reference>
<Reference Include="ICities">
<HintPath>D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\ICities.dll</HintPath>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\ICities.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.0.1\lib\net35\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<HintPath>D:\SteamGames\steamapps\common\Cities_Skylines\Managed\System.Core.dll</HintPath>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Managed\System.Core.dll</HintPath>
</Reference>
<Reference Include="System.XML" />
<Reference Include="UnityEngine">
<HintPath>D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -144,7 +144,7 @@
<PostBuildEvent>mkdir "%25LOCALAPPDATA%25\Colossal Order\Cities_Skylines\Addons\Mods\$(SolutionName)"
del "%25LOCALAPPDATA%25\Colossal Order\Cities_Skylines\Addons\Mods\$(SolutionName)\$(TargetFileName)"
xcopy /y "$(TargetPath)" "%25LOCALAPPDATA%25\Colossal Order\Cities_Skylines\Addons\Mods\$(SolutionName)"
xcopy /y "$(TargetPath)" "D:\SteamGames\steamapps\workshop\content\255710\481786333"</PostBuildEvent>
xcopy /y "$(TargetPath)" "C:\Program Files (x86)\Steam\steamapps\workshop\content\255710\481786333"</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>
Expand Down
5 changes: 1 addition & 4 deletions TLM/TLM/ThreadingExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,8 @@ public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
}

LoadingExtension.Instance.DetourInited = true;

if (SerializableDataExtension.ConfigLoaded)
SerializableDataExtension.LoadDataState();
}

if (!LoadingExtension.Instance.NodeSimulationLoaded)
{
LoadingExtension.Instance.NodeSimulationLoaded = true;
Expand Down

0 comments on commit 57c6de1

Please sign in to comment.