-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModlistPlugin.cs
More file actions
227 lines (187 loc) · 8.3 KB
/
ModlistPlugin.cs
File metadata and controls
227 lines (187 loc) · 8.3 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Mono.Cecil;
using UnityEngine;
namespace Silksong.Modlist;
public static class Constants
{
public const string Guid = "org.silksong-modding.modlist";
}
internal enum DisplayMode
{
Full = 0,
Reduced = 1,
Hidden = 2
}
internal record struct PatcherMeta
{
public string AsmName;
public string AsmVersion;
public string TypeName;
}
[BepInAutoPlugin(id: Constants.Guid)]
public partial class ModlistPlugin : BaseUnityPlugin
{
private List<BepInPlugin> _majors = [];
private List<BepInPlugin> _minors = [];
private List<PatcherMeta> _patchers = [];
public string GameVersion => global::Constants.GetConstantValue<string>("GAME_VERSION");
// consts are inlined at compile time; GetConstantValue allows a runtime check to get the accurate value.
private GameObject? _modListGO;
private ModListDraw? _modListDraw;
public static ModlistPlugin Instance { get; private set; }
internal ConfigEntry<DisplayMode> DisplayMode;
private void Awake()
{
Instance = this;
DisplayMode = Config.Bind("General",
"DisplayMode",
Modlist.DisplayMode.Full,
"How to display the top-left mod list. The mod count on the title screen will always be visible.");
Logger.LogInfo($"Silksong version: {GameVersion}");
_patchers = GetPatchers(); // This needs to run only once, as patching context is over by the time plugins are loaded.
}
// We need to load _after_ all other plugins are loaded - on Awake() all dependent mods are guaranteed to _not_ be loaded!
private void Start()
{
Harmony.CreateAndPatchAll(typeof(ModlistPatches));
_modListGO = new GameObject("Silksong.ModList");
_modListDraw = _modListGO.AddComponent<ModListDraw>();
DontDestroyOnLoad(_modListGO);
DisplayMode.SettingChanged += (sender, args) => RefreshModList();
GenerateModList();
}
private void RefreshModList() // TODO: duplicate stub
{
GenerateModList();
}
private void GenerateModList()
{
(_majors, _minors) = GetPlugins();
_modListDraw!.enabled = DisplayMode.Value != Modlist.DisplayMode.Hidden;
_modListDraw!.drawString = GetListString();
}
private string GetListString()
{
switch (DisplayMode.Value)
{
case Modlist.DisplayMode.Hidden:
return "";
case Modlist.DisplayMode.Reduced:
return $"{ModListPrefix}\n{MajorListJoined}\n+ {_minors.Count} others & {_patchers.Count} patchers";
case Modlist.DisplayMode.Full:
default:
return $"{ModListPrefix}\n{PatcherListJoined}\n{MajorListJoined}\n{MinorListJoined}";
}
}
private string ModListPrefix => $"Silksong v{GameVersion} (Modlist {Version})";
public List<string> MajorList => _majors.Select(plugin => $"{plugin.Name}: {plugin.Version}").ToList();
public List<string> MinorList => _minors.Select(plugin => $"{plugin.Name}: {plugin.Version}").ToList();
public List<string> PatcherList => _patchers.Select(patcher => $"{patcher.AsmName}: {patcher.AsmVersion} (patcher)").ToList();
private string MajorListJoined => string.Join("\n", MajorList);
private string MinorListJoined => string.Join("\n", MinorList);
private string PatcherListJoined => string.Join("\n", PatcherList);
internal string MajorNamesJoined => string.Join(", ", _majors.Select(plugin => $"{plugin.Name}"));
public int ModCount => _majors.Count + _minors.Count + _patchers.Count + 1; // add 1 for Modlist itself
#region Discovery
/// <summary>
/// Hacky solution reimplementing BepInEx patcher loading to discover preloader patchers & their assembly versions.
///
/// NB: does not correctly emulate behaviour where a patcher is prevented from executing by another patcher.
/// We believe this behaviour is vanishingly rare & potential workarounds would be even hackier.
/// </summary>
private List<PatcherMeta> GetPatchers()
{
List<PatcherMeta> metas = [];
foreach (string dllPath in Directory.GetFiles(Path.GetFullPath(Paths.PatcherPluginPath), "*.dll",
SearchOption.AllDirectories))
{
try
{
var asm = AssemblyDefinition.ReadAssembly(dllPath, BepInEx.Bootstrap.TypeLoader.ReaderParameters);
var patchersInModule = asm.MainModule.Types.Where((typeDef) =>
{
// Definition based on BepInEx.Preloader.Patching.AssemblyPatcher
if (typeDef.IsInterface || typeDef.IsAbstract && !typeDef.IsSealed)
return false;
var targetDlls = typeDef.Methods.FirstOrDefault(
m => m.Name.Equals("get_TargetDLLs", StringComparison.InvariantCultureIgnoreCase) &&
m.IsPublic && m.IsStatic);
if (targetDlls == null ||
targetDlls.ReturnType.FullName != "System.Collections.Generic.IEnumerable`1<System.String>")
return false;
var patch = typeDef.Methods.FirstOrDefault(
m => m.Name.Equals("Patch") && m.IsPublic && m.IsStatic &&
m.ReturnType.FullName == "System.Void" && m.Parameters.Count == 1 &&
(m.Parameters[0].ParameterType.FullName == "Mono.Cecil.AssemblyDefinition&" ||
m.Parameters[0].ParameterType.FullName == "Mono.Cecil.AssemblyDefinition"));
return patch != null;
})
.Select(typeDef =>
new PatcherMeta() { AsmName = asm.Name.Name, AsmVersion = asm.Name.Version.ToString(), TypeName = typeDef.Name });
metas.AddRange(patchersInModule);
} catch (BadImageFormatException e)
{
Logger.LogDebug($"Skipping loading {dllPath} because it's not a valid .NET assembly. Full error: {e.Message}");
}
catch (Exception e)
{
Logger.LogError(e.ToString());
}
}
return metas;
}
private (List<BepInPlugin>, List<BepInPlugin>) GetPlugins()
{
Dictionary<string, PluginInfo> infos = BepInEx.Bootstrap.Chainloader.PluginInfos!;
List<BepInPlugin> majors = [];
List<BepInPlugin> minors = [];
foreach (var (pluginId, pluginInfo) in infos)
{
if (pluginInfo == null)
{
Logger.LogWarning($"Plugin {pluginId} has no PluginInfo, ignoring...");
continue;
}
if (pluginId == Constants.Guid)
{
// exclude self, we explicitly add ourselves in the prefix
continue;
}
var metadata = (Attribute.GetCustomAttribute(pluginInfo.Instance.GetType(), typeof(BepInPlugin)) as BepInPlugin)!;
var dependency = pluginInfo.Dependencies.FirstOrDefault(x =>
x.Flags.HasFlag(BepInDependency.DependencyFlags.HardDependency) &&
x.DependencyGUID == Constants.Guid);
if (dependency != null)
{
majors.Add(metadata);
}
else
{
minors.Add(metadata);
}
}
majors.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));
minors.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));
return (majors, minors);
}
#endregion
}
// ReSharper disable InconsistentNaming
public static partial class ModlistPatches
{
[HarmonyPatch(typeof(SetVersionNumber), nameof(SetVersionNumber.Start))]
[HarmonyPostfix]
public static void SetVersionNumber_Start(SetVersionNumber __instance)
{
var currentText = __instance.textUi.text;
currentText += $" | {ModlistPlugin.Instance.ModCount} Mods\n{ModlistPlugin.Instance.MajorNamesJoined}";
__instance.textUi.text = currentText;
}
}