automatic content serialization in xna game studio 3 · automatic content serialization with xna...
TRANSCRIPT
Automatic Content Serializationwith XNA Game Studio 3.1
Shawn HargreavesSoftware Development EngineerMicrosoft
Content Pipeline Stages
1. Artists and designers create awesome content
...
3. Profit!
Common Content Operations
Import data from editor file formats
Validate
Preprocess / optimize
Save data into game engine file format
Track dependencies
Game
Visual Studio
Content
TypeReader
Game
Object.xnb
File
Content
TypeWriter
ProcessorContent
Object
XNA Framework Content PipelineSource
Asset
File
ImporterContent
Object
Source
Asset
File
.xnb
File
Importer
Processor
Content
TypeReader
Content
TypeWriter
Content
Object
Content
Object
Game
Object
File format specific
Game agnostic
Built-in:
TextureImporter
XImporter
FbxImporter
File format agnostic
Game specific
Built-in:
TextureProcessor
ModelProcessor
XNA Framework Content Pipeline
Source
Asset
File
.xnb
File
Importer
Processor
Content
TypeReader
Content
TypeWriter
Content
Object
Content
Object
Game
Object
Managed types
Microsoft.Xna.Framework.Content.Pipeline
Built-in:
TextureContent
MeshContent
SoundEffectContent
Native hardware types
Microsoft.Xna.Framework
Built-in:
Texture
Model
SoundEffect
XNA Framework Content Pipeline
Source
Asset
File
.xnb
File
Importer
Processor
Content
TypeReader
Content
TypeWriter
Content
Object
Content
Object
Game
Object
IntermediateSerializer
XmlImporter
Automatic XNB Serialization
XNA Framework Content Pipeline
XML vs XNBXML and XNB are not the same thing!
But follow similar patterns
IntermediateSerializer
Reads and writes XML
XNB
Optimized for efficient loading
Type header, followed by whatever binary data the type desires
Manual XNB Serialization
Savingwriter = new ContentTypeWriter<T>();
WriteTypeHeader(writer.GetRuntimeReader());
writer.Write(value);
Loadingstring readerName = ReadTypeHeader();
ContentTypeReader reader = Activator.CreateInstance(readerName);
return reader.Read();
Generates ContentTypeWriter and ContentTypeReader using reflection
Only works for “simple” types
Default public constructor
All interesting data accessible via fields or properties
Recursive
Calls into other ContentTypeWriter/Reader as needed
Automatic XNB Serialization
Automatic XNB Serialization
Demo
IntermediateSerializer
To see how a type is represented in XML
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create("test.xml", settings))
{
IntermediateSerializer.Serialize(writer, testObject, null);
}
IntermediateSerializer Attributes
Default is to serialize public fields and properties
Properties before fields
In the order they are declared
To include private members: [ContentSerializer]
To exclude public members: [ContentSerializerIgnore]
ElementName
CollectionItemName
FlattenContent
Optional
AllowNull
http://blogs.msdn.com/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx
ContentSerializerAttribute Properties
Shared Resources
Cyclic data structures must be explicitly marked
[ContentSerializer(SharedResource = true)]
Collections of shared resources are trickyhttp://blogs.msdn.com/shawnhar/archive/2008/11/20/serializing-collections-of-shared-resources.aspx
[ContentSerializerIgnore]
public int Elf { get; set; }
[ContentSerializer(ElementName = "Elf")]
private string ElfSerializationHelper
{
get { return Elf.ToString("X8"); }
set { Elf = int.Parse(value, NumberStyles.HexNumber); }
}
Serialization Helper Properties
Source
Asset
File
Type Translation
.xnb
File
Importer
Processor
Content
TypeReader
Content
TypeWriter
Content
Object
Content
Object
Game
Object
What if these types are not all the same?
Type TranslationDesign time and runtime types may differ
Defined in different assemblies
Different member types (Texture2DContent vs. Texture2D)
Design time type declares its runtime equivalent
[ContentSerializerRuntimeType("Foo.Bar, MyAssembly")]
Generic content typeshttp://blogs.msdn.com/shawnhar/archive/2009/07/02/automatic-xnb-serialization-and-content-classes.aspx
Type Translation
Demo
www.microsoftgamefest.com
© 2009-2010 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.