Port NBT impl from LibAC Dart
This commit is contained in:
parent
0a022634c1
commit
ad7b619706
55 changed files with 3226 additions and 2983 deletions
146
Utilities/UUID.cs
Normal file
146
Utilities/UUID.cs
Normal file
|
@ -0,0 +1,146 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.Linq;
|
||||
|
||||
namespace LibAC.Utilities
|
||||
{
|
||||
public class UUID
|
||||
{
|
||||
private readonly byte[] _bytes;
|
||||
|
||||
public static readonly UUID ZERO = UUID.Generate(0);
|
||||
|
||||
public UUID(byte[] bytes)
|
||||
{
|
||||
_bytes = bytes;
|
||||
}
|
||||
|
||||
public static bool Validate(string uuid)
|
||||
{
|
||||
return uuid.Length == (16 * 2) + 4; // Basic length check
|
||||
}
|
||||
|
||||
public static UUID Parse(string uuid)
|
||||
{
|
||||
if (Validate(uuid))
|
||||
{
|
||||
string[] segments = uuid.Split('-');
|
||||
if (segments.Length != 5)
|
||||
{
|
||||
throw new FormatException("Invalid UUID format");
|
||||
}
|
||||
|
||||
string msbString = string.Join("", segments.Take(3));
|
||||
string lsbString = string.Join("", segments.Skip(3));
|
||||
|
||||
byte[] byteArray = new byte[16];
|
||||
int i = 0;
|
||||
for (i = 0; i < msbString.Length; i += 2)
|
||||
{
|
||||
byteArray[i / 2] = Convert.ToByte(msbString.Substring(i, 2), 16);
|
||||
}
|
||||
|
||||
for (i = 0; i < lsbString.Length; i += 2)
|
||||
{
|
||||
byteArray[msbString.Length / 2 + i / 2] = Convert.ToByte(lsbString.Substring(i, 2), 16);
|
||||
}
|
||||
|
||||
return new UUID(byteArray);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder hexBuilder = new StringBuilder();
|
||||
foreach (byte b in _bytes)
|
||||
{
|
||||
hexBuilder.Append(b.ToString("x2"));
|
||||
}
|
||||
|
||||
return $"{hexBuilder.ToString().Substring(0, 8)}-{hexBuilder.ToString().Substring(8, 4)}-{hexBuilder.ToString().Substring(12, 4)}-{hexBuilder.ToString().Substring(16, 4)}-{hexBuilder.ToString().Substring(20, 12)}";
|
||||
}
|
||||
|
||||
public static UUID Generate(int version, params object[] parameters)
|
||||
{
|
||||
if (version != 4 && version != 0 && version != 3 && version != 5)
|
||||
{
|
||||
return Generate(4);
|
||||
}
|
||||
|
||||
byte[] bytes0 = new byte[16];
|
||||
|
||||
|
||||
switch (version)
|
||||
{
|
||||
case 0:
|
||||
return new UUID(bytes0);
|
||||
case 3:
|
||||
if (parameters.Length != 2)
|
||||
{
|
||||
throw new ArgumentException("UUID v3 requires two parameters: [namespace, name]");
|
||||
}
|
||||
|
||||
string namespaceStr = parameters[0] as string;
|
||||
string nameStr = parameters[1] as string;
|
||||
|
||||
using (MD5 md5 = MD5.Create())
|
||||
{
|
||||
byte[] namespaceBytes = Encoding.UTF8.GetBytes(namespaceStr);
|
||||
byte[] nameBytes = Encoding.UTF8.GetBytes(nameStr);
|
||||
byte[] combined = namespaceBytes.Concat(nameBytes).ToArray();
|
||||
|
||||
byte[] hash = md5.ComputeHash(combined);
|
||||
|
||||
Array.Copy(hash, 0, bytes0, 0, 16);
|
||||
bytes0[6] = (byte)(bytes0[6] & 0x0F | 0x30); // Set version to 3
|
||||
bytes0[8] = (byte)(bytes0[8] & 0x3F | 0x80); // Set variant to RFC4122
|
||||
return new UUID(bytes0);
|
||||
}
|
||||
case 4:
|
||||
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
|
||||
{
|
||||
byte[] bytes4 = new byte[16];
|
||||
rng.GetBytes(bytes4);
|
||||
bytes4[6] = (byte)(bytes4[6] & 0x0F | 0x40); // Set version to 4
|
||||
bytes4[8] = (byte)(bytes4[8] & 0x3F | 0x80); // Set variant to RFC4122
|
||||
return new UUID(bytes4);
|
||||
}
|
||||
case 5:
|
||||
if (parameters.Length != 2)
|
||||
{
|
||||
throw new ArgumentException("UUID v5 requires two parameters: [namespace, name]");
|
||||
}
|
||||
|
||||
string namespaceStr5 = parameters[0] as string;
|
||||
string nameStr5 = parameters[1] as string;
|
||||
|
||||
using (SHA1 sha1 = SHA1.Create())
|
||||
{
|
||||
byte[] namespaceBytes5 = Encoding.UTF8.GetBytes(namespaceStr5);
|
||||
byte[] nameBytes5 = Encoding.UTF8.GetBytes(nameStr5);
|
||||
byte[] combined5 = namespaceBytes5.Concat(nameBytes5).ToArray();
|
||||
|
||||
byte[] hash = sha1.ComputeHash(combined5);
|
||||
|
||||
byte[] bytes5 = new byte[16];
|
||||
Array.Copy(hash, 0, bytes5, 0, 16);
|
||||
bytes5[6] = (byte)(bytes5[6] & 0x0F | 0x50); // Set version to 5
|
||||
bytes5[8] = (byte)(bytes5[8] & 0x3F | 0x80); // Set variant to RFC4122
|
||||
return new UUID(bytes5);
|
||||
}
|
||||
default:
|
||||
throw new ArgumentException($"Unsupported UUID version: {version}");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] GetBytes()
|
||||
{
|
||||
return _bytes.Take(16).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
90
Utilities/Vector2d.cs
Normal file
90
Utilities/Vector2d.cs
Normal file
|
@ -0,0 +1,90 @@
|
|||
using System;
|
||||
|
||||
namespace LibAC.Utilities
|
||||
{
|
||||
public class Vector2d
|
||||
{
|
||||
public static readonly Vector2d ZERO = new Vector2d(0, 0);
|
||||
public double X { get; set; }
|
||||
public double Z { get; set; }
|
||||
|
||||
public Vector2d(double X = 0.0, double Z = 0.0)
|
||||
{
|
||||
this.X = X;
|
||||
this.Z = Z;
|
||||
}
|
||||
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (other is Vector2d)
|
||||
{
|
||||
Vector2d otherVector = (Vector2d)other;
|
||||
return X == otherVector.X && Z == otherVector.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Vector2d operator +(Vector2d v1, Vector2d v2)
|
||||
{
|
||||
Vector2d result = v1.Clone();
|
||||
result.X += v2.X;
|
||||
result.Z += v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector2d operator -(Vector2d v1, Vector2d v2)
|
||||
{
|
||||
Vector2d result = v1.Clone();
|
||||
result.X -= v2.X;
|
||||
result.Z -= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector2d operator *(Vector2d v1, Vector2d v2)
|
||||
{
|
||||
Vector2d result = v1.Clone();
|
||||
result.X *= v2.X;
|
||||
result.Z *= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector2d operator /(Vector2d v1, Vector2d v2)
|
||||
{
|
||||
Vector2d result = v1.Clone();
|
||||
result.X /= v2.X;
|
||||
result.Z /= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool operator >(Vector2d v1, Vector2d v2)
|
||||
{
|
||||
return (v1.X > v2.X) || (v1.X > v2.Z);
|
||||
}
|
||||
|
||||
public static bool operator <(Vector2d v1, Vector2d v2)
|
||||
{
|
||||
return (v1.X < v2.X) || (v1.X < v2.Z);
|
||||
}
|
||||
|
||||
public Vector2d Clone()
|
||||
{
|
||||
return new Vector2d(X, Z);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"<{X}, {Z}>";
|
||||
}
|
||||
|
||||
public bool Inside(Vector2d min, Vector2d max)
|
||||
{
|
||||
return (min.X <= X && max.X >= X) && (min.Z <= Z && max.Z >= Z);
|
||||
}
|
||||
|
||||
// Override GetHashCode if you override Equals
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(X, Z);
|
||||
}
|
||||
}
|
||||
}
|
90
Utilities/Vector2i.cs
Normal file
90
Utilities/Vector2i.cs
Normal file
|
@ -0,0 +1,90 @@
|
|||
using System;
|
||||
|
||||
namespace LibAC.Utilities
|
||||
{
|
||||
public class Vector2i
|
||||
{
|
||||
public static readonly Vector2i ZERO = new Vector2i(0, 0);
|
||||
public int X { get; set; }
|
||||
public int Z { get; set; }
|
||||
|
||||
public Vector2i(int X = 0, int Z = 0)
|
||||
{
|
||||
this.X = X;
|
||||
this.Z = Z;
|
||||
}
|
||||
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (other is Vector2i)
|
||||
{
|
||||
Vector2i otherVector = (Vector2i)other;
|
||||
return X == otherVector.X && Z == otherVector.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Vector2i operator +(Vector2i v1, Vector2i v2)
|
||||
{
|
||||
Vector2i result = v1.Clone();
|
||||
result.X += v2.X;
|
||||
result.Z += v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector2i operator -(Vector2i v1, Vector2i v2)
|
||||
{
|
||||
Vector2i result = v1.Clone();
|
||||
result.X -= v2.X;
|
||||
result.Z -= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector2i operator *(Vector2i v1, Vector2i v2)
|
||||
{
|
||||
Vector2i result = v1.Clone();
|
||||
result.X *= v2.X;
|
||||
result.Z *= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector2i operator /(Vector2i v1, Vector2i v2)
|
||||
{
|
||||
Vector2i result = v1.Clone();
|
||||
result.X = (int)Math.Round((double)v1.X / v2.X);
|
||||
result.Z = (int)Math.Round((double)v1.Z / v2.Z);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool operator >(Vector2i v1, Vector2i v2)
|
||||
{
|
||||
return (v1.X > v2.X) || (v1.X > v2.Z);
|
||||
}
|
||||
|
||||
public static bool operator <(Vector2i v1, Vector2i v2)
|
||||
{
|
||||
return (v1.X < v2.X) || (v1.X < v2.Z);
|
||||
}
|
||||
|
||||
public Vector2i Clone()
|
||||
{
|
||||
return new Vector2i(X, Z);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"<{X}, {Z}>";
|
||||
}
|
||||
|
||||
public bool Inside(Vector2i min, Vector2i max)
|
||||
{
|
||||
return (min.X <= X && max.X >= X) && (min.Z <= Z && max.Z >= Z);
|
||||
}
|
||||
|
||||
// Override GetHashCode if you override Equals
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(X, Z);
|
||||
}
|
||||
}
|
||||
}
|
96
Utilities/Vector3d.cs
Normal file
96
Utilities/Vector3d.cs
Normal file
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
|
||||
namespace LibAC.Utilities
|
||||
{
|
||||
public class Vector3d
|
||||
{
|
||||
public static readonly Vector3d ZERO = new Vector3d(0, 0, 0);
|
||||
public double X { get; set; }
|
||||
public double Y { get; set; }
|
||||
public double Z { get; set; }
|
||||
|
||||
public Vector3d(double X = 0.0, double Y = 0.0, double Z = 0.0)
|
||||
{
|
||||
this.X = X;
|
||||
this.Y = Y;
|
||||
this.Z = Z;
|
||||
}
|
||||
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (other is Vector3d)
|
||||
{
|
||||
Vector3d otherVector = (Vector3d)other;
|
||||
return X == otherVector.X && Y == otherVector.Y && Z == otherVector.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Vector3d operator +(Vector3d v1, Vector3d v2)
|
||||
{
|
||||
Vector3d result = v1.Clone();
|
||||
result.X += v2.X;
|
||||
result.Y += v2.Y;
|
||||
result.Z += v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector3d operator -(Vector3d v1, Vector3d v2)
|
||||
{
|
||||
Vector3d result = v1.Clone();
|
||||
result.X -= v2.X;
|
||||
result.Y -= v2.Y;
|
||||
result.Z -= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector3d operator *(Vector3d v1, Vector3d v2)
|
||||
{
|
||||
Vector3d result = v1.Clone();
|
||||
result.X *= v2.X;
|
||||
result.Y *= v2.Y;
|
||||
result.Z *= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector3d operator /(Vector3d v1, Vector3d v2)
|
||||
{
|
||||
Vector3d result = v1.Clone();
|
||||
result.X /= v2.X;
|
||||
result.Y /= v2.Y;
|
||||
result.Z /= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool operator >(Vector3d v1, Vector3d v2)
|
||||
{
|
||||
return (v1.X > v2.X) || (v1.Y > v2.Y) || (v1.X > v2.Z);
|
||||
}
|
||||
|
||||
public static bool operator <(Vector3d v1, Vector3d v2)
|
||||
{
|
||||
return (v1.X < v2.X) || (v1.Y < v2.Y) || (v1.X < v2.Z);
|
||||
}
|
||||
|
||||
public Vector3d Clone()
|
||||
{
|
||||
return new Vector3d(X, Y, Z);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"<{X}, {Y}, {Z}>";
|
||||
}
|
||||
|
||||
public bool Inside(Vector3d min, Vector3d max)
|
||||
{
|
||||
return (min.X <= X && max.X >= X) && (min.Y <= Y && max.Y >= Y) && (min.Z <= Z && max.Z >= Z);
|
||||
}
|
||||
|
||||
// Override GetHashCode if you override Equals
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(X, Y, Z);
|
||||
}
|
||||
}
|
||||
}
|
96
Utilities/Vector3i.cs
Normal file
96
Utilities/Vector3i.cs
Normal file
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
|
||||
namespace LibAC.Utilities
|
||||
{
|
||||
public class Vector3i
|
||||
{
|
||||
public static readonly Vector3i ZERO = new Vector3i(0, 0, 0);
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
public int Z { get; set; }
|
||||
|
||||
public Vector3i(int X = 0, int Y = 0, int Z = 0)
|
||||
{
|
||||
this.X = X;
|
||||
this.Y = Y;
|
||||
this.Z = Z;
|
||||
}
|
||||
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (other is Vector3i)
|
||||
{
|
||||
Vector3i otherVector = (Vector3i)other;
|
||||
return X == otherVector.X && Y == otherVector.Y && Z == otherVector.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Vector3i operator +(Vector3i v1, Vector3i v2)
|
||||
{
|
||||
Vector3i result = v1.Clone();
|
||||
result.X += v2.X;
|
||||
result.Y += v2.Y;
|
||||
result.Z += v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector3i operator -(Vector3i v1, Vector3i v2)
|
||||
{
|
||||
Vector3i result = v1.Clone();
|
||||
result.X -= v2.X;
|
||||
result.Y -= v2.Y;
|
||||
result.Z -= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector3i operator *(Vector3i v1, Vector3i v2)
|
||||
{
|
||||
Vector3i result = v1.Clone();
|
||||
result.X *= v2.X;
|
||||
result.Y *= v2.Y;
|
||||
result.Z *= v2.Z;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Vector3i operator /(Vector3i v1, Vector3i v2)
|
||||
{
|
||||
Vector3i result = v1.Clone();
|
||||
result.X = (int)Math.Round((double)result.X / v2.X);
|
||||
result.Y = (int)Math.Round((double)result.Y / v2.Y);
|
||||
result.Z = (int)Math.Round((double)result.Z / v2.Z);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool operator >(Vector3i v1, Vector3i v2)
|
||||
{
|
||||
return (v1.X > v2.X) || (v1.Y > v2.Y) || (v1.X > v2.Z);
|
||||
}
|
||||
|
||||
public static bool operator <(Vector3i v1, Vector3i v2)
|
||||
{
|
||||
return (v1.X < v2.X) || (v1.Y < v2.Y) || (v1.X < v2.Z);
|
||||
}
|
||||
|
||||
public Vector3i Clone()
|
||||
{
|
||||
return new Vector3i(X, Y, Z);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"<{X}, {Y}, {Z}>";
|
||||
}
|
||||
|
||||
public bool Inside(Vector3i min, Vector3i max)
|
||||
{
|
||||
return (min.X <= X && max.X >= X) && (min.Y <= Y && max.Y >= Y) && (min.Z <= Z && max.Z >= Z);
|
||||
}
|
||||
|
||||
// Override GetHashCode if you override Equals
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(X, Y, Z);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue