From c3a3adbaeb1e6b85cd1e411567eb4910e73740c9 Mon Sep 17 00:00:00 2001
From: Emmanuel Hansen <emmausssss@gmail.com>
Date: Sun, 25 Jun 2023 12:17:21 +0000
Subject: [PATCH] add hle service generator

---
 src/Ryujinx.HLE.Generators/CodeGenerator.cs   | 63 +++++++++++++
 .../IpcServiceGenerator.cs                    | 94 +++++++++++++++++++
 .../Ryujinx.HLE.Generators.csproj             | 19 ++++
 .../ServiceSyntaxReceiver.cs                  | 30 ++++++
 .../HOS/Services/Sm/IUserInterface.cs         |  7 +-
 src/Ryujinx.HLE/Ryujinx.HLE.csproj            |  6 +-
 6 files changed, 214 insertions(+), 5 deletions(-)
 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/src/Ryujinx.HLE.Generators/CodeGenerator.cs b/src/Ryujinx.HLE.Generators/CodeGenerator.cs
new file mode 100644
index 000000000..726a07f0c
--- /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 000000000..bfeb972fc
--- /dev/null
+++ b/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs
@@ -0,0 +1,94 @@
+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.EnterScope($"namespace Ryujinx.rd");
+            generator.EnterScope($"public class Rd");
+
+            generator.AppendLine($"public string rd = \"\"\"");
+            foreach (var className in syntaxReceiver.Types)
+            {
+                if (className.Modifiers.Any(SyntaxKind.AbstractKeyword) || className.Modifiers.Any(SyntaxKind.PrivateKeyword))
+                    continue;
+
+                var name = GetFullName(className, context).Replace("global::", "");
+                generator.AppendLine($"<Type Name=\"{name}\"  Dynamic=\"Required All\" />");
+            }
+            generator.AppendLine($"\"\"\";");
+
+            generator.LeaveScope();
+            generator.LeaveScope();
+            context.AddSource($"rd.g.cs", generator.ToString());
+            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 000000000..eeab9c0e9
--- /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 000000000..6fc1c7199
--- /dev/null
+++ b/src/Ryujinx.HLE.Generators/ServiceSyntaxReceiver.cs
@@ -0,0 +1,30 @@
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using Microsoft.CodeAnalysis.CSharp;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Linq;
+
+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;
+                }
+
+                var name = classDeclaration.Identifier.ToString();
+
+                Types.Add(classDeclaration);
+            }
+        }
+    }
+}
diff --git a/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs b/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs
index 3dc82035f..7a90c664e 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 dbcb82212..9f179926f 100644
--- a/src/Ryujinx.HLE/Ryujinx.HLE.csproj
+++ b/src/Ryujinx.HLE/Ryujinx.HLE.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <TargetFramework>net8.0</TargetFramework>
@@ -11,7 +11,11 @@
     <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.Generators\Ryujinx.Horizon.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
     <ProjectReference Include="..\Ryujinx.Horizon.Kernel.Generators\Ryujinx.Horizon.Kernel.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
     <ProjectReference Include="..\Ryujinx.Horizon\Ryujinx.Horizon.csproj" />
     <ProjectReference Include="..\Ryujinx.Memory\Ryujinx.Memory.csproj" />