ConanServerManager/lib/structs/settings.dart

264 lines
6.8 KiB
Dart

import 'dart:io';
import 'package:archive/archive.dart';
import 'package:dio/dio.dart';
import 'package:libac_flutter/nbt/NbtIo.dart';
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
import 'package:libac_flutter/packets/packets.dart';
import 'package:libac_flutter/utils/IOTools.dart';
import 'package:libac_flutter/utils/uuid/UUID.dart';
import 'package:servermanager/statemachine.dart';
import 'package:servermanager/structs/credentials.dart';
import 'package:servermanager/structs/mod.dart';
import 'package:servermanager/structs/settingsEntry.dart';
class Settings {
final String windows =
"https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip";
final String linux =
"https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz";
final String Base2FAPath =
"https://github.com/zontreck/steamcmd-2fa/releases/download/0.2.0/steamcmd-2fa";
Settings._();
static final Settings Instance = Settings._();
String steamcmd_path = "";
String game_path = "";
Credentials serverLoginCreds = Credentials(
username: "admin", password: "changeMe123", secret: "", has_2fa: false);
UUID remoteLoginToken = UUID.ZERO;
PacketClient? client;
StateMachine subsys = StateMachine();
factory Settings() {
return Instance;
}
SettingsEntry? inst;
Future<void> Read() async {
try {
var tag = await NbtIo.read("settings.dat");
inst = SettingsEntry.deserialize(tag.get("entry") as CompoundTag);
serverLoginCreds = Credentials.deserialize(
tag.get(Credentials.TAG_NAME)!.asCompoundTag());
} catch (E) {
inst = SettingsEntry();
}
}
void Write() {
if (!isValid()) return;
CompoundTag tag = CompoundTag();
tag.put("entry", inst!.serialize());
tag.put(Credentials.TAG_NAME, serverLoginCreds.save());
NbtIo.write("settings.dat", tag);
}
bool isValid() {
if (inst == null) {
return false;
} else {
return true;
}
}
Future<void> Open() async {
Close();
Instance.Read();
}
static void Close() async {
if (Instance.isValid()) {
Instance.Write();
Instance.inst = null;
}
}
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({bool valid = true}) {
return Process.run(getSteamCmd(), [
"+@sSteamCmdForcePlatformType",
"windows",
"+force_install_dir",
getServerPath(),
"+login",
"anonymous",
"+app_update",
"443030",
"public",
if (valid) "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("content")
.resolve("440900")
.resolve("${mod.mod_id}")
.resolve("${mod.mod_pak}")
.build();
if (Platform.isWindows) {
paths.add(pth);
} else {
var rpl = []; // proton's
rpl.addAll(pth.split('/'));
rpl[0] = "Z:";
paths.add(rpl.join("\\"));
}
}
await file.writeAsString(paths.join("\n"),
flush: true, mode: FileMode.writeOnly);
}
Future<void> initializeSteamCmd() async {
// Yes, Proceed
var x = Directory(steamcmd_path);
try {
await x.delete(recursive: true);
} catch (e) {}
await x.create(recursive: true);
Directory.current = Directory(steamcmd_path);
final dio = Dio();
Process proc;
if (Platform.isWindows) {
// Download zip file
final path = "${steamcmd_path}${Platform.pathSeparator}windows.zip";
final reply = await dio.download(windows, path);
final bytes = File(path).readAsBytesSync();
final arc = ZipDecoder().decodeBytes(bytes);
for (final file in arc) {
final name = file.name;
if (file.isFile) {
final data = file.content as List<int>;
File(name)
..createSync(recursive: true)
..writeAsBytesSync(data);
} else {
Directory(name).create(recursive: true);
}
}
Process.runSync("echo", ["X", ">", "cxinit"]);
proc = await Process.start("steamcmd.exe", ["+quit"]);
} else {
// Download tgz file
final path = "${steamcmd_path}${Platform.pathSeparator}linux.tgz";
final reply = await dio.download(linux, path);
final bytes = File(path).readAsBytesSync();
final arc = GZipDecoder().decodeBytes(bytes);
final arc2 = TarDecoder().decodeBytes(arc);
for (final file in arc2) {
final name = file.name;
if (file.isFile) {
final data = file.content as List<int>;
File(name)
..createSync(recursive: true)
..writeAsBytesSync(data);
} else {
Directory(name).create(recursive: true);
}
}
Process.runSync("chmod", ["+x", "steamcmd.sh"]);
Process.runSync("chmod", ["+x", "linux32/steamcmd"]);
Process.runSync("touch", ["cxinit"]);
proc = await Process.start("./steamcmd.sh", ["+quit"]);
}
Directory.current = Directory(game_path);
}
}