IDisposeable and ISerializeable

Dec 13, 2007 at 8:17 PM
We should consider adding these, or some diviation of them. We should be able to take any object and dispose it, which should release all attached resources.

I think that not all serialization is supported on xbox, shaw do you have any insights on this? So here we might consider having our own serializer. This will be extremely useful when it comes to save games as we would simply serialize the Scene, and deserialize it on load.
Dec 13, 2007 at 9:21 PM
BinarySerializer does not exist. I've heard that XmlSerializer does, or at least a subset of it.

If you're curious if a certain class/namespace is implemented on Xbox, just open the Xbox 360 assemblies in Reflector. They empty, reference versions are at $(PROGRAM_FILES)/Microsoft XNA/XNA Game Studio/v2.0/References/Xbox360. All of the methods are empty, but it lets you know what is and what is not included.

I don't have much experience with ISerializable, but it may make more sense to have every scene entity implement a Save and a Load method (as part of the entity interface), which takes in a Stream object and lets the individual entities decide what it wants to save/load from disk.
Dec 14, 2007 at 12:15 AM
ISerialize does exatly that, if you only have get/set properties you do not have to do anything, only in case you have special state you wish to persist you will need to implement a serialize method and another constructor.
Dec 14, 2007 at 3:12 PM
Guys, open the xna docs, and read the programming guides on staorage. There is a whole page (!) on how to use XMLSerializer to load and save data. I was going to bring this up as we would need to support Serializable for when I get around to storage.
Dec 14, 2007 at 3:47 PM
The documentation is simplistic to say the least, but it does give you a very general overview of how serialization can be done. I see our biggest problem as being how to save the entity hierarchy properly and deserialize it properly.
Dec 14, 2007 at 4:57 PM
I'll have a look at it later, but as long as the .net framework on the xbox360 supports xmlnode and xmlattribute we can store any information we want, though we will have to do some legwork. The best solution would of cause be that the framework would support the same features as the normal one becase you would then not have to do too much work, entities would just persist.
Dec 14, 2007 at 7:18 PM
How well does the default XML serializer handle inheritance? All the examples I see involve concrete, known types. How would you handle arbitrary entities?
Dec 14, 2007 at 7:28 PM
That works without any problems, consider
    public class A
    {
    }
 
    public class B : A
    {
        private int myVar;
 
        public int MyProperty
        {
            get { return this.myVar; }
            set { this.myVar = value; }
        }
	
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            A node = new B();
            XmlSerializer serializer = new XmlSerializer(node.GetType());
            using (StreamWriter sw = new StreamWriter("test.xml"))
            {
                serializer.Serialize(sw, node);
            }
        }
    }
Will produce the following output:
  <?xml version="1.0" encoding="utf-8" ?> 
- <B xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyProperty>0</MyProperty> 
  </B>
Dec 14, 2007 at 8:10 PM
If A had some properties, would they also be included in B's serialization?

Also, when you deserialize the above example, would you serialize it with type A (i.e. new Serializer(typeof(A))) since you wouldn't know ahead of time what type of entity it is? Does this work properly?
Dec 14, 2007 at 9:32 PM
Yes the base properties would also be serialized :)

You can't use the base type this would throw, but you can just get the type at runtime.
Dec 14, 2007 at 10:00 PM
So if we can't deserialize on the base type... how would you deserialize that XML you gave to get back a B object? At run-time you won't know that the serialization is for an object of type B.
Dec 15, 2007 at 12:38 AM
This is why the scene or scenemanager will have a custom serializer/deserializer methods. it will retrieve the type information before initializing the serializer for the node. I can try to make a mockup later this weekend. Though I think we should wait with implementation until version 0.20 as it will more clear at that time how the entity graph will look like.