---
title: "XNA学习笔记6-源码分析"
author: "andyjackson"
date: "2010-12-07T14:51:11Z"
tags: []
categories: ["编程语言"]
slug: "xna-6"
draft: false
lastmod: "2010-12-07T14:51:11Z"
id: 835656
url: "https://ajwang.iteye.com/blog/835656"
---

ContentManage作为Game类的一个非常重要的助手，负责个所有Content的加载和管理工作。从Texture、SpritFont到Model、Effect都是通过该类的Load方法获得。Load方法的部分代码如下：
```java

public virtual T Load<T>(string assetName){
   object obj2;
   if (this.loadedAssets.TryGetValue(assetName, out obj2)){
      if (!(obj2 is T)){
          throw new ContentLoadException("......");
       }
       return (T) obj2;
   }
   T local = this.ReadAsset<T>(assetName, null);
   this.loadedAssets.Add(assetName, local);
   return local;
}

```
首先检查loadedAssets的Dictionary中是否有key为assetName的元素。如果存在，说明之前已经加载进来了，只需要将该元素的值返回；否则，调用ReadAsset方法，从内容管道加载。
```java

protected T ReadAsset<T>(
   string assetName,Action<IDisposable>recordDisposableObject){
   using (Stream stream = this.OpenStream(assetName)){
      using (ContentReader reader = 
        new ContentReader(this, stream, assetName, recordDisposableObject)){
          return reader.ReadAsset<T>();
      }
   }
}

```
调用OpenStream方法将二进制流从本地加载进来，然后调用ContentReader的ReadAsset方法，并返回其结果。
```java

internal T ReadAsset<T>(){
   T local;
   try{
      int sharedResourceCount = this.ReadHeader();
      T local2 = this.ReadObject<T>();
      this.ReadSharedResources(sharedResourceCount);
      local = local2;
   }catch (IOException exception){
        throw this.CreateContentLoadException("....");
   }
   return local;
}

```
这段代码很容易读懂，首先调用ReadHeader方法，读取数据流的头部，然后调用ReadObject读取数据流实体，整个加载的关键就在这两个方法上(尤其是后一个方法)。

      msdn上是这样解释ContentReader类的"A worker object that implements most of ContentManager.Load."也就是说它完成了几乎所有的Load过程。ReadHeader方法结构看了一下：
```java

    private int ReadHeader()
    {
      int typeCount = base.Read7BitEncodedInt();
      this.typeReaders = ContentTypeReaderManager.ReadTypeManifest(typeCount, this);
      int num2 = base.Read7BitEncodedInt();
      if (num2 > 0)
      {
        this.sharedResourceFixups = new List<Action<object>>[num2];
        for (int i = 0; i < num2; i++)
        {
          this.sharedResourceFixups[i] = new List<Action<object>>();
        }
      }
      return num2;
    }

```
ContentTypeReaderManager类是一个对ContentTypeReader管理的一个类，里面有3个Dictionary用于存放已加载进来的ContentTypeReader。这段代码主要是通过读取数据流检测当前数据流的ContentTypeReader，然后缓存等作用(即预处理的工作)。

      ReadObject<T>()方法最终调用的是自己类的InvokeReader<T>方法:
