Skip to content

Commit a72051d

Browse files
jasminegamedevaxiand
authored andcommitted
Implement menu item descriptions. Closes EXOK#45
2 parents 4eb8bc3 + 42e086a commit a72051d

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

Celeste64.HookGen/IncrementalHookGenerator.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class IncrementalHookGenerator : IIncrementalGenerator
1818
private const string ILHookNamespace = $"IL.{GameNamespace}";
1919

2020
private const string BindingFlags = "global::System.Reflection.BindingFlags";
21-
private const string MethodInfo = "global::System.Reflection.MethodInfo";
21+
private const string MethodBase = "global::System.Reflection.MethodBase";
2222
private const string OnHookGenTargetAttribute = "global::Celeste64.Mod.InternalOnHookGenTargetAttribute";
2323
private const string ILHookGenTargetAttribute = "global::Celeste64.Mod.InternalILHookGenTargetAttribute";
2424

@@ -151,11 +151,20 @@ private static void GenerateCode(SourceProductionContext context, Compilation co
151151
var bindingFlags = method.IsStatic
152152
? $"{BindingFlags}.Static | {BindingFlags}.Public"
153153
: $"{BindingFlags}.Instance | {BindingFlags}.Public";
154-
var methodInfo = isOverloaded
155-
? $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags}, [{
156-
string.Join(", ", method.Parameters.Select(param => $"typeof({param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})"))}])"
157-
: $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags})";
158-
code.AppendLine($"{Indent}public static readonly {MethodInfo} m_{methodName} = {methodInfo};");
154+
var paramTypes = string.Join(", ", method.Parameters.Select(param => $"typeof({param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})"));
155+
156+
string methodBase;
157+
if (method.Name is ".ctor" or ".cctor")
158+
{
159+
methodBase = $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetConstructor({bindingFlags}, [{paramTypes}])";
160+
}
161+
else
162+
{
163+
methodBase = isOverloaded
164+
? $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags}, [{paramTypes}])"
165+
: $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags})";
166+
}
167+
code.AppendLine($"{Indent}public static readonly {MethodBase} m_{methodName} = {methodBase};");
159168
}
160169

161170
// Hook attribute

Content/Text/English.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"OptionsInvertCamera": "Inverted Camera",
2626

2727
"FujiOptions": "Fuji Options...",
28+
"FujiOptions.Description": "Configure options for the Fuji mod loader",
2829
"FujiWriteLog": "Write Log File",
2930
"FujiFailedToLoad": "Failed to load the following mods:",
3031
"FujiEnableDebugMenu": "Enable Debug Menu (F6)",

Source/Celeste64.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.1.0-prerelease.2"/>
2424
<PackageReference Include="ImGui.NET" Version="1.90.1.1"/>
2525

26-
<ProjectReference Include="..\Celeste64.HookGen\Celeste64.HookGen.csproj" OutputItemType="Analyzer">
27-
<Private>false</Private>
28-
</ProjectReference>
26+
<ProjectReference Include="..\Celeste64.HookGen\Celeste64.HookGen.csproj" OutputItemType="Analyzer" Private="false" PrivateAssets="All" />
2927
</ItemGroup>
3028

3129
<PropertyGroup Condition="($(RuntimeIdentifier) == '' and $([MSBuild]::IsOSPlatform('Linux'))) or $(RuntimeIdentifier.StartsWith('linux'))">

Source/Helpers/Menu.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ public abstract class Item
1313
public virtual bool Selectable { get; } = true;
1414
public virtual bool Pressed() => false;
1515
public virtual void Slide(int dir) {}
16+
17+
public string Description { get; set; } = "";
18+
19+
public Item Describe(string str)
20+
{
21+
this.Description = str;
22+
23+
return this;
24+
}
1625
}
1726

1827
public class Submenu(string label, Menu? rootMenu, Menu? submenu = null) : Item
@@ -398,5 +407,18 @@ public virtual void Render(Batcher batch, Vec2 position)
398407
batch.PushMatrix(position);
399408
CurrentMenu.RenderItems(batch);
400409
batch.PopMatrix();
410+
411+
// let's not crash if the menu has no items
412+
// such as the modselectionmenu
413+
if(CurrentMenu.items.Count > 0)
414+
{
415+
var currentItem = CurrentMenu.items[CurrentMenu.Index];
416+
417+
var text = currentItem.Description;
418+
var justify = new Vec2(0.5f, -8f);
419+
var color = Color.Gray;
420+
421+
UI.Text(batch, text, position, justify, color);
422+
}
401423
}
402424
}

Source/Mod/Core/ModLoader.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Reflection;
22
using System.Text.Json;
3+
using MonoMod.Cil;
34
using MonoMod.RuntimeDetour;
45

56
namespace Celeste64.Mod;
@@ -255,7 +256,7 @@ private static void FindAndRegisterHooks(Type type)
255256

256257
foreach (var (info, attr) in onHookMethods)
257258
{
258-
Log.Info($"Registering On-hook for method '{attr.Target}' in type '{attr.Target.DeclaringType}' with hook method '{info}'");
259+
Log.Info($"Registering On-hook for method '{attr.Target}' in type '{attr.Target.DeclaringType}' with hook method '{info}' in type '{info.DeclaringType}'");
259260
HookManager.Instance.RegisterHook(new Hook(attr.Target, info));
260261
}
261262

@@ -267,8 +268,8 @@ private static void FindAndRegisterHooks(Type type)
267268

268269
foreach (var (info, attr) in ilHookMethods)
269270
{
270-
Log.Info($"Registering IL-hook for method '{attr.Target}' in type '{attr.Target.DeclaringType}' with hook method '{info}'");
271-
HookManager.Instance.RegisterHook(new Hook(attr.Target, info));
271+
Log.Info($"Registering IL-hook for method '{attr.Target}' in type '{attr.Target.DeclaringType}' with hook method '{info}' in type '{info.DeclaringType}'");
272+
HookManager.Instance.RegisterILHook(new ILHook(attr.Target, info.CreateDelegate<ILContext.Manipulator>()));
272273
}
273274
}
274-
}
275+
}

Source/Mod/Hook/InternalHookGenTargetAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace Celeste64.Mod;
88
[AttributeUsage(AttributeTargets.Method)]
99
public class InternalOnHookGenTargetAttribute : Attribute
1010
{
11-
internal MethodInfo Target = null!;
11+
internal MethodBase Target = null!;
1212
}
1313

1414
[AttributeUsage(AttributeTargets.Method)]
1515
public class InternalILHookGenTargetAttribute : Attribute
1616
{
17-
internal MethodInfo Target = null!;
17+
internal MethodBase Target = null!;
1818
}

Source/Mod/Menu/GameOptionsMenu.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public GameOptionsMenu()
2626
this.Add(new Menu.Slider(Loc.Str("OptionsBGM"), 0, 10, () => Save.Instance.MusicVolume, Save.Instance.SetMusicVolume));
2727
this.Add(new Menu.Slider(Loc.Str("OptionsSFX"), 0, 10, () => Save.Instance.SfxVolume, Save.Instance.SetSfxVolume));
2828
this.Add(new Menu.Spacer());
29-
this.Add(new Menu.Submenu(Loc.Str("FujiOptions"), this, FujiOptionsMenu));
29+
this.Add(new Menu.Submenu(Loc.Str("FujiOptions"), this, FujiOptionsMenu)
30+
.Describe(Loc.Str("FujiOptions.Description")));
3031
}
3132
}

0 commit comments

Comments
 (0)