arguments parsing
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user