Repository navigation
Expand file tree
/
Copy pathSpiritMod.Call.cs
More file actions
275 lines (238 loc) · 7.31 KB
/
Copy pathSpiritMod.Call.cs
File metadata and controls
275 lines (238 loc) · 7.31 KB
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
using SpiritMod.NPCs.Tides.Tide;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using SpiritMod.Mechanics.QuestSystem;
using SpiritMod.Mechanics.PortraitSystem;
using SpiritMod.GlobalClasses.Items;
using SpiritMod.Utilities;
namespace SpiritMod;
public partial class SpiritMod : Mod
{
public override object Call(params object[] args)
{
if (args.Length < 1)
{
var stack = new System.Diagnostics.StackTrace(true);
Logger.Error("Call Error: No arguments given:\n" + stack.ToString());
return null;
}
CallContext context;
int? contextNum = args[0] as int?;
if (contextNum.HasValue)
context = (CallContext)contextNum.Value;
else
context = ParseCallName(args[0] as string);
if (context == CallContext.Invalid && !contextNum.HasValue)
{ //Check if it has a valid value
var stack = new System.Diagnostics.StackTrace(true);
Logger.Error("Call Error: Context invalid or null:\n" + stack.ToString());
return null;
}
if (context <= CallContext.Invalid || context >= CallContext.Limit)
{ //Check if value is in-bounds
var stack = new System.Diagnostics.StackTrace(true);
Logger.Error("Call Error: Context invalid:\n" + stack.ToString());
return null;
}
try
{
if (context == CallContext.Downed) //Gets if a boss has been downed
return BossDowned(args);
else if (context == CallContext.GlyphGet) //Gets an item's glyph
return GetGlyph(args);
else if (context == CallContext.GlyphSet) //Sets an item's glyph
{
SetGlyph(args);
return null;
}
else if (context == CallContext.AddQuest) //Adds a quest
//return QuestManager.ModCallAddQuest(args);
throw new InvalidOperationException("Crossmod quests have been removed for the time being. Sorry! Will be replaced or fixed in the future.");
else if (context == CallContext.UnlockQuest) //Unlocks a quest
{
QuestManager.ModCallUnlockQuest(args);
return null;
}
else if (context == CallContext.GetQuestIsUnlocked) //Self explanatory until...
return QuestManager.ModCallGetQuestValueFromContext(args, 0);
else if (context == CallContext.GetQuestIsCompleted)
return QuestManager.ModCallGetQuestValueFromContext(args, 2);
else if (context == CallContext.GetQuestIsActive)
return QuestManager.ModCallGetQuestValueFromContext(args, 1);
else if (context == CallContext.GetQuestRewardsGiven) //...here
return QuestManager.ModCallGetQuestValueFromContext(args, 3);
else if (context == CallContext.Portrait) //Adds a new portrait from another mod
{
PortraitManager.ModCallAddPortrait(args);
return null;
}
else if (context == CallContext.Events) //Gets or sets event bools
return EventCall(args);
else if (context == CallContext.AddItemDefinition)
{
if (args[1] is int from && args[2] is int to)
ContentDefinition.ItemDefinitions.Add(from, to);
}
}
catch (Exception e)
{
Logger.Error("Call Error: " + e.Message + "\n" + e.StackTrace);
}
return null;
}
private object EventCall(object[] args)
{
if (args[1] is not bool get)
{
var stack = new System.Diagnostics.StackTrace(true);
Logger.Error("Call Error: Invalid argument for Event call:\n" + stack.ToString());
return null;
}
if (get)
return GetEventFromCall(args[2]);
else
return SetEventFromCall(args[2], args[3], args.Length > 4 ? args[4] : null);
}
private static bool? GetEventFromCall(object nameVal)
{
if (nameVal is not string name)
return null;
return name.ToUpper() switch
{
"THETIDE" => TideWorld.TheTide,
"TIDE" => TideWorld.TheTide,
"CALMNIGHT" => MyWorld.calmNight,
"BLUEMOON" => MyWorld.blueMoon,
_ => null,
};
}
private static bool? SetEventFromCall(object nameVal, object valueVal, object optionalVal)
{
if (nameVal is not string name)
return null;
if (valueVal is not bool value)
return null;
name = name.ToUpper();
if (name == "THETIDE" || name == "TIDE")
{
if (optionalVal is not bool additional)
additional = false;
if (!value && TideWorld.TheTide)
{
TideWorld.TideWave = 5;
TideWorld.TideWaveIncrease(additional);
return true;
}
else if (value && !TideWorld.TheTide)
{
if (Main.netMode == NetmodeID.MultiplayerClient)
WriteToPacket(Instance.GetPacket(), (byte)MessageType.StartTide).Send();
else
{
TideWorld.TheTide = true;
TideWorld.TideWaveIncrease();
}
return true;
}
return false;
}
else if (name == "CALMNIGHT")
{
bool oldCalmNight = MyWorld.calmNight;
MyWorld.calmNight = value;
return oldCalmNight != value;
}
else if (name == "BLUEMOON")
{
bool oldBlueMoon = MyWorld.blueMoon;
MyWorld.blueMoon = value;
return oldBlueMoon != value;
}
return null;
}
private static CallContext ParseCallName(string context)
{
if (context == null)
return CallContext.Invalid;
return context switch
{
"downed" => CallContext.Downed,
"getGlyph" => CallContext.GlyphGet,
"setGlyph" => CallContext.GlyphSet,
"AddQuest" => CallContext.AddQuest,
"UnlockQuest" => CallContext.UnlockQuest,
"IsQuestUnlocked" => CallContext.GetQuestIsUnlocked,
"IsQuestActive" => CallContext.GetQuestIsActive,
"IsQuestCompleted" => CallContext.GetQuestIsCompleted,
"QuestRewardsGiven" => CallContext.GetQuestRewardsGiven,
"Portrait" => CallContext.Portrait,
"AddItemDefinition" => CallContext.AddItemDefinition,
_ => CallContext.Invalid,
};
}
private static bool BossDowned(object[] args)
{
if (args.Length < 2)
throw new ArgumentException("No boss name specified");
string name = args[1] as string;
return name switch
{
"Scarabeus" => MyWorld.DownedScarabeus,
"Moon Jelly Wizard" => MyWorld.DownedMoonWizard,
"Vinewrath Bane" => MyWorld.DownedVinewrath,
"Ancient Avian" => MyWorld.DownedAncientAvian,
"Starplate Raider" => MyWorld.DownedStarplate,
"Infernon" => MyWorld.DownedInfernon,
"Dusking" => MyWorld.DownedDusking,
"Atlas" => MyWorld.DownedAtlas,
_ => throw new ArgumentException("Invalid boss name:" + name),
};
}
private static void SetGlyph(object[] args)
{
if (args.Length < 2)
throw new ArgumentException("Missing argument: Item");
else if (args.Length < 3)
throw new ArgumentException("Missing argument: Glyph");
if (args[1] is not Item item)
throw new ArgumentException("First argument must be of type Item");
int? glyphID = args[2] as int?;
if (!glyphID.HasValue)
throw new ArgumentException("Second argument must be of type int");
if (!item.TryGetGlobalItem(out GlyphGlobalItem glyphItem))
return;
GlyphType glyph = (GlyphType)glyphID;
if (glyph < GlyphType.None || glyph >= GlyphType.Count)
throw new ArgumentException("Glyph must be in range [" + (int)GlyphType.None + "," + (int)GlyphType.Count + ")");
glyphItem.SetGlyph(item, glyph);
}
private static int GetGlyph(object[] args)
{
if (args.Length < 2)
throw new ArgumentException("Missing argument: Item");
if (args[1] is not Item item)
throw new ArgumentException("First argument for GetGlyph must be an Item (item)!");
if (item.TryGetGlobalItem(out GlyphGlobalItem glyphItem))
return (int)glyphItem.Glyph;
return 0;
}
}
internal enum CallContext
{
Invalid = -1,
Downed,
GlyphGet,
GlyphSet,
AddQuest,
UnlockQuest,
GetQuestIsUnlocked,
GetQuestIsActive,
GetQuestIsCompleted,
GetQuestRewardsGiven,
Portrait,
Events,
AddItemDefinition,
Limit
}