Nerfed/Nerfed.Editor/Project/EditorProject.cs

173 lines
5.3 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
2024-09-08 20:58:13 +02:00
private static readonly List<(string, EditorAssemblyLoader.EditorAssemblyLoadContextWrapper)> editorAssemblyLoadContextWrappers = [];
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;
}
2024-09-08 20:58:13 +02:00
UnloadAssemblies();
2024-07-24 23:53:37 +02:00
Compiler.Compiler.Compile(ProjectFilePath, "Debug");
2024-09-08 20:58:13 +02:00
LoadAssemblies();
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 23:53:37 +02:00
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 23:53:37 +02:00
string contentDirectory = Path.Combine(ProjectDirectory, "Content");
if (!Directory.Exists(contentDirectory))
2024-07-19 15:24:50 +02:00
{
2024-07-24 23:53:37 +02:00
Directory.CreateDirectory(contentDirectory);
2024-07-19 15:24:50 +02:00
}
2024-07-24 23:53:37 +02:00
ProjectContentDirectory = contentDirectory;
string scriptsDirectory = Path.Combine(ProjectContentDirectory, "Scripts");
if (!Directory.Exists(scriptsDirectory))
2024-07-20 00:46:08 +02:00
{
2024-07-24 23:53:37 +02:00
Directory.CreateDirectory(scriptsDirectory);
2024-07-20 00:46:08 +02:00
}
2024-07-24 23:53:37 +02:00
string scriptsRuntimePath = Path.Combine(scriptsDirectory, "Runtime");
2024-07-20 00:46:08 +02:00
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-24 23:53:37 +02:00
string gameplayRuntimeFilePath = Path.Combine(scriptsRuntimePath, Compiler.Generator.AssemblyDefinitionExtensionName);
if (!File.Exists(gameplayRuntimeFilePath))
2024-07-21 22:31:04 +02:00
{
2024-07-24 23:53:37 +02:00
Compiler.AssemblyDefinition.Create(gameplayRuntimeFilePath, "Gameplay", out Compiler.AssemblyDefinition project);
2024-07-21 22:31:04 +02:00
}
2024-07-24 23:53:37 +02:00
string tempDirectory = Path.Combine(ProjectDirectory, "Temp");
if (!Directory.Exists(tempDirectory))
2024-07-19 15:24:50 +02:00
{
2024-07-24 23:53:37 +02:00
Directory.CreateDirectory(tempDirectory);
2024-07-19 15:24:50 +02:00
}
2024-07-24 23:53:37 +02:00
ProjectTempDirectory = tempDirectory;
2024-07-19 15:24:50 +02:00
}
2024-09-08 20:58:13 +02:00
private static void LoadAssemblies()
{
string[] assemblies = Directory.GetFiles(Path.Combine(ProjectDirectory, "bin"), "*.dll", SearchOption.AllDirectories);
foreach (string assembly in assemblies)
{
(System.Reflection.Assembly, EditorAssemblyLoader.EditorAssemblyLoadContextWrapper) a = EditorAssemblyLoader.LoadFromPath(assembly);
string name = a.Item1.GetName().Name;
editorAssemblyLoadContextWrappers.Add((name, a.Item2));
Log.Info($"loaded {name}");
}
Nerfed.Runtime.Generator.Hook.InvokeHooks();
}
private static void UnloadAssemblies()
{
for (int i = editorAssemblyLoadContextWrappers.Count - 1; i >= 0; i--)
{
(string, EditorAssemblyLoader.EditorAssemblyLoadContextWrapper) a = editorAssemblyLoadContextWrappers[i];
if (EditorAssemblyLoader.Unload(a.Item2))
{
Log.Info($"Unloaded {a.Item1}");
editorAssemblyLoadContextWrappers.RemoveAt(i);
}
else
{
Log.Error($"Could not unload {a.Item1}");
}
}
}
2024-07-19 15:24:50 +02:00
}