-
Notifications
You must be signed in to change notification settings - Fork 4
/
CargoData.cs
255 lines (231 loc) · 9.24 KB
/
CargoData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using CargoInfoMod.Data;
using ColossalFramework.Plugins;
using ICities;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using TransferType = TransferManager.TransferReason;
namespace CargoInfoMod
{
public struct CargoParcel
{
public ushort building;
public ushort transferSize;
public CarFlags flags;
public int ResourceType => ((flags & CarFlags.Resource) - CarFlags.Oil) / 0x10;
public static readonly CarFlags[] ResourceTypes =
{
CarFlags.Oil,
CarFlags.Petrol,
CarFlags.Ore,
CarFlags.Coal,
CarFlags.Logs,
CarFlags.Lumber,
CarFlags.Grain,
CarFlags.Food,
CarFlags.Goods
};
public CargoParcel(ushort buildingID, bool incoming, byte transferType, ushort transferSize, Vehicle.Flags flags)
{
this.transferSize = transferSize;
this.building = buildingID;
this.flags = incoming ? CarFlags.None: CarFlags.Sent;
if ((flags & Vehicle.Flags.Exporting) != 0)
this.flags |= CarFlags.Exported;
else if ((flags & Vehicle.Flags.Importing) != 0)
this.flags |= CarFlags.Imported;
switch ((TransferType)transferType)
{
case TransferType.Oil:
this.flags |= CarFlags.Oil;
break;
case TransferType.Ore:
this.flags |= CarFlags.Ore;
break;
case TransferType.Logs:
this.flags |= CarFlags.Logs;
break;
case TransferType.Grain:
this.flags |= CarFlags.Grain;
break;
case TransferType.Petrol:
this.flags |= CarFlags.Petrol;
break;
case TransferType.Coal:
this.flags |= CarFlags.Coal;
break;
case TransferType.Lumber:
this.flags |= CarFlags.Lumber;
break;
case TransferType.Food:
this.flags |= CarFlags.Food;
break;
case TransferType.Goods:
this.flags |= CarFlags.Goods;
break;
default:
Debug.LogErrorFormat("Unexpected transfer type: {0}", Enum.GetName(typeof(TransferType), transferType));
break;
}
}
}
public class CargoData : SerializableDataExtensionBase
{
public static CargoData Instance;
public const float TruckCapacity = 8000f;
private ModInfo mod;
private Dictionary<ushort, CargoStats2> cargoStatIndex;
private HashSet<int> cargoStations;
public CargoData()
{
cargoStations = new HashSet<int>();
cargoStatIndex = new Dictionary<ushort, CargoStats2>();
Instance = this;
}
public override void OnCreated(ISerializableData serializedData)
{
base.OnCreated(serializedData);
mod = PluginManager.instance.FindPluginInfo(Assembly.GetExecutingAssembly()).userModInstance as ModInfo;
if (LoadingManager.instance.m_loadingComplete)
{
OnLoadData();
Setup();
}
if (mod != null)
mod.data = this;
else
Debug.LogError("Could not find parent IUserMod!");
}
public override void OnLoadData()
{
Debug.Log("Restoring previous data...");
var data = serializableDataManager.LoadData(ModInfo.Namespace);
if (data == null)
{
Debug.Log("No previous data found");
return;
}
var ms = new MemoryStream(data);
try
{
// Try deserialize older data format first
var binaryFormatter = new BinaryFormatter();
var indexData = binaryFormatter.Deserialize(ms);
if (indexData is Dictionary<ushort, CargoStats>)
{
Debug.LogWarning("Loaded v1 data, upgrading...");
cargoStatIndex = ((Dictionary<ushort, CargoStats>) indexData).ToDictionary(kv => kv.Key, kv => new CargoStats2
{
CarsCounted =
{
[(int) (CarFlags.Previous | CarFlags.Goods | CarFlags.Sent)] = kv.Value.carsSentLastTime * (int)TruckCapacity,
[(int) (CarFlags.Previous | CarFlags.Goods)] = kv.Value.carsReceivedLastTime * (int)TruckCapacity
}
});
}
else if (indexData is Dictionary<ushort, CargoStats2>)
{
Debug.Log("Loaded v2 data");
cargoStatIndex = (Dictionary<ushort, CargoStats2>) indexData;
}
}
catch (SerializationException e)
{
Debug.LogErrorFormat("While trying to load data: {0}", e.ToString());
}
Debug.LogFormat("Loaded stats for {0} stations", cargoStatIndex.Count);
}
public void Setup()
{
Debug.Log("Looking up cargo station prefabs...");
cargoStations.Clear();
for (uint i = 0; i < PrefabCollection<BuildingInfo>.LoadedCount(); i++)
{
var prefab = PrefabCollection<BuildingInfo>.GetLoaded(i);
if (prefab == null)
{
Debug.LogWarningFormat("Uninitialized building prefab #{0}! (this should not happen)", i);
continue;
}
if (prefab.m_buildingAI is CargoStationAI)
{
Debug.LogFormat("Cargo station prefab found: {0}", prefab.name);
cargoStations.Add(prefab.m_prefabDataIndex);
}
}
Debug.LogFormat("Found {0} cargo station prefabs", cargoStations.Count);
for (ushort i = 0; i < BuildingManager.instance.m_buildings.m_size; i++)
{
AddBuilding(i);
}
BuildingManager.instance.EventBuildingCreated += AddBuilding;
BuildingManager.instance.EventBuildingReleased += RemoveBuilding;
}
public override void OnSaveData()
{
Debug.Log("Saving data...");
try
{
var ms = new MemoryStream();
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(ms, cargoStatIndex);
serializableDataManager.SaveData(ModInfo.Namespace, ms.ToArray());
Debug.LogFormat("Saved stats for {0} stations", cargoStatIndex.Count);
}
catch (SerializationException e)
{
Debug.LogError("While serializing data: " + e.Message);
}
}
public void AddBuilding(ushort buildingID)
{
var building = BuildingManager.instance.m_buildings.m_buffer[buildingID];
if (cargoStations.Contains(building.m_infoIndex) && !cargoStatIndex.ContainsKey(buildingID))
{
var buildingName = BuildingManager.instance.GetBuildingName(buildingID, InstanceID.Empty);
// Restoring previous values of truck statistics
cargoStatIndex.Add(buildingID, new CargoStats2());
Debug.LogFormat("Cargo station added to index: {0}", buildingName);
}
}
public void RemoveBuilding(ushort buildingID)
{
if (cargoStatIndex.ContainsKey(buildingID))
{
var buildingName = BuildingManager.instance.GetBuildingName(buildingID, InstanceID.Empty);
cargoStatIndex.Remove(buildingID);
Debug.LogFormat("Cargo station removed from index: {0}", buildingName);
}
}
public void UpdateCounters()
{
foreach (var pair in cargoStatIndex)
{
for (var i = 0; i < pair.Value.CarsCounted.Length; i++)
{
var previ = (int) ((CarFlags) i | CarFlags.Previous);
if (previ == i) continue;
pair.Value.CarsCounted[previ] = pair.Value.CarsCounted[i];
pair.Value.CarsCounted[i] = 0;
}
}
}
public bool TryGetEntry(ushort building, out CargoStats2 stats)
{
return cargoStatIndex.TryGetValue(building, out stats);
}
public void Count(CargoParcel cargo)
{
if (cargo.building == 0 ||
!(BuildingManager.instance.m_buildings.m_buffer[cargo.building].Info.m_buildingAI is CargoStationAI))
return;
if (cargoStatIndex.TryGetValue(cargo.building, out CargoStats2 stats))
stats.CarsCounted[(int)cargo.flags] += cargo.transferSize;
}
}
}