82 lines
2.9 KiB
C#
82 lines
2.9 KiB
C#
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using CliWrap;
|
||
using DTLib.Extensions;
|
||
using Newtonsoft.Json;
|
||
using Млаумчерб.Клиент.классы;
|
||
|
||
namespace Млаумчерб.Клиент;
|
||
|
||
public interface IGame
|
||
{
|
||
string Name { get; }
|
||
IOPath InstallationDirectory { get; }
|
||
Progress<NetworkTransferResult> BeginUpdate();
|
||
void CancelUpdate();
|
||
Task Launch();
|
||
void Close();
|
||
}
|
||
|
||
public class MinecraftVersion : IGame
|
||
{
|
||
public string Name { get; }
|
||
public IOPath InstallationDirectory { get; }
|
||
|
||
private IOPath JavaExecutableFilePath;
|
||
|
||
private MinecraftVersionDescriptor descriptor;
|
||
private JavaArguments javaArgs;
|
||
private GameArguments gameArgs;
|
||
private CancellationTokenSource? cts;
|
||
private CommandTask<CommandResult>? commandTask;
|
||
|
||
public MinecraftVersion(IOPath descriptorFilePath)
|
||
{
|
||
Name = descriptorFilePath.LastName().ToString().Replace(".json", "");
|
||
InstallationDirectory = Path.Concat(Приложение.Настройки.путь_к_кубачу, Name);
|
||
string descriptorText = File.ReadAllText(descriptorFilePath);
|
||
descriptor = JsonConvert.DeserializeObject<MinecraftVersionDescriptor>(descriptorText)
|
||
?? throw new Exception($"can't parse descriptor file '{descriptorFilePath}'");
|
||
javaArgs = new JavaArguments(descriptor);
|
||
gameArgs = new GameArguments(descriptor);
|
||
JavaExecutableFilePath = Path.Concat(Приложение.Настройки.путь_к_жабе, "bin",
|
||
OperatingSystem.IsWindows() ? "javaw.exe" : "javaw");
|
||
}
|
||
|
||
public Progress<NetworkTransferResult> BeginUpdate()
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public void CancelUpdate()
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public async Task Launch()
|
||
{
|
||
var javaArgsList = javaArgs.FillPlaceholders([]);
|
||
var gameArgsList = gameArgs.FillPlaceholders([]);
|
||
var command = Cli.Wrap(JavaExecutableFilePath.ToString())
|
||
.WithWorkingDirectory(InstallationDirectory.ToString())
|
||
.WithArguments(javaArgsList)
|
||
.WithArguments(gameArgsList);
|
||
Приложение.Логгер.LogInfo(nameof(MinecraftVersion),
|
||
$"launching the game" +
|
||
"\njava: " + command.TargetFilePath +
|
||
"\nworking_dir: " + command.WorkingDirPath +
|
||
"\njava_arguments: \n\t" + javaArgsList.MergeToString("\n\t") +
|
||
"\ngame_arguments: \n\t" + gameArgsList.MergeToString("\n\t"));
|
||
cts = new();
|
||
commandTask = command.ExecuteAsync(cts.Token);
|
||
var result = await commandTask;
|
||
Приложение.Логгер.LogInfo(nameof(MinecraftVersion), $"game exited with code {result.ExitCode}");
|
||
}
|
||
|
||
public void Close()
|
||
{
|
||
cts?.Cancel();
|
||
}
|
||
|
||
public override string ToString() => Name;
|
||
} |