Add ability to save nbt and snbt

This commit is contained in:
zontreck 2024-07-22 04:01:23 -07:00
parent 3f5f4a5a46
commit 093bb2fb02

View file

@ -1,7 +1,12 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_treeview/flutter_treeview.dart'; import 'package:flutter_treeview/flutter_treeview.dart';
import 'package:libac_dart/nbt/NbtIo.dart'; import 'package:libac_dart/nbt/NbtIo.dart';
import 'package:libac_dart/nbt/SnbtIo.dart';
import 'package:libac_dart/nbt/Stream.dart';
import 'package:libac_dart/nbt/Tag.dart'; import 'package:libac_dart/nbt/Tag.dart';
import 'package:libac_dart/nbt/impl/CompoundTag.dart'; import 'package:libac_dart/nbt/impl/CompoundTag.dart';
import 'package:nbteditor/Constants.dart'; import 'package:nbteditor/Constants.dart';
@ -120,6 +125,71 @@ class EditorState extends State<Editor> {
await Navigator.pushNamed(context, "/snbt"); await Navigator.pushNamed(context, "/snbt");
setState(() {}); setState(() {});
}, },
),
ListTile(
title: Text("S A V E N B T"),
subtitle: Text("Save to NBT"),
leading: Image(
image: AssetImage("Icons/PNG/AppLogo.png"),
),
onTap: () async {
// Prompt for where to save
String? filePath = await FilePicker.platform
.saveFile(dialogTitle: "Where do you want to save the file?");
if (filePath == null) {
print("No file selected");
return;
}
print(filePath);
var result = await showDialog(
context: context,
builder: (builder) {
return AlertDialog(
title: Text("Compress the data?"),
actions: [
ElevatedButton(
onPressed: () {
Navigator.pop(context, true);
},
child: Text("YES")),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("No"))
],
);
});
if (result == null) {
// Save uncompressed
NbtIo.write(
filePath!, controller.children[0].data as CompoundTag);
} else
NbtIo.writeCompressed(
filePath!, controller.children[0].data as CompoundTag);
},
),
ListTile(
title: Text("S A V E S N B T"),
subtitle: Text("Save to SNBT"),
leading: Image(
image: AssetImage("Icons/PNG/String.png"),
),
onTap: () async {
// Prompt for where to save
String? filePath = await FilePicker.platform
.saveFile(dialogTitle: "Where do you want to save the file?");
if (filePath == null) {
print("No file selected");
return;
}
print(filePath);
SnbtIo.writeToFile(
filePath!, controller.children[0].data as CompoundTag);
},
) )
]), ]),
), ),