From b9f5a4c56b50076b15e2250d04e01e980ddc181e Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Jul 2024 22:31:19 +0200 Subject: [PATCH] renamed some files and structure --- Nerfed.Compiler/AssemblyDefinition.cs | 64 ++++++++++ Nerfed.Compiler/CSProject.cs | 64 ---------- Nerfed.Compiler/Compiler.cs | 157 +------------------------ Nerfed.Compiler/Generator.cs | 161 ++++++++++++++++++++++++++ Nerfed.Compiler/Program.cs | 13 ++- Nerfed.Editor/Project/Project.cs | 8 +- 6 files changed, 242 insertions(+), 225 deletions(-) create mode 100644 Nerfed.Compiler/AssemblyDefinition.cs delete mode 100644 Nerfed.Compiler/CSProject.cs create mode 100644 Nerfed.Compiler/Generator.cs diff --git a/Nerfed.Compiler/AssemblyDefinition.cs b/Nerfed.Compiler/AssemblyDefinition.cs new file mode 100644 index 0000000..56f89ad --- /dev/null +++ b/Nerfed.Compiler/AssemblyDefinition.cs @@ -0,0 +1,64 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Nerfed.Compiler; + +public class AssemblyDefinition +{ + public string Name { get; set; } + public string Guid { get; set; } + //public bool IsEditor { get; set; } + // Add platform stuff here..? + // Add dll's here..? + // Add dependencies here..? + + public static bool Create(string assemblyDefinitionFilePath, string name, out AssemblyDefinition assemblyDefinition) + { + assemblyDefinition = null; + + if (File.Exists(assemblyDefinitionFilePath)) + { + Console.WriteLine($"ERROR: File already exists!"); + return false; + } + + // Create project file. + assemblyDefinition = new AssemblyDefinition + { + Name = name, + Guid = System.Guid.NewGuid().ToString("B").ToUpper(), + }; + + Save(assemblyDefinition, assemblyDefinitionFilePath); + + return true; + } + + public static bool Save(AssemblyDefinition assemblyDefinition, string assemblyDefinitionFilePath) + { + string jsonString = JsonSerializer.Serialize(assemblyDefinition, AssemblyDefinitionContext.Default.AssemblyDefinition); + + File.WriteAllText(assemblyDefinitionFilePath, jsonString); + + return true; + } + + public static bool Open(string assemblyDefinitionFilePath, out AssemblyDefinition assemblyDefinition) + { + string jsonString = File.ReadAllText(assemblyDefinitionFilePath); + assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition); + + if (assemblyDefinition == null) + { + Console.WriteLine($"ERROR: Could not open {typeof(AssemblyDefinition)}."); + return false; + } + + return true; + } +} + +[JsonSerializable(typeof(AssemblyDefinition))] +public partial class AssemblyDefinitionContext : JsonSerializerContext +{ +} \ No newline at end of file diff --git a/Nerfed.Compiler/CSProject.cs b/Nerfed.Compiler/CSProject.cs deleted file mode 100644 index 1e047c7..0000000 --- a/Nerfed.Compiler/CSProject.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace Nerfed.Compiler; - -public class CSProject -{ - public string Name { get; set; } - public string Guid { get; set; } - //public bool IsEditor { get; set; } - // Add platform stuff here..? - // Add dll's here..? - // Add dependencies here..? - - public static bool Create(string projectFilePath, string name, out CSProject project) - { - project = null; - - if (File.Exists(projectFilePath)) - { - Console.WriteLine($"ERROR: File already exists!"); - return false; - } - - // Create project file. - project = new CSProject - { - Name = name, - Guid = System.Guid.NewGuid().ToString("B").ToUpper(), - }; - - Save(project, projectFilePath); - - return true; - } - - public static bool Save(CSProject project, string projectFilePath) - { - string jsonString = JsonSerializer.Serialize(project, CSProjectContext.Default.CSProject); - - File.WriteAllText(projectFilePath, jsonString); - - return true; - } - - public static bool Open(string projectFilePath, out CSProject project) - { - string jsonString = File.ReadAllText(projectFilePath); - project = JsonSerializer.Deserialize(jsonString, CSProjectContext.Default.CSProject); - - if (project == null) - { - Console.WriteLine($"ERROR: Could not open {typeof(CSProject)}."); - return false; - } - - return true; - } -} - -[JsonSerializable(typeof(CSProject))] -public partial class CSProjectContext : JsonSerializerContext -{ -} \ No newline at end of file diff --git a/Nerfed.Compiler/Compiler.cs b/Nerfed.Compiler/Compiler.cs index 86c3756..2ffd3d7 100644 --- a/Nerfed.Compiler/Compiler.cs +++ b/Nerfed.Compiler/Compiler.cs @@ -1,23 +1,17 @@ using System.Diagnostics; -using System.Reflection; using System.Runtime.InteropServices; -using System.Text; -using System.Text.Json; namespace Nerfed.Compiler; public static class Compiler { - public const string CSProjectFileName = ".csproject"; - public const string CSProjFileName = ".csproj"; - public static bool Compile(string projectFilePath, string configuration = "Debug") { - string projectPath = Path.GetDirectoryName(projectFilePath); + string projectDirectory = Path.GetDirectoryName(projectFilePath); if (!File.Exists(projectFilePath)) { - Console.WriteLine($"ERROR: Project file not found at {projectPath}."); + Console.WriteLine($"ERROR: Project file not found at {projectDirectory}."); return false; } @@ -29,7 +23,7 @@ public static bool Compile(string projectFilePath, string configuration = "Debug // TODO: Check project version, to make sure we can compile it or something... // Generate solution. - GenerateSolution(projectPath, project, out string solutionFilePath); + Generator.GenerateSolution(projectDirectory, project, out string solutionFilePath); // Compile solution. ProcessStartInfo processInfo = new() @@ -85,147 +79,4 @@ public static bool Compile(string projectFilePath, string configuration = "Debug return true; } - - public static void GenerateSolution(string projectPath, Project project, out string solutionPath) - { - // Clear files. - string[] csProjectFiles = Directory.GetFiles(projectPath, $"*{CSProjFileName}", SearchOption.TopDirectoryOnly); - foreach (string csProjFile in csProjectFiles) - { - File.Delete(csProjFile); - } - - // Generate projects. - string[] csProjectFilePaths = Directory.GetFiles(projectPath, CSProjectFileName, SearchOption.AllDirectories); - foreach (string csProjectFilePath in csProjectFilePaths) - { - GenerateCSProject(csProjectFilePath, projectPath); - } - - // Generate solution. - string[] csProjPaths = Directory.GetFiles(projectPath, $"*{CSProjFileName}", SearchOption.TopDirectoryOnly); - string[] csProjGuids = new string[csProjPaths.Length]; - for (int i = 0; i < csProjPaths.Length; i++) - { - csProjGuids[i] = Guid.NewGuid().ToString("B").ToUpper(); - } - - StringBuilder solutionContent = new StringBuilder(); - - // Write the solution file header - solutionContent.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00"); - solutionContent.AppendLine("# Visual Studio Version 17"); - solutionContent.AppendLine("VisualStudioVersion = 17.10.35013.160"); - solutionContent.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1"); - - // Add each project to the solution file - for (int i = 0; i < csProjPaths.Length; i++) - { - string csProjPath = csProjPaths[i]; - string projectGuid = csProjGuids[i]; - string projectName = Path.GetFileNameWithoutExtension(csProjPath); - string projectRelativePath = Path.GetRelativePath(projectPath, csProjPath); - // FAE04EC0-301F-11D3-BF4B-00C04F79EFBC for C# projects. - solutionContent.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{projectName}\", \"{projectRelativePath}\", \"{projectGuid}\""); - solutionContent.AppendLine("EndProject"); - } - - // Add global sections (these can be extended as needed) - solutionContent.AppendLine("Global"); - solutionContent.AppendLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution"); - solutionContent.AppendLine(" Test|x64 = Test|x64"); - solutionContent.AppendLine(" Release|x64 = Release|x64"); - solutionContent.AppendLine(" Debug|x64 = Debug|x64"); - solutionContent.AppendLine(" EndGlobalSection"); - solutionContent.AppendLine(" GlobalSection(ProjectConfigurationPlatforms) = postSolution"); - - for (int i = 0; i < csProjPaths.Length; i++) - { - string projectGuid = csProjGuids[i]; - solutionContent.AppendLine($" {projectGuid}.Test|x64.ActiveCfg = Test|x64"); - solutionContent.AppendLine($" {projectGuid}.Test|x64.Build.0 = Test|x64"); - solutionContent.AppendLine($" {projectGuid}.Release|x64.ActiveCfg = Release|x64"); - solutionContent.AppendLine($" {projectGuid}.Release|x64.Build.0 = Release|x64"); - solutionContent.AppendLine($" {projectGuid}.Debug|x64.ActiveCfg = Debug|x64"); - solutionContent.AppendLine($" {projectGuid}.Debug|x64.Build.0 = Debug|x64"); - } - - solutionContent.AppendLine(" EndGlobalSection"); - solutionContent.AppendLine(" GlobalSection(SolutionProperties) = preSolution"); - solutionContent.AppendLine(" HideSolutionNode = FALSE"); - solutionContent.AppendLine(" EndGlobalSection"); - solutionContent.AppendLine("EndGlobal"); - - // Write the solution file content to disk - string solutionName = project.Name + ".sln"; - string filePath = Path.Combine(projectPath, solutionName); - File.WriteAllText(filePath, solutionContent.ToString()); - - solutionPath = filePath; - } - - private static void GenerateCSProject(string csProjectFilePath, string projectPath) - { - if (!File.Exists(csProjectFilePath)) - { - return; - } - - string jsonString = File.ReadAllText(csProjectFilePath); - CSProject csProject = JsonSerializer.Deserialize(jsonString, CSProjectContext.Default.CSProject); - - Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); - Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!"); - - // TODO: get all dependencies. - // TODO: properly get assemblies. - - StringBuilder projectContent = new StringBuilder(); - - projectContent.AppendLine(""); - - projectContent.AppendLine(" "); - projectContent.AppendLine(" net8.0"); - projectContent.AppendLine(" enable"); - projectContent.AppendLine(" disable"); - projectContent.AppendLine(" true"); - projectContent.AppendLine(" true"); - projectContent.AppendLine(" true"); - projectContent.AppendLine(" false"); - projectContent.AppendLine(" false"); - projectContent.AppendLine(" Debug;Test;Release"); - projectContent.AppendLine(" x64"); - projectContent.AppendLine(" "); - - projectContent.AppendLine(" "); - projectContent.AppendLine(" TRACE;LOG_INFO;PROFILING"); - projectContent.AppendLine(" "); - - projectContent.AppendLine(" "); - projectContent.AppendLine(" TRACE;LOG_ERROR;PROFILING"); - projectContent.AppendLine(" true"); - projectContent.AppendLine(" "); - - projectContent.AppendLine(" "); - projectContent.AppendLine(" TRACE;LOG_ERROR"); - projectContent.AppendLine(" true"); - projectContent.AppendLine(" "); - - projectContent.AppendLine(" "); - projectContent.AppendLine($" "); - projectContent.AppendLine(" "); - - projectContent.AppendLine(" "); - projectContent.AppendLine(" "); - projectContent.AppendLine($" {runtimeAssembly.Location}"); - projectContent.AppendLine(" false"); - projectContent.AppendLine(" "); - projectContent.AppendLine(" "); - - projectContent.AppendLine(""); - - string projectName = csProject.Name + ".csproj"; - string filePath = Path.Combine(projectPath, projectName); - File.WriteAllText(filePath, projectContent.ToString()); - } -} +} \ No newline at end of file diff --git a/Nerfed.Compiler/Generator.cs b/Nerfed.Compiler/Generator.cs new file mode 100644 index 0000000..885fe04 --- /dev/null +++ b/Nerfed.Compiler/Generator.cs @@ -0,0 +1,161 @@ +using System.Reflection; +using System.Text; +using System.Text.Json; + +namespace Nerfed.Compiler; + +public static class Generator +{ + public const string AssemblyDefinitionExtensionName = ".asmdef"; + public const string CSProjectExtensionName = ".csproj"; + public const string SolutionExtensionName = ".sln"; + + public static void GenerateSolution(string projectDirectory, Project project, out string solutionFilePath) + { + // Clear files. + ClearCSProjectFiles(projectDirectory); + + // Generate projects. + string[] assemblyDefinitionFilePaths = Directory.GetFiles(projectDirectory, AssemblyDefinitionExtensionName, SearchOption.AllDirectories); + foreach (string assemblyDefinitionFilePath in assemblyDefinitionFilePaths) + { + GenerateCSProject(assemblyDefinitionFilePath, projectDirectory, out string csProjectFilePath); + } + + // Generate solution. + string[] csProjectPaths = Directory.GetFiles(projectDirectory, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly); + string[] csProjectGuids = new string[csProjectPaths.Length]; + for (int i = 0; i < csProjectPaths.Length; i++) + { + csProjectGuids[i] = Guid.NewGuid().ToString("B").ToUpper(); + } + + StringBuilder content = new StringBuilder(); + + // Write the solution file header + content.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00"); + content.AppendLine("# Visual Studio Version 17"); + content.AppendLine("VisualStudioVersion = 17.10.35013.160"); + content.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1"); + + // Add each project to the solution file + for (int i = 0; i < csProjectPaths.Length; i++) + { + string csProjectPath = csProjectPaths[i]; + string csProjectGuid = csProjectGuids[i]; + string csProjectName = Path.GetFileNameWithoutExtension(csProjectPath); + string csProjectRelativePath = Path.GetRelativePath(projectDirectory, csProjectPath); + // FAE04EC0-301F-11D3-BF4B-00C04F79EFBC for C# projects. + content.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{csProjectName}\", \"{csProjectRelativePath}\", \"{csProjectGuid}\""); + content.AppendLine("EndProject"); + } + + // Add global sections (these can be extended as needed) + content.AppendLine("Global"); + content.AppendLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution"); + content.AppendLine(" Test|x64 = Test|x64"); + content.AppendLine(" Release|x64 = Release|x64"); + content.AppendLine(" Debug|x64 = Debug|x64"); + content.AppendLine(" EndGlobalSection"); + content.AppendLine(" GlobalSection(ProjectConfigurationPlatforms) = postSolution"); + + for (int i = 0; i < csProjectPaths.Length; i++) + { + string projectGuid = csProjectGuids[i]; + content.AppendLine($" {projectGuid}.Test|x64.ActiveCfg = Test|x64"); + content.AppendLine($" {projectGuid}.Test|x64.Build.0 = Test|x64"); + content.AppendLine($" {projectGuid}.Release|x64.ActiveCfg = Release|x64"); + content.AppendLine($" {projectGuid}.Release|x64.Build.0 = Release|x64"); + content.AppendLine($" {projectGuid}.Debug|x64.ActiveCfg = Debug|x64"); + content.AppendLine($" {projectGuid}.Debug|x64.Build.0 = Debug|x64"); + } + + content.AppendLine(" EndGlobalSection"); + content.AppendLine(" GlobalSection(SolutionProperties) = preSolution"); + content.AppendLine(" HideSolutionNode = FALSE"); + content.AppendLine(" EndGlobalSection"); + content.AppendLine("EndGlobal"); + + // Write the solution file content to disk + string solutionName = project.Name + SolutionExtensionName; + solutionFilePath = Path.Combine(projectDirectory, solutionName); + File.WriteAllText(solutionFilePath, content.ToString()); + } + + private static bool GenerateCSProject(string assemblyDefinitionFilePath, string projectPath, out string csProjectFilePath) + { + if (!File.Exists(assemblyDefinitionFilePath)) + { + csProjectFilePath = string.Empty; + return false; + } + + string jsonString = File.ReadAllText(assemblyDefinitionFilePath); + AssemblyDefinition assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition); + + Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); + Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!"); + + // TODO: get all dependencies. + // TODO: properly get assemblies. + + StringBuilder content = new StringBuilder(); + + content.AppendLine(""); + + content.AppendLine(" "); + content.AppendLine(" net8.0"); + content.AppendLine(" enable"); + content.AppendLine(" disable"); + content.AppendLine(" true"); + content.AppendLine(" true"); + content.AppendLine(" true"); + content.AppendLine(" false"); + content.AppendLine(" false"); + content.AppendLine(" Debug;Test;Release"); + content.AppendLine(" x64"); + content.AppendLine(" "); + + content.AppendLine(" "); + content.AppendLine(" TRACE;LOG_INFO;PROFILING"); + content.AppendLine(" "); + + content.AppendLine(" "); + content.AppendLine(" TRACE;LOG_ERROR;PROFILING"); + content.AppendLine(" true"); + content.AppendLine(" "); + + content.AppendLine(" "); + content.AppendLine(" TRACE;LOG_ERROR"); + content.AppendLine(" true"); + content.AppendLine(" "); + + content.AppendLine(" "); + content.AppendLine($" "); + content.AppendLine(" "); + + content.AppendLine(" "); + content.AppendLine(" "); + content.AppendLine($" {runtimeAssembly.Location}"); + content.AppendLine(" false"); + content.AppendLine(" "); + content.AppendLine(" "); + + content.AppendLine(""); + + string csProjectName = assemblyDefinition.Name + CSProjectExtensionName; + csProjectFilePath = Path.Combine(projectPath, csProjectName); + File.WriteAllText(csProjectFilePath, content.ToString()); + + return true; + } + + private static void ClearCSProjectFiles(string projectPath) + { + string[] csProjectFiles = Directory.GetFiles(projectPath, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly); + foreach (string csProjectFile in csProjectFiles) + { + File.Delete(csProjectFile); + } + } +} \ No newline at end of file diff --git a/Nerfed.Compiler/Program.cs b/Nerfed.Compiler/Program.cs index 1c34f84..0757bf2 100644 --- a/Nerfed.Compiler/Program.cs +++ b/Nerfed.Compiler/Program.cs @@ -1,10 +1,15 @@ -namespace Nerfed.Compiler +namespace Nerfed.Compiler; + +public class Program { - public class Program + internal static void Main(string[] args) { - internal static void Main(string[] args) + if (args.Length != 2) { - Compiler.Compile(args[0], args[1]); + Console.WriteLine("projectFilePath, configuration (Debug, Test, Release)"); + return; } + + Compiler.Compile(args[0], args[1]); } } \ No newline at end of file diff --git a/Nerfed.Editor/Project/Project.cs b/Nerfed.Editor/Project/Project.cs index 2f32698..1a09d43 100644 --- a/Nerfed.Editor/Project/Project.cs +++ b/Nerfed.Editor/Project/Project.cs @@ -39,7 +39,7 @@ internal static bool Open(string path) ProjectFilePath = path; ProjectPath = Path.GetDirectoryName(path); - string projectSolutionFilePath = Path.Combine(ProjectPath, Project.Name + ".sln"); + string projectSolutionFilePath = Path.Combine(ProjectPath, Project.Name + Compiler.Generator.SolutionExtensionName); if (File.Exists(projectSolutionFilePath)) { ProjectSolutionFilePath = projectSolutionFilePath; @@ -90,7 +90,7 @@ internal static void GenerateSolution() return; } - Nerfed.Compiler.Compiler.GenerateSolution(ProjectPath, Project, out string solutionFilePath); + Nerfed.Compiler.Generator.GenerateSolution(ProjectPath, Project, out string solutionFilePath); ProjectSolutionFilePath = solutionFilePath; } @@ -119,10 +119,10 @@ private static void SetupDefaultFolders() } // Test create csproject. - string gameplayRuntimePath = Path.Combine(scriptsRuntimePath, ".csproject"); + string gameplayRuntimePath = Path.Combine(scriptsRuntimePath, Compiler.Generator.AssemblyDefinitionExtensionName); if (!File.Exists(gameplayRuntimePath)) { - Compiler.CSProject.Create(gameplayRuntimePath, "Gameplay", out Compiler.CSProject project); + Compiler.AssemblyDefinition.Create(gameplayRuntimePath, "Gameplay", out Compiler.AssemblyDefinition project); } string tempPath = Path.Combine(ProjectPath, "Temp");