Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
PopulatePoolFromAttribute.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using Loot.Api.Core;
6 using Terraria.ModLoader;
7 
8 namespace Loot.Api.Attributes
9 {
17  [AttributeUsage(AttributeTargets.Class)]
19  {
20  public string[] Namespaces { get; set; }
21 
22  public PopulatePoolFromAttribute(params string[] namespaces)
23  {
24  Assembly asm = Assembly.GetExecutingAssembly();
25 
26  // TODO custom assembly not tested!
27  foreach (string ns in namespaces)
28  {
29  Assembly useAssembly = asm;
30  string root = ns;
31  int index = ns.IndexOf('.');
32  if (index > 0)
33  {
34  root = ns.Substring(0, index);
35  }
36 
37  var mod = ModLoader.GetMod(root);
38  if (mod != null)
39  {
40  useAssembly = mod.Code;
41  }
42 
43  if (!useAssembly.GetTypes().Any(x => x.Namespace != null && x.Namespace.StartsWith(ns)))
44  {
45  throw new ArgumentException($"Namespace not found", nameof(ns));
46  }
47  }
48 
49  Namespaces = namespaces;
50  }
51 
52  public Dictionary<string, List<Type>> GetClasses()
53  {
54  var dict = new Dictionary<string, List<Type>>();
55  foreach (string s in Namespaces)
56  {
57  dict.Add(s, GetModifierClasses(s).ToList());
58  }
59 
60  return dict;
61  }
62 
63  private IEnumerable<Type> GetModifierClasses(string ns)
64  {
65  Assembly asm = Assembly.GetExecutingAssembly();
66  Assembly useAssembly = asm;
67  string root = ns;
68  int index = ns.IndexOf('.');
69  if (index > 0)
70  {
71  root = ns.Substring(0, index);
72  }
73 
74  var mod = ModLoader.GetMod(root);
75  if (mod != null)
76  {
77  useAssembly = mod.Code;
78  }
79 
80  return useAssembly
81  .GetTypes()
82  .Where(type =>
83  type.IsClass
84  && type.IsSubclassOf(typeof(Modifier))
85  && type.Namespace != null && type.Namespace.StartsWith(ns));
86  }
87  }
88 }
Defines a modifier, which is an unloaded GlobalItem Making it a GlobalItem gives easy access to all h...
Definition: Modifier.cs:21
Will populate a ModifierPool based on a namespace All Modifiers in that namespace will become part of...
Dictionary< string, List< Type > > GetClasses()