Serialization and Versioning

Started by
1 comment, last by yaroslavd 20 years, 1 month ago
Hi, guys. I''m making an RPG with C#. I''m having trouble with deciding how to deal with saving/loading maps, tiles, characters, weapons, etc. I also want a method that is backwards/forwards compatible. This is the best I can think of so far, please tell me if I''m approaching this the right way: 1. Make SinceAttribute and DeprecatedSinceAttribute attribute classes that accept an (int version) parameter in the constructor. 2. Tag all class members that I want to record with these attributes as needed. For example, if CurrentVersion of the class is 6, and I want to add a member to this class, I''ll change CurrentVersion to 7, add the member, and tag the member with [Since(7)]. 3. Make all saveable/loadable classes implement this interface:

public interface IRecordable
{
  IRecordable(SerializationInfo info, StreamingContext context); //Don''t know if I can do this. Just let me know if I can''t.
  String name {get;set};
  int CurrentVersion {get};
}
 
4. Make all saveable/loadable classes implement ISerializable. 5. In GetObjectData(), record CurrentVersion first, and then all the members whose SinceAttribute is <= CurrentVersion and whose DeprecatedSinceAttribute is > CurrentVersion. 6. In Constructor(SerializationInfo info, StreamingContext context), read in CurrentVersion first, and then all the members whose SinceAttribute is <= CurrentVersion and whose DeprecatedSinceAttribute is > CurrentVersion. Please tell me if this is a good way to go about recording my game info. Thanks in advance.
Advertisement
Eh, I''m looking at serialization right now. My problem is; I relied on the default serialization mechanism, but I now want to switch to ISerializable, without losing my data. Not obvious.

Now, for your problem:
quote:Original post by yaroslavd
1. Make SinceAttribute and DeprecatedSinceAttribute attribute classes that accept an (int version) parameter in the constructor.
Why? Why not just have an internal "CurrentVersion" private readonly variable? You write it to the stream first. When you read it, you check the version number, and act accordingly.
quote:2. Tag all class members that I want to record with these attributes as needed. For example, if CurrentVersion of the class is 6, and I want to add a member to this class, I''ll change CurrentVersion to 7, add the member, and tag the member with [Since(7)].
I haven''t used the attributes much (never took the time to really read about it), but if you implement ISerializable, from what I understand, there is no use in using attributes unless you want to use them in your own code.
quote:3. Make all saveable/loadable classes implement this interface:
public interface IRecordable{  IRecordable(SerializationInfo info, StreamingContext context); //Don''t know if I can do this. Just let me know if I can''t.  String name {get;set};  int CurrentVersion {get};}  
IIRC, interfaces can''t have member fields. What you need is a base class. But I don''t understand why you want to do IRecordable, anyway.
quote:4. Make all saveable/loadable classes implement ISerializable.

5. In GetObjectData(), record CurrentVersion first, and then all the members whose SinceAttribute is <= CurrentVersion and whose DeprecatedSinceAttribute is > CurrentVersion.

6. In Constructor(SerializationInfo info, StreamingContext context), read in CurrentVersion first, and then all the members whose SinceAttribute is <= CurrentVersion and whose DeprecatedSinceAttribute is > CurrentVersion.
Looks good. If that''s why you want to use the attributes, that''d be fine. I really should learn about the introspective capabilities of .Net :\

I have to wonder why you''re planning for forward-compatibility. That''s pretty demanding and restrictive, IMO.

Cédric
Thank you for your response, although next time please read the post completely before answering. Thanks for your input, nevertheless.

quote:
quote:3. Make all saveable/loadable classes implement this interface:
public interface IRecordable{  IRecordable(SerializationInfo info, StreamingContext context); //Don''t know if I can do this. Just let me know if I can''t.  String name {get;set};  int CurrentVersion {get};}   
IIRC, interfaces can''t have member fields. What you need is a base class. But I don''t understand why you want to do IRecordable, anyway.


I don''t have member fields in there. I have member properties, which are allowed.

Anyone else have any input?

This topic is closed to new replies.

Advertisement