Skip to content

Commit 6ec8b5b

Browse files
committed
Add mainmenu support
1 parent 7c9fe57 commit 6ec8b5b

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

Reactor.Example/ExamplePlugin.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public partial class ExamplePlugin : BasePlugin
2525

2626
public override void Load()
2727
{
28-
ReactorPingTracker.Register<ExamplePlugin>(ReactorPingTracker.AlwaysShow);
28+
ReactorCredits.Register<ExamplePlugin>(ReactorCredits.AlwaysShow);
2929

3030
this.AddComponent<ExampleComponent>();
3131

Reactor/Patches/Miscellaneous/PingTrackerPatch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal static class PingTrackerPatch
1111
[HarmonyPriority(Priority.Last)]
1212
public static void Postfix(PingTracker __instance)
1313
{
14-
var extraText = ReactorPingTracker.GetText();
14+
var extraText = ReactorCredits.GetText(ReactorCredits.Location.PingTracker);
1515
if (extraText != null)
1616
{
1717
if (!__instance.text.text.EndsWith("\n", StringComparison.InvariantCulture)) __instance.text.text += "\n";

Reactor/Patches/ReactorVersionShower.cs

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BepInEx;
33
using BepInEx.Unity.IL2CPP;
44
using HarmonyLib;
5+
using Reactor.Utilities;
56
using Reactor.Utilities.Extensions;
67
using TMPro;
78
using UnityEngine;
@@ -98,6 +99,13 @@ public static void UpdateText()
9899
Text.text = "Reactor " + Version.Parse(ReactorPlugin.Version).WithoutBuild();
99100
Text.text += "\nBepInEx " + Paths.BepInExVersion.WithoutBuild();
100101
Text.text += "\nMods: " + IL2CPPChainloader.Instance.Plugins.Count;
102+
103+
var creditsText = ReactorCredits.GetText(ReactorCredits.Location.MainMenu);
104+
if (creditsText != null)
105+
{
106+
Text.text += "\n" + creditsText;
107+
}
108+
101109
TextUpdated?.Invoke(Text);
102110
}
103111

Reactor/Utilities/ReactorPingTracker.cs Reactor/Utilities/ReactorCredits.cs

+48-23
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,78 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using BepInEx.Unity.IL2CPP;
5+
using Reactor.Patches;
56
using Reactor.Utilities.Extensions;
67

78
namespace Reactor.Utilities;
89

910
/// <summary>
10-
/// Controls the PingTracker.
11+
/// Provides a way for mods to show their version information in-game.
1112
/// </summary>
12-
public static class ReactorPingTracker
13+
public static class ReactorCredits
1314
{
14-
private readonly struct ModIdentifier(string name, string version, Func<bool>? shouldShow, bool isPreRelease)
15+
private readonly struct ModIdentifier(string name, string version, Func<Location, bool>? shouldShow, bool isPreRelease)
1516
{
1617
private const string NormalColor = "#fff";
1718
private const string PreReleaseColor = "#f00";
1819

1920
public string Name => name;
2021

21-
public bool ShouldShow => shouldShow == AlwaysShow || shouldShow();
2222
public string Text { get; } = $"{name} {version}".EscapeRichText().Color(isPreRelease ? PreReleaseColor : NormalColor);
23+
24+
public bool ShouldShow(Location location)
25+
{
26+
return shouldShow == AlwaysShow || shouldShow(location);
27+
}
2328
}
2429

2530
private static readonly List<ModIdentifier> _modIdentifiers = [];
2631

32+
/// <summary>
33+
/// Represents the location of where the credit is shown.
34+
/// </summary>
35+
public enum Location
36+
{
37+
/// <summary>
38+
/// In the main menu under Reactor/BepInEx versions.
39+
/// </summary>
40+
MainMenu,
41+
42+
/// <summary>
43+
/// During game under the ping tracker.
44+
/// </summary>
45+
PingTracker,
46+
}
47+
2748
/// <summary>
2849
/// A special value indicating a mod should always show.
2950
/// </summary>
30-
public const Func<bool>? AlwaysShow = null;
51+
public const Func<Location, bool>? AlwaysShow = null;
3152

3253
/// <summary>
33-
/// Registers a mod with the <see cref="ReactorPingTracker"/>, adding it to the list of mods that will be displayed in the PingTracker.
54+
/// Registers a mod with the <see cref="ReactorCredits"/>, adding it to the list of mods that will be displayed.
3455
/// </summary>
3556
/// <param name="name">The user-friendly name of the mod. Can contain spaces or special characters.</param>
3657
/// <param name="version">The version of the mod.</param>
37-
/// <param name="isPreRelease">If this version is a development or beta version. If true, it will display the mod in red in the PingTracker.</param>
58+
/// <param name="isPreRelease">If this version is a development or beta version. If true, it will display the mod in red.</param>
3859
/// <param name="shouldShow">
3960
/// This function will be called every frame to determine if the mod should be displayed or not.
4061
/// This function should return false if your mod is currently disabled or has no effect on gameplay at the time.
41-
/// If you want the mod to be displayed at all times, you can set this parameter to <see cref="ReactorPingTracker.AlwaysShow"/>.
62+
/// If you want the mod to be displayed at all times, you can set this parameter to <see cref="ReactorCredits.AlwaysShow"/>.
4263
/// </param>
43-
public static void Register(string name, string version, bool isPreRelease, Func<bool>? shouldShow)
64+
public static void Register(string name, string version, bool isPreRelease, Func<Location, bool>? shouldShow)
4465
{
4566
const int MaxLength = 60;
4667

4768
if (name.Length + version.Length > MaxLength)
4869
{
49-
Error($"Not registering mod \"{name}\" with version \"{version}\" in {nameof(ReactorPingTracker)} because the combined length of the mod name and version is greater than {MaxLength} characters.");
70+
Error($"Not registering mod \"{name}\" with version \"{version}\" in {nameof(ReactorCredits)} because the combined length of the mod name and version is greater than {MaxLength} characters.");
5071
return;
5172
}
5273

5374
if (_modIdentifiers.Any(m => m.Name == name))
5475
{
55-
Error($"Mod \"{name}\" is already registered in {nameof(ReactorPingTracker)}.");
76+
Error($"Mod \"{name}\" is already registered in {nameof(ReactorCredits)}.");
5677
return;
5778
}
5879

@@ -62,20 +83,22 @@ public static void Register(string name, string version, bool isPreRelease, Func
6283

6384
if (!isPreRelease)
6485
{
65-
Info($"Mod \"{name}\" registered in {nameof(ReactorPingTracker)} with version {version}.");
86+
Info($"Mod \"{name}\" registered in {nameof(ReactorCredits)} with version {version}.");
6687
}
6788
else
6889
{
69-
Warning($"Mod \"{name}\" registered in {nameof(ReactorPingTracker)} with DEVELOPMENT/BETA version {version}.");
90+
Warning($"Mod \"{name}\" registered in {nameof(ReactorCredits)} with DEVELOPMENT/BETA version {version}.");
7091
}
92+
93+
ReactorVersionShower.UpdateText();
7194
}
7295

7396
/// <summary>
74-
/// Registers a mod with the <see cref="ReactorPingTracker"/>, adding it to the list of mods that will be displayed in the PingTracker.
97+
/// Registers a mod with the <see cref="ReactorCredits"/>, adding it to the list of mods that will be displayed.
7598
/// </summary>
7699
/// <typeparam name="T">The BepInEx plugin type to get the name and version from.</typeparam>
77-
/// <param name="shouldShow"><inheritdoc cref="Register(string,string,bool,System.Func{bool})" path="/param[@name='shouldShow']"/></param>
78-
public static void Register<T>(Func<bool>? shouldShow) where T : BasePlugin
100+
/// <param name="shouldShow"><inheritdoc cref="Register(string,string,bool,System.Func{Location,bool})" path="/param[@name='shouldShow']"/></param>
101+
public static void Register<T>(Func<Location, bool>? shouldShow) where T : BasePlugin
79102
{
80103
var pluginInfo = IL2CPPChainloader.Instance.Plugins.Values.SingleOrDefault(p => p.TypeName == typeof(T).FullName)
81104
?? throw new ArgumentException("Couldn't find the metadata for the provided plugin type", nameof(T));
@@ -85,14 +108,16 @@ public static void Register<T>(Func<bool>? shouldShow) where T : BasePlugin
85108
Register(metadata.Name, metadata.Version.WithoutBuild().Clean(), metadata.Version.IsPreRelease, shouldShow);
86109
}
87110

88-
internal static string? GetText()
111+
internal static string? GetText(Location location)
89112
{
90-
var mods = _modIdentifiers.Where(m => m.ShouldShow).Select(m => m.Text).ToArray();
91-
if (mods.Length == 0)
92-
{
93-
return null;
94-
}
113+
var modTexts = _modIdentifiers.Where(m => m.ShouldShow(location)).Select(m => m.Text).ToArray();
114+
if (modTexts.Length == 0) return null;
95115

96-
return ("<space=3em>" + string.Join(", ", mods)).Size("50%").Align("center");
116+
return location switch
117+
{
118+
Location.MainMenu => string.Join('\n', modTexts),
119+
Location.PingTracker => ("<space=3em>" + string.Join(", ", modTexts)).Size("50%").Align("center"),
120+
_ => throw new ArgumentOutOfRangeException(nameof(location), location, null),
121+
};
97122
}
98123
}

0 commit comments

Comments
 (0)