Thread: Serialization
View Single Post
Old 07-26-2008, 08:42 PM   #32 (permalink)
Moroth
Newbie
 
Join Date: Mar 2008
Age: 23
Posts: 36
Default Patching scripts without versions to add new variables.

I've seen this issue time and time again. The solution that most people respond with is, "You have to wipe your server or old items". Well here is a very simple method to update items, that never included a "version" variable.

This will work anytime you need to update a script that does not already have a "Version Variable".
In this example, we will also demonstrate adding a "Version Variable" for easier future updates.


So, lets take a look at the code.
Serialize = "Writes the data"
Deserialize = "Reads the data"

Code:
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
		}
The reason we get an error and have to delete our objests is because, its trying to "Load" data that doesn't exist.

So How do we fix this?
Simple, we update the data it's trying to "load" before it loads the new format.

How is this done?
- Create the Serialize method that you want to "upgrade to".

Code:
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int)1 ); // Version

			writer.Write( ContainerName );
		}
- Leave the Deserialize method the same, so it can load our exsiting data.
- Launch/Restart your server.
- It should load fine, once it's up - Shutdown w/save
- Make the changes to the Deserialize method, to support your new data structure.
Code:
public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			switch (version)
			{
				case 1: { ContainerName = reader.ReadString(); } goto case 0;
				case 0: {  } break;
			}
		}
- Launch your server again
- Done You now have a working upgrade to your script.

Last edited by Moroth; 07-27-2008 at 02:40 PM.
Moroth is offline   Reply With Quote