Add a missing instruction, and a testcase

This commit is contained in:
zontreck 2024-05-23 11:40:16 -07:00
parent 95d769bf89
commit 312176d37f
2 changed files with 12 additions and 178 deletions

View file

@ -13,6 +13,7 @@ class NbtUUID {
factory NbtUUID.fromUUID(UUID id) {
ByteLayer layer = ByteLayer();
layer.writeBytes(id.getBytes());
layer.resetPosition();
int MSB = layer.readLong();
int LSB = layer.readLong();
@ -27,191 +28,15 @@ class NbtUUID {
int getLeastSignificantBits() => LSB;
UUID toUUID() {
ByteLayer layer = ByteLayer();
layer.writeUnsignedLong(MSB);
layer.writeUnsignedLong(LSB);
return UUID(layer.readBytes(16));
}
}
/*
class NbtUUID extends UUID{
late final int LSB;
late final int MSB;
late final List<int> _bytes;
NbtUUID(int msb, int lsb) {
MSB = msb;
LSB = lsb;
ByteLayer layer = ByteLayer();
layer.writeLong(MSB);
layer.writeLong(LSB);
layer.resetPosition();
_bytes = layer.readBytes(16);
}
static final UUID ZERO = UUID.generate(0);
/// Validates whether the given [uuid] is a valid UUID.
static bool validate(String uuid) {
if (uuid.length == ((16 * 2) + 4)) {
return true; // Likely is true. This is just a surface level check
} else {
return false;
}
}
/// Parses the given [uuid] string and returns a UUID object.
static UUID parse(String uuid) {
if (validate(uuid)) {
final segments = uuid.split('-');
if (segments.length != 5) {
throw const FormatException('Invalid UUID format');
}
final msbString = segments.sublist(0, 3).join('');
final lsbString = segments.sublist(3, 5).join('');
int msb = 0;
int lsb = 0;
int i = 0;
ByteLayer layer = ByteLayer();
for (i = 0; i < msbString.length; i += 2) {
String hex = msbString.substring(i, i + 2);
int byte = int.parse(hex, radix: 16);
layer.writeByte(byte);
}
for (i = 0; i < lsbString.length; i += 2) {
String hex = lsbString.substring(i, i + 2);
int byte = int.parse(hex, radix: 16);
layer.writeByte(byte);
}
layer.resetPosition();
msb = layer.readLong();
lsb = layer.readLong();
return UUID(msb, lsb);
} else {
return UUID.ZERO;
}
return UUID(layer.readBytes(16));
}
@override
String toString() {
String hexBuilder = "";
for (int byte in _bytes) {
hexBuilder += byte.toRadixString(16).padLeft(2, '0');
}
return '${hexBuilder.substring(0, 8)}-${hexBuilder.substring(8, 12)}-${hexBuilder.substring(12, 16)}-${hexBuilder.substring(16, 20)}-${hexBuilder.substring(20, 32)}';
}
/// Returns the Most Significant Bits (MSB) long value of the UUID.
int getMostSignificantBits() => MSB;
/// Returns the Least Significant Bits (LSB) long value of the UUID.
int getLeastSignificantBits() => LSB;
/// Factory method to generate UUID of the specific version.
factory UUID.generate(int version, {List<Object>? parameters}) {
List<Object> params = [];
if (parameters == null) {
if (version != 4 && version != 0) {
return UUID.generate(4);
}
} else {
params = parameters;
}
switch (version) {
case 0:
return UUID(0, 0);
case 3:
{
if (params.length != 2) {
throw Exception(
"UUID v3 requires two parameters, [namespace,name]");
}
String namespace = params[0] as String;
String name = params[1] as String;
ByteLayer layer = ByteLayer();
final namespaceBytes = utf8.encode(namespace);
layer.writeBytes(namespaceBytes);
final nameBytes = utf8.encode(name);
layer.writeBytes(nameBytes);
var bytes = md5.convert(List.from(layer.bytes)).bytes;
layer.clear();
layer.writeBytes(bytes);
layer.unsetSetBit(6, 0xF0, 0x30);
layer.unsetSetBit(8, 0xC0, 0x80);
layer.resetPosition();
var msb = layer.readUnsignedLong();
var lsb = layer.readUnsignedLong();
layer.resetPosition();
return UUID(msb, lsb);
}
case 4:
{
ByteLayer layer = ByteLayer();
final random = Random.secure();
layer.writeLong(
(random.nextInt(0xFFFFFFFF) << 32) | random.nextInt(0xFFFFFFFF));
layer.writeLong(
(random.nextInt(0xFFFFFFFF) << 32) | random.nextInt(0xFFFFFFFF));
layer.unsetSetBit(6, 0xF0, 0x40);
layer.unsetSetBit(8, 0xC0, 0x80);
layer.resetPosition();
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
}
case 5:
{
ByteLayer layer = ByteLayer();
if (params.length != 2) {
throw Exception(
"UUID v5 requires two parameters, [namespace,name]");
}
String namespace = params[0] as String;
String name = params[1] as String;
if (namespace.isNotEmpty) {
final namespaceBytes = utf8.encode(namespace);
layer.writeBytes(namespaceBytes);
}
if (name.isNotEmpty) {
final nameBytes = utf8.encode(name);
layer.writeBytes(nameBytes);
}
final hashBytes = sha1.convert(List.from(layer.bytes)).bytes;
layer.clear();
layer.writeBytes(hashBytes);
layer.unsetSetBit(6, 0xF0, 0x50);
layer.unsetSetBit(8, 0xC0, 0x80);
layer.resetPosition();
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
}
default:
throw ArgumentError('Unsupported UUID version: $version');
}
return toUUID().toString();
}
}
*/

View file

@ -86,4 +86,13 @@ void main() {
// Expect that the file exists
expect(file.existsSync(), true);
});
test("Write NULL UUID to NBT", () async {
CompoundTag tag = CompoundTag();
NbtUtils.writeUUID(tag, "test", NbtUUID.fromUUID(UUID.ZERO));
NbtUUID ID = NbtUtils.readUUID(tag, "test");
expect(ID.MSB, 0);
expect(ID.LSB, 0);
});
}