Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
LoadableContentBase.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Linq;
5 using Loot.Api.Loaders;
6 using Terraria.ModLoader;
7 
8 namespace Loot.Api.Content
9 {
16  public abstract class LoadableContentBase<T> where T : ILoadableContent, ICloneable
17  {
18  internal Dictionary<string, List<(string name, T content)>> Map;
19  internal Dictionary<uint, T> Content;
20 
21  internal bool SkipModChecks;
22 
23  public uint IdCount { get; private set; }
24 
25  private uint GetNextId()
26  {
27  uint @return = IdCount;
28  IdCount++;
29  return @return;
30  }
31 
32  internal void _Initialize()
33  {
34  Map = new Dictionary<string, List<(string, T)>>();
35  Content = new Dictionary<uint, T>();
36 
37  Initialize();
38  }
39 
40  internal virtual void Initialize()
41  {
42  }
43 
44  internal void _Load()
45  {
46  Load();
47  }
48 
49  internal virtual void Load()
50  {
51  }
52 
53  internal void _Unload()
54  {
55  Map?.Clear();
56  Content?.Clear();
57  IdCount = 0;
58 
59  Unload();
60  }
61 
62  internal virtual void Unload()
63  {
64  }
65 
66  internal void AddMod(Mod mod)
67  {
68  Map.Add(mod.Name, new List<(string, T)>());
69  }
70 
71  internal virtual bool CheckContentPiece(T contentPiece) => true;
72 
73  internal void AddContent(Type type, Mod mod)
74  {
75  T contentPiece = (T) Activator.CreateInstance(type);
76  AddContent(contentPiece, mod);
77  }
78 
79  internal void AddContent(T contentPiece, Mod mod)
80  {
81  if (!typeof(ILoadableContentSetter).IsAssignableFrom(typeof(T)))
82  {
83  throw new Exception("Invalid type passed to AddContent");
84  }
85 
86  string s = $"[{nameof(T)}]{nameof(contentPiece)}";
87  if (!SkipModChecks)
88  {
89  RegistryLoader.CheckModLoading(mod, s);
90  }
91 
92  if (!Map.TryGetValue(mod.Name, out var lrm))
93  {
94  throw new Exception($"Map for {mod.Name} not found (trying to add {s}");
95  }
96 
97  if (!CheckContentPiece(contentPiece))
98  {
99  throw new Exception($"A problem occurred trying to add {s}");
100  }
101 
102  if (lrm.Exists(x => x.name.Equals(contentPiece.Name)))
103  {
104  throw new Exception($"You have already added {s}");
105  }
106 
107  ((ILoadableContentSetter) contentPiece).Mod = mod;
108  ((ILoadableContentSetter) contentPiece).Type = GetNextId();
109  Content[contentPiece.Type] = contentPiece;
110  Map[mod.Name].Add((contentPiece.Name, contentPiece));
111  }
112 
113  public T GetContent(Type type)
114  {
115  T contentPiece = Content.Values.FirstOrDefault(x => x.GetType().FullName == type.FullName);
116  return (T) contentPiece?.Clone();
117  }
118 
119  public T GetContent(string modName, string contentKey)
120  {
121  T contentPiece = _GetContent(modName, contentKey);
122  return (T) contentPiece?.Clone();
123  }
124 
125  private T _GetContent(string modName, string key)
126  {
127  return Map[modName].FirstOrDefault(x => x.name.Equals(key)).content;
128  }
129 
130  public T GetContent(uint type)
131  {
132  return type < IdCount ? (T) Content[type].Clone() : default;
133  }
134 
135  public ReadOnlyCollection<T> GetContent()
136  => Content.Select(e => (T) e.Value?.Clone()).ToList().AsReadOnly();
137  }
138 }
This class is used for Content holders in the Content namespace As a modder you should not use this c...
T GetContent(string modName, string contentKey)
Defines a piece of loadable content The interface is used in classes where the deriving class already...
T _GetContent(string modName, string key)
This class is responsible for loading mods into Even More Modifiers Mods can be registered and trigge...