start working on project generation

This commit is contained in:
max 2024-07-19 15:24:50 +02:00
parent 92cf24fe9f
commit f978c49532
4 changed files with 250 additions and 6 deletions

View File

@ -1,4 +1,5 @@
using ImGuiNET; using ImGuiNET;
using Nerfed.Editor.Project;
using Nerfed.Runtime; using Nerfed.Runtime;
using Nerfed.Runtime.Graphics; using Nerfed.Runtime.Graphics;
using Nerfed.Runtime.Gui; using Nerfed.Runtime.Gui;
@ -56,6 +57,7 @@ private static void UpdateMainMenu()
} }
ImGui.EndMenu(); ImGui.EndMenu();
} }
ImGui.EndMainMenuBar(); ImGui.EndMainMenuBar();
} }
} }
@ -66,6 +68,8 @@ private static void HandleOnGui()
UpdateDock(); UpdateDock();
ImGui.ShowDemoWindow(); ImGui.ShowDemoWindow();
ProjectGui.OnGui();
} }
} }
} }

View File

@ -0,0 +1,198 @@
using Nerfed.Runtime;
using System.Reflection;
using System.Text.Json;
namespace Nerfed.Editor.Project;
public class ProjectConfig
{
public string Name { get; set; }
public uint Version { get; set; }
public string RuntimeProject { get; set; }
}
internal static class Project
{
private const string projectFileName = ".project";
internal static ProjectConfig LoadedConfig { get; private set; }
internal static string ProjectDirectory { get; private set; }
internal static string ProjectContentDirectory { get; private set; }
internal static string ProjectTempDirectory { get; private set; }
internal static bool Create(string path, string projectName)
{
// Check if folder is empty.
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if(Directory.GetFiles(path).Length != 0)
{
Log.Error($"Directory {path} is not empty!");
return false;
}
// Create project file.
ProjectConfig projectConfig = new ProjectConfig
{
Name = projectName,
Version = /*Nerfed.Runtime.Verion*/ 1,
};
// Create solution.
if (!GenerateCSProjectFiles(path, projectConfig))
{
Log.Error($"Generating cs project files failed!");
return false;
}
string projectFilePath = Path.Combine(path, projectFileName);
SaveProject(projectFilePath, projectConfig);
// Open the project.
Open(projectFilePath);
Log.Info($"Succesfully created project.");
return true;
}
internal static bool Open(string path)
{
string projectFilePath = path;
if (!File.Exists(projectFilePath)) {
projectFilePath = Path.Combine(path, projectFileName);
if (!File.Exists(projectFilePath))
{
Log.Error($"Project file not found at {path} or {projectFilePath}");
return false;
}
}
// Load from file.
string jsonString = File.ReadAllText(projectFilePath);
ProjectConfig projectConfig = JsonSerializer.Deserialize<ProjectConfig>(jsonString);
// Check version.
if(projectConfig.Version != /*Nerfed.Runtime.Verion*/ 1)
{
Log.Error($"Project runtime version does not match editor runtime verion!");
return false;
}
LoadedConfig = projectConfig;
SetupDefaultFolders(Path.GetDirectoryName(projectFilePath));
Log.Info($"Opened project: {projectConfig.Name}");
return true;
}
internal static bool SaveProject(string path, ProjectConfig projectConfig)
{
JsonSerializerOptions options = new JsonSerializerOptions(JsonSerializerOptions.Default)
{
WriteIndented = true,
};
string jsonString = JsonSerializer.Serialize(projectConfig, options);
File.WriteAllText(path, jsonString);
return true;
}
// Compile the project using the builder.
internal static bool CompileProject()
{
return false;
}
// Inspirered by https://github.com/ProwlEngine/Prowl/blob/main/Prowl.Editor/Project.cs
// Maybe the builder should do this?
// When compiling the 'runtimeAssembly.Location' needs to be updated to where ever the one is that is being used.
private static bool GenerateCSProjectFiles(string path, ProjectConfig projectConfig)
{
Log.Info("Generating CS project files");
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!");
string propertyGroup =
$@"
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<IsPackable>false</IsPackable>
<Configurations>Debug;Test;Release</Configurations>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|x64' "">
<DefineConstants>TRACE;LOG_INFO;PROFILING</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Test|x64' "">
<DefineConstants>TRACE;LOG_ERROR;PROFILING</DefineConstants>
<Optimize>true</Optimize>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Release|x64' "">
<DefineConstants>TRACE;LOG_ERROR</DefineConstants>
<Optimize>true</Optimize>
</PropertyGroup>
";
string runtimeProject =
$@"
<Project Sdk=""Microsoft.NET.Sdk"">
{propertyGroup}
<ItemGroup>
<Reference Include=""Nerfed.Runtime"">
<HintPath>{runtimeAssembly.Location}</HintPath>
<Private>false</Private>
</Reference>
</ItemGroup>
</Project>
";
string runtimeProjectName = projectConfig.Name + ".Runtime.csproj";
string filePath = Path.Combine(path, runtimeProjectName);
File.WriteAllText(filePath, runtimeProject);
projectConfig.RuntimeProject = runtimeProjectName;
Log.Info($"Generated runtime at {filePath}");
// TODO: Editor project.
return true;
}
private static void SetupDefaultFolders(string path)
{
ProjectDirectory = path;
string contentPath = Path.Combine(path, "Content");
if (!Directory.Exists(contentPath))
{
Directory.CreateDirectory(contentPath);
}
ProjectContentDirectory = contentPath;
string tempPath = Path.Combine(path, "Temp");
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
ProjectTempDirectory = tempPath;
}
}

View File

@ -0,0 +1,42 @@
using ImGuiNET;
namespace Nerfed.Editor.Project
{
internal static class ProjectGui
{
private static string projectDirectory = string.Empty;
private static string projectName = string.Empty;
internal static void OnGui()
{
ImGui.Begin("Project");
ImGui.InputText("Project Directory", ref projectDirectory, 512);
ImGui.InputText("Project Name", ref projectName, 512);
if (ImGui.Button("Create Project"))
{
Project.Create(projectDirectory, projectName);
}
if(ImGui.Button("Open Project"))
{
Project.Open(projectDirectory);
}
ImGui.Text("Loaded project: ");
if(Project.LoadedConfig != null)
{
ImGui.Text(Project.LoadedConfig.Name);
ImGui.Text(Project.LoadedConfig.Version.ToString());
ImGui.Text(Project.LoadedConfig.RuntimeProject);
}
else
{
ImGui.Text("None");
}
ImGui.End();
}
}
}

View File

@ -32,12 +32,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Libraries\SDL2CS\src\SDL2.cs"/> <Compile Include="Libraries\SDL2CS\src\SDL2.cs" />
<Compile Include="Libraries\RefreshCS\RefreshCS.cs"/> <Compile Include="Libraries\RefreshCS\RefreshCS.cs" />
<Compile Include="Libraries\FAudio\csharp\FAudio.cs"/> <Compile Include="Libraries\FAudio\csharp\FAudio.cs" />
<Compile Include="Libraries\WellspringCS\WellspringCS.cs"/> <Compile Include="Libraries\WellspringCS\WellspringCS.cs" />
<Compile Include="Libraries\dav1dfile\csharp\dav1dfile.cs"/> <Compile Include="Libraries\dav1dfile\csharp\dav1dfile.cs" />
<Compile Include="Libraries\ImGui.NET\src\ImGui.NET\**\*.cs"/> <Compile Include="Libraries\ImGui.NET\src\ImGui.NET\**\*.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>