Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
RegistryLoader.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.Attributes;
6 using Loot.Api.Core;
7 using Terraria.ModLoader;
8 
9 namespace Loot.Api.Loaders
10 {
15  public static class RegistryLoader
16  {
17  internal static IDictionary<string, Mod> Mods;
18  public static int ModCount => Mods.Count;
19 
20  internal static void Initialize()
21  {
22  Mods = new Dictionary<string, Mod>();
23  }
24 
25  internal static void Load()
26  {
27  Mods.Add(Loot.Instance.Name, Loot.Instance);
28  }
29 
30  internal static void Unload()
31  {
32  Mods?.Clear();
33  Mods = null;
34  }
35 
36  internal static void CheckModLoading(Mod mod, string source, bool invert = false)
37  {
38  if (mod == null)
39  {
40  throw new NullReferenceException($"Mod is null in {source}");
41  }
42 
43  if (mod.GetType().GetField("loading", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(mod) is bool b)
44  {
45  if (!invert && !b)
46  {
47  throw new Exception($"{source} can only be called from Mod.Load or Mod.Autoload");
48  }
49 
50  if (invert && b)
51  {
52  throw new Exception($"{source} can only be called if mod {mod.Name} is already loaded");
53  }
54  }
55  }
56 
57  internal static bool CheckModRegistered(Mod mod, string source, bool @throw = true)
58  {
59  bool modIsPresent = Mods.ContainsKey(mod.Name);
60  if (modIsPresent && @throw)
61  {
62  throw new Exception($"Mod {mod.Name} is already registered in {source}");
63  }
64 
65  return modIsPresent;
66  }
67 
71  public static void RegisterMod(Mod mod)
72  {
73  CheckModLoading(mod, "RegisterMod");
74  CheckModRegistered(mod, "RegisterMod");
75 
76  Mods.Add(new KeyValuePair<string, Mod>(mod.Name, mod));
77  ContentLoader.RegisterMod(mod);
78  }
79 
80  internal static void ProcessMods()
81  {
82  foreach (var kvp in Mods)
83  {
84  AddContent(kvp.Value);
85  }
86  }
87 
93  internal static void AddContent(Mod mod)
94  {
95  CheckModLoading(mod, "SetupContent", invert: true);
96 
97  var ordered = Mods.FirstOrDefault(x => x.Key.Equals(mod.Name))
98  .Value
99  .Code
100  .GetTypes()
101  .OrderBy(x => x.FullName, StringComparer.InvariantCulture)
102  .Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute<DoNotLoadAttribute>() == null)
103  .ToList();
104 
105  var rarities = ordered.Where(x => x.IsSubclassOf(typeof(ModifierRarity)));
106  var modifiers = ordered.Where(x => x.IsSubclassOf(typeof(Modifier)));
107  var pools = ordered.Where(x => x.IsSubclassOf(typeof(ModifierPool)));
108  var effects = ordered.Where(x => x.IsSubclassOf(typeof(ModifierEffect)));
109 
110  // Kinda meh, but no need to recheck here..
111  ContentLoader.SkipModChecks(true);
112 
113  // important: load things in order. (modifiers relies on all.. etc.)
114  foreach (Type type in rarities)
115  {
116  ContentLoader.ModifierRarity.AddContent(type, mod);
117  }
118 
119  foreach (Type type in modifiers)
120  {
121  ContentLoader.Modifier.AddContent(type, mod);
122  }
123 
124  foreach (Type type in pools)
125  {
126  ContentLoader.ModifierPool.AddContent(type, mod);
127  }
128 
129  foreach (Type type in effects)
130  {
131  ContentLoader.ModifierEffect.AddContent(type, mod);
132  }
133 
134  ContentLoader.SkipModChecks(false);
135  }
136  }
137 }
Defines a modifier, which is an unloaded GlobalItem Making it a GlobalItem gives easy access to all h...
Definition: Modifier.cs:21
static ModifierContent Modifier
A ModifierEffect signifies the effect of a modifier on a player It should house the implementation...
static ModifierPoolContent ModifierPool
Defines the rarity of a modifier
static ModifierRarityContent ModifierRarity
Defines a modifier pool. A modifier pool holds a certain amount of effects in an array It allows to r...
Definition: ModifierPool.cs:37
static ModifierEffectContent ModifierEffect
static void RegisterMod(Mod mod)
Registers specified mod, enabling autoloading for that mod
This class holds all Content holders of this mod You can use this to access content loaded into the m...
This class is responsible for loading mods into Even More Modifiers Mods can be registered and trigge...