Migrate settings into its own special entry to utilize type adapters fully.

This commit is contained in:
Zontreck 2023-11-03 14:17:07 -07:00
parent 8f28b5a399
commit d03613b0c7
7 changed files with 74 additions and 58 deletions

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:hive/hive.dart';
import 'package:servermanager/credentials.dart';
import 'package:servermanager/mod.dart';
import 'package:servermanager/settingsEntry.dart';
import 'package:servermanager/steamcmd.dart';
class Settings {
@ -13,27 +14,19 @@ class Settings {
return Instance;
}
SettingsEntry? inst;
void Read() {
if (!isValid()) return;
var box = Hive.box("settings");
var ml = box.get("modlist", defaultValue: []);
for (final item in ml) {
try {
Mod X = item as Mod;
mods.add(X);
} catch (E) {}
}
steam_creds = box.get("creds");
inst = box.get("entry") as SettingsEntry;
}
void Write() {
if (!isValid()) return;
var box = Hive.box("settings");
box.put("modlist", mods);
if (steam_creds != null) box.put("creds", steam_creds);
box.put("entry", inst);
}
bool isValid() {
@ -45,7 +38,7 @@ class Settings {
Future<Box<E>> Open<E>() {
Close();
return Hive.openBox("settings", path: game_path);
return Hive.openBox("settings", path: inst!.game_path);
}
static void Close() {
@ -55,37 +48,33 @@ class Settings {
}
String getServerPath() {
return game_path + Platform.pathSeparator + "server";
return inst!.game_path + Platform.pathSeparator + "server";
}
List<Mod> mods = [];
String steamcmd_path = "";
String game_path = "";
Credentials? steam_creds;
bool checkInitDone() {
if (File(steamcmd_path + Platform.pathSeparator + "cxinit").existsSync()) {
if (File(inst!.steamcmd_path + Platform.pathSeparator + "cxinit")
.existsSync()) {
return true;
} else
return false;
}
String getSteamCmd() {
return steamcmd_path +
return inst!.steamcmd_path +
Platform.pathSeparator +
"steamcmd" +
(Platform.isWindows ? ".exe" : ".sh");
}
String getSteamCmd2FA() {
return steamcmd_path +
return inst!.steamcmd_path +
Platform.pathSeparator +
"steamcmd-2fa" +
(Platform.isWindows ? ".exe" : "");
}
String getModPath() {
return game_path + Platform.pathSeparator + "mods";
return inst!.game_path + Platform.pathSeparator + "mods";
}
Future<void> createModFolderIfNotExists() async {