Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ModContentManager.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Terraria.ModLoader;
5 
6 namespace Loot.Api.ModContent
7 {
11  public class ModContentManager
12  {
13  private IDictionary<string, TextureModContent> _contents;
14  private IEnumerable<TextureModContent> _modContents => _contents.Select(x => x.Value);
15 
16  public T GetContent<T>() where T : TextureModContent
17  => (T) _modContents.FirstOrDefault(x => x.GetType() == typeof(T));
18 
19  public TextureModContent GetContent(Type type)
20  => _modContents.FirstOrDefault(x => x.GetType() == type);
21 
22  public TextureModContent GetContent(string key)
23  => _contents.TryGetValue(key, out var modContent) ? modContent : null;
24 
25  public void AddContent(string key, TextureModContent textureModContent)
26  {
27  if (_contents.ContainsKey(key))
28  {
29  throw new Exception($"Key '{key}' already present in ContentManager");
30  }
31 
32  if (_contents.Values.Contains(textureModContent))
33  {
34  Loot.Logger.WarnFormat("ModContent with registry key {0} was already present", textureModContent.GetRegistryKey());
35  }
36 
37  textureModContent._Initialize();
38  _contents.Add(key, textureModContent);
39  }
40 
41  internal void Initialize(Mod mod)
42  {
43  _contents = new Dictionary<string, TextureModContent>();
44  var assembly = mod.Code;
45  var modContents = assembly.GetTypes().Where(x => x.BaseType == typeof(TextureModContent) && !x.IsAbstract);
46  foreach (var modContent in modContents)
47  {
48  TextureModContent obj = (TextureModContent) Activator.CreateInstance(modContent);
49  AddContent(obj.GetRegistryKey(), obj);
50  }
51  }
52 
53  internal void Load()
54  {
55  foreach (var content in _modContents)
56  {
57  content._Load();
58  }
59  }
60 
61  internal void Unload()
62  {
63  foreach (var content in _modContents)
64  {
65  content._Unload();
66  }
67 
68  _contents = null;
69  }
70  }
71 }
This class is responsible for managing instances of TextureModContent for registered mods...
IDictionary< string, TextureModContent > _contents
An abstract class that contains a mapping of strings to texture instances