-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSummonersAssociationSystem.cs
280 lines (251 loc) · 10.1 KB
/
SummonersAssociationSystem.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
271
272
273
274
275
276
277
278
279
280
using SummonersAssociation.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace SummonersAssociation
{
//Handles registering MinionModels (both automatically and by mod calls)
public class SummonersAssociationSystem : ModSystem
{
internal static bool SupportedMinionsFinalized { get; private set; }
internal static int MagicMirrorRecipeGroup { get; private set; }
internal static LocalizedText RecipeGroupGenericText { get; private set; }
public override void OnModLoad() {
SupportedMinionsFinalized = false;
RecipeGroupGenericText ??= Language.GetOrRegister(Mod.GetLocalizationKey($"RecipeGroups.RecipeGroupGeneric"));
}
public override void AddRecipeGroups() {
// Automatically register MinionModels
var item = new Item();
var projectile = new Projectile();
for (int i = ItemID.Count; i < ItemLoader.ItemCount; i++) {
item = ItemLoader.GetItem(i).Item;
if (!(item.buffType > 0 && item.shoot >= ProjectileID.Count)) {
continue;
}
projectile = ProjectileLoader.GetProjectile(item.shoot).Projectile;
if (projectile.minionSlots <= 0) {
continue;
}
// Avoid automatic support for manually supported
if (!SummonersAssociation.SupportedMinions.Any(x => x.ItemID == i || x.ContainsProjID(projectile.type) || x.BuffID == item.buffType)) {
AddMinion(new MinionModel(item.type, item.buffType, projectile.type));
}
}
SupportedMinionsFinalized = true;
var group = new RecipeGroup(() => RecipeGroupGenericText.Format(Language.GetTextValue("LegacyMisc.37"), Lang.GetItemNameValue(ItemID.MagicMirror)), new int[]
{
ItemID.MagicMirror,
ItemID.IceMirror
});
MagicMirrorRecipeGroup = RecipeGroup.RegisterGroup("SummonersAssociation:MagicMirrors", group);
}
//Examples:
//############
//if (ModLoader.TryGetMod("SummonersAssociation", out Mod summonersAssociation))
//{
// Calls here
//}
//
// Regular call for a regular summon weapon
// summonersAssociation.Call(
// "AddMinionInfo",
// ItemType<MinionItem>(),
// BuffType<MinionBuff>(),
// ProjectileType<MinionProjectile>()
// );
//
// If the weapon summons two (or more) minions
// summonersAssociation.Call(
// "AddMinionInfo",
// ItemType<MinionItem>(),
// BuffType<MinionBuff>(),
// new List<int> {
// ProjectileType<MinionProjectile1>(),
// ProjectileType<MinionProjectile2>()
// }
// );
//
// If you need to add any additional info to the projectile, such as overriding the minion slot
// (for example useful if you have a Stardust Dragon-like minion and you only want it to count one segment towards the number of summoned minions)
// summonersAssociation.Call(
// "AddMinionInfo",
// ItemType<MinionItem>(),
// BuffType<MinionBuff>(),
// new Dictionary<string, object>() {
// ["ProjID"] = ProjectileType<MinionProjectile>(),
// ["Slot"] = 1f
// }
// );
//
// If you need to add any additional info to multiple projectiles, such as overriding the minion slot
// (for example useful if you have some complex minion that consists of multiple parts)
// summonersAssociation.Call(
// "AddMinionInfo",
// ItemType<MinionItem>(),
// BuffType<MinionBuff>(),
// new List<Dictionary<string, object>> {
// new Dictionary<string, object>() {
// ["ProjID"] = ProjectileType<MinionProjectile1>(),
// ["Slot"] = 0.25f
// },
// new Dictionary<string, object>() {
// ["ProjID"] = ProjectileType<MinionProjectile2>(),
// ["Slot"] = 1f //This can be omitted aswell (then it'll default to Projectile.minionSlots), only ProjID is mandatory
// }
// }
// );
//
// Storm Tiger-like "counter" projectile that is a minion but should not be teleported
// Or if a minion hovers over your head and should never move
// summonersAssociation.Call(
// "AddTeleportConditionMinion",
// ProjectileType<MinionCounterProjectile>()
// );
//
// Customizable condition (no condition: defaults to false):
// summonersAssociation.Call(
// "AddTeleportConditionMinion",
// ProjectileType<MinionProjectile>(),
// (Func<Projectile, bool>) ((Projectile p) => false) //return false here to prevent it from teleporting, otherwise, true
// );
//
// Get a copy of the stored information about all minions (See SummonersAssociationIntegrationExample.cs for more info)
// var data = (List<Dictionary<string, object>>)summonersAssociation.Call(
// "GetSupportedMinions",
// );
//
public static object Call(params object[] args) {
/* message string, then
* if "AddMinionInfo": all calls have the same number of args
* int, int, List<Dictionary<string, object>>
* or
* int, int, List<int>
* or
* int, int, Dictionary<string, object>
* or
* int, int, int
*
* else if "TeleportConditionMinions":
* int
* or
* int, Func<Projectile, bool>
*
* else if "GetSupportedMinions":
* Mod, apiVersionString
* returns List<Dictionary<string, object>>
* ...
*/
var modSA = SummonersAssociation.Instance;
var logger = modSA.Logger;
try {
string message = args[0] as string;
if (message == "AddMinionInfo") {
if (SupportedMinionsFinalized)
throw new Exception($"{modSA.Name} Call Error: The attempted message, \"{message}\", was sent too late. {modSA.Name} expects Call messages to happen during Mod.PostSetupContent.");
int itemID = Convert.ToInt32(args[1]);
int buffID = Convert.ToInt32(args[2]);
if (itemID <= 0 || itemID >= ItemLoader.ItemCount)
throw new Exception("Invalid item '" + itemID + "' registered");
string itemMsg = " ### Minion from item '" + (itemID < ItemID.Count ? Lang.GetItemNameValue(itemID) : ItemLoader.GetItem(itemID).DisplayName) + "' not added";
if (buffID <= 0 || buffID >= BuffLoader.BuffCount)
throw new Exception("Invalid buff '" + buffID + "' registered" + itemMsg);
object projArg = args[3];
if (args.Length > 3 + 1) {
throw new Exception($"\"{message}\" does not take more than 3 parameters");
}
else if (projArg is List<Dictionary<string, object>> projDataDicts) {
if (projDataDicts.Count == 0) throw new Exception("ProjModel list empty" + itemMsg);
var addedProjIDs = new HashSet<int>(); // Sanitize lists to not contain duplicates
var projDataList = new List<ProjModel>();
foreach (var projModelDict in projDataDicts) {
var projModel = ProjModel.FromDictionary(projModelDict, itemMsg);
int projID = projModel.ProjID;
if (!addedProjIDs.Contains(projID))
{
projDataList.Add(projModel);
addedProjIDs.Add(projID);
}
}
AddMinion(new MinionModel(itemID, buffID, projDataList));
}
else if (projArg is List<int> projIDs) {
if (projIDs.Count == 0) throw new Exception("Projectile list empty" + itemMsg);
// Sanitize list via Distinct() to not contain duplicates
foreach (int projID in projIDs.Distinct()) {
CheckProj(itemMsg, projID);
}
AddMinion(new MinionModel(itemID, buffID, projIDs));
}
else if (projArg is Dictionary<string, object> projModelDict) {
AddMinion(new MinionModel(itemID, buffID, ProjModel.FromDictionary(projModelDict, itemMsg)));
}
else if (projArg is int) {
int projID = Convert.ToInt32(args[3]);
CheckProj(itemMsg, projID);
AddMinion(new MinionModel(itemID, buffID, projID));
}
else {
throw new Exception($"\"{projArg}\" does not have a suitable type for \"{message}\"");
}
return "Success";
}
else if (message == "AddTeleportConditionMinion") {
//New with v0.4.6
int projID = Convert.ToInt32(args[1]);
if (projID <= 0 || projID >= ProjectileLoader.ProjectileCount) throw new Exception("Invalid projectile '" + projID + "' registered");
Func<Projectile, bool> func = SummonersAssociation.ProjectileFalse;
if (args.Length == 3 && args[2] is Func<Projectile, bool>) {
func = args[2] as Func<Projectile, bool>;
}
SummonersAssociation.TeleportConditionMinions[projID] = func;
return "Success";
}
else if (message == "GetSupportedMinions") {
//New with v0.4.7
if (args[1] is not Mod mod) {
throw new Exception($"Call Error: The Mod argument for the attempted message, \"{message}\" has returned null.");
}
var apiVersion = args[2] is string ? new Version(args[2] as string) : modSA.Version; // Future-proofing. Allowing new info to be returned while maintaining backwards compat if necessary.
logger.Info($"{(mod.DisplayName ?? "A mod")} has registered for {message} via Call");
if (!SupportedMinionsFinalized) {
logger.Warn($"Call Warning: The attempted message, \"{message}\", was sent too early. Expect the Call message to return incomplete data. For best results, call in PostAddRecipes.");
}
var list = SummonersAssociation.SupportedMinions.Select(m => m.ConvertToDictionary(apiVersion)).ToList();
return list;
}
else {
throw new Exception($"\"{message}\" is not an accepted call");
}
}
catch (Exception e) {
logger.Error(modSA.Name + " Call Error: " + e.StackTrace + ": " + e.Message + "\nConsult https://github.com/JavidPack/SummonersAssociation/wiki/Support-using-Mod-Call");
}
return "Failure";
}
private static void CheckProj(string itemMsg, int type) {
if (type <= 0 || type >= ProjectileLoader.ProjectileCount)
throw new Exception("Invalid projectile '" + type + "' registered" + itemMsg);
}
/// <summary>
/// Almost the same as Add, but merges projectile lists on the same buff and registers its projectile(s) without creating a new model
/// </summary>
private static void AddMinion(MinionModel model) {
MinionModel existing = SummonersAssociation.SupportedMinions.SingleOrDefault(m => m.BuffID == model.BuffID);
if (existing != null) {
foreach (var data in model.ProjData) {
if (!existing.ContainsProjID(data.ProjID)) {
existing.ProjData.Add(data);
}
}
}
else {
SummonersAssociation.SupportedMinions.Add(model);
}
}
}
}