Skip to content

Commit

Permalink
Native AOT experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejNepozitek committed Jan 14, 2024
1 parent 2094900 commit 7841e39
Show file tree
Hide file tree
Showing 93 changed files with 6,994 additions and 25 deletions.
17 changes: 17 additions & 0 deletions Edgar.NativeAot/Edgar.NativeAot.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Edgar.NativeAotLib\Edgar.NativeAotLib.csproj" />
<ProjectReference Include="..\src\Edgar\Edgar.csproj" />
</ItemGroup>

</Project>
83 changes: 83 additions & 0 deletions Edgar.NativeAot/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Edgar.Geometry;
using Edgar.GraphBasedGenerator.Common;
using Edgar.GraphBasedGenerator.Grid2D;
using Edgar.Legacy.Utils;
using Edgar.NativeAotLib;
using Edgar.Utils;
using System.Diagnostics;
using File = System.IO.File;

namespace Edgar.NativeAot
{
internal class Program
{
static void Main(string[] args)
{
GenerateFromUnity();
return;

Console.WriteLine("Running...");
Console.WriteLine(DateTime.Now);

var graph = GraphsDatabase.GetExample4();
var roomTemplates = new List<RoomTemplateGrid2D>()
{
new RoomTemplateGrid2D(PolygonGrid2D.GetSquare(10), new SimpleDoorModeGrid2D(1, 1)),
};

var levelDescription = new LevelDescriptionGrid2D<int>();
foreach (var graphVertex in graph.Vertices)
{
levelDescription.AddRoom(graphVertex, new RoomDescriptionGrid2D(false, roomTemplates));
}

foreach (var graphEdge in graph.Edges)
{
levelDescription.AddConnection(graphEdge.From, graphEdge.To);
}

var levelDescriptionJson = SystemTextJsonUtils.SerializeToJson(levelDescription);
File.WriteAllText("levelDescription.json", levelDescriptionJson);
var deserializedLevelDescription = SystemTextJsonUtils.DeserializeFromJson(levelDescriptionJson);
var levelDescriptionJson2 = SystemTextJsonUtils.SerializeToJson(deserializedLevelDescription);
File.WriteAllText("levelDescriptionRe.json", levelDescriptionJson2);

if (levelDescriptionJson != levelDescriptionJson2)
{
throw new InvalidOperationException();
}

var random = new Random(0);
var generator = new GraphBasedGeneratorGrid2D<int>(levelDescription);
generator.InjectRandomGenerator(random);

var layout = generator.GenerateLayout();

var layoutJson = SystemTextJsonUtils.SerializeToJson(layout);
File.WriteAllText("layout.json", layoutJson);

Console.WriteLine(DateTime.Now);
}

private static void GenerateFromUnity()
{
var json = File.ReadAllText("levelDescriptionUnity.json");
var levelDescription = SystemTextJsonUtils.DeserializeFromJson(json);

var sw = new Stopwatch();
sw.Start();

var random = new Random(1198642031);
var generator = new GraphBasedGeneratorGrid2D<int>(levelDescription);
generator.InjectRandomGenerator(random);

var layout = generator.GenerateLayout();
var layoutJson = SystemTextJsonUtils.SerializeToJson(layout);

Console.WriteLine($"Layout generated in {sw.ElapsedMilliseconds / 1000f:F} seconds");
Console.WriteLine($"Iterations: {generator.IterationsCount}, time: {generator.TimeTotal}");

File.WriteAllText("layoutUnity.json", layoutJson);
}
}
}
9 changes: 9 additions & 0 deletions Edgar.NativeAotHelpers/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Edgar.NativeAotHelpers
{
public class Class1
{

}
}
11 changes: 11 additions & 0 deletions Edgar.NativeAotHelpers/Edgar.NativeAotHelpers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions Edgar.NativeAotLib/Edgar.NativeAotLib.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Edgar\Edgar.csproj" />
</ItemGroup>

<PropertyGroup>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>

<CompilerGeneratedFilesOutputPath>GeneratedFiles</CompilerGeneratedFilesOutputPath>

</PropertyGroup>

