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

@ -103,8 +103,11 @@ class GameServerPageState extends State<GameServerPage> {
}); });
// Now, invoke SteamCmd to download the workshop mods. This is an authenticated action, and does require Scmd2fa // Now, invoke SteamCmd to download the workshop mods. This is an authenticated action, and does require Scmd2fa
var result = await Process.run(settings.getSteamCmd2FA(), var result = await Process.run(settings.getSteamCmd2FA(), [
["--raw", "--secret", settings.steam_creds!.secret]); "--raw",
"--secret",
settings.inst!.steam_creds!.secret
]);
var code = result.stdout; var code = result.stdout;
// Build download command // Build download command
@ -114,11 +117,11 @@ class GameServerPageState extends State<GameServerPage> {
"+force_install_dir", "+force_install_dir",
settings.getModPath(), settings.getModPath(),
"+login", "+login",
settings.steam_creds!.username, settings.inst!.steam_creds!.username,
settings.steam_creds!.password, settings.inst!.steam_creds!.password,
code code
]; ];
for (Mod M in settings.mods) { for (Mod M in settings.inst!.mods) {
manifest.add("+workshop_download_item"); manifest.add("+workshop_download_item");
manifest.add("${M.mod_id}"); manifest.add("${M.mod_id}");
} }
@ -173,28 +176,28 @@ class ModManagerState extends State<ModManager> {
if (oldIndex < newIndex) { if (oldIndex < newIndex) {
// From top to Bottom // From top to Bottom
int end = newIndex - 1; int end = newIndex - 1;
Mod item = settings.mods[oldIndex]; Mod item = settings.inst!.mods[oldIndex];
int i = 0; int i = 0;
int local = oldIndex; int local = oldIndex;
do { do {
settings.mods[local] = settings.mods[++local]; settings.inst!.mods[local] = settings.inst!.mods[++local];
i++; i++;
} while (i < end - oldIndex); } while (i < end - oldIndex);
settings.mods[end] = item; settings.inst!.mods[end] = item;
} else if (oldIndex > newIndex) { } else if (oldIndex > newIndex) {
//From bottom to top //From bottom to top
Mod item = settings.mods[oldIndex]; Mod item = settings.inst!.mods[oldIndex];
for (int i = oldIndex; i > newIndex; i--) { for (int i = oldIndex; i > newIndex; i--) {
settings.mods[i] = settings.mods[i - 1]; settings.inst!.mods[i] = settings.inst!.mods[i - 1];
} }
settings.mods[newIndex] = item; settings.inst!.mods[newIndex] = item;
} }
setState(() { setState(() {
settings.Write(); settings.Write();
}); });
}, },
itemBuilder: (ctx, idx) { itemBuilder: (ctx, idx) {
Mod mod = settings.mods[idx]; Mod mod = settings.inst!.mods[idx];
return Padding( return Padding(
key: Key("${mod.mod_instance_id()}"), key: Key("${mod.mod_instance_id()}"),
padding: EdgeInsets.all(12), padding: EdgeInsets.all(12),
@ -211,17 +214,17 @@ class ModManagerState extends State<ModManager> {
if (reply != null) if (reply != null)
setState(() { setState(() {
settings.mods[idx] = reply as Mod; settings.inst!.mods[idx] = reply as Mod;
}); });
else else
setState(() { setState(() {
settings.mods.removeAt(idx); settings.inst!.mods.removeAt(idx);
}); });
}, },
), ),
); );
}, },
itemCount: settings.mods.length, itemCount: settings.inst!.mods.length,
), ),
onWillPop: () async { onWillPop: () async {
Navigator.pop(context); Navigator.pop(context);
@ -238,7 +241,7 @@ class ModManagerState extends State<ModManager> {
if (reply != null) { if (reply != null) {
Mod mod = reply as Mod; Mod mod = reply as Mod;
setState(() { setState(() {
settings.mods.add(mod); settings.inst!.mods.add(mod);
settings.Write(); settings.Write();
}); });
} }

View file

@ -61,18 +61,18 @@ class HomePageState extends State<HomePage> {
leading: Icon(CupertinoIcons.gear), leading: Icon(CupertinoIcons.gear),
subtitle: Text("Linux Proton"), subtitle: Text("Linux Proton"),
onTap: () { onTap: () {
if (settings.steamcmd_path.isNotEmpty) if (settings.inst!.steamcmd_path.isNotEmpty)
Navigator.pushNamed(context, "/proton"); Navigator.pushNamed(context, "/proton");
}, },
), // Not yet implemented ), // Not yet implemented
ListTile( ListTile(
title: Text("SteamCMD"), title: Text("SteamCMD"),
leading: Icon(Icons.comment_rounded), leading: Icon(Icons.comment_rounded),
subtitle: settings.steamcmd_path.isEmpty subtitle: settings.inst!.steamcmd_path.isEmpty
? Text("Path Not Set") ? Text("Path Not Set")
: Text(settings.steamcmd_path), : Text(settings.inst!.steamcmd_path),
onTap: () { onTap: () {
if (settings.steamcmd_path.isNotEmpty) { if (settings.inst!.steamcmd_path.isNotEmpty) {
Navigator.pushNamed(context, "/steamcmd"); Navigator.pushNamed(context, "/steamcmd");
} }
}, },
@ -80,18 +80,18 @@ class HomePageState extends State<HomePage> {
ListTile( ListTile(
title: Text("Server Path"), title: Text("Server Path"),
leading: Icon(CupertinoIcons.folder), leading: Icon(CupertinoIcons.folder),
subtitle: settings.game_path.isEmpty subtitle: settings.inst!.game_path.isEmpty
? Text("Not Set") ? Text("Not Set")
: Text(settings.game_path), : Text(settings.inst!.game_path),
onTap: () async { onTap: () async {
var path = await getDirectoryPath(); var path = await getDirectoryPath();
setState(() { setState(() {
if (path != null && path.isNotEmpty) { if (path != null && path.isNotEmpty) {
settings.game_path = path; settings.inst!.game_path = path;
settings.steamcmd_path = settings.inst!.steamcmd_path =
path + Platform.pathSeparator + "scmd"; path + Platform.pathSeparator + "scmd";
Directory.current = Directory(settings.game_path); Directory.current = Directory(settings.inst!.game_path);
} }
}); });

View file

@ -6,12 +6,14 @@ import 'package:servermanager/home.dart';
import 'package:servermanager/mod.dart'; import 'package:servermanager/mod.dart';
import 'package:servermanager/proton.dart'; import 'package:servermanager/proton.dart';
import 'package:servermanager/settings.dart'; import 'package:servermanager/settings.dart';
import 'package:servermanager/settingsEntry.dart';
import 'package:servermanager/steamcmd.dart'; import 'package:servermanager/steamcmd.dart';
Future<void> main() async { Future<void> main() async {
await Hive.initFlutter(); await Hive.initFlutter();
Hive.registerAdapter(CredentialsAdapter()); Hive.registerAdapter(CredentialsAdapter());
Hive.registerAdapter(ModAdapter()); Hive.registerAdapter(ModAdapter());
Hive.registerAdapter(SettingsEntryAdapter());
runApp(MyApp()); runApp(MyApp());
} }

View file

@ -14,7 +14,7 @@ class Proton extends StatefulWidget {
Future<void> runProton(String command, List<String> argx) async { Future<void> runProton(String command, List<String> argx) async {
Settings settings = Settings(); Settings settings = Settings();
Directory dir = Directory dir =
Directory(settings.game_path + Platform.pathSeparator + "pfx"); Directory(settings.inst!.game_path + Platform.pathSeparator + "pfx");
if (dir.existsSync()) { if (dir.existsSync()) {
await dir.delete(recursive: true); await dir.delete(recursive: true);

View file

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

20
lib/settingsEntry.dart Normal file
View file

@ -0,0 +1,20 @@
import 'package:hive/hive.dart';
import 'package:servermanager/credentials.dart';
import 'package:servermanager/mod.dart';
part 'settingsEntry.g.dart';
@HiveType(typeId: 0)
class SettingsEntry {
@HiveField(0)
List<Mod> mods = [];
@HiveField(1)
String steamcmd_path = "";
@HiveField(2)
String game_path = "";
@HiveField(3)
Credentials? steam_creds;
}

View file

@ -54,21 +54,22 @@ class SteamCMDState extends State<SteamCMD> {
changed: (X) {}, changed: (X) {},
onSubmit: () async { onSubmit: () async {
// Yes, Proceed // Yes, Proceed
var x = Directory(settings.steamcmd_path); var x = Directory(settings.inst!.steamcmd_path);
try { try {
await x.delete(recursive: true); await x.delete(recursive: true);
} catch (e) {} } catch (e) {}
await x.create(recursive: true); await x.create(recursive: true);
Directory.current = Directory(settings.steamcmd_path); Directory.current =
Directory(settings.inst!.steamcmd_path);
final dio = Dio(); final dio = Dio();
Process proc; Process proc;
if (Platform.isWindows) { if (Platform.isWindows) {
// Download zip file // Download zip file
final path = settings.steamcmd_path + final path = settings.inst!.steamcmd_path +
Platform.pathSeparator + Platform.pathSeparator +
"windows.zip"; "windows.zip";
final reply = await dio.download(windows, path); final reply = await dio.download(windows, path);
@ -94,7 +95,7 @@ class SteamCMDState extends State<SteamCMD> {
await Process.start("steamcmd.exe", ["+quit"]); await Process.start("steamcmd.exe", ["+quit"]);
} else { } else {
// Download tgz file // Download tgz file
final path = settings.steamcmd_path + final path = settings.inst!.steamcmd_path +
Platform.pathSeparator + Platform.pathSeparator +
"linux.tgz"; "linux.tgz";
final reply = await dio.download(linux, path); final reply = await dio.download(linux, path);
@ -123,7 +124,8 @@ class SteamCMDState extends State<SteamCMD> {
await Process.start("./steamcmd.sh", ["+quit"]); await Process.start("./steamcmd.sh", ["+quit"]);
} }
Directory.current = Directory(settings.game_path); Directory.current =
Directory(settings.inst!.game_path);
}, },
onCancel: () {}, onCancel: () {},
isDefault: false, isDefault: false,
@ -140,7 +142,7 @@ class SteamCMDState extends State<SteamCMD> {
final dio = Dio(); final dio = Dio();
await dio.download( await dio.download(
Base2FAPath + (Platform.isWindows ? ".exe" : ""), Base2FAPath + (Platform.isWindows ? ".exe" : ""),
settings.steamcmd_path + settings.inst!.steamcmd_path +
Platform.pathSeparator + Platform.pathSeparator +
(Platform.isWindows (Platform.isWindows
? "steamcmd-2fa.exe" ? "steamcmd-2fa.exe"
@ -148,7 +150,7 @@ class SteamCMDState extends State<SteamCMD> {
if (!Platform.isWindows) if (!Platform.isWindows)
var proc = await Process.start("chmod", [ var proc = await Process.start("chmod", [
"+x", "+x",
settings.steamcmd_path + settings.inst!.steamcmd_path +
Platform.pathSeparator + Platform.pathSeparator +
"steamcmd-2fa" "steamcmd-2fa"
]); ]);
@ -159,11 +161,11 @@ class SteamCMDState extends State<SteamCMD> {
subtitle: Text("Steam Credentials"), subtitle: Text("Steam Credentials"),
onTap: () async { onTap: () async {
var creds = await Navigator.pushNamed(context, "/steamcmd/creds", var creds = await Navigator.pushNamed(context, "/steamcmd/creds",
arguments: settings.steam_creds); arguments: settings.inst!.steam_creds);
if (creds != null) { if (creds != null) {
Credentials cred = creds as Credentials; Credentials cred = creds as Credentials;
setState(() { setState(() {
settings.steam_creds = cred; settings.inst!.steam_creds = cred;
settings.Write(); settings.Write();
}); });
} }