2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 03:32:57 +00:00
This commit is contained in:
ahmed 2025-02-28 15:23:43 +03:00
parent 87c27df7eb
commit 47272f5463
3 changed files with 107 additions and 6 deletions

71
Esiur/Data/UUID.cs Normal file
View File

@ -0,0 +1,71 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace Esiur.Data
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct UUID
{
//4e7db2d8-a785-1b99-1854-4b4018bc5677
byte a1;
byte a2;
byte a3;
byte a4;
byte b1;
byte b2;
byte c1;
byte c2;
byte d1;
byte d2;
byte e1;
byte e2;
byte e3;
byte e4;
byte e5;
byte e6;
public UUID(byte[] data)
{
if (data.Length < 16)
throw new Exception("UUID data size must be at least 16 bytes");
for(var i = 0; i < 16; i++)
Data[i] = data[i];
}
public override string ToString()
{
return $"{a1.ToString("x2")}{a2.ToString("x2")}{a3.ToString("x2")}{a4.ToString("x2")}-{b1.ToString("x2")}{b2.ToString("x2")}-{c1.ToString("x2")}{c2.ToString("x2")}-{d1.ToString("x2")}{d2.ToString("x2")}-{e1.ToString("x2")}{e2.ToString("x2")}{e3.ToString("x2")}{e4.ToString("x2")}{e5.ToString("x2")}{e6.ToString("x2")}";
}
public static bool operator == (UUID a, UUID b)
{
return a.a1 == b.a1
&& a.a2 == b.a2
&& a.a3 == b.a3
&& a.a4 == b.a4
&& a.b1 == b.b1
&& a.b2 == b.b2
&& a.c1 == b.c1
&& a.c2 == b.c2
&& a.d1 == b.d1
&& a.d2 == b.d2
&& a.e1 == b.e1
&& a.e2 == b.e2
&& a.e3 == b.e3
&& a.e4 == b.e4
&& a.e5 == b.e5
&& a.e6 == b.e6;
}
public static bool operator !=(UUID a, UUID b)
{
return !(a == b);
}
}
}

View File

@ -167,6 +167,9 @@ public class TypeTemplate
var tn = Encoding.UTF8.GetBytes(GetTypeClassName(type)); var tn = Encoding.UTF8.GetBytes(GetTypeClassName(type));
var hash = SHA256.Create().ComputeHash(tn).Clip(0, 16); var hash = SHA256.Create().ComputeHash(tn).Clip(0, 16);
hash[6] = (byte)((hash[6] & 0xF) | 0x80);
hash[8] = (byte)((hash[8] & 0xF) | 0x80);
var rt = new Guid(hash); var rt = new Guid(hash);
return rt; return rt;
} }

View File

@ -55,18 +55,45 @@ namespace Test
class Program class Program
{ {
static async Task Main(string[] args)
static void TestSerialization(object x)
{ {
var x = new uint[] {1,2};// new byte[1024];
// var rr = DC.ToHex(Codec.Compose(aa, null));
var y = Codec.Compose(x, null); var y = Codec.Compose(x, null);
var rr = DC.ToHex(y); var rr = DC.ToHex(y);
Console.WriteLine(rr); Console.WriteLine(x.GetType().Name + ": " + rr);
}
[Export]
public class StudentRecord:IRecord
{
public string Name { get; set; }
public byte Grade { get; set; }
}
static async Task Main(string[] args)
{
TestSerialization(new Map<string, byte?>
{
["C++"] = 1,
["C#"] = 2,
["JS"] = null
});
TestSerialization(new StudentRecord() { Name = "Ali", Grade = 90});
var tn = Encoding.UTF8.GetBytes("MyProject.Tools.Logging.LogRecord");
var hash = System.Security.Cryptography.SHA256.Create().ComputeHash(tn).Clip(0, 16);
hash[6] = (byte)((hash[6] & 0xF) | 0x80);
hash[8] = (byte)((hash[8] & 0xF) | 0x80);
var g = new Guid(hash);
Console.WriteLine(g);
var rp = RepresentationType.FromType(x.GetType());
var hhhh = Warehouse.GetTemplateByType(typeof(IMyRecord)); var hhhh = Warehouse.GetTemplateByType(typeof(IMyRecord));