Skip to content

Commit

Permalink
More improvements to map command and Add-Ons.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrRetro2033 committed Oct 30, 2024
1 parent d8850a7 commit 32d1da8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 72 deletions.
2 changes: 2 additions & 0 deletions bin/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:ansix/ansix.dart';

class Cli {
static AnsiTreeViewTheme get treeTheme => AnsiTreeViewTheme(
compact: true,
headerTheme: AnsiTreeHeaderTheme(hideHeader: true),
keyTheme: AnsiTreeNodeKeyTheme(textStyle: AnsiTextStyle(bold: true)),
valueTheme: AnsiTreeNodeValueTheme(hideIfEmpty: true),
anchorTheme: AnsiTreeAnchorTheme(
style: AnsiBorderStyle.rounded, color: AnsiColor.magenta));
Expand Down
84 changes: 46 additions & 38 deletions bin/scripting/addon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ mixin Lua {
/// Returns the state, with the script loaded.
LuaState _initLuaVM(
String script, Map<String, int Function(LuaState)> dartFunctions) {
// print(script);
LuaState state = LuaState.newState();
state.openLibs();
for (String key in dartFunctions.keys) {
state.pushDartFunction(dartFunctions[key]!);
state.setGlobal(key);
state.register(key, dartFunctions[key]!);
}
state.doString(script);
return state;
Expand Down Expand Up @@ -375,6 +375,8 @@ class TestAddon extends Addon {
/// The pattern feature set is used to read and write to files with a specific format.
/// It requires a `read` and `write` function.
class PatternAddon extends Addon with Lua {
static const bool defaultIsLittleEndian = false;

@override
Map<String, int Function(LuaState)> get dartFunctions => {
"ru8": readU8,
Expand All @@ -386,13 +388,13 @@ class PatternAddon extends Addon with Lua {
"rfield": readBitfield,
"validate": validateTable
};
Uint8List? data;
ByteData? data;

PatternAddon(super.path);

Map<String, dynamic> read(String file) {
LuaState state = _initLuaVM(_getCode(), dartFunctions);
data = File(file).readAsBytesSync();
data = File(file).readAsBytesSync().buffer.asByteData();
state.getGlobal("read");
state.pushString(file);
state.pCall(1, 1, 0);
Expand Down Expand Up @@ -454,52 +456,51 @@ class PatternAddon extends Addon with Lua {

int readU8(LuaState state) {
int address = _getAddressFromLua(state);
state.pushInteger(data![address]);
state.pushInteger(data!.getUint8(address));
return 1;
}

int readU16(LuaState state) {
int address = _getAddressFromLua(state);
state.pushInteger(data![address] << 8 | data![address + 1]);
bool isLittleEndian =
state.isBoolean(2) ? state.toBoolean(2) : defaultIsLittleEndian;
state.pushInteger(
data!.getUint16(address, isLittleEndian ? Endian.little : Endian.big));
return 1;
}

int readU32(LuaState state) {
int address = _getAddressFromLua(state);
state.pushInteger(data![address] << 24 |
data![address + 1] << 16 |
data![address + 2] << 8 |
data![address + 3]);
bool isLittleEndian =
state.isBoolean(2) ? state.toBoolean(2) : defaultIsLittleEndian;
state.pushInteger(
data!.getUint32(address, isLittleEndian ? Endian.little : Endian.big));
return 1;
}

int readU64(LuaState state) {
int address = _getAddressFromLua(state);
state.pushInteger(data![address] << 56 |
data![address + 1] << 48 |
data![address + 2] << 40 |
data![address + 3] << 32 |
data![address + 4] << 24 |
data![address + 5] << 16 |
data![address + 6] << 8 |
data![address + 7]);
bool isLittleEndian =
state.isBoolean(2) ? state.toBoolean(2) : defaultIsLittleEndian;
state.pushInteger(
data!.getUint64(address, isLittleEndian ? Endian.little : Endian.big));
return 1;
}

int readString8(LuaState state) {
int address = _getAddressFromLua(state);
int length = _getAddressFromLua(state);
String string = utf8.decode(data!.sublist(address, address + length));
String string = utf8.decode(data!.buffer.asUint8List(address, length));
state.pushString(string);
return 1;
}

int readString16(LuaState state) {
final address = _getAddressFromLua(state, idx: 1);
int length = _getAddressFromLua(state, idx: 2);
length *= 2;
String string =
String.fromCharCodes(data!.sublist(address, address + length).toList());
// length *= 2;
String string = String.fromCharCodes(
data!.buffer.asUint16List(address, length).toList());
state.pushString(string);
return 1;
}
Expand All @@ -510,26 +511,33 @@ class PatternAddon extends Addon with Lua {
int size = 0;
Map<String, int> sizedTable = {};
for (String key in table.keys) {
if (table[key] is int) {
size += table[key] as int;
sizedTable[key] = table[key] as int;
} else if (table[key] is String &&
(table[key] as String).startsWith("pad")) {
size += int.parse((table[key] as String).substring(3));
sizedTable[key] = int.parse((table[key] as String).substring(3));
} else {
try {
sizedTable[key] = int.parse(table[key]);
size += sizedTable[key] as int;
} catch (e) {
sizedTable[key] = 0;
}
continue;
if (table[key] is Map<String, dynamic>) {
Map<String, dynamic> subTable = table[key] as Map<String, dynamic>;
sizedTable[subTable["1"]] = subTable["2"];

size += sizedTable[subTable["1"]]!;
}
// if (table[key] is int) {
// size += table[key] as int;
// sizedTable[key] = table[key] as int;
// } else if (table[key] is String &&
// (table[key] as String).startsWith("pad")) {
// size += int.parse((table[key] as String).substring(3));
// sizedTable[key] = int.parse((table[key] as String).substring(3));
// } else {
// try {
// sizedTable[key] = int.parse(table[key]);
// size += sizedTable[key] as int;
// } catch (e) {
// print(e);
// }
// continue;
// }
}
int byteSize = (size / 8).ceil();
BigInt combinedBitfield = BigInt.zero;
for (int i in data!.sublist(address, address + byteSize)) {
for (int i
in data!.buffer.asUint8List(address, byteSize).toList().reversed) {
combinedBitfield = (combinedBitfield << 8) | BigInt.from(i);
}

Expand Down
50 changes: 16 additions & 34 deletions bin/version_control/constellation.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'dart:convert';
import 'dart:io';

import 'package:ansix/ansix.dart';

import '../uuid.dart';
import '../arceus.dart';
import '../extensions.dart';
import '../cli.dart';
import 'star.dart';
import 'dossier.dart';
import 'users.dart';

import 'package:terminal_decorate/terminal_decorate.dart';

/// # `class` Constellation
/// ## Represents a constellation.
class Constellation {
Expand Down Expand Up @@ -358,42 +359,23 @@ class Starmap {
/// ## Shows the map of the constellation.
/// This is a tree view of the constellation's stars and their children.
void showMap() {
print(root!.getDisplayName());
if (root!.isAlone) {
return;
}
List<Star> children = getChildren(root!);
for (Star star in children) {
if (children.last == star) {
_printChildren(star, 0, isLast: true);
break;
}
_printChildren(star, 0);
}
print(AnsiTreeView(_getTree(root!, {}), theme: Cli.treeTheme));
}

void _printChildren(Star parent, int level,
{bool isBranch = false, bool isLast = false}) {
String indent = " " * level;
String pipeing = (isLast || (parent.isAlone && isBranch)) ? "╰─" : "├─";
String shell = level == 0 ? "" : "│ ";
print(
" ${shell.magenta}$indent${pipeing.magenta} ${parent.getDisplayName()}");
List<Star> children = getChildren(parent);
// indent = "\t" * (level + 1);
if (parent.singleChild) {
if (!isBranch) {
level += 1;
Map<String, dynamic> _getTree(Star star, Map<String, dynamic> tree,
{bool branch = false}) {
tree[star.getDisplayName()] = {};
if (star.singleChild) {
if (branch) {
tree[star.getDisplayName()].addAll(_getTree(star.children.first, {}));
return tree;
}
_printChildren(children.first, level, isBranch: true);
return;
}
for (Star child in children) {
if (children.last == child) {
_printChildren(child, level + 1, isLast: true);
break;
return _getTree(star.children.first, tree);
} else {
for (Star child in getChildren(star)) {
tree[star.getDisplayName()].addAll(_getTree(child, {}, branch: true));
}
_printChildren(child, level + 1);
}
return tree;
}
}

0 comments on commit 32d1da8

Please sign in to comment.