Start to update stuff
This commit is contained in:
parent
110b2d150c
commit
399d884681
18 changed files with 217 additions and 240 deletions
|
@ -1,8 +1,10 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:libac_flutter/nbt/NbtIo.dart';
|
||||
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
|
||||
import 'package:libac_flutter/packets/packets.dart';
|
||||
import 'package:libac_flutter/utils/IOTools.dart';
|
||||
import 'package:servermanager/mod.dart';
|
||||
import 'package:servermanager/pathtools.dart';
|
||||
import 'package:servermanager/settingsEntry.dart';
|
||||
import 'package:servermanager/statemachine.dart';
|
||||
|
||||
|
@ -12,6 +14,7 @@ class Settings {
|
|||
|
||||
String steamcmd_path = "";
|
||||
String game_path = "";
|
||||
PacketClient? client;
|
||||
|
||||
StateMachine subsys = StateMachine();
|
||||
|
||||
|
@ -21,45 +24,43 @@ class Settings {
|
|||
|
||||
SettingsEntry? inst;
|
||||
|
||||
void Read() {
|
||||
if (!isValid()) return;
|
||||
var box = Hive.box("settings");
|
||||
Future<void> Read() async {
|
||||
try {
|
||||
var tag = await NbtIo.read(
|
||||
PathHelper.builder(game_path).resolve("settings.dat").build());
|
||||
|
||||
inst = box.get("entry", defaultValue: SettingsEntry()) as SettingsEntry;
|
||||
inst = SettingsEntry.deserialize(tag.get("entry") as CompoundTag);
|
||||
} catch (E) {
|
||||
inst = SettingsEntry();
|
||||
}
|
||||
}
|
||||
|
||||
void Write() {
|
||||
if (!isValid()) return;
|
||||
var box = Hive.box("settings");
|
||||
box.put("entry", inst);
|
||||
CompoundTag tag = CompoundTag();
|
||||
tag.put("entry", inst!.serialize());
|
||||
|
||||
box.compact();
|
||||
NbtIo.write(
|
||||
PathHelper.builder(game_path).resolve("settings.dat").build(), tag);
|
||||
}
|
||||
|
||||
bool isValid() {
|
||||
if (!Hive.isBoxOpen("settings")) {
|
||||
if (inst == null) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Box<E>> Open<E>() {
|
||||
Future<void> Open() async {
|
||||
Close();
|
||||
return Hive.openBox(
|
||||
"settings",
|
||||
path: game_path,
|
||||
compactionStrategy: (entries, deletedEntries) {
|
||||
return deletedEntries > 1;
|
||||
},
|
||||
);
|
||||
Instance.Read();
|
||||
}
|
||||
|
||||
static void Close() {
|
||||
if (Hive.isBoxOpen("settings")) {
|
||||
var box = Hive.box("settings");
|
||||
box.compact();
|
||||
box.close();
|
||||
static void Close() async {
|
||||
if (Instance.isValid()) {
|
||||
Instance.Write();
|
||||
Instance.inst = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue