From 2c5c0392f9ff80a3907bbf376a13f797ebbc12cc Mon Sep 17 00:00:00 2001
From: Emmanuel Hansen <emmausssss@gmail.com>
Date: Sat, 31 Aug 2024 14:39:26 +0000
Subject: [PATCH] Make HLE project AOT friendly (#7085)

* add hle service generator

remove usage of reflection in device state

* remove rd.xml generation

* make applet manager reflection free

* fix typos

* fix encoding

* fix style report

* remove rogue generator reference

* remove double assignment
---
 Ryujinx.sln                                   |  6 ++
 src/Ryujinx.Graphics.Device/DeviceState.cs    |  5 +-
 src/Ryujinx.Graphics.Device/SizeCalculator.cs | 63 ---------------
 .../Engine/Threed/StateUpdateTracker.cs       |  5 +-
 src/Ryujinx.HLE.Generators/CodeGenerator.cs   | 63 +++++++++++++++
 .../IpcServiceGenerator.cs                    | 76 +++++++++++++++++++
 .../Ryujinx.HLE.Generators.csproj             | 19 +++++
 .../ServiceSyntaxReceiver.cs                  | 24 ++++++
 src/Ryujinx.HLE/HOS/Applets/AppletManager.cs  | 33 ++++----
 .../HOS/Services/Sm/IUserInterface.cs         |  7 +-
 src/Ryujinx.HLE/Ryujinx.HLE.csproj            |  1 +
 11 files changed, 215 insertions(+), 87 deletions(-)
 delete mode 100644 src/Ryujinx.Graphics.Device/SizeCalculator.cs
 create mode 100644 src/Ryujinx.HLE.Generators/CodeGenerator.cs
 create mode 100644 src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs
 create mode 100644 src/Ryujinx.HLE.Generators/Ryujinx.HLE.Generators.csproj
 create mode 100644 src/Ryujinx.HLE.Generators/ServiceSyntaxReceiver.cs

diff --git a/Ryujinx.sln b/Ryujinx.sln
index b8304164..76ebd573 100644
--- a/Ryujinx.sln
+++ b/Ryujinx.sln
@@ -87,6 +87,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ryujinx.Horizon", "src\Ryuj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ryujinx.Horizon.Kernel.Generators", "src\Ryujinx.Horizon.Kernel.Generators\Ryujinx.Horizon.Kernel.Generators.csproj", "{7F55A45D-4E1D-4A36-ADD3-87F29A285AA2}"
 EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ryujinx.HLE.Generators", "src\Ryujinx.HLE.Generators\Ryujinx.HLE.Generators.csproj", "{B575BCDE-2FD8-4A5D-8756-31CDD7FE81F0}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -249,6 +251,10 @@ Global
 		{7F55A45D-4E1D-4A36-ADD3-87F29A285AA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{7F55A45D-4E1D-4A36-ADD3-87F29A285AA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{7F55A45D-4E1D-4A36-ADD3-87F29A285AA2}.Release|Any CPU.Build.0 = Release|Any CPU
+		{B575BCDE-2FD8-4A5D-8756-31CDD7FE81F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{B575BCDE-2FD8-4A5D-8756-31CDD7FE81F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{B575BCDE-2FD8-4A5D-8756-31CDD7FE81F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{B575BCDE-2FD8-4A5D-8756-31CDD7FE81F0}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
diff --git a/src/Ryujinx.Graphics.Device/DeviceState.cs b/src/Ryujinx.Graphics.Device/DeviceState.cs
index de8582a3..54178a41 100644
--- a/src/Ryujinx.Graphics.Device/DeviceState.cs
+++ b/src/Ryujinx.Graphics.Device/DeviceState.cs
@@ -39,7 +39,10 @@ namespace Ryujinx.Graphics.Device
             {
                 var field = fields[fieldIndex];
 
-                int sizeOfField = SizeCalculator.SizeOf(field.FieldType);
+                var currentFieldOffset = (int)Marshal.OffsetOf<TState>(field.Name);
+                var nextFieldOffset = fieldIndex + 1 == fields.Length ? Unsafe.SizeOf<TState>() : (int)Marshal.OffsetOf<TState>(fields[fieldIndex + 1].Name);
+
+                int sizeOfField = nextFieldOffset - currentFieldOffset;
 
                 for (int i = 0; i < ((sizeOfField + 3) & ~3); i += 4)
                 {
diff --git a/src/Ryujinx.Graphics.Device/SizeCalculator.cs b/src/Ryujinx.Graphics.Device/SizeCalculator.cs
deleted file mode 100644
index 54820ec3..00000000
--- a/src/Ryujinx.Graphics.Device/SizeCalculator.cs
+++ /dev/null
@@ -1,63 +0,0 @@
-using System;
-using System.Reflection;
-
-namespace Ryujinx.Graphics.Device
-{
-    public static class SizeCalculator
-    {
-        public static int SizeOf(Type type)
-        {
-            // Is type a enum type?
-            if (type.IsEnum)
-            {
-                type = type.GetEnumUnderlyingType();
-            }
-
-            // Is type a pointer type?
-            if (type.IsPointer || type == typeof(IntPtr) || type == typeof(UIntPtr))
-            {
-                return IntPtr.Size;
-            }
-
-            // Is type a struct type?
-            if (type.IsValueType && !type.IsPrimitive)
-            {
-                // Check if the struct has a explicit size, if so, return that.
-                if (type.StructLayoutAttribute.Size != 0)
-                {
-                    return type.StructLayoutAttribute.Size;
-                }
-
-                // Otherwise we calculate the sum of the sizes of all fields.
-                int size = 0;
-                var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
-
-                for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++)
-                {
-                    size += SizeOf(fields[fieldIndex].FieldType);
-                }
-
-                return size;
-            }
-
-            // Primitive types.
-            return (Type.GetTypeCode(type)) switch
-            {
-                TypeCode.SByte => sizeof(sbyte),
-                TypeCode.Byte => sizeof(byte),
-                TypeCode.Int16 => sizeof(short),
-                TypeCode.UInt16 => sizeof(ushort),
-                TypeCode.Int32 => sizeof(int),
-                TypeCode.UInt32 => sizeof(uint),
-                TypeCode.Int64 => sizeof(long),
-                TypeCode.UInt64 => sizeof(ulong),
-                TypeCode.Char => sizeof(char),
-                TypeCode.Single => sizeof(float),
-                TypeCode.Double => sizeof(double),
-                TypeCode.Decimal => sizeof(decimal),
-                TypeCode.Boolean => sizeof(bool),
-                _ => throw new ArgumentException($"Length for type \"{type.Name}\" is unknown."),
-            };
-        }
-    }
-}
diff --git a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdateTracker.cs b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdateTracker.cs
index e54855a8..effcb7bb 100644
--- a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdateTracker.cs
+++ b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdateTracker.cs
@@ -79,7 +79,10 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
             {
                 var field = fields[fieldIndex];
 
-                int sizeOfField = SizeCalculator.SizeOf(field.FieldType);
+                var currentFieldOffset = (int)Marshal.OffsetOf<TState>(field.Name);
+                var nextFieldOffset = fieldIndex + 1 == fields.Length ? Unsafe.SizeOf<TState>() : (int)Marshal.OffsetOf<TState>(fields[fieldIndex + 1].Name);
+
+                int sizeOfField = nextFieldOffset - currentFieldOffset;
 
                 if (fieldToDelegate.TryGetValue(field.Name, out int entryIndex))
                 {
diff --git a/src/Ryujinx.HLE.Generators/CodeGenerator.cs b/src/Ryujinx.HLE.Generators/CodeGenerator.cs
new file mode 100644
index 00000000..7e4848ad
--- /dev/null
+++ b/src/Ryujinx.HLE.Generators/CodeGenerator.cs
@@ -0,0 +1,63 @@
+using System.Text;
+
+namespace Ryujinx.HLE.Generators
+{
+    class CodeGenerator
+    {
+        private const int IndentLength = 4;
+
+        private readonly StringBuilder _sb;
+        private int _currentIndentCount;
+
+        public CodeGenerator()
+        {
+            _sb = new StringBuilder();
+        }
+
+        public void EnterScope(string header = null)
+        {
+            if (header != null)
+            {
+                AppendLine(header);
+            }
+
+            AppendLine("{");
+            IncreaseIndentation();
+        }
+
+        public void LeaveScope(string suffix = "")
+        {
+            DecreaseIndentation();
+            AppendLine($"}}{suffix}");
+        }
+
+        public void IncreaseIndentation()
+        {
+            _currentIndentCount++;
+        }
+
+        public void DecreaseIndentation()
+        {
+            if (_currentIndentCount - 1 >= 0)
+            {
+                _currentIndentCount--;
+            }
+        }
+
+        public void AppendLine()
+        {
+            _sb.AppendLine();
+        }
+
+        public void AppendLine(string text)
+        {
+            _sb.Append(' ', IndentLength * _currentIndentCount);
+            _sb.AppendLine(text);
+        }
+
+        public override string ToString()
+        {
+            return _sb.ToString();
+        }
+    }
+}
diff --git a/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs b/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs
new file mode 100644
index 00000000..19fdbe19
--- /dev/null
+++ b/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs
@@ -0,0 +1,76 @@
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using System.Linq;
+
+namespace Ryujinx.HLE.Generators
+{
+    [Generator]
+    public class IpcServiceGenerator : ISourceGenerator
+    {
+        public void Execute(GeneratorExecutionContext context)
+        {
+            var syntaxReceiver = (ServiceSyntaxReceiver)context.SyntaxReceiver;
+            CodeGenerator generator = new CodeGenerator();
+
+            generator.AppendLine("using System;");
+            generator.EnterScope($"namespace Ryujinx.HLE.HOS.Services.Sm");
+            generator.EnterScope($"partial class IUserInterface");
+
+            generator.EnterScope($"public IpcService? GetServiceInstance(Type type, ServiceCtx context, object? parameter = null)");
+            foreach (var className in syntaxReceiver.Types)
+            {
+                if (className.Modifiers.Any(SyntaxKind.AbstractKeyword) || className.Modifiers.Any(SyntaxKind.PrivateKeyword) || !className.AttributeLists.Any(x => x.Attributes.Any(y => y.ToString().StartsWith("Service"))))
+                    continue;
+                var name = GetFullName(className, context).Replace("global::", "");
+                if (!name.StartsWith("Ryujinx.HLE.HOS.Services"))
+                    continue;
+                var constructors = className.ChildNodes().Where(x => x.IsKind(SyntaxKind.ConstructorDeclaration)).Select(y => y as ConstructorDeclarationSyntax);
+
+                if (!constructors.Any(x => x.ParameterList.Parameters.Count >= 1))
+                    continue;
+
+                if (constructors.Where(x => x.ParameterList.Parameters.Count >= 1).FirstOrDefault().ParameterList.Parameters[0].Type.ToString() == "ServiceCtx")
+                {
+                    generator.EnterScope($"if (type == typeof({GetFullName(className, context)}))");
+                    if (constructors.Any(x => x.ParameterList.Parameters.Count == 2))
+                    {
+                        var type = constructors.Where(x => x.ParameterList.Parameters.Count == 2).FirstOrDefault().ParameterList.Parameters[1].Type;
+                        var model = context.Compilation.GetSemanticModel(type.SyntaxTree);
+                        var typeSymbol = model.GetSymbolInfo(type).Symbol as INamedTypeSymbol;
+                        var fullName = typeSymbol.ToString();
+                        generator.EnterScope("if (parameter != null)");
+                        generator.AppendLine($"return new {GetFullName(className, context)}(context, ({fullName})parameter);");
+                        generator.LeaveScope();
+                    }
+
+                    if (constructors.Any(x => x.ParameterList.Parameters.Count == 1))
+                    {
+                        generator.AppendLine($"return new {GetFullName(className, context)}(context);");
+                    }
+
+                    generator.LeaveScope();
+                }
+            }
+
+            generator.AppendLine("return null;");
+            generator.LeaveScope();
+
+            generator.LeaveScope();
+            generator.LeaveScope();
+            context.AddSource($"IUserInterface.g.cs", generator.ToString());
+        }
+
+        private string GetFullName(ClassDeclarationSyntax syntaxNode, GeneratorExecutionContext context)
+        {
+            var typeSymbol = context.Compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetDeclaredSymbol(syntaxNode);
+
+            return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
+        }
+
+        public void Initialize(GeneratorInitializationContext context)
+        {
+            context.RegisterForSyntaxNotifications(() => new ServiceSyntaxReceiver());
+        }
+    }
+}
diff --git a/src/Ryujinx.HLE.Generators/Ryujinx.HLE.Generators.csproj b/src/Ryujinx.HLE.Generators/Ryujinx.HLE.Generators.csproj
new file mode 100644
index 00000000..eeab9c0e
--- /dev/null
+++ b/src/Ryujinx.HLE.Generators/Ryujinx.HLE.Generators.csproj
@@ -0,0 +1,19 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <TargetFramework>netstandard2.0</TargetFramework>
+        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
+        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
+        <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
+        <IsRoslynComponent>true</IsRoslynComponent>
+    </PropertyGroup>
+
+    <ItemGroup>
+        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers">
+            <PrivateAssets>all</PrivateAssets>
+            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+        </PackageReference>
+        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
+    </ItemGroup>
+
+</Project>
diff --git a/src/Ryujinx.HLE.Generators/ServiceSyntaxReceiver.cs b/src/Ryujinx.HLE.Generators/ServiceSyntaxReceiver.cs
new file mode 100644
index 00000000..e4269cb9
--- /dev/null
+++ b/src/Ryujinx.HLE.Generators/ServiceSyntaxReceiver.cs
@@ -0,0 +1,24 @@
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using System.Collections.Generic;
+
+namespace Ryujinx.HLE.Generators
+{
+    internal class ServiceSyntaxReceiver : ISyntaxReceiver
+    {
+        public HashSet<ClassDeclarationSyntax> Types = new HashSet<ClassDeclarationSyntax>();
+
+        public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
+        {
+            if (syntaxNode is ClassDeclarationSyntax classDeclaration)
+            {
+                if (classDeclaration.BaseList == null)
+                {
+                    return;
+                }
+
+                Types.Add(classDeclaration);
+            }
+        }
+    }
+}
diff --git a/src/Ryujinx.HLE/HOS/Applets/AppletManager.cs b/src/Ryujinx.HLE/HOS/Applets/AppletManager.cs
index 30300f1b..3c34d5c7 100644
--- a/src/Ryujinx.HLE/HOS/Applets/AppletManager.cs
+++ b/src/Ryujinx.HLE/HOS/Applets/AppletManager.cs
@@ -8,27 +8,24 @@ namespace Ryujinx.HLE.HOS.Applets
 {
     static class AppletManager
     {
-        private static readonly Dictionary<AppletId, Type> _appletMapping;
-
-        static AppletManager()
-        {
-            _appletMapping = new Dictionary<AppletId, Type>
-            {
-                { AppletId.Error,            typeof(ErrorApplet)            },
-                { AppletId.PlayerSelect,     typeof(PlayerSelectApplet)     },
-                { AppletId.Controller,       typeof(ControllerApplet)       },
-                { AppletId.SoftwareKeyboard, typeof(SoftwareKeyboardApplet) },
-                { AppletId.LibAppletWeb,     typeof(BrowserApplet)          },
-                { AppletId.LibAppletShop,    typeof(BrowserApplet)          },
-                { AppletId.LibAppletOff,     typeof(BrowserApplet)          },
-            };
-        }
-
         public static IApplet Create(AppletId applet, Horizon system)
         {
-            if (_appletMapping.TryGetValue(applet, out Type appletClass))
+            switch (applet)
             {
-                return (IApplet)Activator.CreateInstance(appletClass, system);
+                case AppletId.Controller:
+                    return new ControllerApplet(system);
+                case AppletId.Error:
+                    return new ErrorApplet(system);
+                case AppletId.PlayerSelect:
+                    return new PlayerSelectApplet(system);
+                case AppletId.SoftwareKeyboard:
+                    return new SoftwareKeyboardApplet(system);
+                case AppletId.LibAppletWeb:
+                    return new BrowserApplet(system);
+                case AppletId.LibAppletShop:
+                    return new BrowserApplet(system);
+                case AppletId.LibAppletOff:
+                    return new BrowserApplet(system);
             }
 
             throw new NotImplementedException($"{applet} applet is not implemented.");
diff --git a/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs b/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs
index 3dc82035..7a90c664 100644
--- a/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs
+++ b/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs
@@ -2,6 +2,7 @@ using Ryujinx.Common.Logging;
 using Ryujinx.HLE.HOS.Ipc;
 using Ryujinx.HLE.HOS.Kernel;
 using Ryujinx.HLE.HOS.Kernel.Ipc;
+using Ryujinx.HLE.HOS.Services.Apm;
 using Ryujinx.Horizon.Common;
 using System;
 using System.Collections.Generic;
@@ -12,7 +13,7 @@ using System.Text;
 
 namespace Ryujinx.HLE.HOS.Services.Sm
 {
-    class IUserInterface : IpcService
+    partial class IUserInterface : IpcService
     {
         private static readonly Dictionary<string, Type> _services;
 
@@ -95,9 +96,7 @@ namespace Ryujinx.HLE.HOS.Services.Sm
                 {
                     ServiceAttribute serviceAttribute = (ServiceAttribute)type.GetCustomAttributes(typeof(ServiceAttribute)).First(service => ((ServiceAttribute)service).Name == name);
 
-                    IpcService service = serviceAttribute.Parameter != null
-                        ? (IpcService)Activator.CreateInstance(type, context, serviceAttribute.Parameter)
-                        : (IpcService)Activator.CreateInstance(type, context);
+                    IpcService service = GetServiceInstance(type, context, serviceAttribute.Parameter);
 
                     service.TrySetServer(_commonServer);
                     service.Server.AddSessionObj(session.ServerSession, service);
diff --git a/src/Ryujinx.HLE/Ryujinx.HLE.csproj b/src/Ryujinx.HLE/Ryujinx.HLE.csproj
index 83a11d4e..a7bb3cd7 100644
--- a/src/Ryujinx.HLE/Ryujinx.HLE.csproj
+++ b/src/Ryujinx.HLE/Ryujinx.HLE.csproj
@@ -12,6 +12,7 @@
     <ProjectReference Include="..\Ryujinx.Graphics.Host1x\Ryujinx.Graphics.Host1x.csproj" />
     <ProjectReference Include="..\Ryujinx.Graphics.Nvdec\Ryujinx.Graphics.Nvdec.csproj" />
     <ProjectReference Include="..\Ryujinx.Graphics.Vic\Ryujinx.Graphics.Vic.csproj" />
+    <ProjectReference Include="..\Ryujinx.HLE.Generators\Ryujinx.HLE.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
     <ProjectReference Include="..\Ryujinx.Horizon.Common\Ryujinx.Horizon.Common.csproj" />
     <ProjectReference Include="..\Ryujinx.Horizon.Kernel.Generators\Ryujinx.Horizon.Kernel.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
     <ProjectReference Include="..\Ryujinx.Horizon\Ryujinx.Horizon.csproj" />