Hey Bill! Glad to hear you're thinking about supporting custom types! That would make SilverSprite even better than it already is! And it's also the top user request on
UserVoice I noticed :)
I'd gladly help out with some sample projects! I typed together some code to illustrate all this serialization behavior and learned a lot more about it in the process as well. I'm still trying to find my way around XNA and this was extremely helpful for
me to do actually. I tried to include as much as possible serialization behavior in as little code as possible.
You can get all code here:
Now for some info:
There's the SerialGame solution containing:
contains all the game data: Class1, Class2
and Class3 (and its ContentReader Class3.Reader for XNB deserialization) and a
SerialDataBase class for these 3 data types, as well as
Shawn Hargreaves' SharedResourceList (and its ContentReader
SharedResourceList.Reader for XNB deserialization). Class1 and Class2 use automatic XNB deserialization, therefore, they have no ContentReader available.
contains the pipeline extensions for building the data types: Class3Writer
for Class3 (Class1 and Class2 use automatic XNB serialization, hence no ContentWriter for them here: they are XNB serialized using reflection), a
SharedResourceListSerializer, i.e. a ContentTypeSerializer for SharedResourceList's XML serialization (for the IntermediateSerializer) and a
SharedResourceListWriter for XNB serialization.
loads an XNB from file and displays a dump of the custom type it just read. This XNB file gets built by SerialGame's
project containing the XML file the test data got serialized into (by the IntermediateSerializer, using reflection and that with help from that SharedResourceListSerializer from the pipeline extension). Building the XNB file uses automatic serialization (reflection)
and the ContentWriters where available to get to its result.
SerialTest is the one that you should run as it demonstrates all of the above in one interface: creating the test data (shows in the text box what got created), serializing
this data to XML (file gets read back and shown in the text box as well) and after the XNB got built from the XML, it allows you to start the game which loads the XNB and displays the contents.
And there's the SilverSerialGame solution containing:
SilverSerialData: which is SerialData using SilverSprite. Everything is just links to the code files of the project above.
SilverSerialGame: which is SerialGame using SilverSprite. Running this one demonstrates the fact that custom types are not supported by SilverSprite yet ;-) It also
contains an empty implementation for ContentSerializerAttribute to avoid getting compilation errors because SilverSprite doesn't have the ContentSerializerAttribute yet. Everything is just links to the code files and XNB content files built
by the projects above.
One of my biggest misunderstandings was the fact that I thought that the ContentReader had anything to do with deserializing my XML files (which
it doesn't!) and that ContentTypeSerializers had (besides the XML serialization) anything to do with reading and writing XNB files (which
they don't!). Another very tricky thing is that I needed to make sure the SharedResourceList's ContentTypeSerializer and its assembly (SerialPipeline) got referenced when serializing the XML (IntermediateSerializer) otherwise the SharedResourceList's
items got dumped in the XML file without handling them as shared resources. I thought that was tricky because there's no clear "connection" in the code between the use of the IntermediateSerializer and the SharedResourceListSerializer...
In the end I realized that for SilverSprite to handle custom types it needs to be able to load XNB files and
deserialize custom types using either reflection
or by using a ContentReader when available. All the other XML (de)serialization stuff (albeit all very handy for level editors and such, i.e. in order to avoid having to edit XML files by hand) was just obscuring what really happens on the
run-time (SilverSprite) side: loading the XNB files.
Hope this helps... It sure did help me understand XML serialization and the XNA content pipeline a bit better.