ConanServerManager/lib/settings.dart
2023-11-05 00:03:15 -07:00

178 lines
4 KiB
Dart

import 'dart:io';
import 'package:hive/hive.dart';
import 'package:servermanager/mod.dart';
import 'package:servermanager/pathtools.dart';
import 'package:servermanager/settingsEntry.dart';
import 'package:servermanager/statemachine.dart';
class Settings {
Settings._();
static final Settings Instance = Settings._();
String steamcmd_path = "";
String game_path = "";
StateMachine subsys = StateMachine();
factory Settings() {
return Instance;
}
SettingsEntry? inst;
void Read() {
if (!isValid()) return;
var box = Hive.box("settings");
inst = box.get("entry", defaultValue: SettingsEntry()) as SettingsEntry;
}
void Write() {
if (!isValid()) return;
var box = Hive.box("settings");
box.put("entry", inst);
box.compact();
}
bool isValid() {
if (!Hive.isBoxOpen("settings")) {
return false;
} else {
return true;
}
}
Future<Box<E>> Open<E>() {
Close();
return Hive.openBox(
"settings",
path: game_path,
compactionStrategy: (entries, deletedEntries) {
return deletedEntries > 1;
},
);
}
static void Close() {
if (Hive.isBoxOpen("settings")) {
var box = Hive.box("settings");
box.compact();
box.close();
}
}
String getServerPath() {
return "$game_path${Platform.pathSeparator}server";
}
bool checkInitDone() {
if (File("$steamcmd_path${Platform.pathSeparator}cxinit").existsSync()) {
return true;
} else {
return false;
}
}
String getSteamCmd() {
return "$steamcmd_path${Platform.pathSeparator}steamcmd${Platform.isWindows ? ".exe" : ".sh"}";
}
String getSteamCmd2FA() {
return "$steamcmd_path${Platform.pathSeparator}steamcmd-2fa${Platform.isWindows ? ".exe" : ""}";
}
String getModPath() {
return "$game_path${Platform.pathSeparator}mods";
}
String getModJailPath() {
return "$game_path${Platform.pathSeparator}mods.jail";
}
Future<void> createModFolderIfNotExists() async {
if (Directory(getModPath()).existsSync()) {
return;
} else {
await Directory(getModPath()).create(recursive: true);
}
}
Future<void> createModJailFolderIfNotExists() async {
if (Directory(getModJailPath()).existsSync()) {
return;
} else {
await Directory(getModJailPath()).create(recursive: true);
}
}
bool serverInstalled() {
return File(
"${getServerPath()}${Platform.pathSeparator}ConanSandboxServer.exe")
.existsSync();
}
Future<ProcessResult> RunUpdate() {
return Process.run(getSteamCmd(), [
"+@sSteamCmdForcePlatformType",
"windows",
"+force_install_dir",
getServerPath(),
"+login",
"anonymous",
"+app_update",
"443030",
"public",
"validate",
"+quit"
]);
}
Future<void> createServerModFolderIfNotExists() async {
Directory dir = Directory(PathHelper(pth: getServerPath())
.resolve("ConanSandbox")
.resolve("Mods")
.build());
if (await dir.exists()) {
return;
} else
await dir.create(recursive: true);
}
File getModListFile() {
return File(PathHelper(pth: getServerPath())
.resolve("ConanSandbox")
.resolve("Mods")
.resolve("modlist.txt")
.build());
}
Future<void> writeOutModListFile() async {
await createServerModFolderIfNotExists();
var file = getModListFile();
List<String> paths = [];
for (Mod mod in inst!.mods) {
var pth = PathHelper(pth: getModPath())
.resolve("steamapps")
.resolve("workshop")
.resolve("440900")
.resolve("${mod.mod_id}")
.resolve("${mod.mod_pak}")
.build();
//if (Platform.isWindows) {
paths.add(pth);
//} else {
// var rpl = ["Z:"]; // proton's
// rpl.addAll(pth.split('/'));
//
// paths.add(rpl.join("\\"));
// }
}
await file.writeAsString(paths.join("\n"),
flush: true, mode: FileMode.writeOnly);
}
}