public static void Serialize(string filename, Configuration config) { config.ComputeHashCodes(); Debug.Log("serializing to " + filename); Debug.Log(config.ToString()); var serializer = new XmlSerializer(typeof(Configuration)); RegisterEvents(ref serializer); using (var writer = new StreamWriter(filename)) { config.OnPreSerialize(); serializer.Serialize(writer, config); } }
public static void LoadAIParameters(Configuration.ImprovedAIConfig parameters) { m_congestionCostFactor = parameters.congestionCostFactor; m_minLaneSpace = parameters.minLaneSpace; m_lookaheadLanes = parameters.lookaheadLanes; m_congestedLaneThreshold = parameters.congestedLaneThreshold; m_obeyTMLanes = parameters.obeyTMLaneFlags; }
public void OnSaveData() { FastList<byte> data = new FastList<byte>(); // Debug.Log("OnSaveData() 1"); GenerateUniqueID(); byte[] uniqueIdBytes = BitConverter.GetBytes(uniqueID); foreach (byte uniqueIdByte in uniqueIdBytes) { data.Add(uniqueIdByte); } // Debug.Log("OnSaveData() 2"); byte[] dataToSave = data.ToArray(); SerializableData.SaveData(dataID, dataToSave); // Debug.Log("OnSaveData() 3"); var filepath = Path.Combine(Application.dataPath, "trafficManagerSave_" + uniqueID + ".xml"); // Debug.Log("OnSaveData()"); var configuration = new Configuration(); // Debug.Log("OnSaveData() 4"); configuration.laneFlags = ""; configuration.nodeCrosswalk = ""; configuration.nodeTrafficLights = ""; for (var i = 0; i < 32768; i++) { if (TrafficPriority.prioritySegments.ContainsKey(i)) { if (TrafficPriority.prioritySegments[i].node_1 != 0) { configuration.prioritySegments.Add(new int[3] { TrafficPriority.prioritySegments[i].node_1, i, (int)TrafficPriority.prioritySegments[i].instance_1.type }); } //Debug.Log("OnSaveData() 5"); if (TrafficPriority.prioritySegments[i].node_2 != 0) { configuration.prioritySegments.Add(new int[3] { TrafficPriority.prioritySegments[i].node_2, i, (int)TrafficPriority.prioritySegments[i].instance_2.type }); } } //Debug.Log("OnSaveData() 6"); if (CustomRoadAI.nodeDictionary.ContainsKey((ushort)i)) { var nodeDict = CustomRoadAI.nodeDictionary[(ushort)i]; configuration.nodeDictionary.Add(new int[4] { nodeDict.NodeId, Convert.ToInt32(nodeDict._manualTrafficLights), Convert.ToInt32(nodeDict._timedTrafficLights), Convert.ToInt32(nodeDict.TimedTrafficLightsActive) }); } // Debug.Log("OnSaveData() 7"); if (TrafficLightsManual.ManualSegments.ContainsKey(i)) { if (TrafficLightsManual.ManualSegments[i].node_1 != 0) { var manualSegment = TrafficLightsManual.ManualSegments[i].instance_1; configuration.manualSegments.Add(new int[10] { (int)manualSegment.node, manualSegment.segment, (int)manualSegment.currentMode, (int)manualSegment.lightLeft, (int)manualSegment.lightMain, (int)manualSegment.lightRight, (int)manualSegment.lightPedestrian, (int)manualSegment.lastChange, (int)manualSegment.lastChangeFrame, Convert.ToInt32(manualSegment.pedestrianEnabled) }); } //Debug.Log("OnSaveData() 8"); if (TrafficLightsManual.ManualSegments[i].node_2 != 0) { var manualSegment = TrafficLightsManual.ManualSegments[i].instance_2; //Debug.Log("OnSaveData() 9"); configuration.manualSegments.Add(new int[10] { (int)manualSegment.node, manualSegment.segment, (int)manualSegment.currentMode, (int)manualSegment.lightLeft, (int)manualSegment.lightMain, (int)manualSegment.lightRight, (int)manualSegment.lightPedestrian, (int)manualSegment.lastChange, (int)manualSegment.lastChangeFrame, Convert.ToInt32(manualSegment.pedestrianEnabled) }); } } // Debug.Log("OnSaveData() 10"); if (TrafficLightsTimed.timedScripts.ContainsKey((ushort)i)) { var timedNode = TrafficLightsTimed.GetTimedLight((ushort)i); configuration.timedNodes.Add(new int[4] { timedNode.nodeID, timedNode.currentStep, timedNode.NumSteps(), Convert.ToInt32(timedNode.isStarted()) }); var nodeGroup = new ushort[timedNode.nodeGroup.Count]; for (var j = 0; j < timedNode.nodeGroup.Count; j++) { nodeGroup[j] = timedNode.nodeGroup[j]; } configuration.timedNodeGroups.Add(nodeGroup); for (var j = 0; j < timedNode.NumSteps(); j++) { configuration.timedNodeSteps.Add(new int[2] { timedNode.steps[j].numSteps, timedNode.steps[j].segments.Count }); for (var k = 0; k < timedNode.steps[j].segments.Count; k++) { configuration.timedNodeStepSegments.Add(new int[4] { (int)timedNode.steps[j].lightLeft[k], (int)timedNode.steps[j].lightMain[k], (int)timedNode.steps[j].lightRight[k], (int)timedNode.steps[j].lightPedestrian[k], }); } } } } //Debug.Log("OnSaveData() 11"); for (var i = 0; i < Singleton<NetManager>.instance.m_nodes.m_buffer.Length; i++) { var nodeFlags = Singleton<NetManager>.instance.m_nodes.m_buffer[i].m_flags; if (nodeFlags != 0) { if (Singleton<NetManager>.instance.m_nodes.m_buffer[i].Info.m_class.m_service == ItemClass.Service.Road) { configuration.nodeTrafficLights += Convert.ToInt16((nodeFlags & NetNode.Flags.TrafficLights) != NetNode.Flags.None); configuration.nodeCrosswalk += Convert.ToInt16((nodeFlags & NetNode.Flags.Junction) != NetNode.Flags.None); } } } //Debug.Log("OnSaveData() 12"); var laneCount = 0; 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 (TrafficPriority.prioritySegments.ContainsKey(laneSegment)) { configuration.laneFlags += i + ":" + Singleton<NetManager>.instance.m_lanes.m_buffer[i].m_flags + ","; laneCount++; } } if (configuration.laneFlags != null && configuration.laneFlags.Length > 0) { configuration.laneFlags = configuration.laneFlags.TrimEnd(','); } configuration.aiConfig.congestionCostFactor = CustomPathFind.congestionCostFactor; configuration.aiConfig.minLaneSpace = CustomPathFind.minLaneSpace; configuration.aiConfig.lookaheadLanes = CustomPathFind.lookaheadLanes; configuration.aiConfig.congestedLaneThreshold = CustomPathFind.congestedLaneThreshold; configuration.aiConfig.obeyTMLaneFlags = CustomPathFind.obeyTMLaneFlags; for (var i = 0; i < CSL_Traffic.RoadManager.sm_lanes.Length; i++) { var lane = CSL_Traffic.RoadManager.sm_lanes[i]; if (lane != null && lane.ConnectionCount() > 0) { configuration.laneMarkers.Add(lane); } } Configuration.Serialize(filepath, configuration); }
public static void Serialize(ISerializableData serializableData, string dataID, Configuration config) { config.ComputeHashCodes(); Debug.Log("serializing to save data"); Debug.Log(config.ToString()); var serializer = new XmlSerializer(typeof(Configuration)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); RegisterEvents(ref serializer); byte[] configData; using (var memoryStream = new MemoryStream()) { config.OnPreSerialize(); serializer.Serialize(memoryStream, config, ns); configData = memoryStream.ToArray(); } serializableData.SaveData(dataID, configData); }