From 460f96967de6f5cb729ed57baaa4dad2178c8cb6 Mon Sep 17 00:00:00 2001 From: ACGNnsj Date: Tue, 28 Mar 2023 20:59:43 +0800 Subject: [PATCH] Slight Code Refactoring (#4373) * Simplify return statements by using ternary expressions * Remove a redundant type conversion * Reduce nesting by inverting "if" statements * Try to improve code readability by using LINQ and inverting "if" statements * Try to improve code readability by using LINQ, using ternary expressions, and inverting "if" statements * Add line breaks to long LINQ * Add line breaks to long LINQ --- .../Kernel/SyscallGenerator.cs | 48 +++++----------- .../Kernel/SyscallSyntaxReceiver.cs | 45 +++++++-------- Ryujinx.Horizon/Sdk/Sf/CommandHandler.cs | 7 +-- .../Sdk/Sf/CommandSerialization.cs | 2 +- .../Sdk/Sf/HipcCommandProcessor.cs | 57 ++++++++++--------- Ryujinx.Horizon/Sm/Impl/ServiceManager.cs | 15 +---- 6 files changed, 68 insertions(+), 106 deletions(-) diff --git a/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallGenerator.cs b/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallGenerator.cs index 8bc0800c..51da2187 100644 --- a/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallGenerator.cs +++ b/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallGenerator.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; namespace Ryujinx.Horizon.Generators.Kernel { @@ -151,24 +152,15 @@ namespace Ryujinx.Horizon.Generators.Kernel GenerateMethod32(generator, context.Compilation, method); GenerateMethod64(generator, context.Compilation, method); - foreach (var attributeList in method.AttributeLists) + foreach (AttributeSyntax attribute in method.AttributeLists.SelectMany(attributeList => + attributeList.Attributes.Where(attribute => + GetCanonicalTypeName(context.Compilation, attribute) == TypeSvcAttribute))) { - foreach (var attribute in attributeList.Attributes) - { - if (GetCanonicalTypeName(context.Compilation, attribute) != TypeSvcAttribute) - { - continue; - } - - foreach (var attributeArg in attribute.ArgumentList.Arguments) - { - if (attributeArg.Expression.Kind() == SyntaxKind.NumericLiteralExpression) - { - LiteralExpressionSyntax numericLiteral = (LiteralExpressionSyntax)attributeArg.Expression; - syscalls.Add(new SyscallIdAndName((int)numericLiteral.Token.Value, method.Identifier.Text)); - } - } - } + syscalls.AddRange(from attributeArg in attribute.ArgumentList.Arguments + where attributeArg.Expression.Kind() == SyntaxKind.NumericLiteralExpression + select (LiteralExpressionSyntax)attributeArg.Expression + into numericLiteral + select new SyscallIdAndName((int)numericLiteral.Token.Value, method.Identifier.Text)); } } @@ -510,28 +502,14 @@ namespace Ryujinx.Horizon.Generators.Kernel private static string GenerateCastFromUInt64(string value, string canonicalTargetTypeName, string targetTypeName) { - if (canonicalTargetTypeName == TypeSystemBoolean) - { - return $"({value} & 1) != 0"; - } - - return $"({targetTypeName}){value}"; + return canonicalTargetTypeName == TypeSystemBoolean ? $"({value} & 1) != 0" : $"({targetTypeName}){value}"; } private static bool IsPointerSized(Compilation compilation, ParameterSyntax parameterSyntax) { - foreach (var attributeList in parameterSyntax.AttributeLists) - { - foreach (var attribute in attributeList.Attributes) - { - if (GetCanonicalTypeName(compilation, attribute) == TypePointerSizedAttribute) - { - return true; - } - } - } - - return false; + return parameterSyntax.AttributeLists.Any(attributeList => + attributeList.Attributes.Any(attribute => + GetCanonicalTypeName(compilation, attribute) == TypePointerSizedAttribute)); } public void Initialize(GeneratorInitializationContext context) diff --git a/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallSyntaxReceiver.cs b/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallSyntaxReceiver.cs index e2e8e1d3..e480a859 100644 --- a/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallSyntaxReceiver.cs +++ b/Ryujinx.Horizon.Kernel.Generators/Kernel/SyscallSyntaxReceiver.cs @@ -16,38 +16,37 @@ namespace Ryujinx.Horizon.Generators.Kernel public void OnVisitSyntaxNode(SyntaxNode syntaxNode) { - if (syntaxNode is ClassDeclarationSyntax classDeclaration && classDeclaration.AttributeLists.Count != 0) + if (!(syntaxNode is ClassDeclarationSyntax classDeclaration) || classDeclaration.AttributeLists.Count == 0) { - foreach (var attributeList in classDeclaration.AttributeLists) - { - if (attributeList.Attributes.Any(x => x.Name.GetText().ToString() == "SvcImpl")) - { - foreach (var memberDeclaration in classDeclaration.Members) - { - if (memberDeclaration is MethodDeclarationSyntax methodDeclaration) - { - VisitMethod(methodDeclaration); - } - } + return; + } - break; - } + if (!classDeclaration.AttributeLists.Any(attributeList => + attributeList.Attributes.Any(x => x.Name.GetText().ToString() == "SvcImpl"))) + { + return; + } + + foreach (var memberDeclaration in classDeclaration.Members) + { + if (memberDeclaration is MethodDeclarationSyntax methodDeclaration) + { + VisitMethod(methodDeclaration); } } } private void VisitMethod(MethodDeclarationSyntax methodDeclaration) { - if (methodDeclaration.AttributeLists.Count != 0) + if (methodDeclaration.AttributeLists.Count == 0) { - foreach (var attributeList in methodDeclaration.AttributeLists) - { - if (attributeList.Attributes.Any(x => x.Name.GetText().ToString() == "Svc")) - { - SvcImplementations.Add(methodDeclaration); - break; - } - } + return; + } + + if (methodDeclaration.AttributeLists.Any(attributeList => + attributeList.Attributes.Any(x => x.Name.GetText().ToString() == "Svc"))) + { + SvcImplementations.Add(methodDeclaration); } } } diff --git a/Ryujinx.Horizon/Sdk/Sf/CommandHandler.cs b/Ryujinx.Horizon/Sdk/Sf/CommandHandler.cs index fe079d47..081ce3be 100644 --- a/Ryujinx.Horizon/Sdk/Sf/CommandHandler.cs +++ b/Ryujinx.Horizon/Sdk/Sf/CommandHandler.cs @@ -40,12 +40,7 @@ namespace Ryujinx.Horizon.Sdk.Sf var runtimeMetadata = context.Processor.GetRuntimeMetadata(); Result result = context.Processor.PrepareForProcess(ref context, runtimeMetadata); - if (result.IsFailure) - { - return result; - } - - return _invoke(ref context, _processor, runtimeMetadata, inRawData, ref outHeader); + return result.IsFailure ? result : _invoke(ref context, _processor, runtimeMetadata, inRawData, ref outHeader); } public static void GetCmifOutHeaderPointer(ref Span outHeader, ref Span outRawData) diff --git a/Ryujinx.Horizon/Sdk/Sf/CommandSerialization.cs b/Ryujinx.Horizon/Sdk/Sf/CommandSerialization.cs index feb2d666..4205d3c1 100644 --- a/Ryujinx.Horizon/Sdk/Sf/CommandSerialization.cs +++ b/Ryujinx.Horizon/Sdk/Sf/CommandSerialization.cs @@ -53,7 +53,7 @@ namespace Ryujinx.Horizon.Sdk.Sf public static void SerializeArg(Span outRawData, int offset, T value) where T : unmanaged { - MemoryMarshal.Cast(outRawData.Slice(offset, Unsafe.SizeOf()))[0] = (T)value; + MemoryMarshal.Cast(outRawData.Slice(offset, Unsafe.SizeOf()))[0] = value; } public static void SerializeCopyHandle(HipcMessageData response, int index, int value) diff --git a/Ryujinx.Horizon/Sdk/Sf/HipcCommandProcessor.cs b/Ryujinx.Horizon/Sdk/Sf/HipcCommandProcessor.cs index 6bba49ae..a0578d48 100644 --- a/Ryujinx.Horizon/Sdk/Sf/HipcCommandProcessor.cs +++ b/Ryujinx.Horizon/Sdk/Sf/HipcCommandProcessor.cs @@ -41,10 +41,8 @@ namespace Ryujinx.Horizon.Sdk.Sf { _args = args; - for (int i = 0; i < args.Length; i++) + foreach (CommandArg argInfo in args) { - var argInfo = args[i]; - switch (argInfo.Type) { case CommandArgType.Buffer: @@ -239,14 +237,13 @@ namespace Ryujinx.Horizon.Sdk.Sf { return mode == HipcBufferMode.NonSecure; } - else if (flags.HasFlag(HipcBufferFlags.MapTransferAllowsNonDevice)) + + if (flags.HasFlag(HipcBufferFlags.MapTransferAllowsNonDevice)) { return mode == HipcBufferMode.NonDevice; } - else - { - return mode == HipcBufferMode.Normal; - } + + return mode == HipcBufferMode.Normal; } public void SetOutBuffers(HipcMessageData response, bool[] isBufferMapAlias) @@ -261,28 +258,30 @@ namespace Ryujinx.Horizon.Sdk.Sf } var flags = _args[i].BufferFlags; - if (flags.HasFlag(HipcBufferFlags.Out)) + if (!flags.HasFlag(HipcBufferFlags.Out)) { - var buffer = _bufferRanges[i]; + continue; + } - if (flags.HasFlag(HipcBufferFlags.Pointer)) + var buffer = _bufferRanges[i]; + + if (flags.HasFlag(HipcBufferFlags.Pointer)) + { + response.SendStatics[recvPointerIndex] = new HipcStaticDescriptor(buffer.Address, (ushort)buffer.Size, recvPointerIndex); + } + else if (flags.HasFlag(HipcBufferFlags.AutoSelect)) + { + if (!isBufferMapAlias[i]) { response.SendStatics[recvPointerIndex] = new HipcStaticDescriptor(buffer.Address, (ushort)buffer.Size, recvPointerIndex); } - else if (flags.HasFlag(HipcBufferFlags.AutoSelect)) + else { - if (!isBufferMapAlias[i]) - { - response.SendStatics[recvPointerIndex] = new HipcStaticDescriptor(buffer.Address, (ushort)buffer.Size, recvPointerIndex); - } - else - { - response.SendStatics[recvPointerIndex] = new HipcStaticDescriptor(0UL, 0, recvPointerIndex); - } + response.SendStatics[recvPointerIndex] = new HipcStaticDescriptor(0UL, 0, recvPointerIndex); } - - recvPointerIndex++; } + + recvPointerIndex++; } } @@ -339,15 +338,17 @@ namespace Ryujinx.Horizon.Sdk.Sf int inObjectIndex = 0; - for (int i = 0; i < _args.Length; i++) + foreach (CommandArg t in _args) { - if (_args[i].Type == CommandArgType.InObject) + if (t.Type != CommandArgType.InObject) { - int index = inObjectIndex++; - var inObject = inObjects[index]; - - objects[index] = inObject?.ServiceObject; + continue; } + + int index = inObjectIndex++; + var inObject = inObjects[index]; + + objects[index] = inObject?.ServiceObject; } return Result.Success; diff --git a/Ryujinx.Horizon/Sm/Impl/ServiceManager.cs b/Ryujinx.Horizon/Sm/Impl/ServiceManager.cs index 44a1ec46..d1f94267 100644 --- a/Ryujinx.Horizon/Sm/Impl/ServiceManager.cs +++ b/Ryujinx.Horizon/Sm/Impl/ServiceManager.cs @@ -37,12 +37,7 @@ namespace Ryujinx.Horizon.Sm.Impl result = GetServiceImpl(out handle, ref _services[serviceIndex]); - if (result == KernelResult.SessionCountExceeded) - { - return SmResult.OutOfSessions; - } - - return result; + return result == KernelResult.SessionCountExceeded ? SmResult.OutOfSessions : result; } private Result GetServiceImpl(out int handle, ref ServiceInfo serviceInfo) @@ -61,13 +56,7 @@ namespace Ryujinx.Horizon.Sm.Impl } // TODO: Validation with GetProcessInfo etc. - - if (HasServiceInfo(name)) - { - return SmResult.AlreadyRegistered; - } - - return RegisterServiceImpl(out handle, processId, name, maxSessions, isLight); + return HasServiceInfo(name) ? SmResult.AlreadyRegistered : RegisterServiceImpl(out handle, processId, name, maxSessions, isLight); } public Result RegisterServiceForSelf(out int handle, ServiceName name, int maxSessions)