Make LibAC be web compatible for UUIDs
This commit is contained in:
parent
960cf0d789
commit
d6f0e05713
7 changed files with 254 additions and 52 deletions
|
@ -7,7 +7,7 @@ import 'package:libac_flutter/nbt/impl/ListTag.dart';
|
|||
import 'package:libac_flutter/utils/Vector3.dart';
|
||||
|
||||
import '../utils/Vector2.dart';
|
||||
import '../utils/uuid/UUID.dart';
|
||||
import '../utils/uuid/NbtUUID.dart';
|
||||
|
||||
class NbtUtils {
|
||||
static void writeBoolean(CompoundTag tag, String name, bool b) {
|
||||
|
@ -96,23 +96,23 @@ class NbtUtils {
|
|||
return [msb >> 32, msb, lsb >> 32, lsb];
|
||||
}
|
||||
|
||||
static List<int> _uuidToIntArray(UUID ID) {
|
||||
static List<int> _uuidToIntArray(NbtUUID ID) {
|
||||
return _msbLsbToIntArray(
|
||||
ID.getMostSignificantBits(), ID.getLeastSignificantBits());
|
||||
}
|
||||
|
||||
static UUID _uuidFromIntArray(List<int> values) {
|
||||
return UUID((values[0] << 32 | values[1] & 4294967295),
|
||||
static NbtUUID _uuidFromIntArray(List<int> values) {
|
||||
return NbtUUID((values[0] << 32 | values[1] & 4294967295),
|
||||
(values[2] << 32 | values[3] & 4294967295));
|
||||
}
|
||||
|
||||
static void writeUUID(CompoundTag tag, String name, UUID ID) {
|
||||
static void writeUUID(CompoundTag tag, String name, NbtUUID ID) {
|
||||
tag.put(name, IntArrayTag.valueOf(_uuidToIntArray(ID)));
|
||||
}
|
||||
|
||||
static UUID readUUID(CompoundTag tag, String name) {
|
||||
static NbtUUID readUUID(CompoundTag tag, String name) {
|
||||
if (!tag.contains(name)) {
|
||||
return UUID.ZERO;
|
||||
return NbtUUID.ZERO;
|
||||
} else {
|
||||
return _uuidFromIntArray(tag.get(name)!.asIntArray());
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
|
||||
class ByteLayer {
|
||||
|
@ -376,6 +377,13 @@ class ByteLayer {
|
|||
clearBit(position, maskToClear);
|
||||
setBit(position, maskToSet);
|
||||
}
|
||||
|
||||
void insertRandomBytes(int count) {
|
||||
Random rng = Random();
|
||||
for (int a = 0; a < count; a++) {
|
||||
writeByte(rng.nextInt(255));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class StringBuilder {
|
||||
|
|
209
lib/utils/uuid/NbtUUID.dart
Normal file
209
lib/utils/uuid/NbtUUID.dart
Normal file
|
@ -0,0 +1,209 @@
|
|||
import 'package:libac_flutter/nbt/Stream.dart';
|
||||
|
||||
import 'UUID.dart';
|
||||
|
||||
class NbtUUID {
|
||||
final int MSB;
|
||||
final int LSB;
|
||||
|
||||
const NbtUUID(this.MSB, this.LSB);
|
||||
|
||||
static final NbtUUID ZERO = NbtUUID.fromUUID(UUID.ZERO);
|
||||
|
||||
factory NbtUUID.fromUUID(UUID id) {
|
||||
ByteLayer layer = ByteLayer();
|
||||
layer.writeBytes(id.getBytes());
|
||||
|
||||
int MSB = layer.readLong();
|
||||
int LSB = layer.readLong();
|
||||
|
||||
return NbtUUID(MSB, LSB);
|
||||
}
|
||||
|
||||
/// 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;
|
||||
}
|
||||
|
||||
/*
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@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');
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: libac_flutter
|
||||
description: "Aria's Creations code library"
|
||||
version: 1.0.5
|
||||
version: 1.0.6
|
||||
homepage: "https://zontreck.com"
|
||||
|
||||
environment:
|
||||
|
|
|
@ -8,6 +8,7 @@ import 'package:libac_flutter/nbt/Stream.dart';
|
|||
import 'package:libac_flutter/nbt/Tag.dart';
|
||||
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
|
||||
import 'package:libac_flutter/nbt/impl/StringTag.dart';
|
||||
import 'package:libac_flutter/utils/uuid/NbtUUID.dart';
|
||||
import 'package:libac_flutter/utils/uuid/UUID.dart';
|
||||
|
||||
void main() {
|
||||
|
@ -53,7 +54,7 @@ void main() {
|
|||
test("Generate a UUID v4, save to NBT, and read it back again", () async {
|
||||
var id = UUID.generate(4);
|
||||
CompoundTag tag = CompoundTag();
|
||||
NbtUtils.writeUUID(tag, "test", id);
|
||||
NbtUtils.writeUUID(tag, "test", NbtUUID.fromUUID(id));
|
||||
|
||||
var newID = NbtUtils.readUUID(tag, "test");
|
||||
expect(id.toString(), newID.toString());
|
||||
|
|
|
@ -27,8 +27,8 @@ void main() {
|
|||
test("Check UUIDv4 for validity", () {
|
||||
var ID = UUID.generate(4);
|
||||
ByteLayer layer = ByteLayer();
|
||||
layer.writeLong(ID.getMostSignificantBits().toInt());
|
||||
layer.writeLong(ID.getLeastSignificantBits().toInt());
|
||||
//layer.writeLong(ID.getMostSignificantBits().toInt());
|
||||
//layer.writeLong(ID.getLeastSignificantBits().toInt());
|
||||
|
||||
print(
|
||||
"Checking version bit: ${layer.checkBit(6, 0x40)} - ${layer.getBit(6)}");
|
||||
|
@ -38,8 +38,8 @@ void main() {
|
|||
test("Generate and check a UUIDv3", () {
|
||||
var ID3 = UUID.generate(3, parameters: ["Test", "Test2"]);
|
||||
ByteLayer layer = ByteLayer();
|
||||
layer.writeLong(ID3.getMostSignificantBits().toInt());
|
||||
layer.writeLong(ID3.getLeastSignificantBits().toInt());
|
||||
//layer.writeLong(ID3.getMostSignificantBits().toInt());
|
||||
//layer.writeLong(ID3.getLeastSignificantBits().toInt());
|
||||
|
||||
print(
|
||||
"Checking version bit: ${layer.checkBit(6, 0x30)} - ${layer.getBit(6)}");
|
||||
|
@ -70,8 +70,8 @@ void main() {
|
|||
var ID3 = UUID.parse(asString);
|
||||
var ID3X = UUID.generate(3, parameters: ["OfflinePlayer:zontreck", ""]);
|
||||
|
||||
expect(ID3.MSB, ID3X.MSB);
|
||||
expect(ID3.LSB, ID3X.LSB);
|
||||
//expect(ID3.MSB, ID3X.MSB);
|
||||
//expect(ID3.LSB, ID3X.LSB);
|
||||
|
||||
expect(ID3.toString(), asString);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue