2024-11-07 02:26:03 +00:00
|
|
|
|
namespace ExampleWorld;//.wit.exports.component.witguest;
|
2024-11-05 20:10:19 +00:00
|
|
|
|
|
2024-11-07 02:26:03 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using ExampleWorld.wit.imports.lyra.api;
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
struct Vec3(float x, float y, float z)
|
2024-11-05 20:10:19 +00:00
|
|
|
|
{
|
2024-11-07 02:26:03 +00:00
|
|
|
|
public const ulong TypeId = 4124409524;
|
|
|
|
|
public float X { get; set; } = x;
|
|
|
|
|
public float Y { get; set; } = y;
|
|
|
|
|
public float Z { get; set; } = z;
|
2024-11-05 20:10:19 +00:00
|
|
|
|
|
2024-11-07 02:26:03 +00:00
|
|
|
|
public const ulong HostSize = sizeof(float) * 3;
|
|
|
|
|
}
|
2024-11-05 20:10:19 +00:00
|
|
|
|
|
|
|
|
|
public class ExampleWorldImpl : IExampleWorld
|
|
|
|
|
{
|
2024-11-07 02:26:03 +00:00
|
|
|
|
internal struct AlignmentHelper<T> where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
public byte Padding;
|
|
|
|
|
public T Target;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int AlignmentOf<T>() where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
return (int)Marshal.OffsetOf<AlignmentHelper<T>>(nameof(AlignmentHelper<T>.Target));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static byte[] GetBytes<T>([DisallowNull] T data) {
|
|
|
|
|
int size = Marshal.SizeOf(data);
|
|
|
|
|
byte[] arr = new byte[size];
|
|
|
|
|
|
|
|
|
|
IntPtr ptr = IntPtr.Zero;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ptr = Marshal.AllocHGlobal(size);
|
|
|
|
|
Marshal.StructureToPtr(data, ptr, true);
|
|
|
|
|
Marshal.Copy(ptr, arr, 0, size);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Marshal.FreeHGlobal(ptr);
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static T? FromBytes<T>(byte[] data) where T : struct
|
|
|
|
|
{
|
|
|
|
|
var ptr = GCHandle.Alloc(data, GCHandleType.Pinned);
|
|
|
|
|
try {
|
|
|
|
|
var res = (T?) Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T));
|
|
|
|
|
return res;
|
|
|
|
|
} finally {
|
|
|
|
|
ptr.Free();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OnInit(IEcs.EcsWorld gameWorld, IEcs.Entity owningEntity)
|
|
|
|
|
{
|
|
|
|
|
var size = new IEcs.WasmTypeId((Vec3.TypeId, 0));
|
|
|
|
|
var info = new IEcs.ComponentInfo("Vec3", (ulong) Marshal.SizeOf<Vec3>(), (ulong) AlignmentOf<Vec3>(), size);
|
|
|
|
|
var pos = new Vec3(7.0f, 30.0f, 18.0f);
|
|
|
|
|
var infos = new List<IEcs.ComponentInfo>
|
|
|
|
|
{
|
|
|
|
|
info
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IEcs.Entity entity = gameWorld.Spawn(GetBytes(pos), infos);
|
|
|
|
|
Console.WriteLine("C#: Spawned {0}", entity.id.id);
|
|
|
|
|
|
|
|
|
|
IEcs.EcsDynamicView res = gameWorld.View(infos);
|
|
|
|
|
(IEcs.Entity, byte[])? row = res.Next();
|
|
|
|
|
|
|
|
|
|
if (row.HasValue) {
|
|
|
|
|
Console.WriteLine("C#: Found position!!!");
|
|
|
|
|
|
|
|
|
|
var en = row.Value.Item1;
|
|
|
|
|
var comp = FromBytes<Vec3>(row.Value.Item2);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("C#: Found component at ({0}, {1}, {2})", comp?.X, comp?.Y, comp?.Z);
|
|
|
|
|
} else {
|
|
|
|
|
Console.WriteLine("C#: Could not find position");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OnUpdate(IEcs.EcsWorld gameWorld, IEcs.Entity owningEntity)
|
2024-11-05 20:10:19 +00:00
|
|
|
|
{
|
2024-11-07 02:26:03 +00:00
|
|
|
|
throw new NotImplementedException();
|
2024-11-05 20:10:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|