diff --git a/guests/csharp/dotnet-guest-test/ExampleWorldImpl.cs b/guests/csharp/dotnet-guest-test/ExampleWorldImpl.cs index 38d14d3..c9c51c7 100644 --- a/guests/csharp/dotnet-guest-test/ExampleWorldImpl.cs +++ b/guests/csharp/dotnet-guest-test/ExampleWorldImpl.cs @@ -1,39 +1,10 @@ namespace ExampleWorld; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using ExampleWorld.wit.imports.lyra.api; -using LyraApi; + using LyraApi.Ecs; - -[StructLayout(LayoutKind.Sequential)] -struct Vec3(float x, float y, float z) : IComponent -{ - /// - /// The X component. - /// - public float X { get; set; } = x; - /// - /// The Y component - /// - public float Y { get; set; } = y; - /// - /// The Z component - /// - public float Z { get; set; } = z; - - public static string HostName => "Vec3"; - public static ulong HostSize => (ulong)Marshal.SizeOf(); - public static ulong HostAlignment => (ulong)MarshalUtils.AlignmentOf(); - public static ulong TypeId => 4124409524; - - public static object? TakeFromBytes(byte[] bytes) - { - byte[] taken = bytes.Take((int)HostSize).ToArray(); - return MarshalUtils.FromBytes(taken); - } -} +using LyraApi.Engine; +using LyraApi.Math; public class ExampleWorldImpl : IExampleWorld { @@ -47,6 +18,11 @@ public class ExampleWorldImpl : IExampleWorld { Console.WriteLine("C#: Found entity at ({0}, {1}, {2})", comp.X, comp.Y, comp.Z); } + + DeltaTime? dt = world.GetResource(); + if (dt != null) { + Console.WriteLine($"C#: Delta time: {dt?.Seconds:0.##}"); + } } public static void OnUpdate(IEcs.EcsWorld gameWorld, IEcs.Entity owningEntity) diff --git a/guests/csharp/dotnet-guest-test/LyraApi/Ecs/IResource.cs b/guests/csharp/dotnet-guest-test/LyraApi/Ecs/IResource.cs new file mode 100644 index 0000000..7cc910c --- /dev/null +++ b/guests/csharp/dotnet-guest-test/LyraApi/Ecs/IResource.cs @@ -0,0 +1,10 @@ +using static ExampleWorld.wit.imports.lyra.api.IEcs; + +namespace LyraApi.Ecs; + +public interface IResource +{ + public abstract static ulong TypeId { get; } + + public abstract static object? FromWasmResult(WorldResourceResult result); +} \ No newline at end of file diff --git a/guests/csharp/dotnet-guest-test/LyraApi/Ecs/World.cs b/guests/csharp/dotnet-guest-test/LyraApi/Ecs/World.cs index fcd9886..f8ed547 100644 --- a/guests/csharp/dotnet-guest-test/LyraApi/Ecs/World.cs +++ b/guests/csharp/dotnet-guest-test/LyraApi/Ecs/World.cs @@ -27,4 +27,10 @@ public class World(IEcs.EcsWorld world) IEcs.EcsDynamicView dynamicView = Inner.View(infos); return new ViewResult(comps, dynamicView).Get(); } + + public T? GetResource() where T : IResource + { + IEcs.WorldResourceResult result = Inner.GetResource(Utils.ToWasmTypeId(T.TypeId)); + return (T?) T.FromWasmResult(result); + } } \ No newline at end of file diff --git a/guests/csharp/dotnet-guest-test/LyraApi/Engine/DeltaTime.cs b/guests/csharp/dotnet-guest-test/LyraApi/Engine/DeltaTime.cs new file mode 100644 index 0000000..461f275 --- /dev/null +++ b/guests/csharp/dotnet-guest-test/LyraApi/Engine/DeltaTime.cs @@ -0,0 +1,23 @@ +using System.Runtime.InteropServices; +using LyraApi; +using LyraApi.Ecs; +using static ExampleWorld.wit.imports.lyra.api.IEcs; + +namespace LyraApi.Engine; + +[StructLayout(LayoutKind.Sequential)] +public struct DeltaTime : IResource { + public float Seconds; + + public static ulong TypeId => 83716348954; + + public static object? FromWasmResult(WorldResourceResult result) + { + return result.Tag switch + { + WorldResourceResult.NONE | WorldResourceResult.WASM_RESOURCE_REP => null, + WorldResourceResult.BYTES => MarshalUtils.FromBytes(result.AsBytes), + _ => null, + }; + } +} \ No newline at end of file diff --git a/guests/csharp/dotnet-guest-test/LyraApi/Math/Vec3.cs b/guests/csharp/dotnet-guest-test/LyraApi/Math/Vec3.cs new file mode 100644 index 0000000..ad0a372 --- /dev/null +++ b/guests/csharp/dotnet-guest-test/LyraApi/Math/Vec3.cs @@ -0,0 +1,32 @@ +using System.Runtime.InteropServices; +using LyraApi.Ecs; + +namespace LyraApi.Math; + +[StructLayout(LayoutKind.Sequential)] +struct Vec3(float x, float y, float z) : IComponent +{ + /// + /// The X component. + /// + public float X { get; set; } = x; + /// + /// The Y component + /// + public float Y { get; set; } = y; + /// + /// The Z component + /// + public float Z { get; set; } = z; + + public static string HostName => "Vec3"; + public static ulong HostSize => (ulong)Marshal.SizeOf(); + public static ulong HostAlignment => (ulong)MarshalUtils.AlignmentOf(); + public static ulong TypeId => 4124409524; + + public static object? TakeFromBytes(byte[] bytes) + { + byte[] taken = bytes.Take((int)HostSize).ToArray(); + return MarshalUtils.FromBytes(taken); + } +} \ No newline at end of file diff --git a/guests/csharp/dotnet-guest-test/LyraApi/Utils.cs b/guests/csharp/dotnet-guest-test/LyraApi/Utils.cs new file mode 100644 index 0000000..95242c4 --- /dev/null +++ b/guests/csharp/dotnet-guest-test/LyraApi/Utils.cs @@ -0,0 +1,8 @@ +using static ExampleWorld.wit.imports.lyra.api.IEcs; + +internal static class Utils { + public static WasmTypeId ToWasmTypeId(ulong typeId) + { + return new WasmTypeId((typeId, 0)); + } +} \ No newline at end of file diff --git a/guests/csharp/dotnet-guest-test/dotnet-guest-test.csproj b/guests/csharp/dotnet-guest-test/dotnet-guest-test.csproj index 8971c96..757eff2 100644 --- a/guests/csharp/dotnet-guest-test/dotnet-guest-test.csproj +++ b/guests/csharp/dotnet-guest-test/dotnet-guest-test.csproj @@ -18,12 +18,12 @@ - +