private static void OnSaveData() { FastList <byte> data = new FastList <byte>(); try { SerializableDataExtension.WriteString(VehicleManagerMod._dataVersion, data); for (int index = 0; index < 16384; ++index) { if (!VehicleManagerMod.m_cachedVehicleData[index].IsEmpty) { SerializableDataExtension.WriteInt32(index, data); SerializableDataExtension.WriteInt32(VehicleManagerMod.m_cachedVehicleData[index].LastStopNewPassengers, data); SerializableDataExtension.WriteInt32(VehicleManagerMod.m_cachedVehicleData[index].LastStopGonePassengers, data); SerializableDataExtension.WriteInt32(VehicleManagerMod.m_cachedVehicleData[index].PassengersThisWeek, data); SerializableDataExtension.WriteInt32(VehicleManagerMod.m_cachedVehicleData[index].PassengersLastWeek, data); SerializableDataExtension.WriteInt32(VehicleManagerMod.m_cachedVehicleData[index].IncomeThisWeek, data); SerializableDataExtension.WriteInt32(VehicleManagerMod.m_cachedVehicleData[index].IncomeLastWeek, data); SerializableDataExtension.WriteFloatArray(VehicleManagerMod.m_cachedVehicleData[index].PassengerData, data); SerializableDataExtension.WriteFloatArray(VehicleManagerMod.m_cachedVehicleData[index].IncomeData, data); SerializableDataExtension.WriteUInt16(VehicleManagerMod.m_cachedVehicleData[index].CurrentStop, data); } } SerializableDataExtension.instance.SerializableData.SaveData(VehicleManagerMod._dataID, data.ToArray()); } catch (Exception ex) { Utils.LogError((object)("Error while saving vehicle data! " + ex.Message + " " + (object)ex.InnerException)); } }
private static VehicleInfo GetCargoVehicleInfo( VehicleManager instance, ushort cargoStation1, ushort cargoStation2, ItemClass.Service service, ItemClass.SubService subService, ItemClass.Level level) { var infoFrom = BuildingManager.instance.m_buildings.m_buffer[cargoStation1].Info; var infoTo = BuildingManager.instance.m_buildings.m_buffer[cargoStation2].Info; var fromOutsideToStation = infoFrom?.m_buildingAI is OutsideConnectionAI && infoFrom?.m_class?.m_subService == infoTo?.m_class?.m_subService; var cargoStationId = fromOutsideToStation ? cargoStation2 : cargoStation1; if (infoFrom?.m_class?.name == "Ferry Cargo Facility") //to support Cargo Ferries { level = ItemClass.Level.Level5; } if (!SerializableDataExtension.BuildingData().TryGetValue(cargoStationId, out var source) || source.Count <= 0) { return(instance.GetRandomVehicleInfo( ref Singleton <SimulationManager> .instance.m_randomizer, service, subService, level)); } var array = source.ToArray(); return(GetRandomVehicleInfoOverride(ref SimulationManager.instance.m_randomizer, service, subService, level, array)); }
private static bool TryLoadData(out Dictionary <ushort, OutsideConnectionSettings> settingsDict) { settingsDict = new Dictionary <ushort, OutsideConnectionSettings>(); Utils.Log("Begin load data."); byte[] buffer = SerializableDataExtension.instance.SerializableData.LoadData(_dataID); if (buffer == null) { Utils.Log("No related data found."); return(false); } int index = 0; try { var version = SerializableDataExtension.ReadUInt16(buffer, ref index); Utils.Log("Start deserialize data. Version: " + version + " bytes read: " + buffer.Length); if (version >= 1) { return(TryLoadData1(version, ref settingsDict, ref buffer, ref index)); } else { Utils.LogError("No protocol defined for version: " + version); } } catch (Exception ex) { Utils.LogError("Error while loading data! " + ex.Message + " " + (object)ex.InnerException); } return(false); }
private static void OnSaveData() { FastList <byte> data = new FastList <byte>(); try { SerializableDataExtension.WriteString(NetManagerMod._dataVersion, data); for (int index = 0; index < 32768; ++index) { if (!NetManagerMod.m_cachedNodeData[index].IsEmpty) { SerializableDataExtension.WriteInt32(index, data); SerializableDataExtension.WriteInt32(NetManagerMod.m_cachedNodeData[index].PassengersIn, data); SerializableDataExtension.WriteInt32(NetManagerMod.m_cachedNodeData[index].PassengersOut, data); SerializableDataExtension.WriteInt32(NetManagerMod.m_cachedNodeData[index].LastWeekPassengersIn, data); SerializableDataExtension.WriteInt32(NetManagerMod.m_cachedNodeData[index].LastWeekPassengersOut, data); SerializableDataExtension.WriteFloatArray(NetManagerMod.m_cachedNodeData[index].PassengerInData, data); SerializableDataExtension.WriteFloatArray(NetManagerMod.m_cachedNodeData[index].PassengerOutData, data); SerializableDataExtension.WriteBool(NetManagerMod.m_cachedNodeData[index].Unbunching, data); } } SerializableDataExtension.instance.SerializableData.SaveData(NetManagerMod._dataID, data.ToArray()); } catch (Exception ex) { Utils.LogError((object)("Error while saving net node data! " + ex.Message + " " + (object)ex.InnerException)); } }
private static void OnSaveData() { Utils.Log("Begin save data."); FastList <byte> buffer = new FastList <byte>(); try { SerializableDataExtension.WriteUInt16(_dataVersion, buffer); SerializableDataExtension.WriteInt32(instance.SettingsDict.Count, buffer); foreach (var keyValue in instance.SettingsDict) { FastList <byte> elementBuffer = new FastList <byte>(); SerializableDataExtension.WriteUInt16(keyValue.Key, elementBuffer); SerializableDataExtension.WriteUInt16((ushort)keyValue.Value.NameMode, elementBuffer); SerializableDataExtension.WriteString(keyValue.Value.SingleGenerationName, elementBuffer); SerializableDataExtension.WriteUInt16((ushort)keyValue.Value.RandomGenerationNames.Count, elementBuffer); foreach (var str in keyValue.Value.RandomGenerationNames) { SerializableDataExtension.WriteString(str, elementBuffer); } SerializableDataExtension.WriteUInt16((ushort)keyValue.Value.CurrentDirectionFlags, elementBuffer); SerializableDataExtension.WriteString(keyValue.Value.Name, elementBuffer); SerializableDataExtension.WriteInt32(keyValue.Value.DummyTrafficFactor, elementBuffer); // addition with version 2 SerializableDataExtension.WriteUInt16((ushort)keyValue.Value.TouristFactors.Length, elementBuffer); foreach (var value in keyValue.Value.TouristFactors) { SerializableDataExtension.WriteInt32(value, elementBuffer); } SerializableDataExtension.WriteUInt16((ushort)keyValue.Value.ImportResourceRatio.Length, elementBuffer); foreach (var value in keyValue.Value.ImportResourceRatio) { SerializableDataExtension.WriteInt32(value, elementBuffer); } SerializableDataExtension.WriteUInt16((ushort)keyValue.Value.ExportResourceRatio.Length, elementBuffer); foreach (var value in keyValue.Value.ExportResourceRatio) { SerializableDataExtension.WriteInt32(value, elementBuffer); } SerializableDataExtension.WriteInt32(keyValue.Value.CargoCapacity, elementBuffer); SerializableDataExtension.WriteInt32(keyValue.Value.ResidentCapacity, elementBuffer); SerializableDataExtension.WriteByteArray(elementBuffer, buffer); } SerializableDataExtension.instance.SerializableData.SaveData(_dataID, buffer.ToArray()); Utils.Log("End save data. Version: " + _dataVersion + " bytes written: " + buffer.m_size); } catch (Exception ex) { Utils.LogError("Error while saving data! " + ex.Message + " " + (object)ex.InnerException); } }
private static void OnSaveData() { FastList <byte> data = new FastList <byte>(); try { SerializableDataExtension.WriteString(TransportLineMod._dataVersion, data); for (ushort lineID = 0; (int)lineID < 256; ++lineID) { SerializableDataExtension.AddToData( BitConverter.GetBytes(TransportLineMod.GetTargetVehicleCount(lineID)), data); SerializableDataExtension.AddToData( BitConverter.GetBytes(Mathf.Max( TransportLineMod.GetNextSpawnTime(lineID) - SimHelper.SimulationTime, 0.0f)), data); SerializableDataExtension.AddToData( BitConverter.GetBytes(TransportLineMod.GetBudgetControlState(lineID)), data); SerializableDataExtension.AddToData(BitConverter.GetBytes(TransportLineMod.GetDepot(lineID)), data); int num = 0; HashSet <string> prefabs = TransportLineMod.GetPrefabs(lineID); if (prefabs != null) { num = prefabs.Count; } SerializableDataExtension.AddToData(BitConverter.GetBytes(num), data); if (num > 0) { foreach (string s in prefabs) { SerializableDataExtension.WriteString(s, data); } } string[] enqueuedVehicles = TransportLineMod.GetEnqueuedVehicles(lineID); SerializableDataExtension.AddToData(BitConverter.GetBytes(enqueuedVehicles.Length), data); if (enqueuedVehicles.Length != 0) { foreach (string s in enqueuedVehicles) { SerializableDataExtension.WriteString(s, data); } } SerializableDataExtension.WriteBool(TransportLineMod.GetUnbunchingState(lineID), data); } SerializableDataExtension.instance.SerializableData.SaveData(TransportLineMod._dataID, data.ToArray()); } catch (Exception ex) { string msg = "Error while saving transport line data! " + ex.Message + " " + (object)ex.InnerException; Utils.LogError((object)msg); CODebugBase <LogChannel> .Log(LogChannel.Modding, msg, ErrorLevel.Error); } }
public static bool TryLoadData(out VehicleData[] data) { data = new VehicleData[16384]; byte[] data1 = SerializableDataExtension.instance.SerializableData.LoadData(VehicleManagerMod._dataID); if (data1 == null) { return(false); } int index1 = 0; string empty = string.Empty; try { Utils.Log((object)"Try to load vehicle data."); string str = SerializableDataExtension.ReadString(data1, ref index1); if (string.IsNullOrEmpty(str) || str.Length != 4) { Utils.LogWarning((object)"Unknown data found."); return(false); } Utils.Log((object)("Found vehicle data version: " + str)); while (index1 < data1.Length) { int index2 = SerializableDataExtension.ReadInt32(data1, ref index1); if (str == "v001") { int num = (int)SerializableDataExtension.ReadByte(data1, ref index1); } data[index2].LastStopNewPassengers = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].LastStopGonePassengers = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].PassengersThisWeek = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].PassengersLastWeek = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].IncomeThisWeek = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].IncomeLastWeek = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].PassengerData = SerializableDataExtension.ReadFloatArray(data1, ref index1); data[index2].IncomeData = SerializableDataExtension.ReadFloatArray(data1, ref index1); if (str != "v001" && str != "v002") { data[index2].CurrentStop = SerializableDataExtension.ReadUInt16(data1, ref index1); } } return(true); } catch (Exception ex) { Utils.LogWarning((object)("Could not load vehicle data. " + ex.Message)); data = new VehicleData[16384]; return(false); } }
private static VehicleInfo GetVehicleInfoWithoutType( VehicleManager instance, ushort buildingID, ItemClass.Service service, ItemClass.SubService subService, ItemClass.Level level) { if (!SerializableDataExtension.BuildingData().TryGetValue(buildingID, out var source) || source.Count <= 0) { return(instance.GetRandomVehicleInfo(ref SimulationManager.instance.m_randomizer, service, subService, level)); } return(GetRandomVehicleInfoOverride(ref SimulationManager.instance.m_randomizer, service, subService, level, source.ToArray())); }
public static bool TryLoadData(out NodeData[] data) { data = new NodeData[32768]; byte[] data1 = SerializableDataExtension.instance.SerializableData.LoadData(NetManagerMod._dataID); if (data1 == null) { return(false); } int index1 = 0; string empty = string.Empty; try { Utils.Log((object)"Try to load net node data."); string str = SerializableDataExtension.ReadString(data1, ref index1); if (string.IsNullOrEmpty(str) || str.Length != 4) { Utils.LogWarning((object)"Unknown data found."); return(false); } Utils.Log((object)("Found net node data version: " + str)); while (index1 < data1.Length) { int index2 = SerializableDataExtension.ReadInt32(data1, ref index1); if (str == "v001") { double num = (double)SerializableDataExtension.ReadFloat(data1, ref index1); } data[index2].PassengersIn = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].PassengersOut = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].LastWeekPassengersIn = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].LastWeekPassengersOut = SerializableDataExtension.ReadInt32(data1, ref index1); data[index2].PassengerInData = SerializableDataExtension.ReadFloatArray(data1, ref index1); data[index2].PassengerOutData = SerializableDataExtension.ReadFloatArray(data1, ref index1); data[index2].Unbunching = str == "v001" || str == "v002" || SerializableDataExtension.ReadBool(data1, ref index1); } return(true); } catch (Exception ex) { Utils.LogWarning((object)("Could not load net node data. " + ex.Message)); data = new NodeData[32768]; return(false); } }
public static void SimulationStep() { if (timeUp) { timeUp = false; try { SerializableDataExtension.LoadDataState(config,true, out bool error2); if (error2) throw new Exception("Tmpe: error when applying loaded data"); } catch (Exception e) { Debug.LogWarning(e); } } }
/* Called when an asset is saved */ public void OnAssetSaved(string name, object asset, out Dictionary <string, byte[]> userData) { Debug.Log("TMPE: OnAssetSaved"); userData = null; if (!LoadingExtension.IsInAssetEditor) { return; } BuildingInfo info = asset as BuildingInfo; if (info == null) { return; } bool success = true; TmpeAssetData data = new TmpeAssetData(); data.Segments = BuildingDecorationPatch.m_pendingSegments; Configuration config = SerializableDataExtension.CreateConfiguration(ref success); userData = new Dictionary <string, byte[]>(); // ??? !!! SerializableDataExtension.OnBeforeGameSaved(ref success); data.TmpeConfiguration = SerializableDataExtension.Serialize(config, ref success); SerializableDataExtension.OnAfterGameSaved(ref success); if (!success) { return; } if (BuildingDecorationPatch.m_pendingSegments?.Count > 0) { userData.Add(SerializableDataExtension.DataId + segmentsString, SerializableDataExtension.Serialize <List <IdentificationTool.AssetSegment> >(data.Segments, ref success)); userData.Add(SerializableDataExtension.DataId + dataString, data.TmpeConfiguration); AssetDataManager.Instance.AssetsWithData[IdentificationTool.GetNameWithoutPrefix(info.name)] = data; } }
private static VehicleInfo GetVehicleInfoForDepot( //only for taxi and cable cars - and only for 1 of 4 cases VehicleManager instance, ushort buildingID, ItemClass.Service service, ItemClass.SubService subService, ItemClass.Level level) { if ( service != ItemClass.Service.PublicTransport || subService != ItemClass.SubService.PublicTransportTaxi && subService != ItemClass.SubService.PublicTransportCableCar || !SerializableDataExtension.BuildingData().TryGetValue(buildingID, out var source) || source.Count <= 0) { return(instance.GetRandomVehicleInfo(ref SimulationManager.instance.m_randomizer, service, subService, level)); } return(GetRandomVehicleInfoOverride(ref SimulationManager.instance.m_randomizer, service, subService, level, source.ToArray())); }
public static List <IdentificationTool.AssetSegment> m_pendingSegments; // Segments pending to be saved public static void ProcessNewPaths(List <ushort> segments, BuildingInfo info) { try { #if DEBUG Debug.Log("Loading TMPE settings..."); #endif TmpeAssetData data = AssetDataManager.Instance.AssetsWithData[IdentificationTool.GetNameWithoutPrefix(info.name)]; Debug.Log("Hoooray! Tmpe settings loaded"); Configuration config = SerializableDataExtension.DeserializeData(data.TmpeConfiguration, out bool error); if (error) { throw new Exception("Tmpe: Failed to deserialize data"); } /* We identify the old ids with the new ids */ IdentificationTool.CreateDictionaries(segments, data.Segments, out Dictionary <ushort, ushort> segmentPairs, out Dictionary <ushort, ushort> nodePairs, out Dictionary <uint, uint> lanePairs); IdentificationTool.TranslateGameConfiguration(config, segmentPairs, nodePairs, lanePairs); /*SerializableDataExtension.LoadDataState(config, out bool error2); * if (error2) * throw new Exception("Tmpe: error when applying loaded data");*/ /* Apply settings after some time elapses (hack) */ ProvisionalDataLoader.StartTimer(config); } catch (Exception ex) { #if DEBUG Debug.Log(ex); #endif } }
private static bool TryLoadData1(int version, ref Dictionary <ushort, OutsideConnectionSettings> settingsDict, ref byte[] buffer, ref int index) { var length = SerializableDataExtension.ReadInt32(buffer, ref index); for (int i = 0; i < length; ++i) { var curElementLength = SerializableDataExtension.ReadInt32(buffer, ref index); var indexBegin = index; var settings = new OutsideConnectionSettings(); var buildingID = SerializableDataExtension.ReadUInt16(buffer, ref index); settings.NameMode = (OutsideConnectionSettings.NameModeType)SerializableDataExtension.ReadUInt16(buffer, ref index); settings.SingleGenerationName = SerializableDataExtension.ReadString(buffer, ref index); var strCount = SerializableDataExtension.ReadUInt16(buffer, ref index); for (int j = 0; j < strCount; ++j) { settings.RandomGenerationNames.Add(SerializableDataExtension.ReadString(buffer, ref index)); } settings.CurrentDirectionFlags = (Building.Flags)SerializableDataExtension.ReadUInt16(buffer, ref index) & Building.Flags.IncomingOutgoing; settings.Name = SerializableDataExtension.ReadString(buffer, ref index); var flags = Utils.QueryBuilding(buildingID).m_flags; settings.OriginalDirectionFlags = flags & Building.Flags.IncomingOutgoing; flags &= (~Building.Flags.IncomingOutgoing) | settings.CurrentDirectionFlags; BuildingManager.instance.m_buildings.m_buffer[buildingID].m_flags = flags; if (2 <= version) { settings.DummyTrafficFactor = SerializableDataExtension.ReadInt32(buffer, ref index); } if (3 <= version) { // even if length is not equal 3, the data must be read from buffer var touristFactorLength = SerializableDataExtension.ReadUInt16(buffer, ref index); var factors = new int[touristFactorLength]; for (ushort k = 0; k < touristFactorLength; ++k) { factors[k] = SerializableDataExtension.ReadInt32(buffer, ref index); } settings.TouristFactors = factors; } if (4 <= version) { var ratioLength = SerializableDataExtension.ReadUInt16(buffer, ref index); var ratios = new int[ratioLength]; for (ushort k = 0; k < ratioLength; ++k) { ratios[k] = SerializableDataExtension.ReadInt32(buffer, ref index); } settings.ImportResourceRatio = ratios; ratioLength = SerializableDataExtension.ReadUInt16(buffer, ref index); ratios = new int[ratioLength]; for (ushort k = 0; k < ratioLength; ++k) { ratios[k] = SerializableDataExtension.ReadInt32(buffer, ref index); } settings.ExportResourceRatio = ratios; } if (5 <= version) { settings.CargoCapacity = SerializableDataExtension.ReadInt32(buffer, ref index); settings.ResidentCapacity = SerializableDataExtension.ReadInt32(buffer, ref index); } ValidateOutsideConnectionSettings(buildingID, ref settings); if (settingsDict.ContainsKey(buildingID)) { Utils.LogWarning("Overrides existing outside connection with buildingID: " + buildingID); } settingsDict[buildingID] = settings; } return(true); }
public static bool TryLoadData(out LineData[] data) { data = new LineData[256]; byte[] data1 = SerializableDataExtension.instance.SerializableData.LoadData(TransportLineMod._dataID); if (data1 == null) { return(false); } int index1 = 0; ushort lineID = 0; string empty = string.Empty; try { Utils.Log((object)"Try to load transport line data."); string str = SerializableDataExtension.ReadString(data1, ref index1); if (string.IsNullOrEmpty(str) || str.Length != 4) { Utils.LogWarning((object)"Unknown data found."); return(false); } Utils.Log((object)("Found transport line data version: " + str)); NetManager instance1 = Singleton <NetManager> .instance; TransportManager instance2 = Singleton <TransportManager> .instance; while (index1 < data1.Length) { if (instance2.m_lines.m_buffer[(int)lineID].Complete) { int int32 = BitConverter.ToInt32(data1, index1); data[(int)lineID].TargetVehicleCount = int32; } index1 += 4; float num = Mathf.Min(BitConverter.ToSingle(data1, index1), (float)OptionsWrapper <Settings> .Options.SpawnTimeInterval); if ((double)num > 0.0) { data[(int)lineID].NextSpawnTime = SimHelper.SimulationTime + num; } index1 += 4; bool boolean = BitConverter.ToBoolean(data1, index1); data[(int)lineID].BudgetControl = boolean; ++index1; ushort uint16 = BitConverter.ToUInt16(data1, index1); data[(int)lineID].Depot = (int)uint16 != 0 ? uint16 : TransportLineMod.GetClosestDepot(lineID, instance1.m_nodes.m_buffer[(int)instance2.m_lines.m_buffer[(int)lineID].GetStop(0)] .m_position); index1 += 2; if (str == "v001") { string name = SerializableDataExtension.ReadString(data1, ref index1); if (name != "Random") { if (data[(int)lineID].Prefabs == null) { data[(int)lineID].Prefabs = new HashSet <string>(); } if ((UnityEngine.Object)PrefabCollection <VehicleInfo> .FindLoaded(name) != (UnityEngine.Object)null) { data[(int)lineID].Prefabs.Add(name); } } } else { int int32 = BitConverter.ToInt32(data1, index1); index1 += 4; for (int index2 = 0; index2 < int32; ++index2) { string name = SerializableDataExtension.ReadString(data1, ref index1); if (data[(int)lineID].Prefabs == null) { data[(int)lineID].Prefabs = new HashSet <string>(); } if ((UnityEngine.Object)PrefabCollection <VehicleInfo> .FindLoaded(name) != (UnityEngine.Object)null) { data[(int)lineID].Prefabs.Add(name); } } } if (str != "v001") { int int32 = BitConverter.ToInt32(data1, index1); index1 += 4; for (int index2 = 0; index2 < int32; ++index2) { string name = SerializableDataExtension.ReadString(data1, ref index1); if (!boolean) { if (data[(int)lineID].QueuedVehicles == null) { data[(int)lineID].QueuedVehicles = new Queue <string>(); } if ((UnityEngine.Object)PrefabCollection <VehicleInfo> .FindLoaded(name) != (UnityEngine.Object)null) { lock (data[(int)lineID].QueuedVehicles) data[(int)lineID].QueuedVehicles.Enqueue(name); } } } } if (str == "v003") { ++index1; } data[(int)lineID].Unbunching = str != "v004" ? OptionsWrapper <Settings> .Options.Unbunching : SerializableDataExtension.ReadBool(data1, ref index1); ++lineID; } return(true); } catch (Exception ex) { Utils.LogWarning((object)("Could not load transport line data. " + ex.Message)); data = new LineData[256]; return(false); } }