Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
09089c35b9 | |||
bd76fc1b25 | |||
d80cc51aff | |||
ad2f527de5 | |||
b4c3b5ed18 | |||
b9f5a4c56b | |||
50a77d5120 | |||
91672d5760 | |||
546b7feca7 | |||
36a134170a | |||
6e41c2579c | |||
2afbd9defe | |||
f978c49532 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -16,6 +16,3 @@
|
||||
[submodule "Nerfed.Runtime/Libraries/ImGui.NET"]
|
||||
path = Nerfed.Runtime/Libraries/ImGui.NET
|
||||
url = https://github.com/ImGuiNET/ImGui.NET.git
|
||||
[submodule "Nerfed.Runtime/Libraries/MoonTools.ECS"]
|
||||
path = Nerfed.Runtime/Libraries/MoonTools.ECS
|
||||
url = https://github.com/MoonsideGames/MoonTools.ECS.git
|
||||
|
1
.idea/.idea.Nerfed/.idea/vcs.xml
generated
1
.idea/.idea.Nerfed/.idea/vcs.xml
generated
@ -4,7 +4,6 @@
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/FAudio" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/ImGui.NET" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/MoonTools.ECS" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/RefreshCS" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/SDL2CS" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/WellspringCS" vcs="Git" />
|
||||
|
64
Nerfed.Compiler/AssemblyDefinition.cs
Normal file
64
Nerfed.Compiler/AssemblyDefinition.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Nerfed.Compiler;
|
||||
|
||||
public class AssemblyDefinition
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Guid { get; set; }
|
||||
//public bool IsEditor { get; set; }
|
||||
// Add platform stuff here..?
|
||||
// Add dll's here..?
|
||||
// Add dependencies here..?
|
||||
|
||||
public static bool Create(string assemblyDefinitionFilePath, string name, out AssemblyDefinition assemblyDefinition)
|
||||
{
|
||||
assemblyDefinition = null;
|
||||
|
||||
if (File.Exists(assemblyDefinitionFilePath))
|
||||
{
|
||||
Console.WriteLine($"ERROR: File already exists!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create project file.
|
||||
assemblyDefinition = new AssemblyDefinition
|
||||
{
|
||||
Name = name,
|
||||
Guid = System.Guid.NewGuid().ToString("B").ToUpper(),
|
||||
};
|
||||
|
||||
Save(assemblyDefinition, assemblyDefinitionFilePath);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Save(AssemblyDefinition assemblyDefinition, string assemblyDefinitionFilePath)
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(assemblyDefinition, AssemblyDefinitionContext.Default.AssemblyDefinition);
|
||||
|
||||
File.WriteAllText(assemblyDefinitionFilePath, jsonString);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Open(string assemblyDefinitionFilePath, out AssemblyDefinition assemblyDefinition)
|
||||
{
|
||||
string jsonString = File.ReadAllText(assemblyDefinitionFilePath);
|
||||
assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition);
|
||||
|
||||
if (assemblyDefinition == null)
|
||||
{
|
||||
Console.WriteLine($"ERROR: Could not open {typeof(AssemblyDefinition)}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(AssemblyDefinition))]
|
||||
public partial class AssemblyDefinitionContext : JsonSerializerContext
|
||||
{
|
||||
}
|
82
Nerfed.Compiler/Compiler.cs
Normal file
82
Nerfed.Compiler/Compiler.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Nerfed.Compiler;
|
||||
|
||||
public static class Compiler
|
||||
{
|
||||
public static bool Compile(string projectFilePath, string configuration = "Debug")
|
||||
{
|
||||
string projectDirectory = Path.GetDirectoryName(projectFilePath);
|
||||
|
||||
if (!File.Exists(projectFilePath))
|
||||
{
|
||||
Console.WriteLine($"ERROR: Project file not found at {projectDirectory}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Project.Open(projectFilePath, out Project project))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Check project version, to make sure we can compile it or something...
|
||||
|
||||
// Generate solution.
|
||||
Generator.GenerateSolution(projectDirectory, project, out string solutionFilePath);
|
||||
|
||||
// Compile solution.
|
||||
ProcessStartInfo processInfo = new()
|
||||
{
|
||||
WorkingDirectory = Path.GetDirectoryName(solutionFilePath),
|
||||
CreateNoWindow = true,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
};
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
processInfo.FileName = "/bin/bash";
|
||||
processInfo.Arguments = $"-c \"dotnet build '{Path.GetFileName(solutionFilePath)}'\"" + (string.IsNullOrWhiteSpace(configuration) ? $" --configuration {configuration}" : "");
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
processInfo.FileName = "cmd.exe";
|
||||
processInfo.Arguments = $"/c dotnet build \"{Path.GetFileName(solutionFilePath)}\"" + (string.IsNullOrWhiteSpace(configuration) ? $" --configuration {configuration}" : "");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"ERROR: Platform not supported!");
|
||||
return false;
|
||||
}
|
||||
|
||||
Process process = Process.Start(processInfo) ?? throw new Exception();
|
||||
process.OutputDataReceived += (sender, dataArgs) => {
|
||||
string data = dataArgs.Data;
|
||||
|
||||
if (data is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(data);
|
||||
};
|
||||
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
process.ErrorDataReceived += (sender, dataArgs) => {
|
||||
if (dataArgs.Data is not null)
|
||||
{
|
||||
Console.WriteLine(dataArgs.Data);
|
||||
}
|
||||
};
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
int exitCode = process.ExitCode;
|
||||
process.Close();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
161
Nerfed.Compiler/Generator.cs
Normal file
161
Nerfed.Compiler/Generator.cs
Normal file
@ -0,0 +1,161 @@
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Nerfed.Compiler;
|
||||
|
||||
public static class Generator
|
||||
{
|
||||
public const string AssemblyDefinitionExtensionName = ".asmdef";
|
||||
public const string CSProjectExtensionName = ".csproj";
|
||||
public const string SolutionExtensionName = ".sln";
|
||||
|
||||
public static void GenerateSolution(string projectDirectory, Project project, out string solutionFilePath)
|
||||
{
|
||||
// Clear files.
|
||||
ClearCSProjectFiles(projectDirectory);
|
||||
|
||||
// Generate projects.
|
||||
string[] assemblyDefinitionFilePaths = Directory.GetFiles(projectDirectory, AssemblyDefinitionExtensionName, SearchOption.AllDirectories);
|
||||
foreach (string assemblyDefinitionFilePath in assemblyDefinitionFilePaths)
|
||||
{
|
||||
GenerateCSProject(assemblyDefinitionFilePath, projectDirectory, out string csProjectFilePath);
|
||||
}
|
||||
|
||||
// Generate solution.
|
||||
string[] csProjectPaths = Directory.GetFiles(projectDirectory, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly);
|
||||
string[] csProjectGuids = new string[csProjectPaths.Length];
|
||||
for (int i = 0; i < csProjectPaths.Length; i++)
|
||||
{
|
||||
csProjectGuids[i] = Guid.NewGuid().ToString("B").ToUpper();
|
||||
}
|
||||
|
||||
StringBuilder content = new StringBuilder();
|
||||
|
||||
// Write the solution file header
|
||||
content.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00");
|
||||
content.AppendLine("# Visual Studio Version 17");
|
||||
content.AppendLine("VisualStudioVersion = 17.10.35013.160");
|
||||
content.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1");
|
||||
|
||||
// Add each project to the solution file
|
||||
for (int i = 0; i < csProjectPaths.Length; i++)
|
||||
{
|
||||
string csProjectPath = csProjectPaths[i];
|
||||
string csProjectGuid = csProjectGuids[i];
|
||||
string csProjectName = Path.GetFileNameWithoutExtension(csProjectPath);
|
||||
string csProjectRelativePath = Path.GetRelativePath(projectDirectory, csProjectPath);
|
||||
// FAE04EC0-301F-11D3-BF4B-00C04F79EFBC for C# projects.
|
||||
content.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{csProjectName}\", \"{csProjectRelativePath}\", \"{csProjectGuid}\"");
|
||||
content.AppendLine("EndProject");
|
||||
}
|
||||
|
||||
// Add global sections (these can be extended as needed)
|
||||
content.AppendLine("Global");
|
||||
content.AppendLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution");
|
||||
content.AppendLine(" Test|x64 = Test|x64");
|
||||
content.AppendLine(" Release|x64 = Release|x64");
|
||||
content.AppendLine(" Debug|x64 = Debug|x64");
|
||||
content.AppendLine(" EndGlobalSection");
|
||||
content.AppendLine(" GlobalSection(ProjectConfigurationPlatforms) = postSolution");
|
||||
|
||||
for (int i = 0; i < csProjectPaths.Length; i++)
|
||||
{
|
||||
string projectGuid = csProjectGuids[i];
|
||||
content.AppendLine($" {projectGuid}.Test|x64.ActiveCfg = Test|x64");
|
||||
content.AppendLine($" {projectGuid}.Test|x64.Build.0 = Test|x64");
|
||||
content.AppendLine($" {projectGuid}.Release|x64.ActiveCfg = Release|x64");
|
||||
content.AppendLine($" {projectGuid}.Release|x64.Build.0 = Release|x64");
|
||||
content.AppendLine($" {projectGuid}.Debug|x64.ActiveCfg = Debug|x64");
|
||||
content.AppendLine($" {projectGuid}.Debug|x64.Build.0 = Debug|x64");
|
||||
}
|
||||
|
||||
content.AppendLine(" EndGlobalSection");
|
||||
content.AppendLine(" GlobalSection(SolutionProperties) = preSolution");
|
||||
content.AppendLine(" HideSolutionNode = FALSE");
|
||||
content.AppendLine(" EndGlobalSection");
|
||||
content.AppendLine("EndGlobal");
|
||||
|
||||
// Write the solution file content to disk
|
||||
string solutionName = project.Name + SolutionExtensionName;
|
||||
solutionFilePath = Path.Combine(projectDirectory, solutionName);
|
||||
File.WriteAllText(solutionFilePath, content.ToString());
|
||||
}
|
||||
|
||||
private static bool GenerateCSProject(string assemblyDefinitionFilePath, string projectPath, out string csProjectFilePath)
|
||||
{
|
||||
if (!File.Exists(assemblyDefinitionFilePath))
|
||||
{
|
||||
csProjectFilePath = string.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
string jsonString = File.ReadAllText(assemblyDefinitionFilePath);
|
||||
AssemblyDefinition assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition);
|
||||
|
||||
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||
Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!");
|
||||
|
||||
// TODO: get all dependencies.
|
||||
// TODO: properly get assemblies.
|
||||
|
||||
StringBuilder content = new StringBuilder();
|
||||
|
||||
content.AppendLine("<Project Sdk=\"Microsoft.NET.Sdk\">");
|
||||
|
||||
content.AppendLine(" <PropertyGroup>");
|
||||
content.AppendLine(" <TargetFramework>net8.0</TargetFramework>");
|
||||
content.AppendLine(" <ImplicitUsings>enable</ImplicitUsings>");
|
||||
content.AppendLine(" <Nullable>disable</Nullable>");
|
||||
content.AppendLine(" <PublishAot>true</PublishAot>");
|
||||
content.AppendLine(" <InvariantGlobalization>true</InvariantGlobalization>");
|
||||
content.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
|
||||
content.AppendLine(" <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>");
|
||||
content.AppendLine(" <IsPackable>false</IsPackable>");
|
||||
content.AppendLine(" <Configurations>Debug;Test;Release</Configurations>");
|
||||
content.AppendLine(" <Platforms>x64</Platforms>");
|
||||
content.AppendLine(" </PropertyGroup>");
|
||||
|
||||
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|x64' \">");
|
||||
content.AppendLine(" <DefineConstants>TRACE;LOG_INFO;PROFILING</DefineConstants>");
|
||||
content.AppendLine(" </PropertyGroup>");
|
||||
|
||||
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Test|x64' \">");
|
||||
content.AppendLine(" <DefineConstants>TRACE;LOG_ERROR;PROFILING</DefineConstants>");
|
||||
content.AppendLine(" <Optimize>true</Optimize>");
|
||||
content.AppendLine(" </PropertyGroup>");
|
||||
|
||||
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|x64' \">");
|
||||
content.AppendLine(" <DefineConstants>TRACE;LOG_ERROR</DefineConstants>");
|
||||
content.AppendLine(" <Optimize>true</Optimize>");
|
||||
content.AppendLine(" </PropertyGroup>");
|
||||
|
||||
content.AppendLine(" <ItemGroup>");
|
||||
content.AppendLine($" <Compile Include=\"{assemblyDefinitionFilePath}/**/*.cs\"/>");
|
||||
content.AppendLine(" </ItemGroup>");
|
||||
|
||||
content.AppendLine(" <ItemGroup>");
|
||||
content.AppendLine(" <Reference Include=\"Nerfed.Runtime\">");
|
||||
content.AppendLine($" <HintPath>{runtimeAssembly.Location}</HintPath>");
|
||||
content.AppendLine(" <Private>false</Private>");
|
||||
content.AppendLine(" </Reference>");
|
||||
content.AppendLine(" </ItemGroup>");
|
||||
|
||||
content.AppendLine("</Project>");
|
||||
|
||||
string csProjectName = assemblyDefinition.Name + CSProjectExtensionName;
|
||||
csProjectFilePath = Path.Combine(projectPath, csProjectName);
|
||||
File.WriteAllText(csProjectFilePath, content.ToString());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void ClearCSProjectFiles(string projectPath)
|
||||
{
|
||||
string[] csProjectFiles = Directory.GetFiles(projectPath, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly);
|
||||
foreach (string csProjectFile in csProjectFiles)
|
||||
{
|
||||
File.Delete(csProjectFile);
|
||||
}
|
||||
}
|
||||
}
|
26
Nerfed.Compiler/Nerfed.Compiler.csproj
Normal file
26
Nerfed.Compiler/Nerfed.Compiler.csproj
Normal file
@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Configurations>Debug;Test;Release</Configurations>
|
||||
<Platforms>x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
||||
<Optimize>false</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Test|x64' ">
|
||||
<Optimize>true</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
|
||||
<Optimize>true</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
15
Nerfed.Compiler/Program.cs
Normal file
15
Nerfed.Compiler/Program.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace Nerfed.Compiler;
|
||||
|
||||
public class Program
|
||||
{
|
||||
internal static void Main(string[] args)
|
||||
{
|
||||
if (args.Length != 2)
|
||||
{
|
||||
Console.WriteLine("projectFilePath, configuration (Debug, Test, Release)");
|
||||
return;
|
||||
}
|
||||
|
||||
Compiler.Compile(args[0], args[1]);
|
||||
}
|
||||
}
|
50
Nerfed.Compiler/Project.cs
Normal file
50
Nerfed.Compiler/Project.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Nerfed.Compiler;
|
||||
|
||||
public class Project
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public static bool Create(string path, string name, out Project project)
|
||||
{
|
||||
// Create project file.
|
||||
project = new Project
|
||||
{
|
||||
Name = name,
|
||||
};
|
||||
|
||||
Save(project, path);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Save(Project project, string projectFilePath)
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(project, ProjectContext.Default.Project);
|
||||
|
||||
File.WriteAllText(projectFilePath, jsonString);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Open(string path, out Project project)
|
||||
{
|
||||
string jsonString = File.ReadAllText(path);
|
||||
project = JsonSerializer.Deserialize(jsonString, ProjectContext.Default.Project);
|
||||
|
||||
if (project == null)
|
||||
{
|
||||
Console.WriteLine($"ERROR: Could not open {typeof(Project)}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(Project))]
|
||||
public partial class ProjectContext : JsonSerializerContext
|
||||
{
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
namespace Nerfed.Editor.Components;
|
||||
|
||||
public readonly record struct SelectedInHierachy;
|
||||
public readonly record struct ClickedInHierachy;
|
@ -1,4 +1,5 @@
|
||||
using ImGuiNET;
|
||||
using Nerfed.Editor.Project;
|
||||
using Nerfed.Runtime;
|
||||
using Nerfed.Runtime.Graphics;
|
||||
using Nerfed.Runtime.Gui;
|
||||
@ -56,6 +57,7 @@ private static void UpdateMainMenu()
|
||||
}
|
||||
ImGui.EndMenu();
|
||||
}
|
||||
|
||||
ImGui.EndMainMenuBar();
|
||||
}
|
||||
}
|
||||
@ -67,11 +69,7 @@ private static void HandleOnGui()
|
||||
|
||||
ImGui.ShowDemoWindow();
|
||||
|
||||
foreach (MoonTools.ECS.System system in Program.editorSystems)
|
||||
{
|
||||
using ProfilerScope scope = new(system.GetType().Name);
|
||||
system.Update(Engine.Timestep);
|
||||
}
|
||||
EditorProjectGui.OnGui();
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,6 @@
|
||||
<IsPackable>false</IsPackable>
|
||||
<Configurations>Debug;Test;Release</Configurations>
|
||||
<Platforms>x64</Platforms>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
||||
@ -24,6 +23,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Nerfed.Compiler\Nerfed.Compiler.csproj" />
|
||||
<ProjectReference Include="..\Nerfed.Runtime\Nerfed.Runtime.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,19 +1,9 @@
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Editor.Systems;
|
||||
using Nerfed.Runtime;
|
||||
using Nerfed.Runtime.Components;
|
||||
using Nerfed.Runtime.Systems;
|
||||
using Nerfed.Runtime.Util;
|
||||
using System.Numerics;
|
||||
using Nerfed.Runtime;
|
||||
|
||||
namespace Nerfed.Editor;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static readonly World world = new World();
|
||||
private static List<MoonTools.ECS.System> systems = new List<MoonTools.ECS.System>();
|
||||
public static List<MoonTools.ECS.System> editorSystems = new List<MoonTools.ECS.System>();
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Engine.OnInitialize += HandleOnInitialize;
|
||||
@ -26,45 +16,6 @@ private static void Main(string[] args)
|
||||
|
||||
private static void HandleOnInitialize()
|
||||
{
|
||||
//systems.Add(new ParentSystem(world));
|
||||
systems.Add(new LocalToWorldSystem(world));
|
||||
editorSystems.Add(new EditorProfilerWindow(world));
|
||||
editorSystems.Add(new EditorHierarchyWindow(world));
|
||||
#if DEBUG
|
||||
editorSystems.Add(new EditorInspectorWindow(world));
|
||||
#endif
|
||||
|
||||
Entity ent1 = world.CreateEntity("parent");
|
||||
world.Set(ent1, new Root());
|
||||
world.Set(ent1, new LocalTransform(new Vector3(1, 0, 0), Quaternion.Identity, Vector3.One));
|
||||
|
||||
Entity ent2 = world.CreateEntity("child");
|
||||
world.Set(ent2, new LocalTransform(new Vector3(0, 1, 0), Quaternion.Identity, Vector3.One));
|
||||
Transform.SetParent(world, ent2, ent1);
|
||||
|
||||
Entity ent3 = world.CreateEntity("entity3");
|
||||
world.Set(ent3, new Root());
|
||||
Transform.SetParent(world, ent3, ent2);
|
||||
|
||||
Entity ent4 = world.CreateEntity("entity4");
|
||||
world.Set(ent4, new Root());
|
||||
|
||||
Entity ent5 = world.CreateBaseEntity("entity5");
|
||||
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
Entity newEnt = world.CreateBaseEntity();
|
||||
world.Set(newEnt, new LocalTransform(new Vector3(i, i, i), Quaternion.Identity, Vector3.One));
|
||||
|
||||
Entity parent = newEnt;
|
||||
for (int j = 0; j < 2; j++) {
|
||||
Entity newChildEnt = world.CreateEntity();
|
||||
world.Set(newChildEnt, new LocalTransform(new Vector3(i + j * i, i - j * i, j - i * i), Quaternion.Identity, Vector3.One));
|
||||
Transform.SetParent(world, newChildEnt, parent);
|
||||
parent = newChildEnt;
|
||||
}
|
||||
}
|
||||
|
||||
// Open project.
|
||||
// Setip EditorGui.
|
||||
EditorGui.Initialize();
|
||||
@ -72,32 +23,16 @@ private static void HandleOnInitialize()
|
||||
|
||||
private static void HandleOnUpdate()
|
||||
{
|
||||
foreach (MoonTools.ECS.System system in systems)
|
||||
{
|
||||
using ProfilerScope scope = new(system.GetType().Name);
|
||||
system.Update(Engine.Timestep);
|
||||
}
|
||||
// Editor Update.
|
||||
EditorGui.Update();
|
||||
|
||||
using (new ProfilerScope("EditorGui.Update"))
|
||||
{
|
||||
// Editor Update.
|
||||
EditorGui.Update();
|
||||
}
|
||||
|
||||
// Try Catch UserCode Update.
|
||||
|
||||
using (new ProfilerScope("world.FinishUpdate"))
|
||||
{
|
||||
world.FinishUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleOnRender()
|
||||
{
|
||||
using (new ProfilerScope("EditorGui.Render"))
|
||||
{
|
||||
EditorGui.Render();
|
||||
}
|
||||
EditorGui.Render();
|
||||
}
|
||||
|
||||
private static void HandleOnQuit()
|
||||
|
8
Nerfed.Editor/Project/EditorAssemblyLoadContext.cs
Normal file
8
Nerfed.Editor/Project/EditorAssemblyLoadContext.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using System.Runtime.Loader;
|
||||
|
||||
namespace Nerfed.Editor.Project;
|
||||
|
||||
internal class EditorAssemblyLoadContext : AssemblyLoadContext
|
||||
{
|
||||
public EditorAssemblyLoadContext() : base(isCollectible: true) { }
|
||||
}
|
124
Nerfed.Editor/Project/EditorAssemblyLoader.cs
Normal file
124
Nerfed.Editor/Project/EditorAssemblyLoader.cs
Normal file
@ -0,0 +1,124 @@
|
||||
using Nerfed.Runtime;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Nerfed.Editor.Project;
|
||||
|
||||
// https://github.com/godotengine/godot/blob/master/modules/mono/glue/GodotSharp/GodotPlugins/Main.c
|
||||
// https://gitlab.com/robertk92/assemblyreloadtest/-/blob/main/AppContextTest/Program.cs
|
||||
|
||||
internal static class EditorAssemblyLoader
|
||||
{
|
||||
internal sealed class EditorAssemblyLoadContextWrapper
|
||||
{
|
||||
private EditorAssemblyLoadContext assemblyLoadContext;
|
||||
private readonly WeakReference weakReference;
|
||||
|
||||
private EditorAssemblyLoadContextWrapper(EditorAssemblyLoadContext assemblyLoadContext, WeakReference weakReference)
|
||||
{
|
||||
this.assemblyLoadContext = assemblyLoadContext;
|
||||
this.weakReference = weakReference;
|
||||
}
|
||||
|
||||
public bool IsCollectible
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
// If assemblyLoadContext is null we already started unloading, so it was collectible.
|
||||
get => assemblyLoadContext?.IsCollectible ?? true;
|
||||
}
|
||||
|
||||
public bool IsAlive
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
get => weakReference.IsAlive;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static (Assembly, EditorAssemblyLoadContextWrapper) CreateAndLoad(AssemblyName assemblyName)
|
||||
{
|
||||
EditorAssemblyLoadContext context = new EditorAssemblyLoadContext();
|
||||
WeakReference reference = new WeakReference(context, trackResurrection: true);
|
||||
EditorAssemblyLoadContextWrapper wrapper = new EditorAssemblyLoadContextWrapper(context, reference);
|
||||
Assembly assembly = context.LoadFromAssemblyName(assemblyName);
|
||||
return (assembly, wrapper);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static (Assembly, EditorAssemblyLoadContextWrapper) CreateAndLoad(string assemblyPath)
|
||||
{
|
||||
EditorAssemblyLoadContext context = new EditorAssemblyLoadContext();
|
||||
WeakReference reference = new WeakReference(context, trackResurrection: true);
|
||||
EditorAssemblyLoadContextWrapper wrapper = new EditorAssemblyLoadContextWrapper(context, reference);
|
||||
Assembly assembly = context.LoadFromAssemblyPath(assemblyPath);
|
||||
return (assembly, wrapper);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
internal void Unload()
|
||||
{
|
||||
assemblyLoadContext?.Unload();
|
||||
assemblyLoadContext = null;
|
||||
}
|
||||
}
|
||||
|
||||
internal static (Assembly, EditorAssemblyLoadContextWrapper) Load(string assemblyFilePath)
|
||||
{
|
||||
string assemblyFileName = Path.GetFileNameWithoutExtension(assemblyFilePath);
|
||||
|
||||
AssemblyName assemblyName = new AssemblyName(assemblyFileName);
|
||||
|
||||
return EditorAssemblyLoadContextWrapper.CreateAndLoad(assemblyName);
|
||||
}
|
||||
|
||||
internal static (Assembly, EditorAssemblyLoadContextWrapper) LoadFromPath(string assemblyFilePath)
|
||||
{
|
||||
return EditorAssemblyLoadContextWrapper.CreateAndLoad(assemblyFilePath);
|
||||
}
|
||||
|
||||
internal static bool Unload(EditorAssemblyLoadContextWrapper assemblyLoadContextWrapper)
|
||||
{
|
||||
if (assemblyLoadContextWrapper == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!assemblyLoadContextWrapper.IsCollectible)
|
||||
{
|
||||
Log.Error($"{assemblyLoadContextWrapper} is not collectable!");
|
||||
return false;
|
||||
}
|
||||
|
||||
assemblyLoadContextWrapper.Unload();
|
||||
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
|
||||
TimeSpan timeout = TimeSpan.FromSeconds(30);
|
||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
||||
|
||||
while (assemblyLoadContextWrapper.IsAlive)
|
||||
{
|
||||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
|
||||
GC.WaitForPendingFinalizers();
|
||||
|
||||
if (!assemblyLoadContextWrapper.IsAlive)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (stopwatch.Elapsed.TotalSeconds % 10 == 0)
|
||||
{
|
||||
Log.Info("Tring to unload assembly...");
|
||||
}
|
||||
|
||||
if (stopwatch.Elapsed >= timeout)
|
||||
{
|
||||
Log.Error("Failed to unload assembly!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
173
Nerfed.Editor/Project/EditorProject.cs
Normal file
173
Nerfed.Editor/Project/EditorProject.cs
Normal file
@ -0,0 +1,173 @@
|
||||
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;
|
||||
|
||||
private static readonly List<(string, EditorAssemblyLoader.EditorAssemblyLoadContextWrapper)> editorAssemblyLoadContextWrappers = [];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
UnloadAssemblies();
|
||||
|
||||
Compiler.Compiler.Compile(ProjectFilePath, "Debug");
|
||||
|
||||
LoadAssemblies();
|
||||
}
|
||||
|
||||
internal static void GenerateSolution()
|
||||
{
|
||||
if(Project == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Compiler.Generator.GenerateSolution(ProjectDirectory, Project, out string solutionFilePath);
|
||||
ProjectSolutionFilePath = solutionFilePath;
|
||||
}
|
||||
|
||||
private static void SetupDefaultFolders()
|
||||
{
|
||||
if (Project == null || ProjectDirectory == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string contentDirectory = Path.Combine(ProjectDirectory, "Content");
|
||||
if (!Directory.Exists(contentDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(contentDirectory);
|
||||
}
|
||||
ProjectContentDirectory = contentDirectory;
|
||||
string scriptsDirectory = Path.Combine(ProjectContentDirectory, "Scripts");
|
||||
if (!Directory.Exists(scriptsDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(scriptsDirectory);
|
||||
}
|
||||
string scriptsRuntimePath = Path.Combine(scriptsDirectory, "Runtime");
|
||||
if (!Directory.Exists(scriptsRuntimePath))
|
||||
{
|
||||
Directory.CreateDirectory(scriptsRuntimePath);
|
||||
}
|
||||
|
||||
// Test create csproject.
|
||||
string gameplayRuntimeFilePath = Path.Combine(scriptsRuntimePath, Compiler.Generator.AssemblyDefinitionExtensionName);
|
||||
if (!File.Exists(gameplayRuntimeFilePath))
|
||||
{
|
||||
Compiler.AssemblyDefinition.Create(gameplayRuntimeFilePath, "Gameplay", out Compiler.AssemblyDefinition project);
|
||||
}
|
||||
|
||||
string tempDirectory = Path.Combine(ProjectDirectory, "Temp");
|
||||
if (!Directory.Exists(tempDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(tempDirectory);
|
||||
}
|
||||
ProjectTempDirectory = tempDirectory;
|
||||
}
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
67
Nerfed.Editor/Project/EditorProjectGui.cs
Normal file
67
Nerfed.Editor/Project/EditorProjectGui.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using ImGuiNET;
|
||||
|
||||
namespace Nerfed.Editor.Project;
|
||||
|
||||
internal static class EditorProjectGui
|
||||
{
|
||||
private static string projectDirectory = string.Empty;
|
||||
private static string projectName = string.Empty;
|
||||
private static string projectFilePath = string.Empty;
|
||||
|
||||
internal static void OnGui()
|
||||
{
|
||||
ImGui.Begin("Project");
|
||||
ImGui.BeginGroup();
|
||||
|
||||
ImGui.InputText("Project Directory", ref projectDirectory, 512);
|
||||
ImGui.InputText("Project Name", ref projectName, 512);
|
||||
|
||||
string newProjectFilePath = Path.Combine(projectDirectory, ".project");
|
||||
ImGui.Text(newProjectFilePath);
|
||||
|
||||
if (ImGui.Button("Create Project"))
|
||||
{
|
||||
EditorProject.Create(newProjectFilePath, projectName);
|
||||
}
|
||||
|
||||
ImGui.EndGroup();
|
||||
ImGui.BeginGroup();
|
||||
|
||||
ImGui.InputText("Project File Path", ref projectFilePath, 512);
|
||||
if (ImGui.Button("Open Project"))
|
||||
{
|
||||
EditorProject.Open(projectFilePath);
|
||||
}
|
||||
|
||||
ImGui.Text("Loaded project: ");
|
||||
if(EditorProject.Project != null)
|
||||
{
|
||||
ImGui.Text(EditorProject.Project.Name);
|
||||
ImGui.Text(EditorProject.ProjectFilePath);
|
||||
ImGui.Text(EditorProject.ProjectSolutionFilePath);
|
||||
ImGui.Text(EditorProject.ProjectDirectory);
|
||||
ImGui.Text(EditorProject.ProjectContentDirectory);
|
||||
ImGui.Text(EditorProject.ProjectTempDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
ImGui.Text("None");
|
||||
}
|
||||
|
||||
ImGui.EndGroup();
|
||||
ImGui.BeginGroup();
|
||||
|
||||
if (ImGui.Button("Generate Solution"))
|
||||
{
|
||||
EditorProject.GenerateSolution();
|
||||
}
|
||||
|
||||
if (ImGui.Button("Compile"))
|
||||
{
|
||||
EditorProject.Compile();
|
||||
}
|
||||
|
||||
ImGui.EndGroup();
|
||||
ImGui.End();
|
||||
}
|
||||
}
|
@ -1,190 +0,0 @@
|
||||
using ImGuiNET;
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Editor.Components;
|
||||
using Nerfed.Runtime;
|
||||
using Nerfed.Runtime.Components;
|
||||
using Nerfed.Runtime.Util;
|
||||
|
||||
namespace Nerfed.Editor.Systems
|
||||
{
|
||||
// Window that draws entities.
|
||||
internal class EditorHierarchyWindow : MoonTools.ECS.System
|
||||
{
|
||||
private const ImGuiTreeNodeFlags baseFlags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.SpanAvailWidth;
|
||||
|
||||
//private readonly Filter rootEntitiesWithTransformFilter;
|
||||
//private readonly Filter rootEntitiesFilterBroken;
|
||||
private readonly Filter rootEntitiesFilter;
|
||||
|
||||
private readonly EditorHierachySelectionSystem hierachySelectionSystem;
|
||||
|
||||
public EditorHierarchyWindow(World world) : base(world)
|
||||
{
|
||||
//rootEntitiesWithTransformFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
||||
|
||||
// TODO: this doesn't work.
|
||||
//rootEntitiesFilterBroken = FilterBuilder.Exclude<Child>().Build();
|
||||
|
||||
// Maybe the parent/child functions should add a root component when not being a child.
|
||||
rootEntitiesFilter = FilterBuilder.Include<Root>().Build();
|
||||
|
||||
// Maybe instead of a root, if we need a component that is always on an entity and has some use we could create something like a VersionComponent which only hold an int.
|
||||
// The version would update each time something changes on the entity.
|
||||
// Or a EditorComponent, just a component that always gets added when in editor mode.
|
||||
|
||||
hierachySelectionSystem = new EditorHierachySelectionSystem(world);
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
ImGui.Begin("Hierarchy");
|
||||
|
||||
ImGuiTreeNodeFlags flags = baseFlags;
|
||||
flags |= ImGuiTreeNodeFlags.DefaultOpen;
|
||||
|
||||
if (ImGui.TreeNodeEx("World", flags))
|
||||
{
|
||||
if (ImGui.BeginDragDropTarget())
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
|
||||
if (payload.NativePtr != null)
|
||||
{
|
||||
Entity* data = (Entity*)payload.Data;
|
||||
Entity child = data[0];
|
||||
|
||||
Log.Info($"Dropped {child.ID}");
|
||||
|
||||
Transform.RemoveParent(World, child);
|
||||
}
|
||||
}
|
||||
ImGui.EndDragDropTarget();
|
||||
}
|
||||
|
||||
//foreach (Entity entity in rootEntitiesWithTransformFilter.Entities)
|
||||
//{
|
||||
// DrawEntityAndChildren(entity);
|
||||
//}
|
||||
|
||||
foreach (Entity entity in rootEntitiesFilter.Entities)
|
||||
{
|
||||
DrawEntityAndChildren(entity);
|
||||
}
|
||||
|
||||
ImGui.TreePop();
|
||||
}
|
||||
|
||||
ImGui.End();
|
||||
|
||||
hierachySelectionSystem.Update(delta);
|
||||
}
|
||||
|
||||
private void DrawEntityAndChildren(in Entity entity)
|
||||
{
|
||||
ImGuiTreeNodeFlags flags = baseFlags;
|
||||
|
||||
if (!World.HasInRelation<ChildParentRelation>(entity))
|
||||
{
|
||||
flags |= ImGuiTreeNodeFlags.Leaf;
|
||||
}
|
||||
|
||||
if (World.Has<SelectedInHierachy>(entity))
|
||||
{
|
||||
flags |= ImGuiTreeNodeFlags.Selected;
|
||||
}
|
||||
|
||||
if (ImGui.TreeNodeEx($"{entity.ID} | {GetTag(entity)}", flags))
|
||||
{
|
||||
// TODO: fix selection, look at ImGui 1.91, https://github.com/ocornut/imgui/wiki/Multi-Select
|
||||
// Selection.
|
||||
if (ImGui.IsItemClicked() && !ImGui.IsItemToggledOpen())
|
||||
{
|
||||
World.Set(entity, new ClickedInHierachy());
|
||||
}
|
||||
|
||||
// Drag and drop.
|
||||
if (ImGui.BeginDragDropSource())
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (Entity* payload = &entity)
|
||||
{
|
||||
ImGui.SetDragDropPayload($"{nameof(EditorHierarchyWindow)}", (IntPtr)payload, (uint)sizeof(Entity));
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.EndDragDropSource();
|
||||
}
|
||||
|
||||
if (ImGui.BeginDragDropTarget())
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
|
||||
if (payload.NativePtr != null)
|
||||
{
|
||||
Entity ent = *(Entity*)payload.Data;
|
||||
|
||||
Log.Info($"Dropped {ent.ID}");
|
||||
|
||||
Transform.SetParent(World, ent, entity);
|
||||
}
|
||||
}
|
||||
ImGui.EndDragDropTarget();
|
||||
}
|
||||
|
||||
// Draw children.
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
DrawEntityAndChildren(childEntity);
|
||||
}
|
||||
|
||||
ImGui.TreePop();
|
||||
}
|
||||
}
|
||||
|
||||
// System for handling the selected entities in the hierachy.
|
||||
private class EditorHierachySelectionSystem : MoonTools.ECS.System
|
||||
{
|
||||
private readonly Filter selectedEntities;
|
||||
private readonly Filter clickedEntities;
|
||||
|
||||
public EditorHierachySelectionSystem(World world) : base(world)
|
||||
{
|
||||
selectedEntities = FilterBuilder.Include<SelectedInHierachy>().Build();
|
||||
clickedEntities = FilterBuilder.Include<ClickedInHierachy>().Build();
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
ImGuiIOPtr io = ImGui.GetIO();
|
||||
|
||||
if (!clickedEntities.Empty && !io.KeyCtrl)
|
||||
{
|
||||
foreach (Entity entity in selectedEntities.Entities)
|
||||
{
|
||||
Remove<SelectedInHierachy>(entity);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Entity entity in clickedEntities.Entities)
|
||||
{
|
||||
// Unselect.
|
||||
if (Has<SelectedInHierachy>(entity))
|
||||
{
|
||||
Remove<SelectedInHierachy>(entity);
|
||||
}
|
||||
// Select.
|
||||
else
|
||||
{
|
||||
Set(entity, new SelectedInHierachy());
|
||||
}
|
||||
|
||||
Remove<ClickedInHierachy>(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
using System.Numerics;
|
||||
using ImGuiNET;
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Editor.Components;
|
||||
using Nerfed.Runtime.Serialization;
|
||||
|
||||
#if DEBUG
|
||||
namespace Nerfed.Editor.Systems
|
||||
{
|
||||
// Window that draws entities.
|
||||
internal class EditorInspectorWindow : MoonTools.ECS.DebugSystem
|
||||
{
|
||||
private readonly Filter selectedEntityFilter;
|
||||
|
||||
public EditorInspectorWindow(World world) : base(world)
|
||||
{
|
||||
selectedEntityFilter = FilterBuilder.Include<SelectedInHierachy>().Build();
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
ImGui.Begin("Inspector");
|
||||
|
||||
foreach (Entity entity in selectedEntityFilter.Entities)
|
||||
{
|
||||
DrawEntityComponents(entity);
|
||||
}
|
||||
|
||||
ImGui.End();
|
||||
}
|
||||
|
||||
private void DrawEntityComponents(Entity entity)
|
||||
{
|
||||
World.ComponentTypeEnumerator componentTypes = World.Debug_GetAllComponentTypes(entity);
|
||||
|
||||
// Add button of all types that we can add. Also filter out types we already have.
|
||||
List<Type> componentTypesToAdd = ComponentHelper.AddComponentByType.Keys.ToList();
|
||||
foreach (Type componentType in componentTypes)
|
||||
{
|
||||
componentTypesToAdd.Remove(componentType);
|
||||
}
|
||||
|
||||
const string popupId = "AddComponentPopup";
|
||||
if (ImGui.Button("Add Component"))
|
||||
{
|
||||
ImGui.OpenPopup(popupId);
|
||||
}
|
||||
|
||||
if (ImGui.BeginPopup(popupId))
|
||||
{
|
||||
foreach (Type componentType in componentTypesToAdd)
|
||||
{
|
||||
if (ImGui.Selectable(componentType.Name))
|
||||
{
|
||||
if (ComponentHelper.AddComponentByType.TryGetValue(componentType, out Action<World, Entity> componentSetter))
|
||||
{
|
||||
componentSetter.Invoke(World, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGui.EndPopup();
|
||||
}
|
||||
|
||||
ImGui.Dummy(new Vector2(16, 16));
|
||||
|
||||
ImGui.Text("ComponentInspectorByType");
|
||||
foreach (Type componentType in componentTypes)
|
||||
{
|
||||
if (ComponentHelper.ComponentInspectorByType.TryGetValue(componentType, out Action<World, Entity> componentInspector))
|
||||
{
|
||||
componentInspector(World, entity);
|
||||
}
|
||||
else if (ComponentHelper.GetComponentByType.TryGetValue(componentType, out Func<World, Entity, ValueType> componentGetter))
|
||||
{
|
||||
ValueType component = componentGetter.Invoke(World, entity);
|
||||
ImGui.Text(component.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
ImGui.Text(componentType.Name);
|
||||
}
|
||||
ImGui.Separator();
|
||||
}
|
||||
|
||||
ImGui.Dummy(new Vector2(16, 16));
|
||||
|
||||
// ImGui.Text("Reflection");
|
||||
// foreach (Type component in componentTypes)
|
||||
// {
|
||||
// System.Reflection.MethodInfo getMethodInfo = typeof(World).GetMethod("Get");
|
||||
// System.Reflection.MethodInfo getComponentMethod = getMethodInfo.MakeGenericMethod(component);
|
||||
// object result = getComponentMethod.Invoke(World, [entity]);
|
||||
//
|
||||
// // process here
|
||||
// ImGui.Text(result.ToString());
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -1,204 +0,0 @@
|
||||
using ImGuiNET;
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime;
|
||||
|
||||
namespace Nerfed.Editor.Systems
|
||||
{
|
||||
internal class EditorProfilerWindow : MoonTools.ECS.System
|
||||
{
|
||||
const ImGuiTableFlags tableFlags = ImGuiTableFlags.Resizable | ImGuiTableFlags.BordersOuter | ImGuiTableFlags.NoBordersInBody | ImGuiTableFlags.ScrollY | ImGuiTableFlags.ScrollX;
|
||||
const ImGuiTreeNodeFlags treeNodeFlags = ImGuiTreeNodeFlags.SpanAllColumns;
|
||||
const ImGuiTreeNodeFlags treeNodeLeafFlags = ImGuiTreeNodeFlags.SpanAllColumns | ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen;
|
||||
|
||||
private int selectedFrame = 0;
|
||||
private int previousSelectedFrame = -1;
|
||||
private IOrderedEnumerable<KeyValuePair<string, (double ms, uint calls)>> orderedCombinedData = null;
|
||||
|
||||
public EditorProfilerWindow(World world) : base(world)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
if (Profiler.Frames.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui.Begin("Profiler");
|
||||
|
||||
ImGui.BeginChild("Toolbar", new System.Numerics.Vector2(0, 0), ImGuiChildFlags.AutoResizeY);
|
||||
if (ImGui.RadioButton("Recording", Profiler.IsRecording))
|
||||
{
|
||||
Profiler.SetActive(!Profiler.IsRecording);
|
||||
}
|
||||
|
||||
ImGui.SameLine();
|
||||
|
||||
if (Profiler.IsRecording)
|
||||
{
|
||||
// Select last frame when recording to see latest frame data.
|
||||
selectedFrame = Profiler.Frames.Count - 1;
|
||||
}
|
||||
if (ImGui.SliderInt(string.Empty, ref selectedFrame, 0, Profiler.Frames.Count - 1))
|
||||
{
|
||||
// Stop recording when browsing frames.
|
||||
Profiler.SetActive(false);
|
||||
}
|
||||
|
||||
Profiler.Frame frame = Profiler.Frames.ElementAt(selectedFrame);
|
||||
double ms = frame.ElapsedMilliseconds();
|
||||
double s = 1000;
|
||||
ImGui.Text($"Frame: {frame.FrameCount} ({ms:0.000} ms | {(s / ms):0} fps)");
|
||||
ImGui.EndChild();
|
||||
|
||||
if (!Profiler.IsRecording) {
|
||||
if (previousSelectedFrame != selectedFrame)
|
||||
{
|
||||
previousSelectedFrame = selectedFrame;
|
||||
orderedCombinedData = CalculateCombinedData(frame);
|
||||
}
|
||||
|
||||
DrawFlameGraph(frame);
|
||||
|
||||
DrawHierachy(frame);
|
||||
|
||||
ImGui.SameLine();
|
||||
|
||||
DrawCombined(orderedCombinedData);
|
||||
}
|
||||
|
||||
ImGui.End();
|
||||
}
|
||||
|
||||
private static void DrawHierachy(Profiler.Frame frame)
|
||||
{
|
||||
if(frame == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui.BeginChild("Hierachy", new System.Numerics.Vector2(150, 0), ImGuiChildFlags.ResizeX);
|
||||
|
||||
if (ImGui.BeginTable("ProfilerData", 3, tableFlags, new System.Numerics.Vector2(0, 0)))
|
||||
{
|
||||
ImGui.TableSetupColumn("name", ImGuiTableColumnFlags.WidthStretch, 0.8f, 0);
|
||||
ImGui.TableSetupColumn("thread", ImGuiTableColumnFlags.WidthStretch, 0.2f, 1);
|
||||
ImGui.TableSetupColumn("ms", ImGuiTableColumnFlags.WidthStretch, 0.2f, 1);
|
||||
ImGui.TableSetupScrollFreeze(0, 1); // Make row always visible
|
||||
ImGui.TableHeadersRow();
|
||||
|
||||
foreach (Profiler.ScopeNode node in frame.RootNodes)
|
||||
{
|
||||
DrawHierachyNode(node);
|
||||
}
|
||||
|
||||
ImGui.EndTable();
|
||||
}
|
||||
|
||||
ImGui.EndChild();
|
||||
}
|
||||
|
||||
private static void DrawHierachyNode(Profiler.ScopeNode node)
|
||||
{
|
||||
ImGui.TableNextRow();
|
||||
ImGui.TableNextColumn();
|
||||
|
||||
bool isOpen = false;
|
||||
bool isLeaf = node.Children.Count == 0;
|
||||
|
||||
if (isLeaf) {
|
||||
ImGui.TreeNodeEx(node.Label, treeNodeLeafFlags);
|
||||
}
|
||||
else
|
||||
{
|
||||
isOpen = ImGui.TreeNodeEx(node.Label, treeNodeFlags);
|
||||
}
|
||||
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{node.ManagedThreadId}");
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{node.ElapsedMilliseconds():0.000}");
|
||||
|
||||
if (isOpen)
|
||||
{
|
||||
for (int i = 0; i < node.Children.Count; i++)
|
||||
{
|
||||
DrawHierachyNode(node.Children[i]);
|
||||
}
|
||||
ImGui.TreePop();
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawCombined(in IOrderedEnumerable<KeyValuePair<string, (double ms, uint calls)>> orderedCombinedData)
|
||||
{
|
||||
if(orderedCombinedData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui.BeginChild("Combined", new System.Numerics.Vector2(0, 0));
|
||||
|
||||
if (ImGui.BeginTable("ProfilerCombinedData", 3, tableFlags, new System.Numerics.Vector2(0, 0)))
|
||||
{
|
||||
ImGui.TableSetupColumn("name", ImGuiTableColumnFlags.WidthStretch, 0.6f, 0);
|
||||
ImGui.TableSetupColumn("ms", ImGuiTableColumnFlags.WidthStretch, 0.2f, 1);
|
||||
ImGui.TableSetupColumn("calls", ImGuiTableColumnFlags.WidthStretch, 0.2f, 2);
|
||||
ImGui.TableSetupScrollFreeze(0, 1); // Make row always visible
|
||||
ImGui.TableHeadersRow();
|
||||
|
||||
foreach (KeyValuePair<string, (double ms, uint calls)> combinedData in orderedCombinedData)
|
||||
{
|
||||
ImGui.TableNextRow();
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{combinedData.Key}");
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{combinedData.Value.ms:0.000}");
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{combinedData.Value.calls}");
|
||||
}
|
||||
|
||||
ImGui.EndTable();
|
||||
}
|
||||
|
||||
ImGui.EndChild();
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<KeyValuePair<string, (double ms, uint calls)>> CalculateCombinedData(Profiler.Frame frame)
|
||||
{
|
||||
Dictionary<string, (double ms, uint calls)> combinedRecordData = new Dictionary<string, (double ms, uint calls)>(128);
|
||||
foreach (Profiler.ScopeNode node in frame.RootNodes)
|
||||
{
|
||||
CalculateCombinedData(node, in combinedRecordData);
|
||||
}
|
||||
return combinedRecordData.OrderByDescending(x => x.Value.ms);
|
||||
}
|
||||
|
||||
private static void CalculateCombinedData(Profiler.ScopeNode node, in Dictionary<string, (double ms, uint calls)> combinedRecordData)
|
||||
{
|
||||
if (combinedRecordData.TryGetValue(node.Label, out (double ms, uint calls) combined))
|
||||
{
|
||||
combinedRecordData[node.Label] = (combined.ms + node.ElapsedMilliseconds(), combined.calls + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
combinedRecordData.Add(node.Label, (node.ElapsedMilliseconds(), 1));
|
||||
}
|
||||
|
||||
for (int i = 0; i < node.Children.Count; i++)
|
||||
{
|
||||
CalculateCombinedData(node.Children[i], combinedRecordData);
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawFlameGraph(Profiler.Frame frame)
|
||||
{
|
||||
if (frame == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ProfilerVisualizer.RenderFlameGraph(frame);
|
||||
}
|
||||
}
|
||||
}
|
91
Nerfed.Runtime.Generator/HookSourceGenerator.cs
Normal file
91
Nerfed.Runtime.Generator/HookSourceGenerator.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Nerfed.Runtime.Generator
|
||||
{
|
||||
[Generator]
|
||||
public class HookSourceGenerator : ISourceGenerator
|
||||
{
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
// Ensure the syntax receiver is not null and is of the expected type
|
||||
if (context.SyntaxReceiver is not HookSyntaxReceiver syntaxReceiver)
|
||||
return;
|
||||
|
||||
// Check if we have collected any hook methods
|
||||
List<MethodDeclarationSyntax> hookMethods = syntaxReceiver.HookMethods;
|
||||
if (hookMethods == null || !hookMethods.Any())
|
||||
return;
|
||||
|
||||
StringBuilder codeBuilder = new StringBuilder();
|
||||
|
||||
codeBuilder.AppendLine("using System;");
|
||||
codeBuilder.AppendLine("");
|
||||
codeBuilder.AppendLine("namespace Nerfed.Runtime.Generator;");
|
||||
codeBuilder.AppendLine("");
|
||||
codeBuilder.AppendLine($"// Generated by {typeof(HookSourceGenerator)}");
|
||||
codeBuilder.AppendLine("public static class Hook");
|
||||
codeBuilder.AppendLine("{");
|
||||
codeBuilder.AppendLine(" public static void InvokeHooks()");
|
||||
codeBuilder.AppendLine(" {");
|
||||
|
||||
foreach (MethodDeclarationSyntax method in hookMethods)
|
||||
{
|
||||
SemanticModel model = context.Compilation.GetSemanticModel(method.SyntaxTree);
|
||||
|
||||
if (model.GetDeclaredSymbol(method) is not IMethodSymbol methodSymbol)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (methodSymbol.DeclaredAccessibility != Accessibility.Public || !methodSymbol.IsStatic)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
codeBuilder.AppendLine($" {methodSymbol.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}.{methodSymbol.Name}();");
|
||||
}
|
||||
|
||||
codeBuilder.AppendLine(" }");
|
||||
codeBuilder.AppendLine("}");
|
||||
|
||||
// Add the generated code to the compilation
|
||||
context.AddSource("Hook.g.cs", codeBuilder.ToString());
|
||||
}
|
||||
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
context.RegisterForSyntaxNotifications(() => new HookSyntaxReceiver());
|
||||
}
|
||||
|
||||
public class HookSyntaxReceiver : ISyntaxReceiver
|
||||
{
|
||||
public List<MethodDeclarationSyntax> HookMethods { get; } = new List<MethodDeclarationSyntax>();
|
||||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
// Check if the node is a method declaration
|
||||
if (syntaxNode is MethodDeclarationSyntax methodDeclaration)
|
||||
{
|
||||
// Ensure the method declaration has attribute lists
|
||||
if (methodDeclaration.AttributeLists.Count == 0)
|
||||
return;
|
||||
|
||||
// Check if the method has the Hook attribute
|
||||
bool hasHookAttribute = methodDeclaration.AttributeLists
|
||||
.SelectMany(attrList => attrList.Attributes)
|
||||
.Any(attr => attr.Name.ToString() == "Hook");
|
||||
|
||||
if (hasHookAttribute)
|
||||
{
|
||||
HookMethods.Add(methodDeclaration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
33
Nerfed.Runtime.Generator/Nerfed.Runtime.Generator.csproj
Normal file
33
Nerfed.Runtime.Generator/Nerfed.Runtime.Generator.csproj
Normal file
@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
||||
<Configurations>Debug;Test;Release</Configurations>
|
||||
<Platforms>x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<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>
|
||||
|
||||
</Project>
|
@ -1,6 +0,0 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime.Components
|
||||
{
|
||||
public readonly record struct LocalToWorld(Matrix4x4 localToWorldMatrix);
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime.Components
|
||||
{
|
||||
public readonly record struct LocalTransform(Vector3 position, Quaternion rotation, Vector3 scale)
|
||||
{
|
||||
public static readonly LocalTransform Identity = new(Vector3.Zero, Quaternion.Identity, Vector3.One);
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
using MoonTools.ECS;
|
||||
|
||||
namespace Nerfed.Runtime.Components
|
||||
{
|
||||
public readonly record struct Root;
|
||||
//public readonly record struct Parent;
|
||||
//public readonly record struct PreviousParent;
|
||||
public readonly record struct Child;
|
||||
// Describes a relation from the child to the parent.
|
||||
public readonly record struct ChildParentRelation;
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
namespace Nerfed.Runtime.Components
|
||||
{
|
||||
public readonly record struct Test();
|
||||
}
|
@ -72,6 +72,7 @@ public static void Run(string[] args)
|
||||
AudioDevice = new AudioDevice();
|
||||
|
||||
OnInitialize?.Invoke();
|
||||
Nerfed.Runtime.Generator.Hook.InvokeHooks();
|
||||
|
||||
while (!quit)
|
||||
{
|
||||
@ -111,14 +112,10 @@ public static void Quit()
|
||||
|
||||
private static void Tick()
|
||||
{
|
||||
Profiler.BeginFrame();
|
||||
|
||||
AdvanceElapsedTime();
|
||||
|
||||
if (framerateCapped)
|
||||
{
|
||||
Profiler.BeginSample("framerateCapped");
|
||||
|
||||
/* We want to wait until the framerate cap,
|
||||
* but we don't want to oversleep. Requesting repeated 1ms sleeps and
|
||||
* seeing how long we actually slept for lets us estimate the worst case
|
||||
@ -141,8 +138,6 @@ private static void Tick()
|
||||
Thread.SpinWait(1);
|
||||
AdvanceElapsedTime();
|
||||
}
|
||||
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
// Do not let any step take longer than our maximum.
|
||||
@ -155,7 +150,6 @@ private static void Tick()
|
||||
{
|
||||
while (accumulatedUpdateTime >= Timestep)
|
||||
{
|
||||
Profiler.BeginSample("Update");
|
||||
Keyboard.Update();
|
||||
Mouse.Update();
|
||||
GamePad.Update();
|
||||
@ -163,26 +157,19 @@ private static void Tick()
|
||||
ProcessSDLEvents();
|
||||
|
||||
// Tick game here...
|
||||
Profiler.BeginSample("OnUpdate");
|
||||
OnUpdate?.Invoke();
|
||||
Profiler.EndSample();
|
||||
|
||||
AudioDevice.WakeThread();
|
||||
accumulatedUpdateTime -= Timestep;
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
double alpha = accumulatedUpdateTime / Timestep;
|
||||
|
||||
// Render here..
|
||||
Profiler.BeginSample("OnRender");
|
||||
OnRender?.Invoke();
|
||||
Profiler.EndSample();
|
||||
|
||||
accumulatedDrawTime -= framerateCapTimeSpan;
|
||||
}
|
||||
|
||||
Profiler.EndFrame();
|
||||
}
|
||||
|
||||
private static TimeSpan AdvanceElapsedTime()
|
||||
|
7
Nerfed.Runtime/Hook/HookAttribute.cs
Normal file
7
Nerfed.Runtime/Hook/HookAttribute.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Nerfed.Runtime.Hook
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||
public class HookAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
11
Nerfed.Runtime/Hook/HookTest.cs
Normal file
11
Nerfed.Runtime/Hook/HookTest.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace Nerfed.Runtime.Hook
|
||||
{
|
||||
public static class HookTest
|
||||
{
|
||||
[Hook]
|
||||
public static void Test()
|
||||
{
|
||||
Log.Info("Hook!");
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
Subproject commit 76b18a6ba9a33f5a93022390be7ed805f9f722e8
|
@ -11,6 +11,7 @@
|
||||
<IsPackable>false</IsPackable>
|
||||
<Configurations>Debug;Test;Release</Configurations>
|
||||
<Platforms>x64</Platforms>
|
||||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
@ -32,13 +33,18 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Libraries\FAudio\csharp\FAudio.cs" />
|
||||
<Compile Include="Libraries\ImGui.NET\src\ImGui.NET\**\*.cs" />
|
||||
<Compile Include="Libraries\MoonTools.ECS\src\**\*.cs" />
|
||||
<Compile Include="Libraries\RefreshCS\RefreshCS.cs" />
|
||||
<Compile Include="Libraries\SDL2CS\src\SDL2.cs" />
|
||||
<Compile Include="Libraries\RefreshCS\RefreshCS.cs" />
|
||||
<Compile Include="Libraries\FAudio\csharp\FAudio.cs" />
|
||||
<Compile Include="Libraries\WellspringCS\WellspringCS.cs" />
|
||||
<Compile Include="Libraries\dav1dfile\csharp\dav1dfile.cs" />
|
||||
<Compile Include="Libraries\ImGui.NET\src\ImGui.NET\**\*.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Nerfed.Runtime.Generator\Nerfed.Runtime.Generator.csproj"
|
||||
OutputItemType="Analyzer"
|
||||
ReferenceOutputAssembly="false" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -1,166 +1,29 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public struct ProfilerScope : IDisposable
|
||||
{
|
||||
public ProfilerScope(string label)
|
||||
{
|
||||
public ProfilerScope(string label) {
|
||||
Profiler.BeginSample(label);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
public void Dispose() {
|
||||
Profiler.EndSample();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Profiler
|
||||
{
|
||||
public class Frame(uint frameCount)
|
||||
{
|
||||
public uint FrameCount { get; } = frameCount;
|
||||
public long StartTime { get; } = Stopwatch.GetTimestamp();
|
||||
public long EndTime { get; private set; }
|
||||
[Conditional("PROFILING")]
|
||||
public static void BeginSample(string label) {
|
||||
|
||||
// Use a concurrent list to collect all thread root nodes per frame.
|
||||
public ConcurrentBag<ScopeNode> RootNodes = new ConcurrentBag<ScopeNode>();
|
||||
|
||||
internal void End()
|
||||
{
|
||||
EndTime = Stopwatch.GetTimestamp();
|
||||
}
|
||||
|
||||
public double ElapsedMilliseconds()
|
||||
{
|
||||
long elapsedTicks = EndTime - StartTime;
|
||||
return ((double)(elapsedTicks * 1000)) / Stopwatch.Frequency;
|
||||
}
|
||||
}
|
||||
|
||||
public class ScopeNode(string label)
|
||||
{
|
||||
public string Label { get; } = label;
|
||||
public long StartTime { get; private set; } = Stopwatch.GetTimestamp(); // Start time in ticks
|
||||
public long EndTime { get; private set; }
|
||||
public int ManagedThreadId { get; } = Environment.CurrentManagedThreadId;
|
||||
public List<ScopeNode> Children { get; } = new List<ScopeNode>();
|
||||
|
||||
internal void End()
|
||||
{
|
||||
EndTime = Stopwatch.GetTimestamp(); // End time in ticks
|
||||
}
|
||||
|
||||
public double ElapsedMilliseconds()
|
||||
{
|
||||
return ((double)(EndTime - StartTime)) * 1000 / Stopwatch.Frequency; // Convert ticks to ms
|
||||
}
|
||||
|
||||
// Add a child node (used for nested scopes)
|
||||
internal ScopeNode AddChild(string label)
|
||||
{
|
||||
ScopeNode child = new ScopeNode(label);
|
||||
Children.Add(child);
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
||||
private const int maxFrames = 128;
|
||||
|
||||
public static bool IsRecording { get; private set; } = true;
|
||||
|
||||
// Store only the last x amount of frames in memory.
|
||||
public static readonly BoundedQueue<Frame> Frames = new(maxFrames);
|
||||
|
||||
// Use ThreadLocal to store a stack of ScopeNodes per thread and enable tracking of thread-local values.
|
||||
private static readonly ThreadLocal<Stack<ScopeNode>> threadLocalScopes = new ThreadLocal<Stack<ScopeNode>>(() => new Stack<ScopeNode>(), true);
|
||||
|
||||
private static Frame currentFrame = null;
|
||||
private static uint frameCount = 0;
|
||||
|
||||
public static void SetActive(bool isRecording)
|
||||
{
|
||||
IsRecording = isRecording;
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void BeginFrame()
|
||||
{
|
||||
if (!IsRecording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
public static void EndSample() {
|
||||
|
||||
currentFrame = new Frame(frameCount);
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void EndFrame()
|
||||
{
|
||||
if (!IsRecording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (Stack<ScopeNode> scopes in threadLocalScopes.Values)
|
||||
{
|
||||
if (scopes.Count > 0)
|
||||
{
|
||||
// Pop the left over root nodes.
|
||||
ScopeNode currentScope = scopes.Pop();
|
||||
currentScope.End();
|
||||
}
|
||||
|
||||
// Clean up the thread-local stack to ensure it's empty for the next frame.
|
||||
scopes.Clear();
|
||||
}
|
||||
|
||||
currentFrame.End();
|
||||
Frames.Enqueue(currentFrame);
|
||||
frameCount++;
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void BeginSample(string label)
|
||||
{
|
||||
if (!IsRecording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack<ScopeNode> scopes = threadLocalScopes.Value; // Get the stack for the current thread
|
||||
|
||||
if (scopes.Count == 0)
|
||||
{
|
||||
// First scope for this thread (new root for this thread)
|
||||
ScopeNode rootScopeNode = new ScopeNode($"Thread-{Environment.CurrentManagedThreadId}");
|
||||
scopes.Push(rootScopeNode);
|
||||
currentFrame.RootNodes.Add(rootScopeNode); // Add root node to the frame list
|
||||
}
|
||||
|
||||
// Create a new child under the current top of the stack
|
||||
ScopeNode newScope = scopes.Peek().AddChild(label);
|
||||
|
||||
scopes.Push(newScope); // Push new scope to the thread's stack
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void EndSample()
|
||||
{
|
||||
if (!IsRecording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack<ScopeNode> scopes = threadLocalScopes.Value;
|
||||
|
||||
if (scopes.Count > 0)
|
||||
{
|
||||
// Only pop if this is not the root node.
|
||||
//ScopeNode currentScope = scopes.Count > 1 ? scopes.Pop() : scopes.Peek();
|
||||
ScopeNode currentScope = scopes.Pop();
|
||||
currentScope.End();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,156 +0,0 @@
|
||||
using ImGuiNET;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public static class ProfilerVisualizer
|
||||
{
|
||||
private const float barHeight = 20f;
|
||||
private const float barPadding = 2f;
|
||||
|
||||
// Render the flame graph across multiple threads
|
||||
public static void RenderFlameGraph(Profiler.Frame frame)
|
||||
{
|
||||
if (frame == null) return;
|
||||
if (frame.RootNodes == null) return;
|
||||
|
||||
// Calculate the total timeline duration (max end time across all nodes)
|
||||
double totalDuration = frame.EndTime - frame.StartTime;
|
||||
double startTime = frame.StartTime;
|
||||
|
||||
// Precompute the maximum depth for each thread's call stack
|
||||
Dictionary<int, int> threadMaxDepths = new Dictionary<int, int>();
|
||||
foreach (IGrouping<int, Profiler.ScopeNode> threadGroup in frame.RootNodes.GroupBy(node => node.ManagedThreadId))
|
||||
{
|
||||
int maxDepth = 0;
|
||||
foreach (Profiler.ScopeNode rootNode in threadGroup)
|
||||
{
|
||||
maxDepth = Math.Max(maxDepth, GetMaxDepth(rootNode, 0));
|
||||
}
|
||||
threadMaxDepths[threadGroup.Key] = maxDepth;
|
||||
}
|
||||
|
||||
// Start a child window to support scrolling
|
||||
ImGui.BeginChild("FlameGraph", new Vector2(0, 64), ImGuiChildFlags.Border | ImGuiChildFlags.ResizeY, ImGuiWindowFlags.HorizontalScrollbar | ImGuiWindowFlags.AlwaysVerticalScrollbar);
|
||||
|
||||
ImDrawListPtr drawList = ImGui.GetWindowDrawList();
|
||||
Vector2 windowPos = ImGui.GetCursorScreenPos();
|
||||
|
||||
// Sort nodes by ThreadID, ensuring main thread (Thread ID 1) is on top
|
||||
IOrderedEnumerable<IGrouping<int, Profiler.ScopeNode>> threadGroups = frame.RootNodes.GroupBy(node => node.ManagedThreadId).OrderBy(g => g.Key);
|
||||
|
||||
// Initial Y position for drawing
|
||||
float baseY = windowPos.Y;
|
||||
bool alternate = false;
|
||||
float contentWidth = ImGui.GetContentRegionAvail().X;
|
||||
|
||||
// Draw each thread's flame graph row by row
|
||||
foreach (IGrouping<int, Profiler.ScopeNode> threadGroup in threadGroups)
|
||||
{
|
||||
int threadId = threadGroup.Key;
|
||||
|
||||
// Compute the base Y position for this thread
|
||||
float threadBaseY = baseY;
|
||||
|
||||
// Calculate the maximum height for this thread's flame graph
|
||||
float threadHeight = (threadMaxDepths[threadId] + 1) * (barHeight + barPadding);
|
||||
|
||||
// Draw the alternating background for each thread row
|
||||
uint backgroundColor = ImGui.ColorConvertFloat4ToU32(alternate ? new Vector4(0.2f, 0.2f, 0.2f, 1f) : new Vector4(0.1f, 0.1f, 0.1f, 1f));
|
||||
drawList.AddRectFilled(new Vector2(windowPos.X, threadBaseY), new Vector2(windowPos.X + contentWidth, threadBaseY + threadHeight), backgroundColor);
|
||||
|
||||
alternate = !alternate;
|
||||
|
||||
// Draw each root node in the group (one per thread)
|
||||
foreach (Profiler.ScopeNode rootNode in threadGroup)
|
||||
{
|
||||
RenderNode(drawList, rootNode, startTime, totalDuration, windowPos.X, threadBaseY, 0, contentWidth, false);
|
||||
}
|
||||
|
||||
// Move to the next thread's row (max depth * height per level)
|
||||
baseY += (threadMaxDepths[threadId] + 1) * (barHeight + barPadding);
|
||||
}
|
||||
|
||||
// Ensure that ImGui knows the size of the content.
|
||||
ImGui.Dummy(new Vector2(contentWidth, baseY));
|
||||
|
||||
ImGui.EndChild();
|
||||
}
|
||||
|
||||
private static void RenderNode(ImDrawListPtr drawList, Profiler.ScopeNode node, double startTime, double totalDuration, float startX, float baseY, int depth, float contentWidth, bool alternate)
|
||||
{
|
||||
if (node == null) return;
|
||||
|
||||
double nodeStartTime = node.StartTime - startTime;
|
||||
double nodeEndTime = node.EndTime - startTime;
|
||||
double nodeDuration = nodeEndTime - nodeStartTime;
|
||||
|
||||
// Calculate the position and width of the bar based on time
|
||||
float xPos = (float)(startX + (nodeStartTime / totalDuration) * contentWidth);
|
||||
float width = (float)((nodeDuration / totalDuration) * contentWidth);
|
||||
|
||||
// Calculate the Y position based on depth
|
||||
float yPos = baseY + (depth * (barHeight + barPadding)) + (barPadding * 0.5f);
|
||||
|
||||
// Define the rectangle bounds for the node
|
||||
Vector2 min = new Vector2(xPos, yPos);
|
||||
Vector2 max = new Vector2(xPos + width, yPos + barHeight);
|
||||
|
||||
// Define color.
|
||||
Vector4 barColor = alternate ? new Vector4(0.4f, 0.6f, 0.9f, 1f) : new Vector4(0.4f, 0.5f, 0.8f, 1f);
|
||||
Vector4 textColor = new Vector4(1f, 1f, 1f, 1f);
|
||||
|
||||
if (depth != 0)
|
||||
{
|
||||
// Draw the bar for the node (colored based on thread depth)
|
||||
drawList.AddRectFilled(min, max, ImGui.ColorConvertFloat4ToU32(barColor));
|
||||
|
||||
// Draw the label if it fits inside the bar
|
||||
string label = $"{node.Label} ({node.ElapsedMilliseconds():0.000} ms)";
|
||||
if (width > ImGui.CalcTextSize(label).X)
|
||||
{
|
||||
drawList.AddText(new Vector2(xPos + barPadding, yPos + barPadding), ImGui.ColorConvertFloat4ToU32(textColor), label);
|
||||
}
|
||||
|
||||
// Add tooltip on hover
|
||||
if (ImGui.IsMouseHoveringRect(min, max))
|
||||
{
|
||||
// Show tooltip when hovering over the node
|
||||
ImGui.BeginTooltip();
|
||||
ImGui.Text($"{node.Label}");
|
||||
ImGui.Text($"{node.ElapsedMilliseconds():0.000} ms");
|
||||
ImGui.Text($"{node.ManagedThreadId}");
|
||||
ImGui.EndTooltip();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Aka root node.
|
||||
string label = $"{node.Label}";
|
||||
drawList.AddText(new Vector2(startX + barPadding, yPos + barPadding), ImGui.ColorConvertFloat4ToU32(textColor), label);
|
||||
}
|
||||
|
||||
// Draw each child node under this node
|
||||
foreach (Profiler.ScopeNode child in node.Children)
|
||||
{
|
||||
alternate = !alternate;
|
||||
RenderNode(drawList, child, startTime, totalDuration, startX, baseY, depth + 1, contentWidth, alternate);
|
||||
}
|
||||
}
|
||||
|
||||
// Recursive function to calculate the maximum depth of the node tree
|
||||
private static int GetMaxDepth(Profiler.ScopeNode node, int currentDepth)
|
||||
{
|
||||
if (node.Children == null || node.Children.Count == 0)
|
||||
{
|
||||
return currentDepth;
|
||||
}
|
||||
|
||||
int maxDepth = currentDepth;
|
||||
foreach (Profiler.ScopeNode child in node.Children)
|
||||
{
|
||||
maxDepth = Math.Max(maxDepth, GetMaxDepth(child, currentDepth + 1));
|
||||
}
|
||||
return maxDepth;
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
using System.Numerics;
|
||||
using ImGuiNET;
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime.Components;
|
||||
|
||||
namespace Nerfed.Runtime.Serialization;
|
||||
|
||||
public static class ComponentHelper
|
||||
{
|
||||
// Auto generate this.
|
||||
public static readonly Dictionary<Type, Func<World, Entity, ValueType>> GetComponentByType = new()
|
||||
{
|
||||
{ typeof(LocalTransform), (world, entity) => world.Get<LocalTransform>(entity) },
|
||||
{ typeof(Root), (world, entity) => world.Get<Root>(entity) },
|
||||
};
|
||||
|
||||
// Auto generate this.
|
||||
public static readonly Dictionary<Type, Action<World, Entity, ValueType>> SetComponentByType = new()
|
||||
{
|
||||
{ typeof(LocalTransform), (world, entity, component) => world.Set(entity, (LocalTransform)component) },
|
||||
{ typeof(Root), (world, entity, component) => world.Set(entity, (Root)component) },
|
||||
};
|
||||
|
||||
// Auto generate this, but it should only contain user assignable components (so something like 'root' should be excluded).
|
||||
// Maybe use an attribute for this.
|
||||
public static readonly Dictionary<Type, Action<World, Entity>> AddComponentByType = new()
|
||||
{
|
||||
{ typeof(LocalTransform), (world, entity) => world.Set(entity, LocalTransform.Identity) },
|
||||
};
|
||||
|
||||
// Auto generate this, but also keep the option for 'custom inspectors'.
|
||||
// Maybe via attribute?
|
||||
public static readonly Dictionary<Type, Action<World, Entity>> ComponentInspectorByType = new()
|
||||
{
|
||||
{
|
||||
typeof(LocalTransform), (world, entity) =>
|
||||
{
|
||||
(Vector3 position, Quaternion rotation, Vector3 scale) = world.Get<LocalTransform>(entity);
|
||||
Vector3 eulerAngles = MathEx.ToEulerAngles(rotation);
|
||||
eulerAngles = new Vector3(float.RadiansToDegrees(eulerAngles.X), float.RadiansToDegrees(eulerAngles.Y), float.RadiansToDegrees(eulerAngles.Z));
|
||||
bool isDirty = false;
|
||||
|
||||
ImGui.BeginGroup();
|
||||
ImGui.Text($"{nameof(LocalTransform)}");
|
||||
isDirty |= ImGui.DragFloat3("Position", ref position, 0.2f, float.MinValue, float.MaxValue /*, "%f0 m" */); // TODO: right format.
|
||||
isDirty |= ImGui.DragFloat3("Rotation", ref eulerAngles);
|
||||
isDirty |= ImGui.DragFloat3("Scale", ref scale);
|
||||
ImGui.EndGroup();
|
||||
|
||||
if (!isDirty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
eulerAngles = new Vector3(float.DegreesToRadians(eulerAngles.X), float.DegreesToRadians(eulerAngles.Y), float.DegreesToRadians(eulerAngles.Z));
|
||||
world.Set(entity, new LocalTransform(position, MathEx.ToQuaternion(eulerAngles), scale));
|
||||
}
|
||||
},
|
||||
{
|
||||
typeof(Root), (world, entity) =>
|
||||
{
|
||||
ImGui.BeginGroup();
|
||||
ImGui.Text($"{nameof(Root)}");
|
||||
ImGui.EndGroup();
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime.Components;
|
||||
using Nerfed.Runtime.Util;
|
||||
using System.Numerics;
|
||||
|
||||
// TODO:
|
||||
// Explore if having a WorldTransform and LocalTransfom component each holding position, rotation, scale values and the matricies is useful.
|
||||
// Often you need to either get or set these values.
|
||||
// If so, we probably need a utility funciton to do so. Since changing these values means that we need to update all the related data + children as well.
|
||||
|
||||
// TODO:
|
||||
// When modifying transform all the children need to be updated as well.
|
||||
|
||||
namespace Nerfed.Runtime.Systems
|
||||
{
|
||||
public class LocalToWorldSystem : MoonTools.ECS.System
|
||||
{
|
||||
private readonly bool useParallelFor = true; // When having a low amount of transforms or when in debug mode this might be slower.
|
||||
private readonly Filter rootEntitiesFilter;
|
||||
private readonly Filter entitiesWithoutLocalToWorldFilter;
|
||||
private readonly Action<int> updateWorldTransform;
|
||||
|
||||
public LocalToWorldSystem(World world) : base(world)
|
||||
{
|
||||
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
||||
if (useParallelFor)
|
||||
{
|
||||
entitiesWithoutLocalToWorldFilter = FilterBuilder.Include<LocalTransform>().Exclude<LocalToWorld>().Build();
|
||||
updateWorldTransform = UpdateWorldTransformByIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
if (rootEntitiesFilter.Empty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (useParallelFor)
|
||||
{
|
||||
Profiler.BeginSample("ParallelFor.LocalToWorldCheck");
|
||||
// This check is needed because some entities might not have a LocalToWorld component yet.
|
||||
// Adding this during the loop will break.
|
||||
foreach (Entity entity in entitiesWithoutLocalToWorldFilter.Entities) {
|
||||
Set(entity, new LocalToWorld(Matrix4x4.Identity));
|
||||
}
|
||||
Profiler.EndSample();
|
||||
|
||||
Profiler.BeginSample("ParallelFor.LocalToWorldUpdate");
|
||||
// This should only be used when the filter doesn't change by executing these functions!
|
||||
// So no entity deletion or setting/removing of components used by the filters in this loop.
|
||||
Parallel.For(0, rootEntitiesFilter.Count, updateWorldTransform);
|
||||
Profiler.EndSample();
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (Entity entity in rootEntitiesFilter.Entities)
|
||||
{
|
||||
Profiler.BeginSample("UpdateWorldTransform");
|
||||
UpdateWorldTransform(entity, Matrix4x4.Identity);
|
||||
Profiler.EndSample();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateWorldTransformByIndex(int entityFilterIndex)
|
||||
{
|
||||
Profiler.BeginSample("UpdateWorldTransformByIndex");
|
||||
Entity entity = rootEntitiesFilter.NthEntity(entityFilterIndex);
|
||||
UpdateWorldTransform(entity, Matrix4x4.Identity);
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
private void UpdateWorldTransform(in Entity entity, Matrix4x4 localToWorldMatrix)
|
||||
{
|
||||
// TODO: Only update dirty transforms.
|
||||
// If a parent is dirty all the children need to update their localToWorld matrix.
|
||||
// How do we check if something is dirty? How do we know if a LocalTransform has been changed?
|
||||
if (Has<LocalTransform>(entity))
|
||||
{
|
||||
LocalTransform localTransform = Get<LocalTransform>(entity);
|
||||
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
Set(entity, localToWorld);
|
||||
}
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
UpdateWorldTransform(childEntity, localToWorldMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public class BoundedQueue<T> : IEnumerable<T>, ICollection, IReadOnlyCollection<T>
|
||||
{
|
||||
private readonly Queue<T> queue = null;
|
||||
private readonly int maxSize = 10;
|
||||
private T lastAddedElement;
|
||||
|
||||
public BoundedQueue(int maxSize)
|
||||
{
|
||||
this.maxSize = maxSize;
|
||||
queue = new Queue<T>(maxSize);
|
||||
}
|
||||
|
||||
public void Enqueue(T item)
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
if (queue.Count > maxSize)
|
||||
{
|
||||
queue.Dequeue(); // Remove the oldest element
|
||||
}
|
||||
|
||||
lastAddedElement = item;
|
||||
}
|
||||
|
||||
public T Dequeue()
|
||||
{
|
||||
return queue.Dequeue();
|
||||
}
|
||||
|
||||
public T Peek()
|
||||
{
|
||||
return queue.Peek();
|
||||
}
|
||||
|
||||
public T LastAddedElement()
|
||||
{
|
||||
return lastAddedElement;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
queue.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(T item)
|
||||
{
|
||||
return queue.Contains(item);
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return queue.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return queue.GetEnumerator();
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
((ICollection)queue).CopyTo(array, index);
|
||||
}
|
||||
|
||||
public int Count => queue.Count;
|
||||
public int Capacity => maxSize;
|
||||
public bool IsSynchronized => ((ICollection)queue).IsSynchronized;
|
||||
public object SyncRoot => ((ICollection)queue).SyncRoot;
|
||||
int IReadOnlyCollection<T>.Count => queue.Count;
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public static class MathEx
|
||||
@ -19,51 +17,4 @@ public static float Denormalize(float value, float newMin, float newMax) {
|
||||
public static float Remap(float value, float oldMin, float oldMax, float newMin, float newMax) {
|
||||
return (value - oldMin) / (oldMax - oldMin) * (newMax - newMin) + newMin;
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/70462758/c-sharp-how-to-convert-quaternions-to-euler-angles-xyz
|
||||
public static Quaternion ToQuaternion(Vector3 v)
|
||||
{
|
||||
float cy = (float)Math.Cos(v.Z * 0.5);
|
||||
float sy = (float)Math.Sin(v.Z * 0.5);
|
||||
float cp = (float)Math.Cos(v.Y * 0.5);
|
||||
float sp = (float)Math.Sin(v.Y * 0.5);
|
||||
float cr = (float)Math.Cos(v.X * 0.5);
|
||||
float sr = (float)Math.Sin(v.X * 0.5);
|
||||
|
||||
return new Quaternion
|
||||
{
|
||||
W = (cr * cp * cy + sr * sp * sy),
|
||||
X = (sr * cp * cy - cr * sp * sy),
|
||||
Y = (cr * sp * cy + sr * cp * sy),
|
||||
Z = (cr * cp * sy - sr * sp * cy),
|
||||
};
|
||||
}
|
||||
|
||||
public static Vector3 ToEulerAngles(Quaternion q)
|
||||
{
|
||||
Vector3 angles = new();
|
||||
|
||||
// roll / x
|
||||
double sinrCosp = 2 * (q.W * q.X + q.Y * q.Z);
|
||||
double cosrCosp = 1 - 2 * (q.X * q.X + q.Y * q.Y);
|
||||
angles.X = (float)Math.Atan2(sinrCosp, cosrCosp);
|
||||
|
||||
// pitch / y
|
||||
double sinp = 2 * (q.W * q.Y - q.Z * q.X);
|
||||
if (Math.Abs(sinp) >= 1)
|
||||
{
|
||||
angles.Y = (float)Math.CopySign(Math.PI / 2, sinp);
|
||||
}
|
||||
else
|
||||
{
|
||||
angles.Y = (float)Math.Asin(sinp);
|
||||
}
|
||||
|
||||
// yaw / z
|
||||
double sinyCosp = 2 * (q.W * q.Z + q.X * q.Y);
|
||||
double cosyCosp = 1 - 2 * (q.Y * q.Y + q.Z * q.Z);
|
||||
angles.Z = (float)Math.Atan2(sinyCosp, cosyCosp);
|
||||
|
||||
return angles;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Nerfed.Runtime.Util;
|
||||
|
||||
public static class RandomId
|
||||
{
|
||||
public static uint GenerateSecureRandomUInt()
|
||||
{
|
||||
byte[] buffer = new byte[4];
|
||||
RandomNumberGenerator.Fill(buffer);
|
||||
return BitConverter.ToUInt32(buffer, 0);
|
||||
}
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime.Components;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime.Util
|
||||
{
|
||||
// https://github.com/needle-mirror/com.unity.entities/blob/master/Unity.Transforms/TransformHelpers.cs
|
||||
public static class Transform
|
||||
{
|
||||
public static Vector3 Forward(in this Matrix4x4 matrix) => new Vector3(matrix.M31, matrix.M32, matrix.M33);
|
||||
public static Vector3 Back(in this Matrix4x4 matrix) => -matrix.Forward();
|
||||
public static Vector3 Up(in this Matrix4x4 matrix) => new Vector3(matrix.M21, matrix.M22, matrix.M23);
|
||||
public static Vector3 Down(in this Matrix4x4 matrix) => -matrix.Up();
|
||||
public static Vector3 Right(in this Matrix4x4 matrix) => new Vector3(matrix.M11, matrix.M12, matrix.M13);
|
||||
public static Vector3 Left(in this Matrix4x4 matrix) => -matrix.Right();
|
||||
//public static Vector3 Translation(in this Matrix4x4 matrix) => new Vector3();
|
||||
//public static Quaternion Rotation(in this Matrix4x4 matrix) => new Quaternion();
|
||||
|
||||
public static Matrix4x4 TRS(in this LocalTransform localTransform)
|
||||
{
|
||||
return Matrix4x4.CreateScale(localTransform.scale) *
|
||||
Matrix4x4.CreateFromQuaternion(localTransform.rotation) *
|
||||
Matrix4x4.CreateTranslation(localTransform.position);
|
||||
}
|
||||
|
||||
// Sets the parent child relation and adds a child component.
|
||||
// Relation goes from child to parent.
|
||||
public static void SetParent(in World world, in Entity child, in Entity parent)
|
||||
{
|
||||
RemoveParent(world, child);
|
||||
|
||||
world.Relate(child, parent, new ChildParentRelation());
|
||||
world.Set(child, new Child());
|
||||
world.Remove<Root>(child);
|
||||
return;
|
||||
}
|
||||
|
||||
// Removes any parent child relation ship, thus making it a 'root' object.
|
||||
public static void RemoveParent(in World world, in Entity child)
|
||||
{
|
||||
if (!world.HasOutRelation<ChildParentRelation>(child))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Entity parent = world.OutRelationSingleton<ChildParentRelation>(child);
|
||||
|
||||
// TODO: Check if Unrelate all also unrelates incomming relations..?
|
||||
world.Unrelate<ChildParentRelation>(child, parent);
|
||||
world.Remove<Child>(child);
|
||||
world.Set(child, new Root());
|
||||
}
|
||||
|
||||
public static Entity CreateBaseEntity(this World world, string tag = "")
|
||||
{
|
||||
Entity entity = world.CreateEntity(tag);
|
||||
world.Set(entity, new Root());
|
||||
return entity;
|
||||
}
|
||||
|
||||
// Force update the transform data of an entity (and children).
|
||||
// Useful for when you need precise up to date transform data.
|
||||
public static void ForceUpdateLocalToWorld(in World world, in Entity entity)
|
||||
{
|
||||
Matrix4x4 parentLocalToWorldMatrix = Matrix4x4.Identity;
|
||||
|
||||
if (world.HasOutRelation<ChildParentRelation>(entity)) {
|
||||
Entity parent = world.OutRelationSingleton<ChildParentRelation>(entity);
|
||||
|
||||
if (world.Has<LocalToWorld>(parent))
|
||||
{
|
||||
parentLocalToWorldMatrix = world.Get<LocalToWorld>(parent).localToWorldMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
ForceUpdateLocalToWorld(world, entity, parentLocalToWorldMatrix);
|
||||
}
|
||||
|
||||
private static void ForceUpdateLocalToWorld(in World world, in Entity entity, Matrix4x4 localToWorldMatrix)
|
||||
{
|
||||
if (world.Has<LocalTransform>(entity))
|
||||
{
|
||||
LocalTransform localTransform = world.Get<LocalTransform>(entity);
|
||||
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
world.Set(entity, localToWorld);
|
||||
|
||||
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
}
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = world.InRelations<ChildParentRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
ForceUpdateLocalToWorld(world, childEntity, localToWorldMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
Nerfed.sln
56
Nerfed.sln
@ -5,38 +5,54 @@ VisualStudioVersion = 17.10.35013.160
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Runtime", "Nerfed.Runtime\Nerfed.Runtime.csproj", "{98E09BAF-587F-4238-89BD-7693C036C233}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nerfed.Builder", "Nerfed.Builder\Nerfed.Builder.csproj", "{1B88DE56-2AD8-441E-9B10-073AA43840BF}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Builder", "Nerfed.Builder\Nerfed.Builder.csproj", "{1B88DE56-2AD8-441E-9B10-073AA43840BF}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nerfed.Editor", "Nerfed.Editor\Nerfed.Editor.csproj", "{FF7D032D-7F0B-4700-A818-0606D66AECF8}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Editor", "Nerfed.Editor\Nerfed.Editor.csproj", "{FF7D032D-7F0B-4700-A818-0606D66AECF8}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF} = {1B88DE56-2AD8-441E-9B10-073AA43840BF}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Compiler", "Nerfed.Compiler\Nerfed.Compiler.csproj", "{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nerfed.Runtime.Generator", "Nerfed.Runtime.Generator\Nerfed.Runtime.Generator.csproj", "{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Test|x64 = Test|x64
|
||||
Release|x64 = Release|x64
|
||||
Debug|x64 = Debug|x64
|
||||
Release|x64 = Release|x64
|
||||
Test|x64 = Test|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.ActiveCfg = Test|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.Build.0 = Test|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.ActiveCfg = Release|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.Build.0 = Release|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.Build.0 = Debug|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.ActiveCfg = Test|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.Build.0 = Test|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.ActiveCfg = Release|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.Build.0 = Release|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.Build.0 = Debug|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.ActiveCfg = Test|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.Build.0 = Test|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.ActiveCfg = Release|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.Build.0 = Release|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Debug|x64.Build.0 = Debug|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.ActiveCfg = Release|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.Build.0 = Release|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.ActiveCfg = Test|x64
|
||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.Build.0 = Test|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.Build.0 = Debug|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.ActiveCfg = Release|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.Build.0 = Release|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.ActiveCfg = Test|x64
|
||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.Build.0 = Test|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.Build.0 = Debug|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.ActiveCfg = Release|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.Build.0 = Release|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.ActiveCfg = Test|x64
|
||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.Build.0 = Test|x64
|
||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Debug|x64.Build.0 = Debug|x64
|
||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Release|x64.ActiveCfg = Release|x64
|
||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Release|x64.Build.0 = Release|x64
|
||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Test|x64.ActiveCfg = Test|x64
|
||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Test|x64.Build.0 = Test|x64
|
||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Debug|x64.Build.0 = Debug|x64
|
||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Release|x64.ActiveCfg = Release|x64
|
||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Release|x64.Build.0 = Release|x64
|
||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Test|x64.ActiveCfg = Test|x64
|
||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Test|x64.Build.0 = Test|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Loading…
Reference in New Issue
Block a user