fadster / TrafficManager_ImprovedAI

Original Traffic Manager with improved AI from Traffic++.
7 stars 3 forks source link

Store save data in city save file #6

Open boformer opened 8 years ago

boformer commented 8 years ago

I think it would be better to store the mod save data in the crp file instead of spamming xml files in the Cities_Data folder.

The Mod API supports that. I just updated Building Themes to store the save data in the crp.

Basically you just have to supply the XMLSerializer with a MemoryStream instead of a StreamWriter.

Then you can get the byte[] representation of the stream and save it using the mod API:

TrafficManagerData theData = ...;
byte[] dataBytes;

var xmlSerializer = new XmlSerializer(typeof(TrafficManagerData));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

using (var memoryStream = new MemoryStream())
{
    xmlSerializer.Serialize(memoryStream, theData, ns);
    dataBytes = memoryStream.ToArray();
}

if (propDataBytes != null) 
{
    gameData.SaveData("TrafficManagerData", dataBytes);
}

Loading:

TrafficManagerData theData = null;
byte[] propDataBytes = gameData.LoadData("TrafficManagerData");

if (propDataBytes != null)
{
    var xmlSerializer = new XmlSerializer(typeof(TrafficManagerData));
    using (var memoryStream = new MemoryStream(propDataBytes))
    {
        theData = xmlSerializer.Deserialize(new MemoryStream(propDataBytes)) as TrafficManagerData;
    }
}

The old data loading should be kept for a smooth transition to the new serialization system.

fadster commented 8 years ago

Sorry, I've been away from CSL for a while. Thanks for the code! I'm a little concerned about merging the data with the main save file. Won't that cause problems if the mod is ever deactivated later on?

boformer commented 8 years ago

No. The data will just be dropped when you save the city without the mod (just like it is now).

fadster commented 8 years ago

Interesting, thanks for the info. I'll try it out and, if all is well, I'll implement it.

fadster commented 8 years ago

In your loading code, you call new MemoryStream(propDataBytes) twice instead of using the memoryStream variable from the using statement. Is that normal? I noticed you have the same code in BuildingThemes.

fadster commented 8 years ago

I implemented the new save system and everything works normally. I then deactivated the mod to make sure a game saved with TM data would still load correctly with the mod disabled. This worked as expected. I then saved the game at this point and tried loading it again but, this time, with the mod enabled. I expected the city to load without any TM data, but to my surprise, the data that had been saved before I deactivated the mod was still there. This could cause errors if changes to the road infrastructure were made between the times I deactivated and reactivated the mod. This issue would, of course, also be present with the current (XML file) save system, but any errors it would cause could be easily cleared by deleting the XML file. With the data saved in the main file, this fix would not be possible. Applying old TM data to a changed road infrastructure could actually be harmless. I will run some tests to verify this and report back. In the meantime, I thought you would want to know that the data isn't in fact dropped from the saved game file when you deactivate the mod, as you indicated above.