127 lines
3.3 KiB
Dart
127 lines
3.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:libac_dart/utils/IOTools.dart';
|
|
import 'package:servermanager/statemachine.dart';
|
|
import 'package:servermanager/structs/settings.dart';
|
|
|
|
// A patch to allow windows to run software using the below methods.
|
|
Future<bool> requiresWine() async {
|
|
if (Platform.isWindows) return false;
|
|
return true;
|
|
}
|
|
|
|
Future<void> runWine(String command, List<String> argx) async {
|
|
bool requiredWine = await requiresWine();
|
|
|
|
Settings settings = Settings();
|
|
Directory dir = Directory(settings.getWinePrefixPath());
|
|
|
|
if (dir.existsSync()) {
|
|
await dir.delete(recursive: true);
|
|
}
|
|
await dir.create(recursive: true);
|
|
|
|
Map<String, String> env = Map.from(Platform.environment);
|
|
env["WINEPREFIX"] = dir.path;
|
|
|
|
try {
|
|
List<String> args = [command];
|
|
args.addAll(argx);
|
|
|
|
ProcessResult? res;
|
|
if (requiredWine) {
|
|
res = await Process.run(
|
|
"wine",
|
|
args, // Run arbitrary command with arguments
|
|
environment: env,
|
|
);
|
|
} else {
|
|
res = await Process.run("cmd", ["/C"] + args, environment: {});
|
|
}
|
|
|
|
print('Exit code: ${res.exitCode}');
|
|
print('stdout: ${res.stdout}');
|
|
print('stderr: ${res.stderr}');
|
|
} catch (e) {
|
|
print('Error executing command: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> runWindows(String command, List<String> args) async {
|
|
runDetachedWine(command, args, Settings.Instance.getServerPath());
|
|
}
|
|
|
|
Future<void> runWinetrick(String trick) async {
|
|
if (!(await requiresWine())) return;
|
|
|
|
Settings settings = Settings();
|
|
Directory dir = Directory(settings.getWinePrefixPath());
|
|
|
|
if (dir.existsSync()) {
|
|
await dir.delete(recursive: true);
|
|
}
|
|
await dir.create(recursive: true);
|
|
|
|
Map<String, String> env = Map.from(Platform.environment);
|
|
//env["WINEPREFIX"] = dir.path;
|
|
|
|
try {
|
|
List<String> args = ["-q", trick];
|
|
|
|
ProcessResult res = await Process.run(
|
|
"winetricks",
|
|
args, // Run arbitrary command with arguments
|
|
environment: env,
|
|
);
|
|
|
|
print('Exit code: ${res.exitCode}');
|
|
print('stdout: ${res.stdout}');
|
|
print('stderr: ${res.stderr}');
|
|
} catch (e) {
|
|
print('Error executing command: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> runDetachedWine(
|
|
String command, List<String> argx, String workingDir) async {
|
|
bool requiredWine = await requiresWine();
|
|
|
|
Settings settings = Settings();
|
|
Directory dir =
|
|
Directory(PathHelper.builder(settings.base_path).resolve("pfx").build());
|
|
|
|
if (dir.existsSync()) {
|
|
await dir.delete(recursive: true);
|
|
}
|
|
await dir.create(recursive: true);
|
|
|
|
Map<String, String> env = Map.from(Platform.environment);
|
|
//env["WINEPREFIX"] = dir.path;
|
|
|
|
try {
|
|
List<String> args = [command];
|
|
args.addAll(argx);
|
|
|
|
if (requiredWine) {
|
|
StateMachine.PROC = await Process.start(
|
|
"wine", args, // Run arbitrary command with arguments
|
|
environment: env,
|
|
workingDirectory: workingDir,
|
|
mode: ProcessStartMode.detachedWithStdio);
|
|
} else {
|
|
StateMachine.PROC = await Process.start("cmd", ["/C"] + args,
|
|
environment: {},
|
|
workingDirectory: workingDir,
|
|
mode: ProcessStartMode.detachedWithStdio);
|
|
}
|
|
|
|
try {
|
|
StateMachine.PROC!.stdout.forEach((line) {});
|
|
StateMachine.PROC!.stderr.forEach((line) {});
|
|
} catch (E) {}
|
|
|
|
StateMachine.monitorProcess();
|
|
} catch (e) {
|
|
print('Error executing command: $e');
|
|
}
|
|
}
|