Nerfed/Nerfed.Editor/Project/Project.cs

135 lines
3.6 KiB
C#

using Nerfed.Runtime;
namespace Nerfed.Editor.Project;
internal static class EditorProject
{
internal static Compiler.Project Project { get; private set; }
internal static string ProjectFilePath { get; private set; }
internal static string ProjectSolutionFilePath { get; private set; }
internal static string ProjectPath { get; private set; }
internal static string ProjectContentPath { get; private set; }
internal static string ProjectTempPath { get; private set; }
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 path)
{
Close();
if(!Compiler.Project.Open(path, out Compiler.Project project))
{
return false;
}
Project = project;
ProjectFilePath = path;
ProjectPath = Path.GetDirectoryName(path);
string projectSolutionFilePath = Path.Combine(ProjectPath, Project.Name + ".sln");
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;
ProjectPath = string.Empty;
ProjectContentPath = string.Empty;
ProjectTempPath = 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.Compiler.GenerateSolution(ProjectPath, Project, out string solutionFilePath);
ProjectSolutionFilePath = solutionFilePath;
}
private static void SetupDefaultFolders()
{
if (Project == null || ProjectPath == null)
{
return;
}
string contentPath = Path.Combine(ProjectPath, "Content");
if (!Directory.Exists(contentPath))
{
Directory.CreateDirectory(contentPath);
}
ProjectContentPath = contentPath;
string scriptsPath = Path.Combine(ProjectContentPath, "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, ".csproject");
if (!File.Exists(gameplayRuntimePath))
{
Compiler.CSProject.Create(gameplayRuntimePath, "Gameplay", out Compiler.CSProject project);
}
string tempPath = Path.Combine(ProjectPath, "Temp");
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
ProjectTempPath = tempPath;
}
}