From 55b38f1678c9df281b8a913f3f424b5d54fd94a7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 26 Apr 2021 03:12:22 +0200 Subject: [PATCH] Start working on FlyCam and create an overcomplicated struct Just some native code that's totally unnecessary --- GamecraftModdingAPI/FlyCam.cs | 26 ++++++ GamecraftModdingAPI/OptionalRef.cs | 99 +++++++++++++++++++++ GamecraftModdingAPI/Players/FlyCamEngine.cs | 28 ++++++ 3 files changed, 153 insertions(+) create mode 100644 GamecraftModdingAPI/FlyCam.cs create mode 100644 GamecraftModdingAPI/OptionalRef.cs create mode 100644 GamecraftModdingAPI/Players/FlyCamEngine.cs diff --git a/GamecraftModdingAPI/FlyCam.cs b/GamecraftModdingAPI/FlyCam.cs new file mode 100644 index 0000000..15a6aa5 --- /dev/null +++ b/GamecraftModdingAPI/FlyCam.cs @@ -0,0 +1,26 @@ +using GamecraftModdingAPI.Players; +using Svelto.ECS.EntityStructs; +using Unity.Mathematics; + +namespace GamecraftModdingAPI +{ + public class FlyCam + { + private static FlyCamEngine Engine; + + public uint Id { get; } + + public FlyCam(uint id) => Id = id; + + public unsafe float3 Position + { + get => Engine.GetComponent(Id).Map(pos => &pos->position); + set => Engine.GetComponent(Id).Map(pos => &pos->position).Set(value); + } + + public static void Init() + { + Engine = new FlyCamEngine(); + } + } +} \ No newline at end of file diff --git a/GamecraftModdingAPI/OptionalRef.cs b/GamecraftModdingAPI/OptionalRef.cs new file mode 100644 index 0000000..a1b09cd --- /dev/null +++ b/GamecraftModdingAPI/OptionalRef.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using GamecraftModdingAPI.Blocks; +using Svelto.DataStructures; +using Svelto.ECS; + +namespace GamecraftModdingAPI +{ + public struct OptionalRef where T : unmanaged + { + private bool exists; + private NB array; + private uint index; + + private unsafe T* pointer; + + public OptionalRef(NB array, uint index) + { + exists = true; + this.array = array; + this.index = index; + unsafe + { + pointer = null; + } + } + + public OptionalRef(ref T value) + { + unsafe + { + fixed(T* p = &value) pointer = p; + } + + exists = true; + array = default; + index = default; + } + + public ref T Get() + { + unsafe + { + if (pointer != null && exists) + return ref *pointer; + } + + if (exists) + return ref array[index]; + throw new InvalidOperationException("Calling Get() on an empty OptionalRef"); + } + + public T Value + { + get + { + unsafe + { + if (pointer != null && exists) + return *pointer; + } + + if (exists) + return array[index]; + return default; + } + } + + public unsafe void Set(T value) //Can't use properties because it complains that you can't set struct members + { + if (pointer != null && exists) + *pointer = value; + } + + public bool Exists => exists; + + public static implicit operator T(OptionalRef opt) => opt.Value; + + public static implicit operator bool(OptionalRef opt) => opt.exists; + + public delegate ref TR Mapper(ref T component) where TR : unmanaged; + public unsafe delegate TR* PMapper(T* component) where TR : unmanaged; + + /*public OptionalRef Map(Mapper mapper) where TR : unmanaged => + exists ? new OptionalRef(ref mapper(ref Get())) : new OptionalRef();*/ + + /*public OptionalRef Map(Expression> expression) where TR : unmanaged + { + if (expression.Body.NodeType == ExpressionType.MemberAccess) + Console.WriteLine(((MemberExpression) expression.Body).Member); + }*/ + + public unsafe OptionalRef Map(PMapper mapper) where TR : unmanaged => + exists ? new OptionalRef(ref *mapper(pointer)) : new OptionalRef(); + } +} \ No newline at end of file diff --git a/GamecraftModdingAPI/Players/FlyCamEngine.cs b/GamecraftModdingAPI/Players/FlyCamEngine.cs new file mode 100644 index 0000000..b8f8cf4 --- /dev/null +++ b/GamecraftModdingAPI/Players/FlyCamEngine.cs @@ -0,0 +1,28 @@ +using GamecraftModdingAPI.Engines; +using Svelto.ECS; +using Techblox.FlyCam; + +namespace GamecraftModdingAPI.Players +{ + public class FlyCamEngine : IApiEngine + { + public void Ready() + { + } + + public EntitiesDB entitiesDB { get; set; } + public void Dispose() + { + } + + public string Name => "TechbloxModdingAPIFlyCamEngine"; + public bool isRemovable => false; + + public OptionalRef GetComponent(uint id) where T : unmanaged, IEntityComponent + { + if (entitiesDB.TryQueryEntitiesAndIndex(id, Techblox.FlyCam.FlyCam.Group, out uint index, out var array)) + return new OptionalRef(array, index); + return new OptionalRef(); + } + } +} \ No newline at end of file