<ItemGroup>
<!-- Exclude the output of source generators from the compilation -->
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>
83 changes: 83 additions & 0 deletions Edgar.NativeAotLib/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Edgar.Geometry;
using Edgar.GraphBasedGenerator.Grid2D;
using Edgar.Legacy.Utils;

namespace Edgar.NativeAotLib {
public static class EntryPoint
{
[UnmanagedCallersOnly(EntryPoint = "GenerateLevel", CallConvs = new[] { typeof(CallConvCdecl) })]
public static IntPtr GenerateLevel(IntPtr levelDescriptionJsonPtr, int seed)
{
try
{
var levelDescriptionJson = Marshal.PtrToStringAnsi(levelDescriptionJsonPtr) ?? string.Empty;
var levelDescription = SystemTextJsonUtils.DeserializeFromJson(levelDescriptionJson)!;

var random = new Random(seed);
var generator = new GraphBasedGeneratorGrid2D<int>(levelDescription);
generator.InjectRandomGenerator(random);

var layout = generator.GenerateLayout();
var layoutJson = SystemTextJsonUtils.SerializeToJson(layout);

var result = Marshal.StringToHGlobalAnsi(layoutJson);

return result;
}
catch
{
return IntPtr.Zero;
}
}

[UnmanagedCallersOnly(EntryPoint = "GenerateLevelDummy")]
public static IntPtr GenerateLevelDummy(IntPtr levelDescriptionJsonPtr, int seed)
{
var levelDescriptionJson = Marshal.PtrToStringAnsi(levelDescriptionJsonPtr) ?? string.Empty;
var result = Marshal.StringToHGlobalAnsi(levelDescriptionJson);

return result;
}

[UnmanagedCallersOnly(EntryPoint = "RunNativeEdgar")]
public static int RunNativeEdgar(int count)
{
var graph = GraphsDatabase.GetExample4();
var roomTemplates = new List<RoomTemplateGrid2D>()
{
new RoomTemplateGrid2D(PolygonGrid2D.GetSquare(10), new SimpleDoorModeGrid2D(1, 1)),
};

var levelDescription = new LevelDescriptionGrid2D<int>();
foreach (var graphVertex in graph.Vertices)
{
levelDescription.AddRoom(graphVertex, new RoomDescriptionGrid2D(false, roomTemplates));
}

foreach (var graphEdge in graph.Edges)
{
levelDescription.AddConnection(graphEdge.From, graphEdge.To);
}

var random = new Random(0);
var generator = new GraphBasedGeneratorGrid2D<int>(levelDescription);
generator.InjectRandomGenerator(random);
var successCount = 0;

for (int i = 0; i < count; i++)
{
var level = generator.GenerateLayout();
if (level != null)
{
successCount++;
}
}

return successCount;
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <auto-generated/>

#nullable enable annotations
#nullable disable warnings

// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0612, CS0618

namespace Edgar.NativeAotLib
{
public partial class LayoutContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<bool>? _Boolean;

/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<bool> Boolean
{
get => _Boolean ??= (global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<bool>)Options.GetTypeInfo(typeof(bool));
}

private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<bool> Create_Boolean(global::System.Text.Json.JsonSerializerOptions options)
{
if (!TryGetTypeInfoForRuntimeCustomConverter<bool>(options, out global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<bool> jsonTypeInfo))
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<bool>(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.BooleanConverter);
}

jsonTypeInfo.OriginatingResolver = this;
return jsonTypeInfo;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <auto-generated/>

#nullable enable annotations
#nullable disable warnings

// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0612, CS0618

namespace Edgar.NativeAotLib
{
public partial class LayoutContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction>? _Direction;

/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction> Direction
{
get => _Direction ??= (global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction>)Options.GetTypeInfo(typeof(global::Edgar.Geometry.OrthogonalLineGrid2D.Direction));
}

private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction> Create_Direction(global::System.Text.Json.JsonSerializerOptions options)
{
if (!TryGetTypeInfoForRuntimeCustomConverter<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction>(options, out global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction> jsonTypeInfo))
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction>(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.GetEnumConverter<global::Edgar.Geometry.OrthogonalLineGrid2D.Direction>(options));
}

jsonTypeInfo.OriginatingResolver = this;
return jsonTypeInfo;
}
}
}
Loading

0 comments on commit 7841e39

Please sign in to comment.