ppy / osu

rhythm is just a *click* away!
https://osu.ppy.sh
MIT License
14.64k stars 2.16k forks source link

Annotate `ControlPoint` and `Mod` for AOT trimming support #28503

Closed smoogipoo closed 2 weeks ago

smoogipoo commented 2 weeks ago

I have a native AOT compilation project here: https://github.com/smoogipoo/osu-native-diffcalc It compiles the most minimal types from osu+osu-framework+osuTK into a project that can be used to perform difficulty calculation with.

In it, I've had to add a few workarounds for these types which use Activator.CreateInstance() down various paths: https://github.com/smoogipoo/osu-native-diffcalc/blob/8fb300329425e52aa078b78728ba802bb8124345/Sources/osu.Game.Native.Desktop/Program.cs#L25-L71

This change should remove the need for these workarounds with very little code-wise overhead from the game itself. This is probably good practice in general though I'm not sure when we'll/if ever move towards trimming more aggressively in production.

Here's a test app:

using System.Diagnostics.CodeAnalysis;

new X().Clone().Print();
new Y().Clone().Print();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public class X
{
    public virtual void Print()
    {
        Console.WriteLine("hello from x!");
    }

    public X Clone() => (X)Activator.CreateInstance(GetType())!;
}

public class Y : X
{
    public override void Print()
    {
        Console.WriteLine("hello from y!");
    }
}

In .csproj:

<PublishAot>true</PublishAot>
<PublishTrimmed>true</PublishTrimmed>