Skip to content

Commit

Permalink
Implement menu item descriptions. Closes EXOK#45
Browse files Browse the repository at this point in the history
  • Loading branch information
jasminegamedev authored and axiand committed Mar 1, 2024
2 parents 4eb8bc3 + 42e086a commit a72051d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
21 changes: 15 additions & 6 deletions Celeste64.HookGen/IncrementalHookGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class IncrementalHookGenerator : IIncrementalGenerator
private const string ILHookNamespace = $"IL.{GameNamespace}";

private const string BindingFlags = "global::System.Reflection.BindingFlags";
private const string MethodInfo = "global::System.Reflection.MethodInfo";
private const string MethodBase = "global::System.Reflection.MethodBase";
private const string OnHookGenTargetAttribute = "global::Celeste64.Mod.InternalOnHookGenTargetAttribute";
private const string ILHookGenTargetAttribute = "global::Celeste64.Mod.InternalILHookGenTargetAttribute";

Expand Down Expand Up @@ -151,11 +151,20 @@ private static void GenerateCode(SourceProductionContext context, Compilation co
var bindingFlags = method.IsStatic
? $"{BindingFlags}.Static | {BindingFlags}.Public"
: $"{BindingFlags}.Instance | {BindingFlags}.Public";
var methodInfo = isOverloaded
? $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags}, [{
string.Join(", ", method.Parameters.Select(param => $"typeof({param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})"))}])"
: $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags})";
code.AppendLine($"{Indent}public static readonly {MethodInfo} m_{methodName} = {methodInfo};");
var paramTypes = string.Join(", ", method.Parameters.Select(param => $"typeof({param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})"));

string methodBase;
if (method.Name is ".ctor" or ".cctor")
{
methodBase = $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetConstructor({bindingFlags}, [{paramTypes}])";
}
else
{
methodBase = isOverloaded
? $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags}, [{paramTypes}])"
: $"typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}).GetMethod(\"{method.Name}\", {bindingFlags})";
}
code.AppendLine($"{Indent}public static readonly {MethodBase} m_{methodName} = {methodBase};");
}

// Hook attribute
Expand Down
1 change: 1 addition & 0 deletions Content/Text/English.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"OptionsInvertCamera": "Inverted Camera",

"FujiOptions": "Fuji Options...",
"FujiOptions.Description": "Configure options for the Fuji mod loader",
"FujiWriteLog": "Write Log File",
"FujiFailedToLoad": "Failed to load the following mods:",
"FujiEnableDebugMenu": "Enable Debug Menu (F6)",
Expand Down
4 changes: 1 addition & 3 deletions Source/Celeste64.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.1.0-prerelease.2"/>
<PackageReference Include="ImGui.NET" Version="1.90.1.1"/>

<ProjectReference Include="..\Celeste64.HookGen\Celeste64.HookGen.csproj" OutputItemType="Analyzer">
<Private>false</Private>
</ProjectReference>
<ProjectReference Include="..\Celeste64.HookGen\Celeste64.HookGen.csproj" OutputItemType="Analyzer" Private="false" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup Condition="($(RuntimeIdentifier) == '' and $([MSBuild]::IsOSPlatform('Linux'))) or $(RuntimeIdentifier.StartsWith('linux'))">
Expand Down
22 changes: 22 additions & 0 deletions Source/Helpers/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public abstract class Item
public virtual bool Selectable { get; } = true;
public virtual bool Pressed() => false;
public virtual void Slide(int dir) {}

public string Description { get; set; } = "";

public Item Describe(string str)
{
this.Description = str;

return this;
}
}

public class Submenu(string label, Menu? rootMenu, Menu? submenu = null) : Item
Expand Down Expand Up @@ -398,5 +407,18 @@ public virtual void Render(Batcher batch, Vec2 position)
batch.PushMatrix(position);
CurrentMenu.RenderItems(batch);
batch.PopMatrix();

// let's not crash if the menu has no items
// such as the modselectionmenu
if(CurrentMenu.items.Count > 0)
{
var currentItem = CurrentMenu.items[CurrentMenu.Index];

var text = currentItem.Description;
var justify = new Vec2(0.5f, -8f);
var color = Color.Gray;

UI.Text(batch, text, position, justify, color);
}
}
}
9 changes: 5 additions & 4 deletions Source/Mod/Core/ModLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using System.Text.Json;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;

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

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

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

foreach (var (info, attr) in ilHookMethods)
{
Log.Info($"Registering IL-hook for method '{attr.Target}' in type '{attr.Target.DeclaringType}' with hook method '{info}'");
HookManager.Instance.RegisterHook(new Hook(attr.Target, info));
Log.Info($"Registering IL-hook for method '{attr.Target}' in type '{attr.Target.DeclaringType}' with hook method '{info}' in type '{info.DeclaringType}'");
HookManager.Instance.RegisterILHook(new ILHook(attr.Target, info.CreateDelegate<ILContext.Manipulator>()));
}
}
}
}
4 changes: 2 additions & 2 deletions Source/Mod/Hook/InternalHookGenTargetAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Celeste64.Mod;
[AttributeUsage(AttributeTargets.Method)]
public class InternalOnHookGenTargetAttribute : Attribute
{
internal MethodInfo Target = null!;
internal MethodBase Target = null!;
}

[AttributeUsage(AttributeTargets.Method)]
public class InternalILHookGenTargetAttribute : Attribute
{
internal MethodInfo Target = null!;
internal MethodBase Target = null!;
}
3 changes: 2 additions & 1 deletion Source/Mod/Menu/GameOptionsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public GameOptionsMenu()
this.Add(new Menu.Slider(Loc.Str("OptionsBGM"), 0, 10, () => Save.Instance.MusicVolume, Save.Instance.SetMusicVolume));
this.Add(new Menu.Slider(Loc.Str("OptionsSFX"), 0, 10, () => Save.Instance.SfxVolume, Save.Instance.SetSfxVolume));
this.Add(new Menu.Spacer());
this.Add(new Menu.Submenu(Loc.Str("FujiOptions"), this, FujiOptionsMenu));
this.Add(new Menu.Submenu(Loc.Str("FujiOptions"), this, FujiOptionsMenu)
.Describe(Loc.Str("FujiOptions.Description")));
}
}

0 comments on commit a72051d

Please sign in to comment.