From 57c6de197e92aeeb220d1ea5794b743eac9cbe28 Mon Sep 17 00:00:00 2001 From: seiggy Date: Mon, 3 Aug 2015 21:28:18 -0400 Subject: [PATCH] added more debug messages, and more refactoring around the save system. Trying to solve some crashing and null reference problems caused from loading bad save data from the old XML format. --- TLM/TLM/Configuration.cs | 5 ++- TLM/TLM/CustomPathManager.cs | 4 +- TLM/TLM/Log.cs | 33 ++++++++++++--- TLM/TLM/SerializableDataExtension.cs | 61 ++++++++++++++-------------- TLM/TLM/TLM.csproj | 12 +++--- TLM/TLM/ThreadingExtension.cs | 5 +-- 6 files changed, 71 insertions(+), 49 deletions(-) diff --git a/TLM/TLM/Configuration.cs b/TLM/TLM/Configuration.cs index 639153ba..c2ca6d5f 100644 --- a/TLM/TLM/Configuration.cs +++ b/TLM/TLM/Configuration.cs @@ -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; diff --git a/TLM/TLM/CustomPathManager.cs b/TLM/TLM/CustomPathManager.cs index ee4680dd..9d3134f4 100644 --- a/TLM/TLM/CustomPathManager.cs +++ b/TLM/TLM/CustomPathManager.cs @@ -4,6 +4,7 @@ using System.Threading; using ColossalFramework; using ColossalFramework.Math; +using JetBrains.Annotations; using UnityEngine; // ReSharper disable InconsistentNaming @@ -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(); diff --git a/TLM/TLM/Log.cs b/TLM/TLM/Log.cs index 8cd710d5..6acbb445 100644 --- a/TLM/TLM/Log.cs +++ b/TLM/TLM/Log.cs @@ -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()); } diff --git a/TLM/TLM/SerializableDataExtension.cs b/TLM/TLM/SerializableDataExtension.cs index 9042e21a..74c18b89 100644 --- a/TLM/TLM/SerializableDataExtension.cs +++ b/TLM/TLM/SerializableDataExtension.cs @@ -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) { @@ -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], @@ -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]); @@ -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], @@ -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]; @@ -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.instance.m_nodes.m_buffer.Length}"); + for (var i = 0; i < _configuration.NodeTrafficLights.Length; i++) { + //Log.Message($"Adding NodeTrafficLights iteration: {i1}"); try { - if (Singleton.instance.m_nodes.m_buffer[i1].Info.m_class.m_service != ItemClass.Service.Road || - Singleton.instance.m_nodes.m_buffer[i1].m_flags == 0) + if (Singleton.instance.m_nodes.m_buffer[i].Info.m_class.m_service != ItemClass.Service.Road || + Singleton.instance.m_nodes.m_buffer[i].m_flags == 0) continue; - var trafficLight = _configuration.NodeTrafficLights[j1]; + + var trafficLight = _configuration.NodeTrafficLights[i]; if (trafficLight == '1') { - Singleton.instance.m_nodes.m_buffer[i1].m_flags |= NetNode.Flags.TrafficLights; + Singleton.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.TrafficLights; } else { - Singleton.instance.m_nodes.m_buffer[i1].m_flags &= ~NetNode.Flags.TrafficLights; + Singleton.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.instance.m_nodes.m_buffer[i2].Info.m_class.m_service != ItemClass.Service.Road || - Singleton.instance.m_nodes.m_buffer[i2].m_flags == 0) - continue; - var crossWalk = _configuration.NodeCrosswalk[j2]; + var crossWalk = _configuration.NodeCrosswalk[i]; if (crossWalk == '1') { - Singleton.instance.m_nodes.m_buffer[i2].m_flags |= NetNode.Flags.Junction; + Singleton.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.Junction; } else { - Singleton.instance.m_nodes.m_buffer[i2].m_flags &= ~NetNode.Flags.Junction; + Singleton.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(','); diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 7582261c..beff3afb 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -33,13 +33,13 @@ - D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\Assembly-CSharp.dll + C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\Assembly-CSharp.dll - D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\ColossalManaged.dll + C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\ColossalManaged.dll - D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\ICities.dll + C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\ICities.dll ..\packages\NLog.4.0.1\lib\net35\NLog.dll @@ -47,11 +47,11 @@ - D:\SteamGames\steamapps\common\Cities_Skylines\Managed\System.Core.dll + C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Managed\System.Core.dll - D:\SteamGames\steamapps\common\Cities_Skylines\Cities_Data\Managed\UnityEngine.dll + C:\Program Files (x86)\Steam\steamapps\common\Cities_Skylines\Cities_Data\Managed\UnityEngine.dll @@ -144,7 +144,7 @@ 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" +xcopy /y "$(TargetPath)" "C:\Program Files (x86)\Steam\steamapps\workshop\content\255710\481786333" diff --git a/TLM/TLM/ThreadingExtension.cs b/TLM/TLM/ThreadingExtension.cs index 658d3475..b801b02b 100644 --- a/TLM/TLM/ThreadingExtension.cs +++ b/TLM/TLM/ThreadingExtension.cs @@ -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;