Make LibAC be web compatible for UUIDs

This commit is contained in:
zontreck 2024-05-19 18:43:26 -07:00
parent 960cf0d789
commit d6f0e05713
7 changed files with 254 additions and 52 deletions

View file

@ -5,20 +5,10 @@ import 'package:crypto/crypto.dart';
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;
LSB = lsb;
ByteLayer layer = ByteLayer();
layer.writeLong(MSB);
layer.writeLong(LSB);
layer.resetPosition();
_bytes = layer.readBytes(16);
UUID(List<int> bytes) {
_bytes = bytes;
}
static final UUID ZERO = UUID.generate(0);
@ -43,9 +33,6 @@ class UUID {
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) {
@ -61,10 +48,8 @@ class UUID {
}
layer.resetPosition();
msb = layer.readLong();
lsb = layer.readLong();
return UUID(msb, lsb);
return UUID(layer.readBytes(16));
} else {
return UUID.ZERO;
}
@ -79,12 +64,6 @@ class UUID {
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 = [];
@ -97,7 +76,16 @@ class UUID {
}
switch (version) {
case 0:
return UUID(0, 0);
{
int i = 0;
int end = 16;
List<int> bytes = [];
for (i = 0; i < end; i++) {
bytes.add(0);
}
return UUID(bytes);
}
case 3:
{
if (params.length != 2) {
@ -122,29 +110,21 @@ class UUID {
layer.unsetSetBit(8, 0xC0, 0x80);
layer.resetPosition();
var msb = layer.readUnsignedLong();
var lsb = layer.readUnsignedLong();
layer.resetPosition();
return UUID(msb, lsb);
return UUID(layer.readBytes(16));
}
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.insertRandomBytes(16);
layer.unsetSetBit(6, 0xF0, 0x40);
layer.unsetSetBit(8, 0xC0, 0x80);
layer.resetPosition();
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
return UUID(layer.readBytes(16));
}
case 5:
{
@ -175,10 +155,14 @@ class UUID {
layer.resetPosition();
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
return UUID(layer.readBytes(16));
}
default:
throw ArgumentError('Unsupported UUID version: $version');
}
}
Iterable<int> getBytes() {
return _bytes.take(16);
}
}