Nerfed/Nerfed.Editor/Project/Project.cs

135 lines
3.9 KiB
C#
Raw Normal View History

2024-07-19 15:24:50 +02:00
using Nerfed.Runtime;
namespace Nerfed.Editor.Project;
2024-07-21 14:03:40 +02:00
internal static class EditorProject
2024-07-19 15:24:50 +02:00
{
2024-07-21 22:40:16 +02:00
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;
2024-07-24 21:40:10 +02:00
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;
2024-07-21 14:03:40 +02:00
internal static bool Create(string projectFilePath, string projectName)
2024-07-19 15:24:50 +02:00
{
2024-07-21 14:03:40 +02:00
Close();
2024-07-19 15:24:50 +02:00
2024-07-21 14:03:40 +02:00
if (!Compiler.Project.Create(projectFilePath, projectName, out Compiler.Project project))
2024-07-19 15:24:50 +02:00
{
return false;
}
Open(projectFilePath);
Log.Info($"Succesfully created project.");
return true;
}
2024-07-24 21:40:10 +02:00
internal static bool Open(string projectFilePath)
2024-07-19 15:24:50 +02:00
{
2024-07-21 14:03:40 +02:00
Close();
2024-07-19 15:24:50 +02:00
2024-07-24 21:40:10 +02:00
if(!Compiler.Project.Open(projectFilePath, out Compiler.Project project))
2024-07-19 15:24:50 +02:00
{
return false;
}
2024-07-21 14:03:40 +02:00
Project = project;
2024-07-24 21:40:10 +02:00
ProjectFilePath = projectFilePath;
ProjectDirectory = Path.GetDirectoryName(projectFilePath);
2024-07-21 22:31:04 +02:00
2024-07-24 21:40:10 +02:00
string projectSolutionFilePath = Path.Combine(ProjectDirectory, Project.Name + Compiler.Generator.SolutionExtensionName);
2024-07-21 22:31:04 +02:00
if (File.Exists(projectSolutionFilePath))
{
ProjectSolutionFilePath = projectSolutionFilePath;
}
2024-07-21 14:03:40 +02:00
SetupDefaultFolders();
Compile();
Log.Info($"Opened project: {project.Name}");
2024-07-19 15:24:50 +02:00
return true;
}
2024-07-21 14:03:40 +02:00
internal static void Close()
2024-07-19 15:24:50 +02:00
{
2024-07-21 14:03:40 +02:00
Project = null;
ProjectFilePath = string.Empty;
ProjectSolutionFilePath = string.Empty;
2024-07-24 21:40:10 +02:00
ProjectDirectory = string.Empty;
ProjectContentDirectory = string.Empty;
ProjectTempDirectory = string.Empty;
2024-07-19 15:24:50 +02:00
}
2024-07-21 14:03:40 +02:00
internal static bool Save()
2024-07-19 15:24:50 +02:00
{
2024-07-21 14:03:40 +02:00
if(Project == null)
{
return false;
}
2024-07-20 00:46:08 +02:00
2024-07-21 14:03:40 +02:00
return Compiler.Project.Save(Project, ProjectFilePath);
2024-07-19 15:24:50 +02:00
}
2024-07-21 14:03:40 +02:00
internal static void Compile()
2024-07-20 00:46:08 +02:00
{
2024-07-21 14:03:40 +02:00
if(Project == null)
{
return;
}
Nerfed.Compiler.Compiler.Compile(ProjectFilePath, "Debug");
2024-07-20 00:46:08 +02:00
}
2024-07-21 14:03:40 +02:00
internal static void GenerateSolution()
2024-07-19 15:24:50 +02:00
{
2024-07-21 14:03:40 +02:00
if(Project == null)
{
return;
}
2024-07-24 21:40:10 +02:00
Nerfed.Compiler.Generator.GenerateSolution(ProjectDirectory, Project, out string solutionFilePath);
2024-07-21 14:03:40 +02:00
ProjectSolutionFilePath = solutionFilePath;
2024-07-19 15:24:50 +02:00
}
2024-07-21 14:03:40 +02:00
private static void SetupDefaultFolders()
2024-07-19 15:24:50 +02:00
{
2024-07-24 21:40:10 +02:00
if (Project == null || ProjectDirectory == null)
2024-07-21 14:03:40 +02:00
{
return;
}
2024-07-19 15:24:50 +02:00
2024-07-24 21:40:10 +02:00
string contentPath = Path.Combine(ProjectDirectory, "Content");
2024-07-19 15:24:50 +02:00
if (!Directory.Exists(contentPath))
{
Directory.CreateDirectory(contentPath);
}
2024-07-24 21:40:10 +02:00
ProjectContentDirectory = contentPath;
string scriptsPath = Path.Combine(ProjectContentDirectory, "Scripts");
2024-07-20 00:46:08 +02:00
if (!Directory.Exists(scriptsPath))
{
Directory.CreateDirectory(scriptsPath);
}
string scriptsRuntimePath = Path.Combine(scriptsPath, "Runtime");
if (!Directory.Exists(scriptsRuntimePath))
{
Directory.CreateDirectory(scriptsRuntimePath);
}
2024-07-19 15:24:50 +02:00
2024-07-21 22:31:04 +02:00
// Test create csproject.
2024-07-23 22:31:19 +02:00
string gameplayRuntimePath = Path.Combine(scriptsRuntimePath, Compiler.Generator.AssemblyDefinitionExtensionName);
2024-07-21 22:31:04 +02:00
if (!File.Exists(gameplayRuntimePath))
{
2024-07-23 22:31:19 +02:00
Compiler.AssemblyDefinition.Create(gameplayRuntimePath, "Gameplay", out Compiler.AssemblyDefinition project);
2024-07-21 22:31:04 +02:00
}
2024-07-24 21:40:10 +02:00
string tempPath = Path.Combine(ProjectDirectory, "Temp");
2024-07-19 15:24:50 +02:00
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
2024-07-24 21:40:10 +02:00
ProjectTempDirectory = tempPath;
2024-07-19 15:24:50 +02:00
}
}