`
andyjackson
  • 浏览: 57311 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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

阅读更多
这是第二篇中用到的一张照片,里面描绘了整个过程:importer->processor->serializer->deserializer。上一篇中讨论了deserializer,这一节开始,逐步讨论其他部分。

先从importer开始
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方法即可,这里用到了模版设计模式。我们可以看看系统提供的一些导入器:
这些导入器是在Microsoft.Xna.Framework.Content.Pipeline.dll外部,其代码非常复杂,而且设计到调用本地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);
      }
    }
  }
}

      下面讨论一下processor
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也差不多,不想写了~~
  • 大小: 3.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics