arguments parsing

This commit is contained in:
Timerix 2024-09-14 14:36:23 +05:00
parent 1ecc8ccc18
commit bf33e186f8
8 changed files with 186 additions and 16 deletions

View File

@ -1,7 +1,4 @@
using DTLib.Filesystem;
using DTLib.Logging;
namespace Млаумчерб.Клиент;
namespace Млаумчерб.Клиент;
public class LauncherLogger : FileLogger
{

View File

@ -6,6 +6,7 @@ global using DTLib.Logging;
global using DTLib.Filesystem;
global using File = DTLib.Filesystem.File;
global using Directory = DTLib.Filesystem.Directory;
global using Path = DTLib.Filesystem.Path;
using System.Globalization;
using Avalonia;

View File

@ -1,6 +1,8 @@
using System.Threading.Tasks;
using DTLib.Filesystem;
using Path = DTLib.Filesystem.Path;
using System.Threading;
using System.Threading.Tasks;
using CliWrap;
using DTLib.Extensions;
using Млаумчерб.Клиент.классы;
namespace Млаумчерб.Клиент;
@ -11,6 +13,7 @@ public interface IGame
Progress<NetworkTransferResult> BeginUpdate();
void EndUpdate();
Task Launch();
void Close();
}
public class MinecraftVersion : IGame
@ -18,6 +21,13 @@ public class MinecraftVersion : IGame
public string Name { get; }
public IOPath InstallationDirectory { get; }
private IOPath JavawFilePath;
private JavaArguments javaArgs;
private GameArguments gameArgs;
private CancellationTokenSource? cts;
private CommandTask<CommandResult> commandTask;
public MinecraftVersion(string name)
{
Name = name;
@ -34,8 +44,28 @@ public class MinecraftVersion : IGame
throw new NotImplementedException();
}
public Task Launch()
public async Task Launch()
{
throw new NotImplementedException();
var javaArgsList = javaArgs.FillPlaceholders([]);
var gameArgsList = gameArgs.FillPlaceholders([]);
var command = Cli.Wrap(JavawFilePath.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();
}
}

View File

@ -18,6 +18,7 @@
<PackageReference Include="Avalonia.Themes.Simple" Version="11.*" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.*" />
<PackageReference Include="AvaloniaGif-Unofficial" Version="1.0.0" />
<PackageReference Include="CliWrap" Version="3.6.6" />
<PackageReference Include="DTLib" Version="1.3.*" />
<PackageReference Include="DTLib.Ben.Demystifier" Version="1.0.*" />
<PackageReference Include="DTLib.Logging" Version="1.3.*" />

View File

@ -1,5 +1,4 @@
using DTLib.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json;
namespace Млаумчерб.Клиент;

View File

@ -1,6 +1,5 @@
using Avalonia.Controls;
using DTLib.Ben.Demystifier;
using DTLib.Logging;
using MsBox.Avalonia;
using MsBox.Avalonia.Dto;
using MsBox.Avalonia.Enums;

View File

@ -17,7 +17,8 @@ public class Os
public class Rule
{
[JsonRequired] public string action { get; set; } = "";
[JsonRequired] public Os os { get; set; } = null!;
public Os? os { get; set; }
public Dictionary<string, bool>? features { get; set; }
}
public class Classifiers
@ -80,6 +81,18 @@ public class JavaVersion
[JsonRequired] public int majorVersion { get; set; }
}
public class ArgValue
{
[JsonRequired] public string value { get; set; } = "";
public List<Rule> rules { get; set; } = new();
}
public class ArgumentsNew
{
[JsonRequired] public List<ArgValue> jvm { get; set; } = new();
[JsonRequired] public List<ArgValue> game { get; set; } = new();
}
public class MinecraftVersionDescriptor
{
[JsonRequired] public string id { get; set; } = "";
@ -94,5 +107,52 @@ public class MinecraftVersionDescriptor
[JsonRequired] public List<Library> libraries { get; set; } = null!;
[JsonRequired] public AssetIndex assetIndex { get; set; } = null!;
[JsonRequired] public string assets { get; set; } = "";
// public string minecraftArguments { get; set; }
public string? minecraftArguments { get; set; }
public ArgumentsNew? arguments { get; set; }
}
public static class Буржуазия
{
public static bool CheckOs(Os os)
{
return os.name switch
{
"osx" => OperatingSystem.IsWindows(),
"linux" => OperatingSystem.IsLinux(),
"windows" => OperatingSystem.IsWindows(),
_ => throw new ArgumentOutOfRangeException(os.name)
};
}
public static bool CheckRules(ICollection<Rule> rules, ICollection<string> enabled_features)
{
bool allowed = false;
foreach (var r in rules)
{
if (r.os == null || CheckOs(r.os))
{
if (r.features != null)
{
foreach (var feature in enabled_features)
{
if(r.features.TryGetValue(feature, out bool is_enabled))
{
if (is_enabled)
{
if (r.action == "allow")
allowed = true;
else return false;
}
}
}
}
if (r.action == "allow")
allowed = true;
else return false;
}
}
return allowed;
}
}

View File

@ -1,6 +1,89 @@
namespace Млаумчерб.Клиент.классы;
using DTLib.Extensions;
namespace Млаумчерб.Клиент.классы;
public class Пролетариат
{
}
public class ArgumentsWithPlaceholders
{
protected List<string> raw_args = new();
public List<string> FillPlaceholders(Dictionary<string, string> values)
{
List<string> result = new();
foreach (var a in raw_args)
{
var f = a;
int begin = a.IndexOf('$');
if (begin != -1)
{
int keyBegin = begin + 2;
int end = a.IndexOf('}', keyBegin);
if (end != -1)
{
var key = a.Substring(keyBegin, end - keyBegin);
if (!values.TryGetValue(key, out var v))
throw new Exception($"can't find value for placeholder '{key}'");
f = v;
}
}
result.Add(f);
}
return result;
}
}
public class JavaArguments : ArgumentsWithPlaceholders
{
private static readonly string[] _initial_arguments =
[
];
private static readonly string[] _enabled_features =
[
];
public JavaArguments(MinecraftVersionDescriptor d)
{
raw_args.AddRange(_initial_arguments);
if (d.arguments is not null)
{
foreach (var av in d.arguments.jvm)
{
if(Буржуазия.CheckRules(av.rules, _enabled_features))
raw_args.Add(av.value);
}
}
}
}
public class GameArguments : ArgumentsWithPlaceholders
{
private static readonly string[] _enabled_features =
[
"has_custom_resolution"
];
public GameArguments(MinecraftVersionDescriptor d)
{
if (d.minecraftArguments is not null)
{
raw_args.AddRange(d.minecraftArguments.SplitToList(' ', quot: '"'));
}
else if (d.arguments is not null)
{
foreach (var av in d.arguments.game)
{
if(Буржуазия.CheckRules(av.rules, _enabled_features))
raw_args.Add(av.value);
}
}
}
}