Merge pull request 'Implement #1' (#4) from feat/1 into main

Reviewed-on: #4
This commit is contained in:
zontreck 2024-05-05 03:20:47 +00:00
commit 9b33f69021
23 changed files with 686 additions and 2 deletions

3
.gitignore vendored
View file

@ -27,3 +27,6 @@ doc/api/
.flutter-plugins
.flutter-plugins-dependencies
.idea
*.iml

10
.metadata Normal file
View file

@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: "54e66469a933b60ddf175f858f82eaeb97e48c8d"
channel: "stable"
project_type: package

3
CHANGELOG.md Normal file
View file

@ -0,0 +1,3 @@
## 0.0.1
* TODO: Describe initial release.

View file

@ -1,3 +1,10 @@
# LibACDart
# LibAC-dart
A Dart Library for my commonly used code and flutter widgets.
This is the Aria's Creations common code library. This contains commonly used code, helpers, and useful widgets specific to Aria's Creations.
# Package Name
_______________
The package name for releases is: `libac_flutter`, despite the project name being `libac_dart`
The packages can be found here: https://git.zontreck.com/AriasCreations/-/packages/pub/libac_flutter

4
analysis_options.yaml Normal file
View file

@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

237
lib/nbt/Stream.dart Normal file
View file

@ -0,0 +1,237 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
class ByteLayer {
Uint8List _byteBuffer = Uint8List(0);
int _position = 0;
ByteLayer() {
_byteBuffer = Uint8List(0); // Initial size, can be adjusted
_position = 0;
}
int get length => _byteBuffer.length;
int get currentPosition => _position;
Uint8List get bytes => _byteBuffer.sublist(0, _position);
void _ensureCapacity(int additionalBytes) {
final requiredCapacity = _position + additionalBytes;
if (requiredCapacity > _byteBuffer.length) {
final newCapacity =
_position * 2 + additionalBytes; // Adjust capacity as needed
final newBuffer = Uint8List(newCapacity);
newBuffer.setAll(0, _byteBuffer);
_byteBuffer = newBuffer;
}
}
void writeInt(int value) {
_ensureCapacity(4);
_byteBuffer.buffer.asByteData().setInt32(_position, value, Endian.big);
_position += 4;
}
void writeDouble(double value) {
_ensureCapacity(8);
_byteBuffer.buffer.asByteData().setFloat64(_position, value, Endian.big);
_position += 8;
}
void writeString(String value) {
final encoded = utf8.encode(value);
writeShort(encoded.length);
_ensureCapacity(encoded.length);
_byteBuffer.setAll(_position, encoded);
_position += encoded.length;
}
int readInt() {
final value =
_byteBuffer.buffer.asByteData().getInt32(_position, Endian.big);
_position += 4;
return value;
}
double readDouble() {
final value =
_byteBuffer.buffer.asByteData().getFloat64(_position, Endian.big);
_position += 8;
return value;
}
String readString() {
final length = readShort();
final encoded = _byteBuffer.sublist(_position, _position + length);
_position += length;
return utf8.decode(encoded);
}
void writeIntZigZag(int value) {
final zigzag = (value << 1) ^ (value >> 31);
writeInt(zigzag);
}
int readIntZigZag() {
final zigzag = readInt();
final value = (zigzag >> 1) ^ -(zigzag & 1);
return value;
}
void writeByte(int value) {
_ensureCapacity(1);
_byteBuffer[_position] = value & 0xFF;
_position++;
}
int readByte() {
final value = _byteBuffer[_position];
_position++;
return value;
}
void writeVarInt(int value) {
while ((value & ~0x7F) != 0) {
writeByte((value & 0x7F) | 0x80);
value = (value >> 7) & 0x1FFFFFFF;
}
writeByte(value & 0x7F);
}
int readVarInt() {
int result = 0;
int shift = 0;
int byte;
do {
byte = readByte();
result |= (byte & 0x7F) << shift;
if ((byte & 0x80) == 0) {
break;
}
shift += 7;
} while (true);
return result;
}
void writeVarIntNoZigZag(int value) {
while ((value & ~0x7F) != 0) {
writeByte((value & 0x7F) | 0x80);
value >>= 7;
}
writeByte(value & 0x7F);
}
int readVarIntNoZigZag() {
int result = 0;
int shift = 0;
int byte;
do {
byte = readByte();
result |= (byte & 0x7F) << shift;
if ((byte & 0x80) == 0) {
break;
}
shift += 7;
} while (true);
return result;
}
void writeShort(int value) {
_ensureCapacity(2);
_byteBuffer.buffer.asByteData().setInt16(_position, value, Endian.big);
_position += 2;
}
int readShort() {
final value =
_byteBuffer.buffer.asByteData().getInt16(_position, Endian.big);
_position += 2;
return value;
}
void writeFloat(double value) {
_ensureCapacity(4);
_byteBuffer.buffer.asByteData().setFloat32(_position, value, Endian.big);
_position += 2;
}
double readFloat() {
final value =
_byteBuffer.buffer.asByteData().getFloat32(_position, Endian.big);
_position += 2;
return value;
}
void writeTagName(String name) {
final encodedName = utf8.encode(name);
writeShort(encodedName.length);
_ensureCapacity(encodedName.length);
_byteBuffer.setAll(_position, encodedName);
_position += encodedName.length;
}
String readTagName() {
final length = readShort();
final encodedName = _byteBuffer.sublist(_position, _position + length);
_position += length;
return utf8.decode(encodedName);
}
void resetPosition() {
_position = 0;
}
void clear() {
resetPosition();
_byteBuffer = Uint8List(0);
}
Future<void> writeToFile(String filePath) async {
final file = File(filePath);
await file.writeAsBytes(bytes);
}
Future<void> readFromFile(String filePath) async {
final file = File(filePath);
final exists = await file.exists();
if (!exists) {
print('File does not exist.');
return;
}
_byteBuffer = await file.readAsBytes();
resetPosition();
}
Future<void> compress() async {
final gzip = GZipCodec();
final compressedData = gzip.encode(_byteBuffer);
_byteBuffer = Uint8List.fromList(compressedData);
_position = _byteBuffer.length;
}
Future<void> decompress() async {
final gzip = GZipCodec();
final decompressedData = gzip.decode(_byteBuffer);
_byteBuffer = Uint8List.fromList(decompressedData);
_position = _byteBuffer.length;
}
void writeLong(int value) {
_ensureCapacity(8);
_byteBuffer.buffer.asByteData().setInt64(_position, value, Endian.big);
_position += 8;
}
int readLong() {
final value =
_byteBuffer.buffer.asByteData().getInt64(_position, Endian.big);
_position += 8;
return value;
}
}

30
lib/nbt/Tag.dart Normal file
View file

@ -0,0 +1,30 @@
import 'Stream.dart';
enum TagType {
End(0),
Byte(1),
Short(2),
Int(3),
Long(4),
Float(5),
Double(6),
ByteArray(7),
String(8),
List(9),
Compound(10),
IntArray(11),
LongArray(12);
final int byte;
const TagType(this.byte);
}
abstract class Tag {
int getType() {
return getTagType().byte;
}
TagType getTagType();
void writeValue(ByteLayer data);
void readValue(ByteLayer data);
}

23
lib/nbt/impl/ByteTag.dart Normal file
View file

@ -0,0 +1,23 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class ByteTag extends Tag {
final int value;
ByteTag({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.Byte;
}
}

View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagByteArray extends Tag {
final List<int> value;
TagByteArray({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.ByteArray;
}
}

View file

@ -0,0 +1,46 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagCompound extends Tag {
late final Map<String, Tag> _value;
TagCompound() {
_value = new Map();
}
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
void put(String name, Tag tag) {
_value[name] = tag;
}
bool contains(String name) {
return _value.containsKey(name);
}
Tag? get(String name) {
if (contains(name))
return _value[name] as Tag;
else {
// Does not exist!
return null;
}
}
void remove(String name) {
_value.remove(name);
}
@override
TagType getTagType() {
return TagType.Compound;
}
}

View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagDouble extends Tag {
final double value;
TagDouble({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.Double;
}
}

17
lib/nbt/impl/TagEnd.dart Normal file
View file

@ -0,0 +1,17 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagEnd extends Tag {
TagEnd();
@override
void readValue(ByteLayer data) {}
@override
void writeValue(ByteLayer data) {}
@override
TagType getTagType() {
return TagType.End;
}
}

View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagFloat extends Tag {
final double value;
TagFloat({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.Float;
}
}

22
lib/nbt/impl/TagInt.dart Normal file
View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagInt extends Tag {
final int value;
TagInt({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.Int;
}
}

View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagIntArray extends Tag {
final List<int> value;
TagIntArray({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.IntArray;
}
}

50
lib/nbt/impl/TagList.dart Normal file
View file

@ -0,0 +1,50 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagList extends Tag {
late final List<Tag> _value;
TagType _internalType = TagType.End;
TagList() {
_value = List.empty(growable: true);
}
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
void add(Tag tag) {
if (_internalType == TagType.End) {
_value.clear();
_value.add(tag);
_internalType = tag.getTagType();
} else {
if (_internalType == tag.getTagType()) {
_value.add(tag);
}
}
}
void remove(Tag tag) {
_value.remove(tag);
}
void removeAt(int index) {
_value.removeAt(index);
}
int indexOf(Tag tag) {
return _value.indexOf(tag);
}
@override
TagType getTagType() {
return TagType.List;
}
}

22
lib/nbt/impl/TagLong.dart Normal file
View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagLong extends Tag {
final int value;
TagLong({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.Long;
}
}

View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagLongArray extends Tag {
final List<int> value;
TagLongArray({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.LongArray;
}
}

View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagShort extends Tag {
final int value;
TagShort({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.Short;
}
}

View file

@ -0,0 +1,22 @@
import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class TagString extends Tag {
final String value;
TagString({required this.value});
@override
void readValue(ByteLayer data) {
// TODO: implement readValue
}
@override
void writeValue(ByteLayer data) {
// TODO: implement writeValue
}
@override
TagType getTagType() {
return TagType.String;
}
}

56
pubspec.yaml Normal file
View file

@ -0,0 +1,56 @@
name: libac_flutter
description: "Aria's Creations code library"
version: 1.0.0
homepage:
environment:
sdk: '>=3.3.4 <4.0.0'
flutter: ">=1.17.0"
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter packages.
flutter:
# To add assets to your package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
# To add custom fonts to your package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages
publish_to: https://git.zontreck.com/api/packages/AriasCreations/pub

BIN
test/bigtest.nbt Normal file

Binary file not shown.

BIN
test/hello_world.nbt Normal file

Binary file not shown.