-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConfigurationSystem.cs
270 lines (230 loc) · 7.93 KB
/
ConfigurationSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using AssortedCrazyThings.Base;
using AssortedCrazyThings.Base.Chatter;
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ModLoader;
namespace AssortedCrazyThings
{
public static class ConfigurationSystem //Cannot be a ModSystem since that load runs post-mod.Load, we need it as early as possible (but after Autoload has taken place, so ILoadable.Load won't work either)
{
//These assume no ILoadable across the whole mod has a duplicate name (contrary to what tml allows)
public static Dictionary<string, ContentType> NonLoadedNames { get; private set; }
public static Dictionary<ContentType, List<string>> NonLoadedNamesByType { get; private set; }
private static Type[] InvalidTypes { get; set; }
/// <summary>
/// For use on generic content that should be always loaded except when everything is disabled
/// </summary>
public const ContentType AllFlags =
ContentType.Bosses |
ContentType.CuteSlimes |
ContentType.HostileNPCs |
ContentType.FriendlyNPCs |
ContentType.DroppedPets |
ContentType.OtherPets |
ContentType.Weapons |
ContentType.Tools |
ContentType.PlaceablesFunctional |
ContentType.PlaceablesDecorative |
ContentType.Armor |
ContentType.VanityArmor |
ContentType.Accessories |
ContentType.VanityAccessories |
ContentType.BossConsolation |
ContentType.AommSupport;
public static void Load()
{
Mod mod = AssUtils.Instance; //Maybe change it to support all loaded mod assemblies
NonLoadedNames = new();
NonLoadedNamesByType = new();
InvalidTypes = GetInvalidTypes();
//Debugging only
var autoloadedContent = mod.GetContent().ToList();
//SimpleModGore gets loaded. Maybe disable gore loading?
var manuallyAddedTypes = new List<Type>();
Type modType = mod.GetType();
foreach (Type type in mod.Code.GetTypes().OrderBy(type => type.FullName, StringComparer.InvariantCulture))
{
//Mirror autoloading conditions
if (type == modType) continue;
if (type.IsAbstract) continue;
if (type.ContainsGenericParameters) continue;
if (type.GetConstructor(Array.Empty<Type>()) == null) continue; //Don't autoload things with no default constructor
if (!typeof(ILoadable).IsAssignableFrom(type)) continue; //Don't autoload non-ILoadables
var autoload = AutoloadAttribute.GetValue(type);
CheckInvalidInheritance(type);
if (autoload.NeedsAutoloading) continue; //Skip things that are autoloaded (this code runs after Autoload())
if (!LoadSide(autoload.Side)) continue; //Skip things that shouldn't load on a particular side
var content = ContentAttribute.GetValue(type);
if (content.Ignore) continue; //Skip things tagged as non-autoloadable, yet shouldn't be loaded through here (loaded elsewhere)
var reasons = FindContentFilterReasons(content);
var instance = (ILoadable)Activator.CreateInstance(type);
if (reasons == ContentType.Always)
{
//No filters
manuallyAddedTypes.Add(type);
mod.AddContent(instance);
continue; //Don't do anything further
}
if (instance is ModType modTypeInstance)
{
string name = modTypeInstance.Name;
NonLoadedNames.Add(name, reasons);
if (!NonLoadedNamesByType.ContainsKey(reasons))
{
NonLoadedNamesByType[reasons] = new List<string>();
}
NonLoadedNamesByType[reasons].Add(name);
}
}
int b = 0;
}
private static Type[] GetInvalidTypes()
{
return new Type[]
{
typeof(ModItem),
typeof(ModProjectile),
typeof(ModBuff),
typeof(ModMount),
typeof(ModNPC),
typeof(ModTile),
typeof(PlayerDrawLayer),
typeof(ModPlayer),
typeof(ModSystem),
typeof(GlobalNPC),
typeof(GlobalBuff),
typeof(GlobalProjectile),
typeof(GlobalItem),
typeof(GlobalTile),
typeof(GlobalWall)
};
}
private static void CheckInvalidInheritance(Type type)
{
//Detect misuse of tml types that have an existing Ass base class
if (!typeof(ILoadable).IsAssignableFrom(type))
{
return;
}
var baseType = type.BaseType;
if (baseType != null && Array.IndexOf(InvalidTypes, baseType) > -1)
{
throw new Exception($"{type} inherits from {baseType}, which is not permitted. Use the base classes in ConfigurationBaseClasses.cs");
}
}
private static ContentType FindContentFilterReasons(ContentAttribute content)
{
//Bitwise "and" results in the overlap, representing the flags that caused the content to be filtered
var flags = ContentConfig.Instance.FilterFlags & content.ContentType;
if (content.NeedsAllToFilterOut && flags != content.ContentType)
{
//If the content needs a full match
return ContentType.Always;
}
return flags;
}
public static void Unload()
{
NonLoadedNames?.Clear();
NonLoadedNames = null;
NonLoadedNamesByType?.Clear();
NonLoadedNamesByType = null;
InvalidTypes = null;
}
public static string ContentTypeToString(ContentType contentType)
{
if (!ExactlyOneFlagSet(contentType))
{
string concat = string.Empty;
foreach (ContentType flag in Enum.GetValues(typeof(ContentType)))
{
if (flag != ContentType.Always && contentType.HasFlag(flag))
{
concat += ContentTypeToString(flag) + "/";
}
}
return concat[0..^1];
}
return contentType switch
{
ContentType.Always => string.Empty,
ContentType.Bosses => "Bosses",
ContentType.CuteSlimes => "Cute Slimes",
ContentType.HostileNPCs => "Hostile NPCs",
ContentType.FriendlyNPCs => "Friendly NPCs",
ContentType.DroppedPets => "Dropped Pets",
ContentType.OtherPets => "Other Pets",
ContentType.Weapons => "Weapons",
ContentType.Tools => "Tools",
ContentType.PlaceablesFunctional => "Placeables (functional)",
ContentType.PlaceablesDecorative => "Placeables (decorative)",
ContentType.Armor => "Armor",
ContentType.VanityArmor => "Vanity Armor",
ContentType.Accessories => "Accessories",
ContentType.VanityAccessories => "Vanity Accessories",
ContentType.BossConsolation => "Boss Consolation Items",
ContentType.AommSupport => "'The Amulet Of Many Minions' content",
_ => string.Empty,
};
}
public static bool ExactlyOneFlagSet(ContentType contentType)
{
return Enum.IsDefined(typeof(ContentType), contentType);
}
//Copied from tmodloader since it's internal
public static bool LoadSide(ModSide side)
{
return side != (Main.dedServ ? ModSide.Client : ModSide.Server);
}
}
[Flags]
public enum ContentType : int
{
Always = 0 << 0,
Bosses = 1 << 1,
CuteSlimes = 1 << 2,
HostileNPCs = 1 << 3,
FriendlyNPCs = 1 << 4,
DroppedPets = 1 << 5,
OtherPets = 1 << 6,
Weapons = 1 << 7,
Tools = 1 << 8,
PlaceablesFunctional = 1 << 9,
PlaceablesDecorative = 1 << 10,
Armor = 1 << 11,
VanityArmor = 1 << 12,
Accessories = 1 << 13,
VanityAccessories = 1 << 14,
BossConsolation = 1 << 15,
AommSupport = 1 << 16,
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class ContentAttribute : Attribute
{
private static readonly ContentAttribute Default = new ContentAttribute(ContentType.Always);
public ContentType ContentType { get; private set; }
public bool NeedsAllToFilterOut { get; private set; }
public bool Ignore { get; private set; }
public ContentAttribute(ContentType contentType, bool needsAllToFilterOut = false, bool ignore = false)
{
ContentType = contentType;
NeedsAllToFilterOut = needsAllToFilterOut;
if (ContentType == ConfigurationSystem.AllFlags)
{
NeedsAllToFilterOut = true; //The opposite makes no sense
}
Ignore = ignore;
}
public static ContentAttribute GetValue(Type type)
{
//Get all attributes on the type.
object[] all = type.GetCustomAttributes(typeof(ContentAttribute), true);
//The first should be the most derived attribute.
var mostDerived = (ContentAttribute)all.FirstOrDefault();
//If there were no declarations, then return default.
return mostDerived ?? Default;
}
}
}