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

Commit

Permalink
Fixed save system.
Browse files Browse the repository at this point in the history
  • Loading branch information
seiggy committed Aug 6, 2015
1 parent 24097aa commit 6469a24
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 45 deletions.
97 changes: 52 additions & 45 deletions TLM/TLM/SerializableDataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -248,7 +248,9 @@ var segmentData in
}

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++)
var saveDataIndex = 0;
var nodeCount = Singleton<NetManager>.instance.m_nodes.m_buffer.Length;
for (var i = 0; i < nodeCount; i++)
{
//Log.Message($"Adding NodeTrafficLights iteration: {i1}");
try
Expand All @@ -257,27 +259,37 @@ var segmentData in
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags == 0)
continue;

var trafficLight = _configuration.NodeTrafficLights[i];

if (trafficLight == '1')
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.TrafficLights;
}
else
// prevent overflow
if (_configuration.NodeTrafficLights.Length > saveDataIndex)
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.TrafficLights;
}

var crossWalk = _configuration.NodeCrosswalk[i];

if (crossWalk == '1')
{
Singleton<NetManager>.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<NetManager>.instance.m_nodes.m_buffer[i].Info.name}");
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.TrafficLights;
}
else
{
//Log.Message($"Removing Traffic Light from Segment: {Singleton<NetManager>.instance.m_nodes.m_buffer[i].Info.name}");
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.TrafficLights;
}
}
else

if (_configuration.NodeCrosswalk.Length > saveDataIndex)
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.Junction;
var crossWalk = _configuration.NodeCrosswalk[saveDataIndex];

if (crossWalk == '1')
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags |= NetNode.Flags.Junction;
}
else
{
Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags &= ~NetNode.Flags.Junction;
}
}
++saveDataIndex;
}
catch (Exception e)
{
Expand Down Expand Up @@ -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<NetManager>.instance.m_lanes.m_buffer.Length; i++)
{
var laneSegment = Singleton<NetManager>.instance.m_lanes.m_buffer[i].m_segment;
if (!LoadingExtension.IsPathManagerCompatibile)
continue;

if (TrafficPriority.PrioritySegments.ContainsKey(laneSegment))
{
configuration.LaneFlags += i + ":" + Singleton<NetManager>.instance.m_lanes.m_buffer[i].m_flags +
",";
}
var laneSegment = Singleton<NetManager>.instance.m_lanes.m_buffer[i].m_segment;

if (TrafficPriority.PrioritySegments.ContainsKey(laneSegment))
{
configuration.LaneFlags += $"{i}:{Singleton<NetManager>.instance.m_lanes.m_buffer[i].m_flags},";
}
}
else
{

// Traffic++ compatibility
if (!LoadingExtension.IsPathManagerCompatibile)
configuration.LaneFlags = "";
}

var binaryFormatter = new BinaryFormatter();
var memoryStream = new MemoryStream();
Expand Down
16 changes: 16 additions & 0 deletions TLM/TLM/ThreadingExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using ColossalFramework;
using ICities;
using TrafficManager.CustomAI;
using UnityEngine;
Expand Down Expand Up @@ -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<NetManager>.instance.m_nodes.m_buffer.Length}");
//SerializableDataExtension.LoadDataState();
//SerializableDataExtension.StateLoaded = true;
}
catch (Exception e)
{
Log.Error($"Exception Loading Save Data: {e.Message}");
}
}
}
}

0 comments on commit 6469a24

Please sign in to comment.