From d03613b0c7d2be503e98bf96d3693ea6150e9c50 Mon Sep 17 00:00:00 2001 From: Zontreck Date: Fri, 3 Nov 2023 14:17:07 -0700 Subject: [PATCH] Migrate settings into its own special entry to utilize type adapters fully. --- lib/game.dart | 35 +++++++++++++++++++---------------- lib/home.dart | 18 +++++++++--------- lib/main.dart | 2 ++ lib/proton.dart | 2 +- lib/settings.dart | 35 ++++++++++++----------------------- lib/settingsEntry.dart | 20 ++++++++++++++++++++ lib/steamcmd.dart | 20 +++++++++++--------- 7 files changed, 74 insertions(+), 58 deletions(-) create mode 100644 lib/settingsEntry.dart diff --git a/lib/game.dart b/lib/game.dart index 9ba796a..c0ffb03 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -103,8 +103,11 @@ class GameServerPageState extends State { }); // Now, invoke SteamCmd to download the workshop mods. This is an authenticated action, and does require Scmd2fa - var result = await Process.run(settings.getSteamCmd2FA(), - ["--raw", "--secret", settings.steam_creds!.secret]); + var result = await Process.run(settings.getSteamCmd2FA(), [ + "--raw", + "--secret", + settings.inst!.steam_creds!.secret + ]); var code = result.stdout; // Build download command @@ -114,11 +117,11 @@ class GameServerPageState extends State { "+force_install_dir", settings.getModPath(), "+login", - settings.steam_creds!.username, - settings.steam_creds!.password, + settings.inst!.steam_creds!.username, + settings.inst!.steam_creds!.password, code ]; - for (Mod M in settings.mods) { + for (Mod M in settings.inst!.mods) { manifest.add("+workshop_download_item"); manifest.add("${M.mod_id}"); } @@ -173,28 +176,28 @@ class ModManagerState extends State { if (oldIndex < newIndex) { // From top to Bottom int end = newIndex - 1; - Mod item = settings.mods[oldIndex]; + Mod item = settings.inst!.mods[oldIndex]; int i = 0; int local = oldIndex; do { - settings.mods[local] = settings.mods[++local]; + settings.inst!.mods[local] = settings.inst!.mods[++local]; i++; } while (i < end - oldIndex); - settings.mods[end] = item; + settings.inst!.mods[end] = item; } else if (oldIndex > newIndex) { //From bottom to top - Mod item = settings.mods[oldIndex]; + Mod item = settings.inst!.mods[oldIndex]; 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(() { settings.Write(); }); }, itemBuilder: (ctx, idx) { - Mod mod = settings.mods[idx]; + Mod mod = settings.inst!.mods[idx]; return Padding( key: Key("${mod.mod_instance_id()}"), padding: EdgeInsets.all(12), @@ -211,17 +214,17 @@ class ModManagerState extends State { if (reply != null) setState(() { - settings.mods[idx] = reply as Mod; + settings.inst!.mods[idx] = reply as Mod; }); else setState(() { - settings.mods.removeAt(idx); + settings.inst!.mods.removeAt(idx); }); }, ), ); }, - itemCount: settings.mods.length, + itemCount: settings.inst!.mods.length, ), onWillPop: () async { Navigator.pop(context); @@ -238,7 +241,7 @@ class ModManagerState extends State { if (reply != null) { Mod mod = reply as Mod; setState(() { - settings.mods.add(mod); + settings.inst!.mods.add(mod); settings.Write(); }); } diff --git a/lib/home.dart b/lib/home.dart index 1ebb55f..ccdbcd1 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -61,18 +61,18 @@ class HomePageState extends State { leading: Icon(CupertinoIcons.gear), subtitle: Text("Linux Proton"), onTap: () { - if (settings.steamcmd_path.isNotEmpty) + if (settings.inst!.steamcmd_path.isNotEmpty) Navigator.pushNamed(context, "/proton"); }, ), // Not yet implemented ListTile( title: Text("SteamCMD"), leading: Icon(Icons.comment_rounded), - subtitle: settings.steamcmd_path.isEmpty + subtitle: settings.inst!.steamcmd_path.isEmpty ? Text("Path Not Set") - : Text(settings.steamcmd_path), + : Text(settings.inst!.steamcmd_path), onTap: () { - if (settings.steamcmd_path.isNotEmpty) { + if (settings.inst!.steamcmd_path.isNotEmpty) { Navigator.pushNamed(context, "/steamcmd"); } }, @@ -80,18 +80,18 @@ class HomePageState extends State { ListTile( title: Text("Server Path"), leading: Icon(CupertinoIcons.folder), - subtitle: settings.game_path.isEmpty + subtitle: settings.inst!.game_path.isEmpty ? Text("Not Set") - : Text(settings.game_path), + : Text(settings.inst!.game_path), onTap: () async { var path = await getDirectoryPath(); setState(() { if (path != null && path.isNotEmpty) { - settings.game_path = path; - settings.steamcmd_path = + settings.inst!.game_path = path; + settings.inst!.steamcmd_path = path + Platform.pathSeparator + "scmd"; - Directory.current = Directory(settings.game_path); + Directory.current = Directory(settings.inst!.game_path); } }); diff --git a/lib/main.dart b/lib/main.dart index 384274a..68cf2c7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -6,12 +6,14 @@ import 'package:servermanager/home.dart'; import 'package:servermanager/mod.dart'; import 'package:servermanager/proton.dart'; import 'package:servermanager/settings.dart'; +import 'package:servermanager/settingsEntry.dart'; import 'package:servermanager/steamcmd.dart'; Future main() async { await Hive.initFlutter(); Hive.registerAdapter(CredentialsAdapter()); Hive.registerAdapter(ModAdapter()); + Hive.registerAdapter(SettingsEntryAdapter()); runApp(MyApp()); } diff --git a/lib/proton.dart b/lib/proton.dart index 0a7c1ec..3cf0c6d 100644 --- a/lib/proton.dart +++ b/lib/proton.dart @@ -14,7 +14,7 @@ class Proton extends StatefulWidget { Future runProton(String command, List argx) async { Settings settings = Settings(); Directory dir = - Directory(settings.game_path + Platform.pathSeparator + "pfx"); + Directory(settings.inst!.game_path + Platform.pathSeparator + "pfx"); if (dir.existsSync()) { await dir.delete(recursive: true); diff --git a/lib/settings.dart b/lib/settings.dart index 40a2166..f912553 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -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> Open() { 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 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 createModFolderIfNotExists() async { diff --git a/lib/settingsEntry.dart b/lib/settingsEntry.dart new file mode 100644 index 0000000..9e23d3e --- /dev/null +++ b/lib/settingsEntry.dart @@ -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 mods = []; + + @HiveField(1) + String steamcmd_path = ""; + + @HiveField(2) + String game_path = ""; + + @HiveField(3) + Credentials? steam_creds; +} diff --git a/lib/steamcmd.dart b/lib/steamcmd.dart index e136c3c..b2e912e 100644 --- a/lib/steamcmd.dart +++ b/lib/steamcmd.dart @@ -54,21 +54,22 @@ class SteamCMDState extends State { changed: (X) {}, onSubmit: () async { // Yes, Proceed - var x = Directory(settings.steamcmd_path); + var x = Directory(settings.inst!.steamcmd_path); try { await x.delete(recursive: true); } catch (e) {} await x.create(recursive: true); - Directory.current = Directory(settings.steamcmd_path); + Directory.current = + Directory(settings.inst!.steamcmd_path); final dio = Dio(); Process proc; if (Platform.isWindows) { // Download zip file - final path = settings.steamcmd_path + + final path = settings.inst!.steamcmd_path + Platform.pathSeparator + "windows.zip"; final reply = await dio.download(windows, path); @@ -94,7 +95,7 @@ class SteamCMDState extends State { await Process.start("steamcmd.exe", ["+quit"]); } else { // Download tgz file - final path = settings.steamcmd_path + + final path = settings.inst!.steamcmd_path + Platform.pathSeparator + "linux.tgz"; final reply = await dio.download(linux, path); @@ -123,7 +124,8 @@ class SteamCMDState extends State { await Process.start("./steamcmd.sh", ["+quit"]); } - Directory.current = Directory(settings.game_path); + Directory.current = + Directory(settings.inst!.game_path); }, onCancel: () {}, isDefault: false, @@ -140,7 +142,7 @@ class SteamCMDState extends State { final dio = Dio(); await dio.download( Base2FAPath + (Platform.isWindows ? ".exe" : ""), - settings.steamcmd_path + + settings.inst!.steamcmd_path + Platform.pathSeparator + (Platform.isWindows ? "steamcmd-2fa.exe" @@ -148,7 +150,7 @@ class SteamCMDState extends State { if (!Platform.isWindows) var proc = await Process.start("chmod", [ "+x", - settings.steamcmd_path + + settings.inst!.steamcmd_path + Platform.pathSeparator + "steamcmd-2fa" ]); @@ -159,11 +161,11 @@ class SteamCMDState extends State { subtitle: Text("Steam Credentials"), onTap: () async { var creds = await Navigator.pushNamed(context, "/steamcmd/creds", - arguments: settings.steam_creds); + arguments: settings.inst!.steam_creds); if (creds != null) { Credentials cred = creds as Credentials; setState(() { - settings.steam_creds = cred; + settings.inst!.steam_creds = cred; settings.Write(); }); }