From 4231f902f2e45c57dde1cb1b1548c0eca207c3b7 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Wed, 19 Jun 2024 05:58:45 -0400 Subject: [PATCH] First working build process --- RecodeItLib/CrossCompiler/ProjectManager.cs | 19 ---- .../CrossCompiler/ReCodeItCrossCompiler.cs | 100 +++++++++++++++--- RecodeItLib/ReCodeItLib.csproj | 1 - RecodeItLib/Remapper/ReCodeItRemapper.cs | 9 +- 4 files changed, 92 insertions(+), 37 deletions(-) diff --git a/RecodeItLib/CrossCompiler/ProjectManager.cs b/RecodeItLib/CrossCompiler/ProjectManager.cs index 5891f72..af53916 100644 --- a/RecodeItLib/CrossCompiler/ProjectManager.cs +++ b/RecodeItLib/CrossCompiler/ProjectManager.cs @@ -1,5 +1,4 @@ using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; using Newtonsoft.Json; using ReCodeIt.Models; using ReCodeIt.Utils; @@ -179,22 +178,4 @@ public static class ProjectManager Logger.Log($"Found {AllProjectSourceFiles.Count} source files in the project", ConsoleColor.Yellow); } - - private static void AnalyzeSyntaxTree(SyntaxNode root) - { - // Example: Find all method declarations - var methodDeclarations = root.DescendantNodes().OfType(); - - foreach (var method in methodDeclarations) - { - Logger.Log($"Method: {method.Identifier.Text}"); - Logger.Log($"Return Type: {method.ReturnType}"); - Logger.Log("Parameters:"); - foreach (var parameter in method.ParameterList.Parameters) - { - Logger.Log($" {parameter.Type} {parameter.Identifier}"); - } - Logger.Log(string.Empty); - } - } } \ No newline at end of file diff --git a/RecodeItLib/CrossCompiler/ReCodeItCrossCompiler.cs b/RecodeItLib/CrossCompiler/ReCodeItCrossCompiler.cs index 5fa7bba..0cfae27 100644 --- a/RecodeItLib/CrossCompiler/ReCodeItCrossCompiler.cs +++ b/RecodeItLib/CrossCompiler/ReCodeItCrossCompiler.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using ReCodeIt.Models; using ReCodeIt.ReMapper; using ReCodeIt.Utils; +using System.Diagnostics; namespace ReCodeIt.CrossCompiler; @@ -19,14 +20,9 @@ public class ReCodeItCrossCompiler public CrossCompilerProjectModel ActiveProject => ProjectManager.ActiveProject; - /// - /// Key: Remapped name, value: old name - /// - public Dictionary ChangedTypes { get; set; } = []; - public void StartRemap() { - ChangedTypes.Clear(); + ActiveProject.ChangedTypes.Clear(); Remapper.InitializeRemap( ActiveProject.RemapModels, @@ -48,7 +44,7 @@ public class ReCodeItCrossCompiler Logger.Log("-----------------------------------------------", ConsoleColor.Yellow); Logger.Log($"Cross patch remap result", ConsoleColor.Yellow); - Logger.Log($"Changed {ChangedTypes.Count} types", ConsoleColor.Yellow); + Logger.Log($"Changed {ActiveProject.ChangedTypes.Count} types", ConsoleColor.Yellow); Logger.Log($"Original assembly path: {ActiveProject.OriginalAssemblyPath}", ConsoleColor.Yellow); Logger.Log($"Original assembly hash: {ActiveProject.OriginalAssemblyHash}", ConsoleColor.Yellow); Logger.Log($"Original patched assembly path: {ActiveProject.RemappedAssemblyPath}", ConsoleColor.Yellow); @@ -59,6 +55,9 @@ public class ReCodeItCrossCompiler public void StartCrossCompile() { AnalyzeSourceFiles(); + + StartBuild(); + MoveResult(); } private void AnalyzeSourceFiles() @@ -67,36 +66,107 @@ public class ReCodeItCrossCompiler { AnalyzeSourcefile(file); } + + var fileName = Path.GetFileName(ActiveProject.OriginalAssemblyPath); + var outPath = Path.Combine(ActiveProject.RemappedAssemblyPath, fileName); + + Logger.Log($"Placing original reference back into cloned build directory", ConsoleColor.Green); + File.Copy(ActiveProject.OriginalAssemblyPath, outPath, true); } private void AnalyzeSourcefile(string file) { - var source = LoadSourceFile(file); + var source = File.ReadAllText(file); var syntaxTree = CSharpSyntaxTree.ParseText(source); var root = syntaxTree.GetCompilationUnitRoot(); + // Get the things we want to change var identifiers = root.DescendantNodes() .OfType() .Where(id => ActiveProject.ChangedTypes.ContainsKey(id.Identifier.Text)); if (!identifiers.Any()) { return; } - Logger.Log($"found {identifiers.Count()} objects to change in file {Path.GetFileNameWithoutExtension(file)}"); + Logger.Log($"changing {identifiers.Count()} identifiers in file {Path.GetFileName(file)}", ConsoleColor.Green); - // Replace "RigClass" with "NewRigClass" + // Do Black Voodoo Magic var newRoot = root.ReplaceNodes(identifiers, (oldNode, newNode) => SyntaxFactory.IdentifierName(ActiveProject.ChangedTypes[oldNode.Identifier.Text]) .WithLeadingTrivia(oldNode.GetLeadingTrivia()) .WithTrailingTrivia(oldNode.GetTrailingTrivia())); + + File.WriteAllText(file, newRoot.ToFullString()); } /// - /// Loads a source file from disk + /// Starts the build process for the active project. /// - /// - /// - private string LoadSourceFile(string path) + private void StartBuild() + { + var path = Path.Combine( + DataProvider.ReCodeItProjectsPath, + ActiveProject.SolutionName); + + var csProjFile = Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories) + .ToList() + .FirstOrDefault(); + + if (csProjFile == null || csProjFile == string.Empty) + { + Logger.Log("No project files found in the solution directory or sub directories", ConsoleColor.Red); + return; + } + + var dirName = Path.GetDirectoryName(csProjFile); + + var solutionName = ActiveProject.SolutionName + ".sln"; + + var arguements = $"build {Path.Combine(path, solutionName)} " + + $"/p:Configuration=Debug " + + $"/p:Platform=\"Any CPU\""; + + // clean the project first + ExecuteDotnetCommand("clean", path); + + // Restore packages + ExecuteDotnetCommand("restore", path); + + Logger.Log(path + ActiveProject.SolutionName + ".sln"); + + // Then build the project + ExecuteDotnetCommand(arguements, path); + } + + private static void ExecuteDotnetCommand(string arguments, string workingDirectory) + { + ProcessStartInfo startInfo = new ProcessStartInfo + { + FileName = "dotnet", + Arguments = arguments, + WorkingDirectory = workingDirectory, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using (Process process = new Process()) + { + process.StartInfo = startInfo; + + process.OutputDataReceived += (sender, e) => Logger.Log(e.Data); + + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + process.WaitForExit(); + + int exitCode = process.ExitCode; + Logger.Log($"dotnet {arguments} exited with code {exitCode}"); + } + } + + private void MoveResult() { - return File.ReadAllText(path); } } \ No newline at end of file diff --git a/RecodeItLib/ReCodeItLib.csproj b/RecodeItLib/ReCodeItLib.csproj index 701e648..98a4c55 100644 --- a/RecodeItLib/ReCodeItLib.csproj +++ b/RecodeItLib/ReCodeItLib.csproj @@ -170,7 +170,6 @@ - diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs index f8550f7..00876d2 100644 --- a/RecodeItLib/Remapper/ReCodeItRemapper.cs +++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs @@ -79,6 +79,11 @@ public class ReCodeItRemapper // We are done, write the assembly WriteAssembly(); + + if (CrossMapMode) + { + ProjectManager.SaveCrossCompilerProjectModel(_compiler.ActiveProject); + } } /// @@ -185,7 +190,7 @@ public class ReCodeItRemapper if (CrossMapMode) { // Store the original types for caching - _compiler.ChangedTypes.Add(remap.NewTypeName, type.Name); + _compiler.ActiveProject.ChangedTypes.Add(remap.NewTypeName, type.Name); } type.Name = remap.NewTypeName; @@ -264,7 +269,7 @@ public class ReCodeItRemapper if (CrossMapMode) {// Store the original types for caching - _compiler.ChangedTypes.Add(highestScore.ProposedNewName, highestScore.Definition.Name); + _compiler.ActiveProject.ChangedTypes.Add(highestScore.ProposedNewName, highestScore.Definition.Name); } // Rename type and all associated type members