[align=center][img]http://dl.iteye.com/upload/attachment/354690/c382e20c-478e-37b9-8eed-950467dd1dd4.png[/img][/align]这是第二篇中用到的一张照片,里面描绘了整个过程:importer->processor->serializer->deserializer。上一篇中讨论了deserializer,这一节开始,逐步讨论其他部分。
[color=blue][b]先从importer开始[/b][/color]:
[color=blue][b]下面讨论一下processor[/b][/color]。
[color=blue][b]先从importer开始[/b][/color]:
namespace Microsoft.Xna.Framework.Content.Pipeline{
public abstract class ContentImporter<T> : IContentImporter{
public abstract T Import(string filename, ContentImporterContext context);
object Import(string filename, ContentImporterContext context){
return this.Import(filename, context);
}
}
}
该类是系统中所有导入器的父类,从代码中很容易看出只需要重写其Import方法即可,这里用到了模版设计模式。我们可以看看系统提供的一些导入器:[align=center][img]http://dl.iteye.com/upload/attachment/362311/840af3f3-ecde-3ad1-82c4-dbb096e698b9.png[/img][/align]这些导入器是在[b]Microsoft.Xna.Framework.Content.Pipeline.dll[/b]外部,其代码非常复杂,而且设计到调用本地C++的库文件,而在当前dll中定义了两个比较简单的导入器:FontDescriptionImporter和XmlImporter。
namespace Microsoft.Xna.Framework.Content.Pipeline
{
[ContentImporter(".spritefont",
DisplayName="Sprite Font Description - XNA Framework",
DefaultProcessor="FontDescriptionProcessor")]
public class FontDescriptionImporter : ContentImporter<FontDescription>
{
public override FontDescription Import(string filename, ContentImporterContext context)
{
FontDescription description = null;
using (XmlReader reader = XmlReader.Create(filename))
{
description = IntermediateSerializer.Deserialize<FontDescription>(
reader, filename);
}
description.Identity = new ContentIdentity(
new FileInfo(filename).FullName, "FontDescriptionImporter");
return description;
}
}
}
[LocalizedContentImporter("XmlImporterDisplayName", ".xml")]
public class XmlImporter : ContentImporter<object>
{
public override object Import(
string filename, ContentImporterContext context)
{
using (XmlReader reader = XmlReader.Create(filename))
{
return IntermediateSerializer.Deserialize<object>(reader, filename);
}
}
}
}
[color=blue][b]下面讨论一下processor[/b][/color]。
namespace Microsoft.Xna.Framework.Content.Pipeline{
public abstract class ContentProcessor<TInput, TOutput> : IContentProcessor{
object IContentProcessor.Process(object input, ContentProcessorContext context){
if (input == null){ throw new ArgumentNullException("input"); }
if (!(input is TInput)){ throw new }
TInput local = (TInput) input;
return this.Process(local, context);
}
public abstract TOutput Process(TInput input, ContentProcessorContext context);
}
}
采用了跟importer和typereader相同的设计思路。自定义的processor只需要重写Process方法即可。serializer也差不多,不想写了~~