```java

    private T InvokeReader<T>(ContentTypeReader reader, object existingInstance)
    {
      T local;
      ContentTypeReader<T> reader2 = reader as ContentTypeReader<T>;
      if (reader2 != null)
      {
        T local2 = (existingInstance == null) ? default(T) : ((T) existingInstance);
        local = reader2.Read(this, local2);
      }
      else
      {
        object obj2 = reader.Read(this, existingInstance);
        if (obj2 != null)
        {
          if (!(obj2 is T))
          {
            throw this.CreateContentLoadException(".......");
          }
          local = (T) obj2;
        }
        else
        {
          local = default(T);
        }
      }
      if (existingInstance != null)
      {
        if (!object.ReferenceEquals(existingInstance, local))
        {
          throw new InvalidOperationException(".......");
        }
        return local;
      }
      if (!reader.TargetIsValueType)
      {
        IDisposable disposable = local as IDisposable;
        if (disposable == null)
        {
          return local;
        }
        if (this.recordDisposableObject != null)
        {
          this.recordDisposableObject(disposable);
          return local;
        }
        this.contentManager.RecordDisposableObject(disposable);
      }
      return local;
    }

```
这段代码应该还是比较好看懂，首先的一个if分子说明，如果当前的ContentTypeReader对象是ContentTypeReader<T>类型的，那么不为空，执行if块，执行ContentTypeReader<T>的<span style="color:red">Read(ContentReader input, T existingInstance)</span>方法，否则进入else快，执行<span style="color:red">ContentTypeReader的Read(ContentReader input, object existingInstance)</span>方法。注意ContentTypeReader<T>类是ContentTypeReader的直接子类，他重写了父类的Read(ContentReader input, object existingInstance)方法：
```java

  public abstract class ContentTypeReader<T> : ContentTypeReader
  {
    protected ContentTypeReader() : base(typeof(T)){}
    
    protected internal override object Read(ContentReader input, object existingInstance)
    {
      T local;
      if (existingInstance == null)
      {
        local = default(T);
      }
      else
      {
        if (!(existingInstance is T))
        {
          throw input.CreateContentLoadException(".....");
        }
        local = (T) existingInstance;
      }
      return this.Read(input, local);
    }
    
    protected internal abstract T Read(ContentReader input, T existingInstance);
  }
}

```
这段代码中，前面的方法，重写了父类对应的方法，但是最终返回的是**this.Read(input, local)**，该方法是一个抽象的方法，由其子类完成。现在条理清楚了，关键就在于所有子类的此方法。下面仅仅列出几个关键的子类：
```java

namespace Microsoft.Xna.Framework.Content
{
    using Microsoft.Xna.Framework.Graphics;
    using System;

    internal class EffectReader : ContentTypeReader<Effect>
    {
        private static Microsoft.Xna.Framework.Graphics.EffectPool sharedEffectPool;

        protected internal override Effect Read(ContentReader input, Effect existingInstance)
        {
            int count = input.ReadInt32();
            return new Effect(input.GraphicsDevice, input.ReadBytes(count), CompilerOptions.None, EffectPool);
        }

        internal static Microsoft.Xna.Framework.Graphics.EffectPool EffectPool
        {
            get
            {
                if (sharedEffectPool == null)
                {
                    sharedEffectPool = new Microsoft.Xna.Framework.Graphics.EffectPool();
                }
                return sharedEffectPool;
            }
        }
    }
}

```

```java

namespace Microsoft.Xna.Framework.Content
{
    using Microsoft.Xna.Framework.Graphics;

    internal class TextureReader : ContentTypeReader<Texture>
    {
        protected internal override Texture Read(ContentReader input, Texture existingInstance)
        {
            return existingInstance;
        }
    }
}

```

```java

namespace Microsoft.Xna.Framework.Content
{
    using Microsoft.Xna.Framework.Graphics;

    internal class ModelReader : ContentTypeReader<Model>
    {
        protected internal override Model Read(ContentReader input, Model existingInstance)
        {
            return Model.Read(input);
        }
    }
}

```

      看了这么多，不难发现用户也可以自定义自己的ContenTypeReader：
```java

    public class TriangleTypeReader : ContentTypeReader<Triangle>
    {
        protected override Triangle Read(ContentReader input, Triangle existingInstance) 
        {
            Vector3 p0 = input.ReadObject<Vector3>(); 
            Vector3 p1 = input.ReadObject<Vector3>(); 
            Vector3 p2 = input.ReadObject<Vector3>(); 
            
            Triangle newTriangle = new Triangle(p0, p1, p2); 
            return newTriangle; 
        }
    }

```
具体解释呆讨论完ContentTypeWriter之后再讲~~~ :wink: