public override void OnSaveData()
        {
            Log.Info("Recalculating segment geometries");
            SegmentGeometry.OnBeforeSaveData();
            Log.Info("Saving Mod Data.");
            var configuration = new Configuration();

            if (TrafficPriority.TrafficSegments != null)
            {
                for (ushort i = 0; i < Singleton <NetManager> .instance.m_segments.m_size; i++)
                {
                    try {
                        SavePrioritySegment(i, configuration);
                    } catch (Exception e) {
                        Log.Error($"Exception occurred while saving priority segment @ {i}: {e.ToString()}");
                    }

                    try {
                        SaveSegmentNodeFlags(i, configuration);
                    } catch (Exception e) {
                        Log.Error($"Exception occurred while saving segment node flags @ {i}: {e.ToString()}");
                    }
                }
            }

            for (ushort i = 0; i < Singleton <NetManager> .instance.m_nodes.m_size; i++)
            {
                /*if (TrafficLightSimulation.LightSimulationByNodeId != null) {
                 *      SaveTrafficLightSimulation(i, configuration);
                 * }*/

                /*if (TrafficLightsManual.ManualSegments != null) {
                 *      SaveManualTrafficLight(i, configuration);
                 * }*/

                TrafficLightSimulation sim = TrafficLightSimulation.GetNodeSimulation(i);
                if (sim != null && sim.IsTimedLight())
                {
                    try {
                        SaveTimedTrafficLight(i, configuration);
                    } catch (Exception e) {
                        Log.Error($"Exception occurred while saving timed traffic light @ {i}: {e.ToString()}");
                    }
                    // TODO save new traffic lights
                }

                try {
                    SaveNodeLights(i, configuration);
                } catch (Exception e) {
                    Log.Error($"Exception occurred while saving node traffic light @ {i}: {e.ToString()}");
                }
            }

#if !TAM
            if (LoadingExtension.IsPathManagerCompatible)
            {
#endif
            for (uint i = 0; i < Singleton <NetManager> .instance.m_lanes.m_buffer.Length; i++)
            {
                try {
                    SaveLaneData(i, configuration);
                } catch (Exception e) {
                    Log.Error($"Exception occurred while saving lane data @ {i}: {e.ToString()}");
                }
            }
#if !TAM
        }
#endif

            foreach (KeyValuePair <uint, ushort> e in Flags.getAllLaneSpeedLimits())
            {
                try {
                    SaveLaneSpeedLimit(new Configuration.LaneSpeedLimit(e.Key, e.Value), configuration);
                } catch (Exception ex) {
                    Log.Error($"Exception occurred while saving lane speed limit @ {e.Key}: {ex.ToString()}");
                }
            }

            foreach (KeyValuePair <uint, ExtVehicleType> e in Flags.getAllLaneAllowedVehicleTypes())
            {
                try {
                    SaveLaneAllowedVehicleTypes(new Configuration.LaneVehicleTypes(e.Key, e.Value), configuration);
                } catch (Exception ex) {
                    Log.Error($"Exception occurred while saving lane vehicle restrictions @ {e.Key}: {ex.ToString()}");
                }
            }

            var binaryFormatter = new BinaryFormatter();
            var memoryStream    = new MemoryStream();

            try {
                binaryFormatter.Serialize(memoryStream, configuration);
                memoryStream.Position = 0;
                Log.Info($"Save data byte length {memoryStream.Length}");
                _serializableData.SaveData(DataId, memoryStream.ToArray());

                // save options
                _serializableData.SaveData("TMPE_Options", new byte[] {
                    (byte)Options.simAccuracy,
                    (byte)0,                    //Options.laneChangingRandomization,
                    (byte)Options.recklessDrivers,
                    (byte)(Options.relaxedBusses ? 1 : 0),
                    (byte)(Options.nodesOverlay ? 1 : 0),
                    (byte)(Options.allowEnterBlockedJunctions ? 1 : 0),
                    (byte)(Options.advancedAI ? 1 : 0),
                    (byte)(Options.highwayRules ? 1 : 0),
                    (byte)(Options.prioritySignsOverlay ? 1 : 0),
                    (byte)(Options.timedLightsOverlay ? 1 : 0),
                    (byte)(Options.speedLimitsOverlay ? 1 : 0),
                    (byte)(Options.vehicleRestrictionsOverlay ? 1 : 0),
                    (byte)(Options.strongerRoadConditionEffects ? 1 : 0),
                    (byte)(Options.allowUTurns ? 1 : 0),
                    (byte)(Options.allowLaneChangesWhileGoingStraight ? 1 : 0),
                    (byte)(Options.enableDespawning ? 1 : 0),
                    (byte)(Options.IsDynamicPathRecalculationActive() ? 1 : 0),
                    (byte)(Options.connectedLanesOverlay ? 1 : 0)
                });
            } catch (Exception ex) {
                Log.Error("Unexpected error saving data: " + ex.Message);
            } finally {
                memoryStream.Close();
            }
        }
        public override void OnSaveData()
        {
            bool error = false;

            /*try {
             *      Log.Info("Recalculating segment geometries");
             *      SegmentGeometry.OnBeforeSaveData();
             * } catch (Exception e) {
             *      Log.Error($"OnSaveData: Exception occurred while calling SegmentGeometry.OnBeforeSaveData: {e.ToString()}");
             *      error = true;
             * }*/

            try {
                Log.Info("Applying all flags");
                Flags.applyAllFlags();
            } catch (Exception e) {
                Log.Error($"OnSaveData: Exception occurred while applying all flags: {e.ToString()}");
                error = true;
            }

            try {
                Log.Info("Saving Mod Data.");
                var configuration = new Configuration();

                TrafficPriorityManager prioMan = TrafficPriorityManager.Instance();

                if (prioMan.TrafficSegments != null)
                {
                    for (ushort i = 0; i < Singleton <NetManager> .instance.m_segments.m_size; i++)
                    {
                        try {
                            SavePrioritySegment(i, configuration);
                        } catch (Exception e) {
                            Log.Error($"Exception occurred while saving priority segment @ {i}: {e.ToString()}");
                            error = true;
                        }

                        try {
                            SaveSegmentNodeFlags(i, configuration);
                        } catch (Exception e) {
                            Log.Error($"Exception occurred while saving segment node flags @ {i}: {e.ToString()}");
                            error = true;
                        }
                    }
                }

                TrafficLightSimulationManager tlsMan = TrafficLightSimulationManager.Instance();

                for (ushort i = 0; i < Singleton <NetManager> .instance.m_nodes.m_size; i++)
                {
                    /*if (TrafficLightSimulation.LightSimulationByNodeId != null) {
                     *      SaveTrafficLightSimulation(i, configuration);
                     * }*/

                    /*if (TrafficLightsManual.ManualSegments != null) {
                     *      SaveManualTrafficLight(i, configuration);
                     * }*/

                    TrafficLightSimulation sim = tlsMan.GetNodeSimulation(i);
                    if (sim != null && sim.IsTimedLight())
                    {
                        try {
                            SaveTimedTrafficLight(i, configuration);
                        } catch (Exception e) {
                            Log.Error($"Exception occurred while saving timed traffic light @ {i}: {e.ToString()}");
                            error = true;
                        }
                        // TODO save new traffic lights
                    }

                    try {
                        SaveNodeLights(i, configuration);
                    } catch (Exception e) {
                        Log.Error($"Exception occurred while saving node traffic light @ {i}: {e.ToString()}");
                        error = true;
                    }
                }

#if !TAM
                if (LoadingExtension.IsPathManagerCompatible)
                {
#endif
                for (uint i = 0; i < Singleton <NetManager> .instance.m_lanes.m_buffer.Length; i++)
                {
                    try {
                        SaveLaneData(i, configuration);
                    } catch (Exception e) {
                        Log.Error($"Exception occurred while saving lane data @ {i}: {e.ToString()}");
                        error = true;
                    }
                }
#if !TAM
            }
#endif

                foreach (KeyValuePair <uint, ushort> e in Flags.getAllLaneSpeedLimits())
                {
                    try {
                        SaveLaneSpeedLimit(new Configuration.LaneSpeedLimit(e.Key, e.Value), configuration);
                    } catch (Exception ex) {
                        Log.Error($"Exception occurred while saving lane speed limit @ {e.Key}: {ex.ToString()}");
                        error = true;
                    }
                }

                foreach (KeyValuePair <uint, ExtVehicleType> e in Flags.getAllLaneAllowedVehicleTypes())
                {
                    try {
                        SaveLaneAllowedVehicleTypes(new Configuration.LaneVehicleTypes(e.Key, e.Value), configuration);
                    } catch (Exception ex) {
                        Log.Error($"Exception occurred while saving lane vehicle restrictions @ {e.Key}: {ex.ToString()}");
                        error = true;
                    }
                }

                var binaryFormatter = new BinaryFormatter();
                var memoryStream    = new MemoryStream();

                try {
                    binaryFormatter.Serialize(memoryStream, configuration);
                    memoryStream.Position = 0;
                    Log.Info($"Save data byte length {memoryStream.Length}");
                    _serializableData.SaveData(DataId, memoryStream.ToArray());
                } catch (Exception ex) {
                    Log.Error("Unexpected error while saving data: " + ex.ToString());
                    error = true;
                } finally {
                    memoryStream.Close();
                }

                try {
                    // save options
                    _serializableData.SaveData("TMPE_Options", new byte[] {
                        (byte)Options.simAccuracy,
                        (byte)0,                        //Options.laneChangingRandomization,
                        (byte)Options.recklessDrivers,
                        (byte)(Options.relaxedBusses ? 1 : 0),
                        (byte)(Options.nodesOverlay ? 1 : 0),
                        (byte)(Options.allowEnterBlockedJunctions ? 1 : 0),
                        (byte)(Options.advancedAI ? 1 : 0),
                        (byte)(Options.highwayRules ? 1 : 0),
                        (byte)(Options.prioritySignsOverlay ? 1 : 0),
                        (byte)(Options.timedLightsOverlay ? 1 : 0),
                        (byte)(Options.speedLimitsOverlay ? 1 : 0),
                        (byte)(Options.vehicleRestrictionsOverlay ? 1 : 0),
                        (byte)(Options.strongerRoadConditionEffects ? 1 : 0),
                        (byte)(Options.allowUTurns ? 1 : 0),
                        (byte)(Options.allowLaneChangesWhileGoingStraight ? 1 : 0),
                        (byte)(Options.enableDespawning ? 1 : 0),
                        (byte)(Options.IsDynamicPathRecalculationActive() ? 1 : 0),
                        (byte)(Options.connectedLanesOverlay ? 1 : 0),
                        (byte)(Options.prioritySignsEnabled ? 1 : 0),
                        (byte)(Options.timedLightsEnabled ? 1 : 0),
                        (byte)(Options.customSpeedLimitsEnabled ? 1 : 0),
                        (byte)(Options.vehicleRestrictionsEnabled ? 1 : 0),
                        (byte)(Options.laneConnectorEnabled ? 1 : 0),
                        (byte)(Options.junctionRestrictionsOverlay ? 1 : 0),
                        (byte)(Options.junctionRestrictionsEnabled ? 1 : 0)
                    });
                } catch (Exception ex) {
                    Log.Error("Unexpected error while saving options: " + ex.Message);
                    error = true;
                }
            } catch (Exception e) {
                error = true;
                Log.Error($"Error occurred while saving data: {e.ToString()}");
                //UIView.library.ShowModal<ExceptionPanel>("ExceptionPanel").SetMessage("An error occurred while saving", "Traffic Manager: President Edition detected an error while saving. To help preventing future errors, please navigate to http://steamcommunity.com/sharedfiles/filedetails/?id=583429740 and follow the steps under 'In case problems arise'.", true);
            }
        }