2012-04-03 57 views
0

我需要允许我的内容管道扩展使用类似于工厂的模式。我开始一个字典类型:从XNA内容管道扩展中访问服务

public delegate T Mapper<T>(MapFactory<T> mf, XElement d); 

public class MapFactory<T> 
{ 
    Dictionary<string, Mapper<T>> map = new Dictionary<string, Mapper<T>>(); 

    public void Add(string s, Mapper<T> m) 
    { 
     map.Add(s, m); 
    } 

    public T Get(XElement xe) 
    { 
     if (xe == null) throw new ArgumentNullException(
      "Invalid document"); 
     var key = xe.Name.ToString(); 
     if (!map.ContainsKey(key)) throw new ArgumentException(
      key + " is not a valid key."); 
     return map[key](this, xe); 
    } 

    public IEnumerable<T> GetAll(XElement xe) 
    { 
     if (xe == null) throw new ArgumentNullException(
      "Invalid document"); 
     foreach (var e in xe.Elements()) 
     { 
      var val = e.Name.ToString(); 
      if (map.ContainsKey(val)) 
       yield return map[val](this, e); 
     } 
    } 
} 

这是一个类型的对象,我想存储:

public partial class TestContent 
{ 
    // Test type 
    public string title; 

    // Once test if true 
    public bool once; 

    // Parameters 
    public Dictionary<string, object> args; 

    public TestContent() 
    { 
     title = string.Empty; 
     args = new Dictionary<string, object>(); 
    } 

    public TestContent(XElement xe) 
    { 
     title = xe.Name.ToString(); 
     args = new Dictionary<string, object>(); 
     xe.ParseAttribute("once", once); 
    } 
} 

XElement.ParseAttribute是一个扩展方法,它的工作原理正如人们所预料。它返回一个布尔值,如果成功则返回true。

问题是我有许多不同类型的测试,每种测试都以特定测试所特有的方式填充对象。元素名称是MapFactory字典的关键。这种类型的测试尽管非典型,但却说明了我的问题。

public class LogicTest : TestBase 
{ 
    string opkey; 
    List<TestBase> items; 

    public override bool Test(BehaviorArgs args) 
    { 
     if (items == null) return false; 
     if (items.Count == 0) return false; 
     bool result = items[0].Test(args); 
     for (int i = 1; i < items.Count; i++) 
     { 
      bool other = items[i].Test(args); 
      switch (opkey) 
      { 
       case "And": 
        result &= other; 
        if (!result) return false; 
        break; 
       case "Or": 
        result |= other; 
        if (result) return true; 
        break; 
       case "Xor": 
        result ^= other; 
        break; 
       case "Nand": 
        result = !(result & other); 
        break; 
       case "Nor": 
        result = !(result | other); 
        break; 
       default: 
        result = false; 
        break; 
      } 
     } 
     return result; 
    } 

    public static TestContent Build(MapFactory<TestContent> mf, XElement xe) 
    { 
     var result = new TestContent(xe); 
     string key = "Or"; 
     xe.GetAttribute("op", key); 
     result.args.Add("key", key); 
     var names = mf.GetAll(xe).ToList(); 
     if (names.Count() < 2) throw new ArgumentException(
       "LogicTest requires at least two entries."); 
     result.args.Add("items", names); 
     return result; 
    } 
} 

我实际的代码是更复杂的工厂有两本字典,一个一个轮流到的XElement的内容类型编写和另一所使用的阅读器创建实际的游戏对象。

我需要在代码中构建这些工厂,因为它们将字符串映射到委托。我有一个包含这些工厂的服务。任务是使这些工厂类可用于内容处理器。无论处理器本身还是它用作参数的上下文都没有任何已知的挂钩来连接IServiceProvider或等价物。

任何想法?

回答

0

我需要基本上按需创建数据结构,而不必访问基础类,因为它们来自第三方,在这种情况下是XNA Game Studio。只有一种方法可以做到这一点,我知道......静态地。

public class TestMap : Dictionary<string, string> 
{ 
    private static readonly TestMap map = new TestMap(); 

    private TestMap() 
    { 
     Add("Logic", "LogicProcessor"); 
     Add("Sequence", "SequenceProcessor"); 
     Add("Key", "KeyProcessor"); 
     Add("KeyVector", "KeyVectorProcessor"); 
     Add("Mouse", "MouseProcessor"); 
     Add("Pad", "PadProcessor"); 
     Add("PadVector", "PadVectorProcessor"); 
    } 

    public static TestMap Map 
    { 
     get { return map; } 
    } 

    public IEnumerable<TestContent> Collect(XElement xe, ContentProcessorContext cpc) 
    { 
     foreach(var e in xe.Elements().Where(e => ContainsKey(e.Name.ToString()))) 
     { 
      yield return cpc.Convert<XElement, TestContent>(
       e, this[e.Name.ToString()]); 
     } 
    } 
} 

我把这个更进一步,创建的内容的处理器对每种类型的TestBase:

/// <summary> 
/// Turns an imported XElement into a TestContent used for a LogicTest 
/// </summary> 
[ContentProcessor(DisplayName = "LogicProcessor")] 
public class LogicProcessor : ContentProcessor<XElement, TestContent> 
{ 
    public override TestContent Process(XElement input, ContentProcessorContext context) 
    { 
     var result = new TestContent(input); 
     string key = "Or"; 
     input.GetAttribute("op", key); 
     result.args.Add("key", key); 
     var items = TestMap.Map.Collect(input, context); 
     if (items.Count() < 2) throw new ArgumentNullException(
       "LogicProcessor requires at least two items."); 
     result.args.Add("items", items); 
     return result; 
    } 
} 

任何试图引用或访问类如调用TestMap.Collect将产生下面的静态类如果需要的话。我基本上将代码从LogicTest.Build移到处理器。我也在处理器中执行任何需要的验证。

当我开始阅读这些课程的时候,我会给ContentService提供帮助。