mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
commit
a380f0d59e
Wabbajack.Common
Wabbajack.Lib/Downloaders
Wabbajack
79
Wabbajack.Common/CLI.cs
Normal file
79
Wabbajack.Common/CLI.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Wabbajack.Common
|
||||||
|
{
|
||||||
|
public static class CLIArguments
|
||||||
|
{
|
||||||
|
[CLIOptions("nosettings")]
|
||||||
|
public static bool NoSettings { get; set; }
|
||||||
|
|
||||||
|
[CLIOptions("apikey")]
|
||||||
|
public static string ApiKey { get; set; }
|
||||||
|
|
||||||
|
[CLIOptions("install", ShortOption = 'i')]
|
||||||
|
public static string InstallPath { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class CLI
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Parses the argument and sets the properties of <see cref="CLIArguments"/>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="args"><see cref="Environment.GetCommandLineArgs"/></param>
|
||||||
|
public static void ParseOptions(string[] args)
|
||||||
|
{
|
||||||
|
if (args.Length == 1) return;
|
||||||
|
// get all properties of the class Options
|
||||||
|
typeof(CLIArguments).GetProperties().Do(p =>
|
||||||
|
{
|
||||||
|
var optionAttr = (CLIOptions[])p.GetCustomAttributes(typeof(CLIOptions));
|
||||||
|
if (optionAttr.Length != 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var cur = optionAttr[0];
|
||||||
|
if (cur?.Option == null) return;
|
||||||
|
|
||||||
|
FillVariable(cur.Option, ref p, ref args, false);
|
||||||
|
FillVariable(cur.ShortOption, ref p, ref args, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FillVariable(dynamic option, ref PropertyInfo p, ref string[] args, bool single)
|
||||||
|
{
|
||||||
|
var s = single ? $"-{option}" : $"--{option}";
|
||||||
|
|
||||||
|
if (!args.Any(a => a.Contains(s))) return;
|
||||||
|
|
||||||
|
if (p.PropertyType == typeof(bool))
|
||||||
|
{
|
||||||
|
p.SetValue(p, true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var filtered = args.Where(a => a.Contains(s)).ToList();
|
||||||
|
if (filtered.Count != 1) return;
|
||||||
|
|
||||||
|
var arg = filtered[0];
|
||||||
|
arg = arg.Replace($"{s}=", "");
|
||||||
|
|
||||||
|
if(p.PropertyType == typeof(string))
|
||||||
|
p.SetValue(p, arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
|
public class CLIOptions : Attribute
|
||||||
|
{
|
||||||
|
// --option, long name of the option. Eg: --output
|
||||||
|
public string Option;
|
||||||
|
// -shortOption, short name of the option. Eg: -o
|
||||||
|
public char ShortOption;
|
||||||
|
|
||||||
|
public CLIOptions(string option)
|
||||||
|
{
|
||||||
|
Option = option;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,7 @@ namespace Wabbajack.Common
|
|||||||
{
|
{
|
||||||
{"", "Wabbajack"},
|
{"", "Wabbajack"},
|
||||||
{"FriendlyTypeName", "Wabbajack"},
|
{"FriendlyTypeName", "Wabbajack"},
|
||||||
{"shell\\open\\command", "\"{appPath}\" -i \"%1\""},
|
{"shell\\open\\command", "\"{appPath}\" -i=\"%1\""},
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<string, string> ExtList = new Dictionary<string, string>
|
private static readonly Dictionary<string, string> ExtList = new Dictionary<string, string>
|
||||||
@ -34,7 +34,7 @@ namespace Wabbajack.Common
|
|||||||
var tempKey = progIDKey?.OpenSubKey("shell\\open\\command");
|
var tempKey = progIDKey?.OpenSubKey("shell\\open\\command");
|
||||||
if (progIDKey == null || tempKey == null) return true;
|
if (progIDKey == null || tempKey == null) return true;
|
||||||
var value = tempKey.GetValue("");
|
var value = tempKey.GetValue("");
|
||||||
return value == null || value.ToString().Equals($"\"{appPath}\" -i \"%1\"");
|
return value == null || value.ToString().Equals($"\"{appPath}\" -i=\"%1\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsAssociated()
|
public static bool IsAssociated()
|
||||||
|
@ -35,6 +35,11 @@ namespace Wabbajack.Lib.Downloaders
|
|||||||
|
|
||||||
public NexusDownloader()
|
public NexusDownloader()
|
||||||
{
|
{
|
||||||
|
if (CLIArguments.ApiKey != null)
|
||||||
|
{
|
||||||
|
CLIArguments.ApiKey.ToEcryptedJson("nexusapikey");
|
||||||
|
}
|
||||||
|
|
||||||
TriggerLogin = ReactiveCommand.CreateFromTask(
|
TriggerLogin = ReactiveCommand.CreateFromTask(
|
||||||
execute: () => Utils.CatchAndLog(NexusApiClient.RequestAndCacheAPIKey),
|
execute: () => Utils.CatchAndLog(NexusApiClient.RequestAndCacheAPIKey),
|
||||||
canExecute: IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler));
|
canExecute: IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler));
|
||||||
|
@ -14,7 +14,7 @@ namespace Wabbajack
|
|||||||
{
|
{
|
||||||
public App()
|
public App()
|
||||||
{
|
{
|
||||||
// Do initialization in MainWindow ctor
|
CLI.ParseOptions(Environment.GetCommandLineArgs());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,16 +129,16 @@ namespace Wabbajack
|
|||||||
.Select(active => !SettingsPane.IsValueCreated || !object.ReferenceEquals(active, SettingsPane.Value)),
|
.Select(active => !SettingsPane.IsValueCreated || !object.ReferenceEquals(active, SettingsPane.Value)),
|
||||||
execute: () => NavigateTo(SettingsPane.Value));
|
execute: () => NavigateTo(SettingsPane.Value));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsStartingFromModlist(out string modlistPath)
|
private static bool IsStartingFromModlist(out string modlistPath)
|
||||||
{
|
{
|
||||||
string[] args = Environment.GetCommandLineArgs();
|
if (CLIArguments.InstallPath == null)
|
||||||
if (args.Length != 3 || !args[1].Contains("-i"))
|
|
||||||
{
|
{
|
||||||
modlistPath = default;
|
modlistPath = default;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
modlistPath = args[2];
|
modlistPath = CLIArguments.InstallPath;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,9 +48,7 @@ namespace Wabbajack
|
|||||||
}).FireAndForget();
|
}).FireAndForget();
|
||||||
|
|
||||||
// Load settings
|
// Load settings
|
||||||
string[] args = Environment.GetCommandLineArgs();
|
if (CLIArguments.NoSettings || !MainSettings.TryLoadTypicalSettings(out var settings))
|
||||||
if ((args.Length > 1 && args[1] == "nosettings")
|
|
||||||
|| !MainSettings.TryLoadTypicalSettings(out var settings))
|
|
||||||
{
|
{
|
||||||
_settings = new MainSettings();
|
_settings = new MainSettings();
|
||||||
RunWhenLoaded(DefaultSettings);
|
RunWhenLoaded(DefaultSettings);
|
||||||
|
Loading…
Reference in New Issue
Block a user