automatic content serialization in xna game studio 3 · automatic content serialization with xna...

21

Upload: others

Post on 01-Aug-2020

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft
Page 2: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

Automatic Content Serializationwith XNA Game Studio 3.1

Shawn HargreavesSoftware Development EngineerMicrosoft

Page 3: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

Content Pipeline Stages

1. Artists and designers create awesome content

...

3. Profit!

Page 4: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

Common Content Operations

Import data from editor file formats

Validate

Preprocess / optimize

Save data into game engine file format

Track dependencies

Page 5: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

Game

Visual Studio

Content

TypeReader

Game

Object.xnb

File

Content

TypeWriter

ProcessorContent

Object

XNA Framework Content PipelineSource

Asset

File

ImporterContent

Object

Page 6: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 7: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 8: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 9: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 10: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

Manual XNB Serialization

Savingwriter = new ContentTypeWriter<T>();

WriteTypeHeader(writer.GetRuntimeReader());

writer.Write(value);

Loadingstring readerName = ReadTypeHeader();

ContentTypeReader reader = Activator.CreateInstance(readerName);

return reader.Read();

Page 11: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 12: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

Automatic XNB Serialization

Demo

Page 13: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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);

}

Page 14: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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]

Page 15: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 16: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 17: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

[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

Page 18: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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?

Page 19: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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

Page 20: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

Type Translation

Demo

Page 21: Automatic Content Serialization in XNA Game Studio 3 · Automatic Content Serialization with XNA Game Studio 3.1 Shawn Hargreaves Software Development Engineer Microsoft

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.