KerepWrapper project
This commit is contained in:
99
KerepWrapper/Autoarr/Autoarr.cs
Normal file
99
KerepWrapper/Autoarr/Autoarr.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
|
||||
public class Autoarr<T> : IEnumerable<T>, IDisposable where T : struct
|
||||
{
|
||||
public readonly IntPtr UnmanagedPtr;
|
||||
//if true, destructor frees allocated unmanaged memory
|
||||
public bool AutoDispose;
|
||||
private readonly AutoarrFunctions<T> Funcs;
|
||||
public readonly uint MaxLength;
|
||||
public uint Length { get; private set; }
|
||||
|
||||
public Autoarr(IntPtr ptr, bool autoDispose)
|
||||
{
|
||||
AutoDispose = autoDispose;
|
||||
Funcs = AutoarrFunctions<T>.GetFunctions();
|
||||
UnmanagedPtr = ptr;
|
||||
MaxLength = Funcs.MaxLength(UnmanagedPtr);
|
||||
Length = Funcs.Length(UnmanagedPtr);
|
||||
}
|
||||
|
||||
public Autoarr(ushort blockCount, ushort blockLength, bool autoDispose=true)
|
||||
{
|
||||
AutoDispose = autoDispose;
|
||||
Funcs = AutoarrFunctions<T>.GetFunctions();
|
||||
UnmanagedPtr = Funcs.Create(blockCount, blockLength);
|
||||
MaxLength = Funcs.MaxLength(UnmanagedPtr);
|
||||
Length = Funcs.Length(UnmanagedPtr);
|
||||
}
|
||||
|
||||
|
||||
public T this[uint i]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (i < Length) return Funcs.Get(UnmanagedPtr, i);
|
||||
throw new IndexOutOfRangeException($"index {i} >= Autoarr.Length {Length}");
|
||||
}
|
||||
set
|
||||
{
|
||||
if (i < Length) Funcs.Set(UnmanagedPtr, i, value);
|
||||
else throw new IndexOutOfRangeException($"index {i} >= Autoarr.Length {Length}");
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if(AutoDispose)
|
||||
Funcs.Free(UnmanagedPtr);
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new AutoarrEnumerator(this);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public void Add(T value)
|
||||
{
|
||||
if (Length < MaxLength)
|
||||
{
|
||||
Funcs.Add(UnmanagedPtr, value);
|
||||
Length++;
|
||||
}
|
||||
else throw new IndexOutOfRangeException($"Autoarr.Length == MaxLength ({MaxLength})");
|
||||
}
|
||||
|
||||
~Autoarr()
|
||||
{
|
||||
if (AutoDispose) Dispose();
|
||||
}
|
||||
|
||||
private class AutoarrEnumerator : IEnumerator<T>
|
||||
{
|
||||
private readonly Autoarr<T> arr;
|
||||
private uint index;
|
||||
|
||||
public AutoarrEnumerator(Autoarr<T> ar) => arr = ar;
|
||||
|
||||
public T Current { get; private set; }
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (index >= arr.Length) return false;
|
||||
Current = arr[index];
|
||||
index++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
38
KerepWrapper/Autoarr/AutoarrFunctions.cs
Normal file
38
KerepWrapper/Autoarr/AutoarrFunctions.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using DTLib;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
using AutoarrPtr=DtsodPtr;
|
||||
|
||||
public abstract class AutoarrFunctions<T>
|
||||
{
|
||||
static AutoarrFunctions()
|
||||
{
|
||||
DependencyResolver.CopyLibs();
|
||||
}
|
||||
|
||||
internal abstract AutoarrPtr Create(ushort maxBlocksCount, ushort maxBlockLength);
|
||||
internal abstract void Free(AutoarrPtr ar);
|
||||
internal abstract T Get(AutoarrPtr ar, uint index);
|
||||
internal abstract void Add(AutoarrPtr ar, T element);
|
||||
internal abstract void Set(AutoarrPtr ar, uint index, T element);
|
||||
internal abstract uint Length(AutoarrPtr ar);
|
||||
internal abstract uint MaxLength(AutoarrPtr ar);
|
||||
|
||||
private static AutoarrFunctions<Unitype> f_uni = new AutoarrUnitypeFunctions();
|
||||
private static AutoarrFunctions<KVPair> f_kvp = new AutoarrKVPairFunctions();
|
||||
static internal AutoarrFunctions<T> GetFunctions()
|
||||
{
|
||||
if (f_kvp is AutoarrFunctions<T> f)
|
||||
return f;
|
||||
else if (f_uni is AutoarrFunctions<T> ff)
|
||||
return ff;
|
||||
else throw new Exception($"unsupported type: {typeof(T)}");
|
||||
/*if (typeof(T) == typeof(Unitype))
|
||||
return (AutoarrFunctions<T>)Convert.ChangeType(f_uni, typeof(AutoarrFunctions<T>));
|
||||
else if (typeof(T) == typeof(KVPair))
|
||||
return (AutoarrFunctions<T>) Convert.ChangeType(f_kvp, typeof(AutoarrFunctions<T>));
|
||||
else throw new Exception($"unsupported type: {typeof(T)}");*/
|
||||
}
|
||||
}
|
||||
52
KerepWrapper/Autoarr/AutoarrKVPairFunctions.cs
Normal file
52
KerepWrapper/Autoarr/AutoarrKVPairFunctions.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
|
||||
internal class AutoarrKVPairFunctions : AutoarrFunctions<KVPair>
|
||||
{
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_create(ushort max_blocks_count, ushort max_block_length, out AutoarrKVPairPtr output);
|
||||
internal override AutoarrKVPairPtr Create(ushort maxBlocksCount, ushort maxBlockLength)
|
||||
{
|
||||
kerep_Autoarr_KVPair_create(maxBlocksCount, maxBlockLength, out var ar);
|
||||
return ar;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_free(AutoarrKVPairPtr ar);
|
||||
internal override void Free(AutoarrKVPairPtr ar) => kerep_Autoarr_KVPair_free(ar);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_get(AutoarrKVPairPtr ar, uint index, out KVPair output);
|
||||
internal override KVPair Get(AutoarrKVPairPtr ar, uint index)
|
||||
{
|
||||
kerep_Autoarr_KVPair_get(ar, index, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep",CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_add(AutoarrKVPairPtr ar, KVPair element);
|
||||
internal override void Add(AutoarrKVPairPtr ar, KVPair element) => kerep_Autoarr_KVPair_add(ar, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_set(AutoarrKVPairPtr ar, uint index, KVPair element);
|
||||
internal override void Set(AutoarrKVPairPtr ar, uint index, KVPair element) =>
|
||||
kerep_Autoarr_KVPair_set(ar, index, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_length(AutoarrKVPairPtr ar, out uint output);
|
||||
internal override uint Length(AutoarrKVPairPtr ar)
|
||||
{
|
||||
kerep_Autoarr_KVPair_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_max_length(AutoarrKVPairPtr ar, out uint output);
|
||||
internal override uint MaxLength(AutoarrKVPairPtr ar)
|
||||
{
|
||||
kerep_Autoarr_KVPair_max_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
52
KerepWrapper/Autoarr/AutoarrUnitypeFunctions.cs
Normal file
52
KerepWrapper/Autoarr/AutoarrUnitypeFunctions.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
|
||||
internal class AutoarrUnitypeFunctions : AutoarrFunctions<Unitype>
|
||||
{
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_create(ushort max_blocks_count, ushort max_block_length, out AutoarrUnitypePtr output);
|
||||
internal override AutoarrUnitypePtr Create(ushort maxBlocksCount, ushort maxBlockLength)
|
||||
{
|
||||
kerep_Autoarr_Unitype_create(maxBlocksCount, maxBlockLength, out var ar);
|
||||
return ar;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_free(AutoarrUnitypePtr ar);
|
||||
internal override void Free(AutoarrUnitypePtr ar) => kerep_Autoarr_Unitype_free(ar);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_get(AutoarrUnitypePtr ar, uint index, out Unitype output);
|
||||
internal override Unitype Get(AutoarrUnitypePtr ar, uint index)
|
||||
{
|
||||
kerep_Autoarr_Unitype_get(ar, index, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep",CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_add(AutoarrUnitypePtr ar, Unitype element);
|
||||
internal override void Add(AutoarrUnitypePtr ar, Unitype element) => kerep_Autoarr_Unitype_add(ar, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_set(AutoarrUnitypePtr ar, uint index, Unitype element);
|
||||
internal override void Set(AutoarrUnitypePtr ar, uint index, Unitype element) =>
|
||||
kerep_Autoarr_Unitype_set(ar, index, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_length(AutoarrUnitypePtr ar, out uint output);
|
||||
internal override uint Length(AutoarrUnitypePtr ar)
|
||||
{
|
||||
kerep_Autoarr_Unitype_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_max_length(AutoarrUnitypePtr ar, out uint output);
|
||||
internal override uint MaxLength(AutoarrUnitypePtr ar)
|
||||
{
|
||||
kerep_Autoarr_Unitype_max_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
114
KerepWrapper/Dtsod/DtsodV24.cs
Normal file
114
KerepWrapper/Dtsod/DtsodV24.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using DTLib.Dtsod;
|
||||
using KerepWrapper.KerepTypes;
|
||||
using KerepWrapper.Autoarr;
|
||||
using Funcs=KerepWrapper.Dtsod.DtsodV24Functions;
|
||||
|
||||
namespace KerepWrapper.Dtsod;
|
||||
|
||||
public class DtsodV24 : IDtsod, IEnumerable<KVPair>, IDisposable
|
||||
{
|
||||
public DtsodVersion Version => DtsodVersion.V24;
|
||||
public readonly DtsodPtr UnmanagedPtr;
|
||||
//if true, destructor frees allocated unmanaged memory
|
||||
public bool AutoDispose = true;
|
||||
public ushort Height => Funcs.Height(UnmanagedPtr);
|
||||
|
||||
public DtsodV24(DtsodPtr ptr) => UnmanagedPtr = ptr;
|
||||
public DtsodV24(string text, bool autoDispose = true) : this(Funcs.Deserialize(text))
|
||||
=> AutoDispose = autoDispose;
|
||||
public DtsodV24(bool autoDispose=true) : this(" ", autoDispose) { }
|
||||
|
||||
public DtsodV24(IDictionary<string,dynamic> dict, bool autoDispose=true) : this(autoDispose)
|
||||
{
|
||||
foreach (KeyValuePair<string, dynamic> pair in dict)
|
||||
{
|
||||
if (pair.Value is not null) AddOrSet(pair.Key, pair.Value);
|
||||
//else Log("y", $"skiping key <{pair.Key}> with null value");
|
||||
}
|
||||
}
|
||||
|
||||
public IDictionary<string, dynamic> ToDictionary()
|
||||
{
|
||||
DtsodDict<string, dynamic> dict = new();
|
||||
foreach (var p in this)
|
||||
dict.Add(p.key,p.value.ToDynamic());
|
||||
return dict;
|
||||
}
|
||||
|
||||
|
||||
public bool TryGet(string key, out dynamic elem)
|
||||
{
|
||||
var g = Funcs.Get(UnmanagedPtr, key);
|
||||
elem = g.ToDynamic();
|
||||
return g.TypeCode == KerepTypeCode.Null;
|
||||
}
|
||||
|
||||
public void AddOrSet(string key, dynamic value) =>
|
||||
Funcs.AddOrSet(UnmanagedPtr, key, new Unitype(value));
|
||||
|
||||
public dynamic this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!TryGet(key, out var v)) throw new KeyNotFoundException($"key <{key}> not found");
|
||||
return v;
|
||||
}
|
||||
set => AddOrSet(key, value);
|
||||
}
|
||||
|
||||
public bool TryRemove(string key) => Funcs.Remove(UnmanagedPtr,key);
|
||||
|
||||
[Obsolete("do you really need to use this? look at TryGet/TryRemove/AddOrSet")]
|
||||
public bool ContainsKey(string key) => Funcs.Contains(UnmanagedPtr, key);
|
||||
|
||||
public override string ToString() => Funcs.Serialize(UnmanagedPtr);
|
||||
|
||||
public void Dispose() => Funcs.Free(UnmanagedPtr);
|
||||
|
||||
~DtsodV24()
|
||||
{
|
||||
if(AutoDispose) Dispose();
|
||||
}
|
||||
|
||||
|
||||
public IEnumerator<KVPair> GetEnumerator() => new DtsodV24Enumerator(this);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
class DtsodV24Enumerator: IEnumerator<KVPair>
|
||||
{
|
||||
private readonly DtsodV24 d;
|
||||
private ushort h;
|
||||
private IEnumerator<KVPair> arEnumerator;
|
||||
|
||||
public DtsodV24Enumerator(DtsodV24 _d) => d = _d;
|
||||
|
||||
bool NextAr()
|
||||
{
|
||||
if (h >= Funcs.Height(d.UnmanagedPtr)) return false;
|
||||
var ar = new Autoarr<KVPair>(Funcs.GetRow(d.UnmanagedPtr, h), false);
|
||||
arEnumerator = ar.GetEnumerator();
|
||||
h++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if(arEnumerator==null)
|
||||
NextAr();
|
||||
while(!arEnumerator.MoveNext())
|
||||
if(!NextAr()) return false;
|
||||
Current = arEnumerator.Current;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset() => h = 0;
|
||||
public KVPair Current { get; private set; }
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
110
KerepWrapper/Dtsod/DtsodV24Functions.cs
Normal file
110
KerepWrapper/Dtsod/DtsodV24Functions.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
global using DtsodPtr=System.IntPtr;
|
||||
global using AutoarrKVPairPtr=System.IntPtr;
|
||||
global using AutoarrUnitypePtr=System.IntPtr;
|
||||
global using CharPtr=System.IntPtr;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib;
|
||||
using DTLib.Extensions;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Dtsod;
|
||||
|
||||
internal static class DtsodV24Functions
|
||||
{
|
||||
private const string kereplib = "kerep";
|
||||
|
||||
static DtsodV24Functions()
|
||||
{
|
||||
DependencyResolver.CopyLibs();
|
||||
}
|
||||
|
||||
static void TryThrowErrmsg(CharPtr err)
|
||||
{
|
||||
if (err == IntPtr.Zero) return;
|
||||
string errmsg = Unmanaged.HGlobalUTF8ToString(err);
|
||||
Marshal.FreeHGlobal(err);
|
||||
throw new Exception(errmsg);
|
||||
}
|
||||
|
||||
|
||||
//parses text to binary values
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_deserialize(string text, out DtsodPtr output, out CharPtr errmsg);
|
||||
internal static DtsodPtr Deserialize(string text)
|
||||
{
|
||||
kerep_DtsodV24_deserialize(text, out var dtsod,out var err);
|
||||
TryThrowErrmsg(err);
|
||||
return dtsod;
|
||||
}
|
||||
|
||||
//creates text representation of dtsod
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_serialize(DtsodPtr dtsod, out CharPtr output, out CharPtr errmsg);
|
||||
internal static string Serialize(DtsodPtr dtsod)
|
||||
{
|
||||
kerep_DtsodV24_serialize(dtsod, out var text, out var err);
|
||||
TryThrowErrmsg(err);
|
||||
return Unmanaged.HGlobalUTF8ToString(text);
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_get(DtsodPtr dtsod, string key, out Unitype output);
|
||||
//returns value or UniNull if key not found
|
||||
internal static Unitype Get(DtsodPtr dtsod, string key)
|
||||
{
|
||||
kerep_DtsodV24_get(dtsod, key, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport(kereplib,EntryPoint = "kerep_DtsodV24_addOrSet",CallingConvention = CallingConvention.Cdecl)]
|
||||
//adds or sets value
|
||||
static extern void kerep_DtsodV24_addOrSet(DtsodPtr dtsod, IntPtr key, Unitype value);
|
||||
|
||||
internal static void AddOrSet(DtsodPtr dtsod, string key, Unitype value)
|
||||
{
|
||||
IntPtr keyptr = key.StringToHGlobalUTF8();
|
||||
kerep_DtsodV24_addOrSet(dtsod, keyptr, value);
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
//checks for dtsod contains value or dont
|
||||
static extern void kerep_DtsodV24_contains(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output);
|
||||
internal static bool Contains(DtsodPtr dtsod, string key)
|
||||
{
|
||||
kerep_DtsodV24_contains(dtsod, key, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_remove(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output);
|
||||
//replaces value with UniNull if key exists in dtsod
|
||||
internal static bool Remove(DtsodPtr dtsod, string key)
|
||||
{
|
||||
kerep_DtsodV24_remove(dtsod, key, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport(kereplib,EntryPoint="kerep_DtsodV24_free", CallingConvention = CallingConvention.Cdecl)]
|
||||
//replaces value with UniNull if key exists in dtsod
|
||||
internal static extern void Free(DtsodPtr dtsod);
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_height(DtsodPtr dtsod, out ushort heigth);
|
||||
//returns current amounts of rows (Autoarrs of KVPairs) in hashtable
|
||||
internal static ushort Height(DtsodPtr ptr)
|
||||
{
|
||||
kerep_DtsodV24_height(ptr, out var h);
|
||||
return h;
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_getrow(DtsodPtr dtsod, ushort h, out AutoarrKVPairPtr row);
|
||||
//Returns row from hashtable.
|
||||
//check current hashtable height before calling this.
|
||||
internal static AutoarrKVPairPtr GetRow(DtsodPtr ptr, ushort height)
|
||||
{
|
||||
kerep_DtsodV24_getrow(ptr, height, out var rowptr);
|
||||
return rowptr;
|
||||
}
|
||||
}
|
||||
27
KerepWrapper/KerepTypes/KVPair.cs
Normal file
27
KerepWrapper/KerepTypes/KVPair.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib.Extensions;
|
||||
|
||||
namespace KerepWrapper.KerepTypes;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct KVPair
|
||||
{
|
||||
public DtsodPtr key;
|
||||
public Unitype value;
|
||||
|
||||
public KVPair(DtsodPtr k, Unitype v)
|
||||
{
|
||||
key = k;
|
||||
value = v;
|
||||
}
|
||||
public KVPair(string k, Unitype v)
|
||||
{
|
||||
key = k.StringToHGlobalUTF8();
|
||||
value = v;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{{{Unmanaged.HGlobalUTF8ToString(key)}, {value}}}";
|
||||
}
|
||||
}
|
||||
49
KerepWrapper/KerepTypes/KerepTypeCode.cs
Normal file
49
KerepWrapper/KerepTypes/KerepTypeCode.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace KerepWrapper.KerepTypes;
|
||||
|
||||
public enum KerepTypeCode : byte
|
||||
{
|
||||
Null, Float32, Float64, Char, Bool,
|
||||
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
|
||||
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
|
||||
CharPtr, STNodePtr, HashtablePtr,
|
||||
UniversalType,
|
||||
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
|
||||
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
|
||||
AutoarrUnitypePtr, AutoarrKVPairPtr
|
||||
}
|
||||
|
||||
public static class KerepTypeHelper
|
||||
{
|
||||
|
||||
static readonly Dictionary<Type, KerepTypeCode> type_comparsion_dict = new()
|
||||
{
|
||||
{typeof(bool), KerepTypeCode.Bool},
|
||||
{typeof(byte), KerepTypeCode.UInt8},
|
||||
{typeof(ushort), KerepTypeCode.UInt16},
|
||||
{typeof(uint), KerepTypeCode.UInt32},
|
||||
{typeof(ulong), KerepTypeCode.UInt64},
|
||||
{typeof(sbyte), KerepTypeCode.Int8},
|
||||
{typeof(short), KerepTypeCode.Int16},
|
||||
{typeof(int), KerepTypeCode.Int32},
|
||||
{typeof(long), KerepTypeCode.Int64},
|
||||
{typeof(float), KerepTypeCode.Float32},
|
||||
{typeof(double), KerepTypeCode.Float64},
|
||||
{typeof(string), KerepTypeCode.CharPtr}
|
||||
};
|
||||
|
||||
public static KerepTypeCode GetKerepTypeCode(object something)
|
||||
{
|
||||
if (type_comparsion_dict.TryGetValue(something.GetType(), out var ktype))
|
||||
return ktype;
|
||||
|
||||
else return something switch
|
||||
{
|
||||
IList<object> => KerepTypeCode.AutoarrUnitypePtr,
|
||||
IDictionary<string, object> => KerepTypeCode.HashtablePtr,
|
||||
_ => throw new Exception($"can't get KerepTypeCode for type {something.GetType()}")
|
||||
};
|
||||
}
|
||||
}
|
||||
106
KerepWrapper/KerepTypes/Unitype.cs
Normal file
106
KerepWrapper/KerepTypes/Unitype.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib.Extensions;
|
||||
using KerepWrapper.Autoarr;
|
||||
using KerepWrapper.Dtsod;
|
||||
|
||||
namespace KerepWrapper.KerepTypes;
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct Unitype
|
||||
{
|
||||
[FieldOffset(0)] public long Int64;
|
||||
[FieldOffset(0)] public ulong UInt64;
|
||||
[FieldOffset(0)] public double Float64;
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
[FieldOffset(0)] public bool Bool;
|
||||
[FieldOffset(0)] public DtsodPtr VoidPtr;
|
||||
[FieldOffset(8)] public KerepTypeCode TypeCode;
|
||||
|
||||
public Unitype(object v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeHelper.GetKerepTypeCode(v);
|
||||
switch (TypeCode)
|
||||
{
|
||||
case KerepTypeCode.Bool:
|
||||
Bool = (bool) v;
|
||||
break;
|
||||
case KerepTypeCode.UInt8:
|
||||
case KerepTypeCode.UInt16:
|
||||
case KerepTypeCode.UInt32:
|
||||
UInt64 = v.ToULong();
|
||||
TypeCode = KerepTypeCode.UInt64;
|
||||
break;
|
||||
case KerepTypeCode.UInt64:
|
||||
UInt64 = (ulong) v;
|
||||
break;
|
||||
case KerepTypeCode.Int8:
|
||||
case KerepTypeCode.Int16:
|
||||
case KerepTypeCode.Int32:
|
||||
Int64 = v.ToLong();
|
||||
TypeCode = KerepTypeCode.Int64;
|
||||
break;
|
||||
case KerepTypeCode.Int64:
|
||||
Int64 = (long) v;
|
||||
break;
|
||||
case KerepTypeCode.Float32:
|
||||
Float64 = v.ToDouble();
|
||||
TypeCode = KerepTypeCode.Float64;
|
||||
break;
|
||||
case KerepTypeCode.Float64:
|
||||
Float64 = (double) v;
|
||||
break;
|
||||
case KerepTypeCode.CharPtr:
|
||||
VoidPtr = ((string)v).StringToHGlobalUTF8();
|
||||
break;
|
||||
case KerepTypeCode.AutoarrUnitypePtr:
|
||||
TypeCode = KerepTypeCode.AutoarrUnitypePtr;
|
||||
var ar = new Autoarr<Unitype>(64,1024,false);
|
||||
foreach (var sub in (List<object>)v)
|
||||
ar.Add(new Unitype(sub));
|
||||
VoidPtr = ar.UnmanagedPtr;
|
||||
break;
|
||||
case KerepTypeCode.HashtablePtr:
|
||||
TypeCode = KerepTypeCode.HashtablePtr;
|
||||
var ht = new DtsodV24((IDictionary<string,object>)v,false);
|
||||
VoidPtr = ht.UnmanagedPtr;
|
||||
break;
|
||||
default: throw new Exception($"can't box value of type {TypeCode}");
|
||||
}
|
||||
}
|
||||
|
||||
public dynamic ToDynamic()
|
||||
{
|
||||
switch (TypeCode)
|
||||
{
|
||||
case KerepTypeCode.Null: return null;
|
||||
case KerepTypeCode.Bool: return Bool;
|
||||
case KerepTypeCode.Int64: return Int64;
|
||||
case KerepTypeCode.UInt64: return UInt64;
|
||||
case KerepTypeCode.Float64: return Float64;
|
||||
case KerepTypeCode.CharPtr: return VoidPtr.HGlobalUTF8ToString();
|
||||
case KerepTypeCode.AutoarrUnitypePtr: return new Autoarr<Unitype>(VoidPtr, false);
|
||||
case KerepTypeCode.AutoarrKVPairPtr: return new Autoarr<KVPair>(VoidPtr, false);
|
||||
case KerepTypeCode.HashtablePtr: return new DtsodV24(VoidPtr);
|
||||
default: throw new Exception($"can't unbox value of type {TypeCode}");
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
switch (TypeCode)
|
||||
{
|
||||
case KerepTypeCode.Null: return "{Null}";
|
||||
case KerepTypeCode.Bool: return $"{{Bool:{Bool}}}";
|
||||
case KerepTypeCode.Int64: return $"{{Int64:{Int64}}}";
|
||||
case KerepTypeCode.UInt64: return $"{{UInt64:{UInt64}}}";
|
||||
case KerepTypeCode.Float64: return $"{{Float64:{Float64}}}";
|
||||
case KerepTypeCode.CharPtr: return $"{{CharPtr:{Unmanaged.HGlobalUTF8ToString(VoidPtr)}}}";
|
||||
case KerepTypeCode.AutoarrUnitypePtr: return $"{{AutoarrUnitypePtr:{VoidPtr.ToString()}}}";
|
||||
case KerepTypeCode.AutoarrKVPairPtr: return $"{{AutoarrKVPairPtr:{VoidPtr.ToString()}}}";
|
||||
case KerepTypeCode.HashtablePtr: return $"{{HashtablePtr:{VoidPtr.ToString()}}}";
|
||||
default: throw new Exception($"can't unbox value of type {TypeCode}");
|
||||
}
|
||||
}
|
||||
}
|
||||
51
KerepWrapper/KerepWrapper.csproj
Normal file
51
KerepWrapper/KerepWrapper.csproj
Normal file
@@ -0,0 +1,51 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<!--package info-->
|
||||
<PackageId>KerepWrapper</PackageId>
|
||||
<Version>0.2.0</Version>
|
||||
<Authors>Timerix</Authors>
|
||||
<Description>Definitely not json</Description>
|
||||
<RepositoryType>GIT</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/Timerix22/DTLib</RepositoryUrl>
|
||||
<PackageProjectUrl>https://github.com/Timerix22/DTLib</PackageProjectUrl>
|
||||
<Configuration>Release</Configuration>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<!--compilation properties-->
|
||||
<TargetFrameworks>net6.0;netstandard2.0;net48</TargetFrameworks>
|
||||
<DebugType>embedded</DebugType>
|
||||
<!--language features-->
|
||||
<LangVersion>10</LangVersion>
|
||||
<Nullable>disable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<!--external dependencies-->
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
|
||||
<Reference Include="System.Dynamic" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--DTLib dependencies-->
|
||||
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<ProjectReference Include="..\DTLib\DTLib.csproj" />
|
||||
<ProjectReference Include="..\DTLib.Dtsod\DTLib.Dtsod.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
||||
<PackageReference Include="DTLib.Dtsod" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--project files-->
|
||||
<ItemGroup>
|
||||
<Content Include="runtimes\**\*">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<Pack>true</Pack>
|
||||
<PackagePath>runtimes/</PackagePath>
|
||||
</Content>
|
||||
<Compile Remove="Experimental/**/*" />
|
||||
<None Include="Experimental/**/*" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
BIN
KerepWrapper/runtimes/win-x64/native/kerep.dll
Normal file
BIN
KerepWrapper/runtimes/win-x64/native/kerep.dll
Normal file
Binary file not shown.
Reference in New Issue
Block a user