XNA学习笔记7-源码分析(续)

[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]:

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也差不多,不想写了~~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值