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
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<GameServerPage> {
"+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<ModManager> {
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<ModManager> {
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<ModManager> {
if (reply != null) {
Mod mod = reply as Mod;
setState(() {
settings.mods.add(mod);
settings.inst!.mods.add(mod);
settings.Write();
});
}

View file

@ -61,18 +61,18 @@ class HomePageState extends State<HomePage> {
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<HomePage> {
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);
}
});

View file

@ -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<void> main() async {
await Hive.initFlutter();
Hive.registerAdapter(CredentialsAdapter());
Hive.registerAdapter(ModAdapter());
Hive.registerAdapter(SettingsEntryAdapter());
runApp(MyApp());
}

View file

@ -14,7 +14,7 @@ class Proton extends StatefulWidget {
Future<void> runProton(String command, List<String> 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);

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 {

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) {},
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<SteamCMD> {
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<SteamCMD> {
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<SteamCMD> {
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<SteamCMD> {
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<SteamCMD> {
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();
});
}