/
ModCallManager.cs
382 lines (359 loc) · 17.3 KB
/
ModCallManager.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using Aequus.Common.Utilities.TypeUnboxing;
using Aequus.Content.Events.DemonSiege;
using Aequus.Content.Necromancy;
using Aequus.NPCs;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
namespace Aequus.Content.CrossMod.ModCalls
{
/// <summary>
/// Check https://terrariamods.wiki.gg/wiki/Aequus/Mod_Calls
/// </summary>
public class ModCallManager : ILoadable
{
public const string Success = "Success";
public const string Failure = "Failure";
private static object[] argument;
private static int nextArg;
private static string type;
private static Mod mod;
public static object HandleModCall(object[] args)
{
argument = args;
nextArg = 0;
type = "Unknown";
mod = null;
try
{
type = Get<string>();
mod = Get<Mod>();
switch (type)
{
case "downedCrabson":
return GetterSetter(ref AequusWorld.downedCrabson);
case "downedOmegaStarite":
return GetterSetter(ref AequusWorld.downedOmegaStarite);
case "downedDustDevil":
return GetterSetter(ref AequusWorld.downedDustDevil);
case "downedHyperStarite":
return GetterSetter(ref AequusWorld.downedHyperStarite);
case "downedUltraStarite":
return GetterSetter(ref AequusWorld.downedUltraStarite);
case "downedRedSprite":
return GetterSetter(ref AequusWorld.downedRedSprite);
case "downedSpaceSquid":
return GetterSetter(ref AequusWorld.downedSpaceSquid);
case "downedEventDemon":
return GetterSetter(ref AequusWorld.downedEventDemon);
case "downedEventCosmic":
return GetterSetter(ref AequusWorld.downedEventCosmic);
case "downedEventAtmosphere":
return GetterSetter(ref AequusWorld.downedEventAtmosphere);
case "chestAdamantiteTier":
return GetterSetter(ref AequusWorld.chestAdamantiteTier);
case "chestMythrilTier":
return GetterSetter(ref AequusWorld.chestMythrilTier);
case "chestCobaltTier":
return GetterSetter(ref AequusWorld.chestCobaltTier);
case "hardmodeChests":
return GetterSetter(ref AequusWorld.hardmodeChests);
case "downedUpriser":
return GetterSetter(ref AequusWorld.downedUpriser);
case "shadowOrbsBrokenTotal":
return GetterSetter(ref AequusWorld.shadowOrbsBrokenTotal);
case "tinkererRerolls":
return GetterSetter(ref AequusWorld.tinkererRerolls);
case "usedWhiteFlag":
return GetterSetter(ref AequusWorld.usedWhiteFlag);
case "xmasHats":
return GetterSetter(ref AequusWorld.xmasHats);
case "xmasWorld":
return GetterSetter(ref AequusWorld.xmasWorld);
// Args: int/NPC - NPC, int/Player - Player | Returns: whether or not the player has been hit
case "Flawless":
{
var npc = GetNPC();
var player = GetPlayer();
return npc.GetGlobalNPC<FlawlessGlobalNPC>().damagedPlayers[player.whoAmI];
}
// Args: int/NPC - NPC | Returns: value of FlawlessGlobalNPC.preventNoHitCheck on the provided NPC
case "FlawlessCheck":
{
var npc = GetNPC();
return GetterSetter(ref npc.GetGlobalNPC<FlawlessGlobalNPC>().preventNoHitCheck);
}
// Args: int/NPC - NPC | Returns: bool[] of size Main.maxPlayers
case "FlawlessStat":
return GetNPC().GetGlobalNPC<FlawlessGlobalNPC>().damagedPlayers;
// Args: int - Buff ID (Optional: Color - Color) | Returns: Color of the provided buff ID
case "PotionBuffColor":
{
int buff = Get<int>();
if (TryGet<Color>(out var color))
{
if (Aequus.LogMore)
Aequus.Instance.Logger.Info($"{mod.Name}: Setting Potion Color for Buff {Lang.GetBuffName(buff)} ({buff}) to {color}");
PotionColorsDatabase.BuffToColor[buff] = color;
}
if (!PotionColorsDatabase.BuffToColor.TryGetValue(buff, out var buffColor))
{
return Color.Transparent;
}
return buffColor;
}
// Args: int - Item (Optional: Color - Color) | Returns: Color of the provided item ID
case "PotionItemColor":
{
int item = Get<int>();
if (TryGet<Color>(out var color))
{
if (Aequus.LogMore)
Aequus.Instance.Logger.Info($"{mod.Name}: Setting Potion Color for Item {Lang.GetItemNameValue(item)} ({item}) to {color}");
PotionColorsDatabase.ItemToBuffColor[item] = color;
}
if (!PotionColorsDatabase.ItemToBuffColor.TryGetValue(item, out var buffColor))
{
return Color.Transparent;
}
return buffColor;
}
// Args: int - Projectile (Optional: bool - shouldBePushable) | Returns: If the provided Projectile ID can be pushed
case "PushableProjectile":
{
int proj = Get<int>();
if (TryGet<bool>(out var shouldBePushable))
{
if (Aequus.LogMore)
Aequus.Instance.Logger.Info($"{mod.Name}: Setting Pushable state for for Projectile {Lang.GetProjectileName(proj)} ({proj}) to {shouldBePushable}");
if (shouldBePushable)
{
PushableEntities.ProjectileIDs.Add(proj);
}
else
{
PushableEntities.ProjectileIDs.Remove(proj);
}
}
return PushableEntities.ProjectileIDs.Contains(proj);
}
// Args: int - NPC (Optional: bool - shouldBePushable) | Returns: If the provided NPC ID can be pushed
case "PushableNPC":
{
int npc = Get<int>();
if (TryGet<bool>(out var shouldBePushable))
{
if (Aequus.LogMore)
Aequus.Instance.Logger.Info($"{mod.Name}: Setting Pushable state for for NPC {Lang.GetNPCNameValue(npc)} ({npc}) to {shouldBePushable}");
if (shouldBePushable)
{
PushableEntities.NPCIDs.Add(npc);
}
else
{
PushableEntities.NPCIDs.Remove(npc);
}
}
return PushableEntities.NPCIDs.Contains(npc);
}
case "SentryAccessory":
{
int item = Get<int>();
if (Aequus.LogMore)
Aequus.Instance.Logger.Info($"{mod.Name}: Setting Sentry On-AI function for {Lang.GetItemNameValue(item)}");
if (TryGet<Action<Projectile, Item, Player>>(out var simpleFunc))
{
SentryAccessoriesDatabase.OnAI.Add(item, (info) =>
{
simpleFunc(info.Projectile, info.Accessory, info.Player);
});
return Success;
}
if (TryGet<Action<Projectile, GlobalProjectile, Item, Player>>(out var simpleFunc2))
{
SentryAccessoriesDatabase.OnAI.Add(item, (info) =>
{
simpleFunc2(info.Projectile, info.SentryAccessories, info.Accessory, info.Player);
});
return Success;
}
}
return Failure;
case "SentryAccessoryOnShoot":
{
int item = Get<int>();
if (Aequus.LogMore)
Aequus.Instance.Logger.Info($"{mod.Name}: Setting Sentry On-Shoot function for {Lang.GetItemNameValue(item)}");
if (TryGet<Action<Projectile, Item, Player>>(out var simpleFunc))
{
SentryAccessoriesDatabase.OnShoot.Add(item, (info) =>
{
simpleFunc(info.Projectile, info.Accessory, info.Player);
});
return Success;
}
if (TryGet<Action<IEntitySource, Projectile, Item, Player>>(out var simpleFunc2))
{
SentryAccessoriesDatabase.OnShoot.Add(item, (info) =>
{
simpleFunc2(info.Source, info.Projectile, info.Accessory, info.Player);
});
return Success;
}
if (TryGet<Action<Projectile, Projectile, Item, Player>>(out var simpleFunc3))
{
SentryAccessoriesDatabase.OnShoot.Add(item, (info) =>
{
simpleFunc3(info.Projectile, info.ParentProjectile, info.Accessory, info.Player);
});
return Success;
}
if (TryGet<Action<IEntitySource, Projectile, Projectile, Item, Player>>(out var simpleFunc4))
{
SentryAccessoriesDatabase.OnShoot.Add(item, (info) =>
{
simpleFunc4(info.Source, info.Projectile, info.ParentProjectile, info.Accessory, info.Player);
});
return Success;
}
}
return Failure;
case "PylonColor":
{
var key = new Point(Helper.UnboxInt.Unbox(args[2]), args.Length > 4 ? Helper.UnboxInt.Unbox(args[3]) : 0);
if (args.Length >= 3)
{
int index = args.Length > 4 ? 4 : 3;
if (Aequus.LogMore)
Aequus.Instance.Logger.Info($"{mod.Name}: Setting pylon color data for {TextHelper.GetInternalNameOrUnknown(key.X, TileID.Search)} ({key.X}): Style={key.Y}");
if (args[index] is Color color)
{
AequusTile.PylonColors[key] = () => color;
}
else if (args[index] is Func<Color> getColor)
{
AequusTile.PylonColors[key] = getColor;
}
}
return AequusTile.PylonColors[key];
}
case "NecromancyDatabase":
return NecromancyDatabase.CallAddNecromancyData(mod, args);
case "NecromancyNoAutogeneration":
return NecromancyDatabase.CallAddNecromancyModBlacklist(mod, args);
case "DemonSiegeSacrifice":
return DemonSiegeSystem.CallAddDemonSiegeData(mod, args);
case "DemonSiegeSacrificeHide":
return DemonSiegeSystem.CallHideDemonSiegeData(mod, args);
}
}
catch
{
}
return null;
}
internal static void TestModCalls()
{
var mod = Aequus.Instance;
//mod.Call("SentryAccessory", mod, ItemID.CobaltShield, (Projectile proj, GlobalProjectile sentryAcc, Item item, Player player) =>
//{
// Main.NewText("tester!?");
//});
//mod.Call("SentryAccessory", mod, ItemID.ObsidianShield, (Projectile proj, Item item, Player player) =>
//{
// Main.NewText("tester 22!?");
//});
//mod.Call("SentryAccessoryOnShoot", mod, ItemID.AnkhShield, (Projectile proj, Item item, Player player) =>
//{
// Main.NewText("tester 333!?");
//});
//mod.Call("SentryAccessoryOnShoot", mod, ItemID.AnkhCharm, (IEntitySource source, Projectile proj, Projectile parentProj, Item item, Player player) =>
//{
// Main.NewText("tester 4444!?");
//});
//mod.Call("SentryAccessoryOnShoot", mod, ItemID.LavaCharm, (IEntitySource source, Projectile proj, Item item, Player player) =>
//{
// Main.NewText($"{source.GetType().FullName}");
//});
}
private static T GetterSetter<T>(ref T value)
{
if (TryGet<T>(out var value2))
{
value = value2;
}
return value;
}
private static NPC GetNPC()
{
if (TryGet(out int whoAmI))
{
return Main.npc[whoAmI];
}
return Get<NPC>();
}
private static Player GetPlayer()
{
if (TryGet(out int whoAmI))
{
return Main.player[whoAmI];
}
return Get<Player>();
}
private static bool TryGet<T>(out T value)
{
value = default(T);
if (nextArg >= argument.Length)
{
return false;
}
if (TypeUnboxer<T>.Instance != null && TypeUnboxer<T>.Instance.TryUnbox(argument[nextArg], out var result))
{
nextArg++;
value = result;
return true;
}
if (argument[nextArg] is T result2)
{
nextArg++;
value = result2;
return true;
}
return false;
}
private static T Get<T>()
{
try
{
if (TypeUnboxer<T>.Instance != null && TypeUnboxer<T>.Instance.TryUnbox(argument[nextArg], out var result))
{
nextArg++;
return result;
}
var result2 = (T)argument[nextArg];
nextArg++;
return result2;
}
catch (Exception ex)
{
string nextArgTextValue = nextArg >= argument.Length ? "Outside of Index, Cannot retrieve value." : argument[nextArg].ToString();
string callerName = mod != null ? mod.Name : "Unknown";
Aequus.Instance.Logger.Error($"Could not get 'arg{nextArg}' ({nextArgTextValue}) as '{typeof(T).FullName}'.\nCaller: {callerName}, Type: {type}\n{ex.Message}\n{ex.StackTrace}");
}
return default(T);
}
public void Load(Mod mod)
{
}
public void Unload()
{
nextArg = 0;
argument = null;
mod = null;
type = null;
}
}
}