-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFile.cs
More file actions
84 lines (70 loc) · 2.8 KB
/
Copy pathMainFile.cs
File metadata and controls
84 lines (70 loc) · 2.8 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
using System.Data;
using System.Reflection;
using Godot;
using HarmonyLib;
using MegaCrit.Sts2.Core.Entities.Players;
using MegaCrit.Sts2.Core.Logging;
using MegaCrit.Sts2.Core.Modding;
using MegaCrit.Sts2.Core.Models;
using MegaCrit.Sts2.Core.Models.Events;
using Pikcube.Common.Extensions;
using Pikcube.Common.Patches;
using Pikcube.Common.Utility;
using Logger = MegaCrit.Sts2.Core.Logging.Logger;
namespace Pikcube.Common;
/// <summary>
/// Pikcube.Common Initializer
/// </summary>
[ModInitializer(nameof(Initialize))]
public partial class MainFile : Node
{
internal static List<DarvOptionPatches.BetterDarvRelicSet> RelicSetCache { get; } = [];
/// <summary>
/// The ModId
/// </summary>
public const string ModId = "Pikcube.Common"; //At the moment, this is used only for the Logger and harmony names.
/// <summary>
/// A logger instance for the library
/// </summary>
public static Logger Logger { get; } = new(ModId, LogType.Generic);
/// <summary>
/// Called when this mod is initalized by the game
/// </summary>
public static void Initialize()
{
Harmony harmony = new(ModId);
harmony.PatchAll();
BetterHooks.AfterOneTimeInitialization += BetterHooks_AfterOneTimeInitialization;
}
private static void BetterHooks_AfterOneTimeInitialization()
{
InitDarvCache();
ModelDbExtensions.PreInit();
}
private static void InitDarvCache()
{
if (RelicSetCache.Count > 0)
{
return;
}
FieldInfo fieldInfo = AccessTools.Field(typeof(Darv), "_validRelicSets");
object val = fieldInfo.GetValue(null) ?? throw new NoNullAllowedException();
MethodInfo method = AccessTools.DeclaredMethod(val.GetType(), "GetEnumerator", []);
object enumerator = method.Invoke(val, []) ?? throw new NoNullAllowedException();
PropertyInfo current = AccessTools.DeclaredProperty(enumerator.GetType(), "Current");
MethodInfo moveNext = AccessTools.DeclaredMethod(enumerator.GetType(), "MoveNext");
while (moveNext.Invoke(enumerator, []) is true)
{
object curVal = current.GetValue(enumerator) ?? throw new NoNullAllowedException();
FieldInfo filterInfo = AccessTools.DeclaredField(curVal.GetType(), "filter");
FieldInfo relicInfo = AccessTools.DeclaredField(curVal.GetType(), "relics");
Func<Player, bool> filter = (Func<Player, bool>?)filterInfo.GetValue(curVal) ?? throw new NoNullAllowedException();
RelicModel[] relics = (RelicModel[]?)relicInfo.GetValue(curVal) ?? throw new NoNullAllowedException();
RelicSetCache.Add(new DarvOptionPatches.BetterDarvRelicSet
{
Filter = filter,
Relics = relics
});
}
}
}