-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatusEffectManager.cs
436 lines (376 loc) · 15.3 KB
/
StatusEffectManager.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;
namespace StatusEffectManager;
[PublicAPI]
[Description("The ItemDrop effect to apply the status effect")]
public enum EffectType
{
Equip,
Attack,
Consume,
Set
}
public struct SE_Item
{
public StatusEffect Effect;
public EffectType Type;
}
[PublicAPI]
public class CustomSE
{
private static readonly List<CustomSE> RegisteredEffects = new();
private static readonly Dictionary<StatusEffect, CustomSE> CustomEffectMap = new();
internal static readonly List<StatusEffect> CustomSEs = new();
internal static readonly Dictionary<SE_Item, string> AddToPrefabs = new();
[Description("Instance of the StatusEffect.")]
public readonly StatusEffect Effect;
public EffectType Type;
private string _folderName = "icons";
private AssetBundle _assetBundle = null!;
[Description("Sets the icon for the StatusEffect. Must be 64x64")]
public string? Icon
{
get => IconName;
set
{
IconName = value;
IconSprite = IconName is null ? null : loadSprite(IconName);
Effect.m_icon = IconSprite;
}
}
[Description("Sets the icon for the StatusEffect. Must be 64x64")]
public Sprite? IconSprite = null;
private string? IconName = null;
private LocalizeKey? _name;
[Description("Sets the in-game name for the StatusEffect")]
public LocalizeKey Name
{
get
{
if (_name is { } name)
{
return name;
}
StatusEffect data = Effect;
if (data.m_name.StartsWith("$"))
{
_name = new LocalizeKey(data.m_name);
}
else
{
string key = "$statuseffect_" + Effect.name.Replace(" ", "_");
_name = new LocalizeKey(key).English(data.m_name);
data.m_name = key;
}
return _name;
}
}
private static Localization? _english;
private static Localization english
{
get
{
if (_english == null)
{
_english = new Localization();
_english.SetupLanguage("English");
}
return _english;
}
}
public CustomSE(string assetBundleFileName, string customEffectName, string folderName = "assets") : this(
EffectManager.RegisterAssetBundle(assetBundleFileName, folderName), customEffectName)
{
}
public CustomSE(AssetBundle bundle, string customEffectName)
{
Effect = EffectManager.RegisterCustomSE(bundle, customEffectName);
RegisteredEffects.Add(this);
CustomEffectMap[Effect] = this;
}
public CustomSE(string customEffectName)
{
Effect = ScriptableObject.CreateInstance<StatusEffect>();
EffectManager.RegisterCustomSE(Effect, customEffectName);
RegisteredEffects.Add(this);
CustomEffectMap[Effect] = this;
}
private byte[]? ReadEmbeddedFileBytes(string name)
{
using MemoryStream stream = new();
if (Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name +
$"{(_folderName == "" ? "" : ".") + _folderName}." +
name) is not { } assemblyStream)
{
return null;
}
assemblyStream.CopyTo(stream);
return stream.ToArray();
}
private Texture2D? loadTexture(string name)
{
if (ReadEmbeddedFileBytes(name) is { } textureData)
{
Texture2D texture = new(0, 0);
texture.LoadImage(textureData);
return texture;
}
return null;
}
private Sprite loadSprite(string name)
{
if (loadTexture(name) is { } texture)
{
return Sprite.Create(texture, new Rect(0, 0, 64, 64), Vector2.zero);
}
if (_assetBundle?.LoadAsset<Sprite>(name) is { } sprite)
{
return sprite;
}
throw new FileNotFoundException($"Could not find a file named {name} for the effect icon");
}
public void AddSEToPrefab(CustomSE customSE, string prefabName)
{
SE_Item test = new()
{
Effect = customSE.Effect, Type = customSE.Type
};
AddToPrefabs.Add(test, prefabName);
}
private static BaseUnityPlugin? _plugin;
private static BaseUnityPlugin plugin
{
get
{
if (_plugin is null)
{
IEnumerable<TypeInfo> types;
try
{
types = Assembly.GetExecutingAssembly().DefinedTypes.ToList();
}
catch (ReflectionTypeLoadException e)
{
types = e.Types.Where(t => t != null).Select(t => t.GetTypeInfo());
}
_plugin = (BaseUnityPlugin)BepInEx.Bootstrap.Chainloader.ManagerObject.GetComponent(types.First(t =>
t.IsClass && typeof(BaseUnityPlugin).IsAssignableFrom(t)));
}
return _plugin;
}
}
private static bool hasConfigSync = true;
private static object? _configSync;
private static object? configSync
{
get
{
if (_configSync == null && hasConfigSync)
{
if (Assembly.GetExecutingAssembly().GetType("ServerSync.ConfigSync") is { } configSyncType)
{
_configSync =
Activator.CreateInstance(configSyncType, plugin.Info.Metadata.GUID + " ItemManager");
configSyncType.GetField("CurrentVersion")
.SetValue(_configSync, plugin.Info.Metadata.Version.ToString());
configSyncType.GetProperty("IsLocked")!.SetValue(_configSync, true);
}
else
{
hasConfigSync = false;
}
}
return _configSync;
}
}
private static ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description)
{
ConfigEntry<T> configEntry = plugin.Config.Bind(group, name, value, description);
configSync?.GetType().GetMethod("AddConfigEntry")!.MakeGenericMethod(typeof(T))
.Invoke(configSync, new object[] { configEntry });
return configEntry;
}
private static ConfigEntry<T> config<T>(string group, string name, T value, string description) =>
config(group, name, value, new ConfigDescription(description));
}
[PublicAPI]
public class LocalizeKey
{
public readonly string Key;
public LocalizeKey(string key) => Key = key.Replace("$", "");
public LocalizeKey English(string key) => addForLang("English", key);
public LocalizeKey Swedish(string key) => addForLang("Swedish", key);
public LocalizeKey French(string key) => addForLang("French", key);
public LocalizeKey Italian(string key) => addForLang("Italian", key);
public LocalizeKey German(string key) => addForLang("German", key);
public LocalizeKey Spanish(string key) => addForLang("Spanish", key);
public LocalizeKey Russian(string key) => addForLang("Russian", key);
public LocalizeKey Romanian(string key) => addForLang("Romanian", key);
public LocalizeKey Bulgarian(string key) => addForLang("Bulgarian", key);
public LocalizeKey Macedonian(string key) => addForLang("Macedonian", key);
public LocalizeKey Finnish(string key) => addForLang("Finnish", key);
public LocalizeKey Danish(string key) => addForLang("Danish", key);
public LocalizeKey Norwegian(string key) => addForLang("Norwegian", key);
public LocalizeKey Icelandic(string key) => addForLang("Icelandic", key);
public LocalizeKey Turkish(string key) => addForLang("Turkish", key);
public LocalizeKey Lithuanian(string key) => addForLang("Lithuanian", key);
public LocalizeKey Czech(string key) => addForLang("Czech", key);
public LocalizeKey Hungarian(string key) => addForLang("Hungarian", key);
public LocalizeKey Slovak(string key) => addForLang("Slovak", key);
public LocalizeKey Polish(string key) => addForLang("Polish", key);
public LocalizeKey Dutch(string key) => addForLang("Dutch", key);
public LocalizeKey Portuguese_European(string key) => addForLang("Portuguese_European", key);
public LocalizeKey Portuguese_Brazilian(string key) => addForLang("Portuguese_Brazilian", key);
public LocalizeKey Chinese(string key) => addForLang("Chinese", key);
public LocalizeKey Japanese(string key) => addForLang("Japanese", key);
public LocalizeKey Korean(string key) => addForLang("Korean", key);
public LocalizeKey Hindi(string key) => addForLang("Hindi", key);
public LocalizeKey Thai(string key) => addForLang("Thai", key);
public LocalizeKey Abenaki(string key) => addForLang("Abenaki", key);
public LocalizeKey Croatian(string key) => addForLang("Croatian", key);
public LocalizeKey Georgian(string key) => addForLang("Georgian", key);
public LocalizeKey Greek(string key) => addForLang("Greek", key);
public LocalizeKey Serbian(string key) => addForLang("Serbian", key);
public LocalizeKey Ukrainian(string key) => addForLang("Ukrainian", key);
private LocalizeKey addForLang(string lang, string value)
{
if (Localization.instance.GetSelectedLanguage() == lang)
{
Localization.instance.AddWord(Key, value);
}
else if (lang == "English" && !Localization.instance.m_translations.ContainsKey(Key))
{
Localization.instance.AddWord(Key, value);
}
return this;
}
}
public static class EffectManager
{
static EffectManager()
{
Harmony harmony = new("org.bepinex.helpers.StatusEffectManager");
harmony.Patch(AccessTools.DeclaredMethod(typeof(ObjectDB), nameof(ObjectDB.Awake)),
postfix: new HarmonyMethod(AccessTools.DeclaredMethod(typeof(EffectManager), nameof(Patch_ObjectDBInit))));
harmony.Patch(AccessTools.DeclaredMethod(typeof(ZNetScene), nameof(ZNetScene.Awake)),
postfix: new HarmonyMethod(AccessTools.DeclaredMethod(typeof(EffectManager),
nameof(Patch_ZNetSceneAwake))));
}
private struct BundleId
{
[UsedImplicitly] public string assetBundleFileName;
[UsedImplicitly] public string folderName;
}
private static readonly Dictionary<BundleId, AssetBundle> bundleCache = new();
public static AssetBundle RegisterAssetBundle(string assetBundleFileName, string folderName = "assets")
{
BundleId id = new() { assetBundleFileName = assetBundleFileName, folderName = folderName };
if (!bundleCache.TryGetValue(id, out AssetBundle assets))
{
assets = bundleCache[id] =
Resources.FindObjectsOfTypeAll<AssetBundle>().FirstOrDefault(a => a.name == assetBundleFileName) ??
AssetBundle.LoadFromStream(Assembly.GetExecutingAssembly()
.GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name +
$"{(folderName == "" ? "" : ".") + folderName}." +
assetBundleFileName));
}
return assets;
}
public static StatusEffect RegisterCustomSE(string assetBundleFileName, string customEffectName,
string folderName = "assets") =>
RegisterCustomSE(RegisterAssetBundle(assetBundleFileName, folderName), customEffectName);
public static StatusEffect RegisterCustomSE(AssetBundle assets, string customEffectName)
{
StatusEffect customSE = (StatusEffect)assets.LoadAsset<ScriptableObject>(customEffectName);
CustomSE.CustomSEs.Add(customSE);
return customSE;
}
public static StatusEffect RegisterCustomSE(StatusEffect customSE, string customEffectName)
{
customSE.name = customEffectName;
CustomSE.CustomSEs.Add(customSE);
return customSE;
}
[HarmonyPriority(Priority.VeryHigh)]
private static void Patch_ObjectDBInit(ObjectDB __instance)
{
foreach (StatusEffect? statusEffect in CustomSE.CustomSEs)
{
if (!__instance.m_StatusEffects.Contains(statusEffect))
{
__instance.m_StatusEffects.Add(statusEffect);
}
}
__instance.UpdateItemHashes();
}
[HarmonyPriority(Priority.VeryHigh)]
private static void Patch_ZNetSceneAwake(ZNetScene __instance)
{
foreach (KeyValuePair<SE_Item, string> valuePair in CustomSE.AddToPrefabs)
{
try
{
GameObject? prefab = __instance.GetPrefab(valuePair.Value);
ItemDrop? itemDrop =
prefab ? prefab.GetComponent<ItemDrop>() : prefab.GetComponentInChildren<ItemDrop>();
Aoe? aoe = prefab ? prefab.GetComponent<Aoe>() : prefab.GetComponentInChildren<Aoe>();
EffectArea? effectArea =
prefab ? prefab.GetComponent<EffectArea>() : prefab.GetComponentInChildren<EffectArea>();
if (itemDrop)
{
switch (valuePair.Key.Type)
{
case EffectType.Equip:
itemDrop.m_itemData.m_shared.m_equipStatusEffect =
valuePair.Key.Effect;
break;
case EffectType.Attack:
itemDrop.m_itemData.m_shared.m_attackStatusEffect =
valuePair.Key.Effect;
break;
case EffectType.Consume:
itemDrop.m_itemData.m_shared.m_consumeStatusEffect =
valuePair.Key.Effect;
break;
case EffectType.Set:
itemDrop.m_itemData.m_shared.m_setSize = 1;
itemDrop.m_itemData.m_shared.m_setName = valuePair.Key.Effect.name;
itemDrop.m_itemData.m_shared.m_setStatusEffect =
valuePair.Key.Effect;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
else if (aoe)
{
aoe.m_statusEffect = valuePair.Key.Effect.name;
}
else if (effectArea)
{
effectArea.m_statusEffect = valuePair.Key.Effect.name;
}
else
{
Debug.LogWarning(
$"The prefab '{prefab.name}' does not have an ItemDrop, AOE, or EffectArea component. Cannot add the StatusEffect to the prefab.");
}
}
catch (Exception e)
{
Debug.LogWarning($"BROKE : {e}");
}
}
}
}