Nerfed/Nerfed.Editor/Project/Project.cs
2024-07-24 21:40:10 +02:00

135 lines
3.9 KiB
C#

using Nerfed.Runtime;
namespace Nerfed.Editor.Project;
internal static class EditorProject
{
internal static Compiler.Project Project { get; private set; } = null;
internal static string ProjectFilePath { get; private set; } = string.Empty;
internal static string ProjectSolutionFilePath { get; private set; } = string.Empty;
internal static string ProjectDirectory { get; private set; } = string.Empty;
internal static string ProjectContentDirectory { get; private set; } = string.Empty;
internal static string ProjectTempDirectory { get; private set; } = string.Empty;
internal static bool Create(string projectFilePath, string projectName)
{
Close();
if (!Compiler.Project.Create(projectFilePath, projectName, out Compiler.Project project))
{
return false;
}
Open(projectFilePath);
Log.Info($"Succesfully created project.");
return true;
}
internal static bool Open(string projectFilePath)
{
Close();
if(!Compiler.Project.Open(projectFilePath, out Compiler.Project project))
{
return false;
}
Project = project;
ProjectFilePath = projectFilePath;
ProjectDirectory = Path.GetDirectoryName(projectFilePath);
string projectSolutionFilePath = Path.Combine(ProjectDirectory, Project.Name + Compiler.Generator.SolutionExtensionName);
if (File.Exists(projectSolutionFilePath))
{
ProjectSolutionFilePath = projectSolutionFilePath;
}
SetupDefaultFolders();
Compile();
Log.Info($"Opened project: {project.Name}");
return true;
}
internal static void Close()
{
Project = null;
ProjectFilePath = string.Empty;
ProjectSolutionFilePath = string.Empty;
ProjectDirectory = string.Empty;
ProjectContentDirectory = string.Empty;
ProjectTempDirectory = string.Empty;
}
internal static bool Save()
{
if(Project == null)
{
return false;
}
return Compiler.Project.Save(Project, ProjectFilePath);
}
internal static void Compile()
{
if(Project == null)
{
return;
}
Nerfed.Compiler.Compiler.Compile(ProjectFilePath, "Debug");
}
internal static void GenerateSolution()
{
if(Project == null)
{
return;
}
Nerfed.Compiler.Generator.GenerateSolution(ProjectDirectory, Project, out string solutionFilePath);
ProjectSolutionFilePath = solutionFilePath;
}
private static void SetupDefaultFolders()
{
if (Project == null || ProjectDirectory == null)
{
return;
}
string contentPath = Path.Combine(ProjectDirectory, "Content");
if (!Directory.Exists(contentPath))
{
Directory.CreateDirectory(contentPath);
}
ProjectContentDirectory = contentPath;
string scriptsPath = Path.Combine(ProjectContentDirectory, "Scripts");
if (!Directory.Exists(scriptsPath))
{
Directory.CreateDirectory(scriptsPath);
}
string scriptsRuntimePath = Path.Combine(scriptsPath, "Runtime");
if (!Directory.Exists(scriptsRuntimePath))
{
Directory.CreateDirectory(scriptsRuntimePath);
}
// Test create csproject.
string gameplayRuntimePath = Path.Combine(scriptsRuntimePath, Compiler.Generator.AssemblyDefinitionExtensionName);
if (!File.Exists(gameplayRuntimePath))
{
Compiler.AssemblyDefinition.Create(gameplayRuntimePath, "Gameplay", out Compiler.AssemblyDefinition project);
}
string tempPath = Path.Combine(ProjectDirectory, "Temp");
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
ProjectTempDirectory = tempPath;
}
}