From 6469a2496ceb3ecfd66bd64287c8581011509ba7 Mon Sep 17 00:00:00 2001 From: seiggy Date: Wed, 5 Aug 2015 20:49:51 -0400 Subject: [PATCH] Fixed save system. --- TLM/TLM/SerializableDataExtension.cs | 97 +++++++++++++++------------- TLM/TLM/ThreadingExtension.cs | 16 +++++ 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/TLM/TLM/SerializableDataExtension.cs b/TLM/TLM/SerializableDataExtension.cs index 2cfd81f9..76772731 100644 --- a/TLM/TLM/SerializableDataExtension.cs +++ b/TLM/TLM/SerializableDataExtension.cs @@ -111,19 +111,19 @@ private void DeserializeData(byte[] data) } ConfigLoaded = true; - //LoadDataState(); - //StateLoaded = true; - - Log.Message("Setting timer to load data."); - var timer = new Timer(1500); - timer.Elapsed += (sender, args) => - { - if (!ConfigLoaded || StateLoaded) return; - Log.Message("Loading State Data from Save."); - LoadDataState(); - StateLoaded = true; - }; - timer.Start(); + LoadDataState(); + StateLoaded = true; + + //Log.Message("Setting timer to load data."); + //var timer = new Timer(1500); + //timer.Elapsed += (sender, args) => + //{ + // if (!ConfigLoaded || StateLoaded) return; + // Log.Message("Loading State Data from Save."); + // LoadDataState(); + // StateLoaded = true; + //}; + //timer.Start(); } public static void LoadDataState() @@ -248,7 +248,9 @@ var segmentData in } 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++) + var saveDataIndex = 0; + var nodeCount = Singleton.instance.m_nodes.m_buffer.Length; + for (var i = 0; i < nodeCount; i++) { //Log.Message($"Adding NodeTrafficLights iteration: {i1}"); try @@ -257,27 +259,37 @@ var segmentData in Singleton.instance.m_nodes.m_buffer[i].m_flags == 0) continue; - var trafficLight = _configuration.NodeTrafficLights[i]; - - if (trafficLight == '1') - { - Singleton.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.TrafficLights; - } - else + // prevent overflow + if (_configuration.NodeTrafficLights.Length > saveDataIndex) { - Singleton.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.TrafficLights; - } - - var crossWalk = _configuration.NodeCrosswalk[i]; - if (crossWalk == '1') - { - Singleton.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.Junction; + var trafficLight = _configuration.NodeTrafficLights[saveDataIndex]; + if (trafficLight == '1') + { + //Log.Message($"Adding Traffic Light at Segment: {Singleton.instance.m_nodes.m_buffer[i].Info.name}"); + Singleton.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.TrafficLights; + } + else + { + //Log.Message($"Removing Traffic Light from Segment: {Singleton.instance.m_nodes.m_buffer[i].Info.name}"); + Singleton.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.TrafficLights; + } } - else + + if (_configuration.NodeCrosswalk.Length > saveDataIndex) { - Singleton.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.Junction; + var crossWalk = _configuration.NodeCrosswalk[saveDataIndex]; + + if (crossWalk == '1') + { + Singleton.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.Junction; + } + else + { + Singleton.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.Junction; + } } + ++saveDataIndex; } catch (Exception e) { @@ -436,26 +448,21 @@ public override void OnSaveData() Convert.ToInt16((nodeFlags & NetNode.Flags.TrafficLights) != NetNode.Flags.None); configuration.NodeCrosswalk += Convert.ToInt16((nodeFlags & NetNode.Flags.Junction) != NetNode.Flags.None); - } - // Traffic++ compatibility - if (LoadingExtension.IsPathManagerCompatibile) - { - for (var i = 0; i < Singleton.instance.m_lanes.m_buffer.Length; i++) - { - var laneSegment = Singleton.instance.m_lanes.m_buffer[i].m_segment; + if (!LoadingExtension.IsPathManagerCompatibile) + continue; - if (TrafficPriority.PrioritySegments.ContainsKey(laneSegment)) - { - configuration.LaneFlags += i + ":" + Singleton.instance.m_lanes.m_buffer[i].m_flags + - ","; - } + var laneSegment = Singleton.instance.m_lanes.m_buffer[i].m_segment; + + if (TrafficPriority.PrioritySegments.ContainsKey(laneSegment)) + { + configuration.LaneFlags += $"{i}:{Singleton.instance.m_lanes.m_buffer[i].m_flags},"; } } - else - { + + // Traffic++ compatibility + if (!LoadingExtension.IsPathManagerCompatibile) configuration.LaneFlags = ""; - } var binaryFormatter = new BinaryFormatter(); var memoryStream = new MemoryStream(); diff --git a/TLM/TLM/ThreadingExtension.cs b/TLM/TLM/ThreadingExtension.cs index b801b02b..89387141 100644 --- a/TLM/TLM/ThreadingExtension.cs +++ b/TLM/TLM/ThreadingExtension.cs @@ -1,5 +1,6 @@ using System; using System.Reflection; +using ColossalFramework; using ICities; using TrafficManager.CustomAI; using UnityEngine; @@ -124,6 +125,21 @@ public override void OnUpdate(float realTimeDelta, float simulationTimeDelta) { LoadingExtension.Instance.UI.Close(); } + + try + { + if (!LoadingExtension.Instance.DetourInited || !SerializableDataExtension.ConfigLoaded || + SerializableDataExtension.StateLoaded) + return; + + Log.Message($"Nodes available at this point: {Singleton.instance.m_nodes.m_buffer.Length}"); + //SerializableDataExtension.LoadDataState(); + //SerializableDataExtension.StateLoaded = true; + } + catch (Exception e) + { + Log.Error($"Exception Loading Save Data: {e.Message}"); + } } } }