From c83f21e93255950bbf2fb07f896d1fb0f4902aff Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:43:01 +0100 Subject: [PATCH 1/4] Replace List<> with HashSet<> --- Ryujinx.CustomTasks/GenerateArrays.cs | 5 +++-- Ryujinx.CustomTasks/SyntaxWalker/ArraySizeCollector.cs | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Ryujinx.CustomTasks/GenerateArrays.cs b/Ryujinx.CustomTasks/GenerateArrays.cs index d65fad3..70f91f1 100644 --- a/Ryujinx.CustomTasks/GenerateArrays.cs +++ b/Ryujinx.CustomTasks/GenerateArrays.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using Ryujinx.CustomTasks.SyntaxWalker; using Ryujinx.CustomTasks.Helper; +using System.Linq; using Task = Microsoft.Build.Utilities.Task; namespace Ryujinx.CustomTasks @@ -15,7 +16,7 @@ namespace Ryujinx.CustomTasks private const string InterfaceFileName = "IArray.g.cs"; private const string ArraysFileName = "Arrays.g.cs"; - private readonly List _outputFiles = new List(); + private readonly HashSet _outputFiles = new HashSet(); [Required] public string ArrayNamespace { get; set; } @@ -58,7 +59,7 @@ namespace Ryujinx.CustomTasks _outputFiles.Add(filePath); } - private ICollection GetArraySizes(string itemPath) + private HashSet GetArraySizes(string itemPath) { Log.LogMessage(MessageImportance.Low, $"Searching for StructArray types in: {itemPath}"); diff --git a/Ryujinx.CustomTasks/SyntaxWalker/ArraySizeCollector.cs b/Ryujinx.CustomTasks/SyntaxWalker/ArraySizeCollector.cs index 5882bbe..832caaf 100644 --- a/Ryujinx.CustomTasks/SyntaxWalker/ArraySizeCollector.cs +++ b/Ryujinx.CustomTasks/SyntaxWalker/ArraySizeCollector.cs @@ -6,7 +6,7 @@ namespace Ryujinx.CustomTasks.SyntaxWalker { class ArraySizeCollector : CSharpSyntaxWalker { - public ICollection ArraySizes { get; } = new List(); + public HashSet ArraySizes { get; } = new HashSet(); private void AddArrayString(string name) { @@ -17,7 +17,7 @@ namespace Ryujinx.CustomTasks.SyntaxWalker string rawArrayType = name.Split('<')[0]; - if (int.TryParse(rawArrayType.Substring(5), out int size) && !ArraySizes.Contains(size)) + if (int.TryParse(rawArrayType.Substring(5), out int size)) { ArraySizes.Add(size); } From 17671611b22b5e295d99184ea1def7f30b741403 Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:44:04 +0100 Subject: [PATCH 2/4] Fix duplicate entries warning If generated sources already exist don't add them to _outputFiles. --- Ryujinx.CustomTasks/GenerateArrays.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Ryujinx.CustomTasks/GenerateArrays.cs b/Ryujinx.CustomTasks/GenerateArrays.cs index 70f91f1..267b7ad 100644 --- a/Ryujinx.CustomTasks/GenerateArrays.cs +++ b/Ryujinx.CustomTasks/GenerateArrays.cs @@ -55,8 +55,20 @@ namespace Ryujinx.CustomTasks private void AddGeneratedSource(string filePath, string content) { + bool addToOutputFiles = true; + + if (File.Exists(filePath)) + { + File.Delete(filePath); + addToOutputFiles = false; + } + File.WriteAllText(filePath, content); - _outputFiles.Add(filePath); + + if (addToOutputFiles) + { + _outputFiles.Add(filePath); + } } private HashSet GetArraySizes(string itemPath) @@ -174,9 +186,6 @@ namespace Ryujinx.CustomTasks string arraysFilePath = Path.Combine(OutputPath, ArraysFileName); List arraySizes = new List(); - File.Delete(interfaceFilePath); - File.Delete(arraysFilePath); - foreach (var item in InputFiles) { string fullPath = item.GetMetadata("FullPath"); From af835e2fac472e03e88f2f2b24b41cc84e78361e Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Fri, 16 Dec 2022 22:51:27 +0100 Subject: [PATCH 3/4] GenArray: Replace input properties --- Ryujinx.CustomTasks/GenerateArrays.cs | 22 ++++++++++++++----- .../build/Ryujinx.CustomTasks.targets | 13 ++++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Ryujinx.CustomTasks/GenerateArrays.cs b/Ryujinx.CustomTasks/GenerateArrays.cs index 267b7ad..aad6127 100644 --- a/Ryujinx.CustomTasks/GenerateArrays.cs +++ b/Ryujinx.CustomTasks/GenerateArrays.cs @@ -25,7 +25,19 @@ namespace Ryujinx.CustomTasks public string OutputPath { get; set; } [Required] - public ITaskItem[] InputFiles { get; set; } + public bool ScanSolution { get; set; } + + [Required] + public string SolutionDir { get; set; } + + [Required] + public string ProjectDir { get; set; } + + [Required] + public string NugetPackagePath { get; set; } + + [Required] + public string TargetFramework { get; set; } [Output] public string[] OutputFiles { get; set; } @@ -186,16 +198,14 @@ namespace Ryujinx.CustomTasks string arraysFilePath = Path.Combine(OutputPath, ArraysFileName); List arraySizes = new List(); - foreach (var item in InputFiles) + foreach (var item in Directory.EnumerateFiles(ScanSolution ? SolutionDir: ProjectDir, "*.cs", SearchOption.AllDirectories)) { - string fullPath = item.GetMetadata("FullPath"); - - if (fullPath.EndsWith(".g.cs") || fullPath.Contains(Path.Combine("obj","Debug")) || fullPath.Contains(Path.Combine("obj", "Release"))) + if (item.EndsWith(".g.cs") || item.Contains(Path.Combine("obj","Debug")) || item.Contains(Path.Combine("obj", "Release"))) { continue; } - foreach (int size in GetArraySizes(fullPath)) + foreach (int size in GetArraySizes(item)) { if (!arraySizes.Contains(size)) { diff --git a/Ryujinx.CustomTasks/build/Ryujinx.CustomTasks.targets b/Ryujinx.CustomTasks/build/Ryujinx.CustomTasks.targets index 13c2e07..5720276 100644 --- a/Ryujinx.CustomTasks/build/Ryujinx.CustomTasks.targets +++ b/Ryujinx.CustomTasks/build/Ryujinx.CustomTasks.targets @@ -3,16 +3,23 @@ - + + + false + + - + + - + From 0cfeddb88eb748c6b9f34449161eb7996896e4d0 Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Fri, 16 Dec 2022 22:52:46 +0100 Subject: [PATCH 4/4] GenArray: Add methods to get project references --- Ryujinx.CustomTasks/GenerateArrays.cs | 140 ++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/Ryujinx.CustomTasks/GenerateArrays.cs b/Ryujinx.CustomTasks/GenerateArrays.cs index aad6127..3a2523b 100644 --- a/Ryujinx.CustomTasks/GenerateArrays.cs +++ b/Ryujinx.CustomTasks/GenerateArrays.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using Ryujinx.CustomTasks.SyntaxWalker; using Ryujinx.CustomTasks.Helper; +using System; using System.Linq; using Task = Microsoft.Build.Utilities.Task; @@ -65,6 +66,145 @@ namespace Ryujinx.CustomTasks return size; } + private string[] GetProjectFiles() + { + if (ScanSolution) + { + return Directory.GetFiles(SolutionDir, "*.csproj", SearchOption.AllDirectories); + } + else + { + return Directory.GetFiles(ProjectDir, "*.csproj", SearchOption.TopDirectoryOnly); + } + } + + private bool TryGetNugetAssemblyPath(string package, string version, out string assemblyPath) + { + if (string.IsNullOrEmpty(version)) + { + assemblyPath = ""; + + return false; + } + + string basePath = Path.Combine(NugetPackagePath, package.ToLower(), version, "lib"); + string filePath; + + if (Directory.Exists(Path.Combine(basePath, TargetFramework))) + { + filePath = Directory.GetFiles(Path.Combine(basePath, TargetFramework), "*.dll", SearchOption.TopDirectoryOnly).FirstOrDefault(); + + if (string.IsNullOrEmpty(filePath)) + { + assemblyPath = ""; + + return false; + } + + assemblyPath = filePath; + + return true; + } + + string[] frameworks = Directory.GetDirectories(basePath); + + List standardList = frameworks.Where(framework => framework.Contains("netstandard")).ToList(); + + if (standardList.Count > 0) + { + filePath = Directory.GetFiles(Path.Combine(basePath, standardList.Max()), "*.dll", SearchOption.TopDirectoryOnly).FirstOrDefault(); + + if (string.IsNullOrEmpty(filePath)) + { + assemblyPath = ""; + + return false; + } + + assemblyPath = filePath; + + return true; + } + + assemblyPath = Directory.GetFiles(Path.Combine(basePath, frameworks.Max()), "*.dll", SearchOption.TopDirectoryOnly).FirstOrDefault(); + + return !string.IsNullOrEmpty(assemblyPath); + } + + private string GetAttributeValue(string line, string attribute) + { + int startIndex = line.IndexOf($"{attribute}=\"", StringComparison.OrdinalIgnoreCase) + 1; + int length = line.Substring(startIndex).IndexOf("\"", StringComparison.OrdinalIgnoreCase); + + return line.Substring(startIndex, length); + } + + private string GetCentralPackageVersion(string packageName) + { + string packagePropsPath = Path.Combine(SolutionDir, "Directory.Packages.props"); + + if (!File.Exists(packagePropsPath)) + { + return ""; + } + + foreach (var line in File.ReadLines(packagePropsPath)) + { + string trimmedLine = line.Trim(); + + if (trimmedLine.StartsWith(" GetReferences(string projectPath) + { + bool isItemGroup = false; + HashSet references = new HashSet(); + + // Filter for PackageReference and ProjectReference + foreach (string line in File.ReadLines(projectPath)) + { + string trimmedLine = line.Trim(); + + if (!isItemGroup && trimmedLine.Contains("")) + { + isItemGroup = true; + } + + switch (isItemGroup) + { + case true when trimmedLine.Contains("", StringComparison.OrdinalIgnoreCase): + isItemGroup = false; + break; + } + } + + return references; + } + private void AddGeneratedSource(string filePath, string content) { bool addToOutputFiles = true;