Fix generator
This commit is contained in:
parent
a9abed7279
commit
c98b8d5d67
1 changed files with 35 additions and 49 deletions
|
@ -7,27 +7,18 @@ import 'package:libac_flutter/nbt/Stream.dart';
|
|||
class UUID {
|
||||
late final int LSB;
|
||||
late final int MSB;
|
||||
late final List<int> _bytes;
|
||||
|
||||
UUID(int msb, int lsb) {
|
||||
MSB = msb.abs();
|
||||
LSB = lsb.abs();
|
||||
}
|
||||
MSB = msb;
|
||||
LSB = lsb;
|
||||
|
||||
UUID.fromBytes(List<int> bytes) {
|
||||
int msb = 0;
|
||||
int lsb = 0;
|
||||
ByteLayer layer = ByteLayer();
|
||||
layer.writeLong(MSB);
|
||||
layer.writeLong(LSB);
|
||||
layer.resetPosition();
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i < 8; ++i) {
|
||||
msb = msb << 8 | bytes[i] & 255;
|
||||
}
|
||||
|
||||
for (i = 8; i < 16; ++i) {
|
||||
lsb = lsb << 8 | bytes[i] & 255;
|
||||
}
|
||||
|
||||
this.MSB = msb;
|
||||
this.LSB = lsb;
|
||||
_bytes = layer.readBytes(16);
|
||||
}
|
||||
|
||||
static final UUID ZERO = UUID.generate(0);
|
||||
|
@ -51,8 +42,24 @@ class UUID {
|
|||
final msbString = segments.sublist(0, 3).join('');
|
||||
final lsbString = segments.sublist(3, 5).join('');
|
||||
|
||||
final msb = int.parse(msbString, radix: 16);
|
||||
final lsb = int.parse(lsbString, radix: 16);
|
||||
int msb = 0;
|
||||
int lsb = 0;
|
||||
|
||||
int i = 0;
|
||||
ByteLayer layer = ByteLayer();
|
||||
for (i = 0; i < msbString.length; i += 2) {
|
||||
int byte = int.parse(msbString.substring(i, i + 1), radix: 16);
|
||||
layer.writeByte(byte);
|
||||
}
|
||||
|
||||
for (i = 0; i < lsbString.length; i += 2) {
|
||||
int byte = int.parse(lsbString.substring(i, i + 1), radix: 16);
|
||||
layer.writeByte(byte);
|
||||
}
|
||||
|
||||
layer.resetPosition();
|
||||
msb = layer.readLong();
|
||||
lsb = layer.readLong();
|
||||
|
||||
return UUID(msb, lsb);
|
||||
} else
|
||||
|
@ -61,9 +68,11 @@ class UUID {
|
|||
|
||||
@override
|
||||
String toString() {
|
||||
final msbHex = MSB.toRadixString(16).padLeft(16, '0');
|
||||
final lsbHex = LSB.toRadixString(16).padLeft(16, '0');
|
||||
return '${msbHex.substring(0, 8)}-${msbHex.substring(8, 12)}-${msbHex.substring(12, 16)}-${lsbHex.substring(0, 4)}-${lsbHex.substring(4, 16)}';
|
||||
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.
|
||||
|
@ -94,47 +103,24 @@ class UUID {
|
|||
|
||||
ByteLayer layer = ByteLayer();
|
||||
|
||||
if (!namespace.isEmpty) {
|
||||
final namespaceBytes = utf8.encode(namespace);
|
||||
layer.writeBytes(namespaceBytes);
|
||||
}
|
||||
|
||||
if (!name.isEmpty) {
|
||||
final nameBytes = utf8.encode(name);
|
||||
layer.writeBytes(nameBytes);
|
||||
}
|
||||
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, 0x0F, 0x30);
|
||||
print(
|
||||
"Existing bit at position 8: ${layer.getBit(8).toRadixString(2)}:${layer.getBit(8)}");
|
||||
layer.unsetSetBit(8, 0x3F, 0x80);
|
||||
print(
|
||||
"New bit at position 8: ${layer.getBit(8).toRadixString(2)}:${layer.getBit(8)}");
|
||||
|
||||
layer.resetPosition();
|
||||
|
||||
String csv = "";
|
||||
for (int byte in layer.readBytes(16)) {
|
||||
csv += "0x${byte.toRadixString(16)}, ";
|
||||
}
|
||||
|
||||
print("BYTES CSV: ${csv}");
|
||||
|
||||
layer.resetPosition();
|
||||
|
||||
var msb = layer.readUnsignedLong();
|
||||
var lsb = layer.readUnsignedLong();
|
||||
|
||||
if (msb < 0) print("Most significant bit is negative! ${msb}");
|
||||
if (lsb < 0) print("Least significant bit is negative! ${lsb}");
|
||||
|
||||
print("Forming UUID using MSB-LSB - ${msb} - ${lsb}");
|
||||
layer.resetPosition();
|
||||
return UUID.fromBytes(layer.readBytes(16));
|
||||
return UUID(msb, lsb);
|
||||
}
|
||||
case 4:
|
||||
|
|
Loading…
Reference in a new issue