alterNERDtive-base: moved loglevel setting from context to config option

This commit is contained in:
alterNERDtive 2020-12-07 21:45:17 +01:00
parent e8134e240d
commit afee22cf62
3 changed files with 41 additions and 31 deletions

View file

@ -249,45 +249,46 @@ namespace alterNERDtive
Log.Notice("Finished startup.");
}
private static void Context_SetLogLevel(dynamic vaProxy)
{
string level = vaProxy.GetText("~level");
try
{
Log.SetCurrentLogLevel(level);
}
catch (ArgumentException)
{
Log.Error($"Invalid LogLevel '{level}'.");
}
}
private static void ConfigurationChanged(string name, dynamic? from, dynamic? to, Guid? guid = null)
private static void ConfigurationChanged(string option, dynamic? from, dynamic? to, Guid? guid = null)
{
try
{
Match match = ConfigurationVariableRegex.Match(name);
Match match = ConfigurationVariableRegex.Match(option);
if (match.Success)
{
string id = match.Groups["id"].Value;
string option = match.Groups["name"].Value;
Log.Debug($"Configuration has changed, '{id}.{option}': '{from}' → '{to}'");
string name = match.Groups["name"].Value;
Log.Debug($"Configuration has changed, '{id}.{name}': '{from}' → '{to}'");
// When loaded from profile but not explicitly set, will be null.
// Then load default.
// Same applies to resetting a saved option (= saving null to the profile).
_ = to ?? Config.ApplyDefault(id, option);
_ = to ?? Config.ApplyDefault(id, name);
if (name == "alterNERDtive-base.eddi.quietMode#" && VA!.GetText("EDDI version") != null) // if null, EDDI isnt up yet
// When not null, check if theres a constraint on valid values.
Configuration.Option o = Config.GetOption(id, name);
if (o.ValidValues != null && !o.ValidValues.Contains(to))
{
Log.Debug($"Resetting speech responder ({(to ?? false ? "off" : "on")}) …");
Commands.Run("alterNERDtive-base.setEDDISpeechResponder");
Log.Error($@"Invalid value ""{to}"" for option ""{id}.{option}"", reverting to default …");
Config.ApplyDefault(id, name);
}
else
{
if (option == "alterNERDtive-base.eddi.quietMode#" && VA!.GetText("EDDI version") != null) // if null, EDDI isnt up yet
{
Log.Debug($"Resetting speech responder ({(to ?? false ? "off" : "on")}) …");
Commands.Run("alterNERDtive-base.setEDDISpeechResponder");
}
else if (option == "alterNERDtive-base.log.logLevel#")
{
Log.SetCurrentLogLevel(to);
}
}
}
}
catch (Exception e)
{
Log.Error($"Unhandled exception while handling changed variable '{name}'. ({e.Message})");
Log.Error($"Unhandled exception while handling changed variable '{option}'. ({e.Message})");
}
}
@ -356,9 +357,6 @@ namespace alterNERDtive
case "log.log":
Context_Log(vaProxy);
break;
case "log.setloglevel":
Context_SetLogLevel(vaProxy);
break;
// invalid
default:
Log.Error($"Invalid plugin context '{vaProxy.Context}'.");

View file

@ -23,7 +23,9 @@ namespace alterNERDtive.util
{ new Option("eddi.quietMode", true, voiceTrigger: "eddi quiet mode", description: "Whether or not to make EDDI shut up.") },
{ new Option("keyPressDuration", (decimal)0.01, voiceTrigger: "key press duration", description: "The time keys will be held down for.") },
{ new Option("delays.quitToDesktop", (decimal)10.0, voiceTrigger: "quit to desktop delay") },
{ new Option("elite.pasteKey", "v", voiceTrigger: "elite paste key", description: "The key used to paste in conjunction with CTRL. The key that would be 'V' on QWERTY.") }
{ new Option("elite.pasteKey", "v", voiceTrigger: "elite paste key", description: "The key used to paste in conjunction with CTRL. The key that would be 'V' on QWERTY.") },
{ new Option("log.logLevel", "NOTICE", voiceTrigger: "log level", validValues: new List<dynamic>{ "ERROR", "WARN", "NOTICE", "INFO", "DEBUG" },
description: @"The level of detail for logging to the VoiceAttack log.\nValid levels are ""ERROR"", ""WARN"", ""NOTICE"", ""INFO"" and ""DEBUG"".\nDefault: ""NOTICE"".") },
}
},
{
@ -49,7 +51,7 @@ namespace alterNERDtive.util
{ new Option("CMDRs", "", voiceTrigger: "commanders") },
{ new Option("confirmCalls", true, voiceTrigger: "confirm calls") },
{ new Option("onDuty", true, voiceTrigger: "on duty") },
{ new Option("platforms", "PC", voiceTrigger: "platforms") }
{ new Option("platforms", "PC", voiceTrigger: "platforms") },
}
},
{
@ -62,7 +64,7 @@ namespace alterNERDtive.util
{ new Option("clearOnShutdown", true, voiceTrigger: "clear on shutdown") },
{ new Option("copyWaypointToClipboard", false, voiceTrigger: "copy waypoint to clipboard") },
{ new Option("defaultToLadenRange", false, voiceTrigger: "default to laden range") },
{ new Option("timeTrip", false, voiceTrigger: "time trip") }
{ new Option("timeTrip", false, voiceTrigger: "time trip") },
}
},
{
@ -73,18 +75,19 @@ namespace alterNERDtive.util
}
};
private class Option
public class Option
{
public readonly string Name;
public readonly dynamic DefaultValue;
public readonly List<dynamic>? ValidValues;
public readonly string VoiceTrigger;
public string TtsDescription { get => ttsDescription ?? VoiceTrigger; }
private readonly string? ttsDescription;
public string Description { get => description ?? "No description available."; }
public readonly string? description;
public Option(string name, dynamic defaultValue, string voiceTrigger, string? ttsDescription = null, string? description = null)
=> (Name, DefaultValue, VoiceTrigger, this.ttsDescription, this.description) = (name, defaultValue, voiceTrigger, ttsDescription, description);
public Option(string name, dynamic defaultValue, string voiceTrigger, List<dynamic>? validValues = null, string? ttsDescription = null, string? description = null)
=> (Name, DefaultValue, VoiceTrigger, ValidValues, this.ttsDescription, this.description) = (name, defaultValue, voiceTrigger, validValues, ttsDescription, description);
public static implicit operator (string, Option)(Option o) => ( o.Name, o );
public static explicit operator bool(Option o) => o.DefaultValue;
@ -117,6 +120,15 @@ namespace alterNERDtive.util
return Defaults[id][name];
}
public Option GetOption(string name)
{
return GetOption(ID, name);
}
public Option GetOption(string id, string name)
{
return Defaults[id][name];
}
public void SetVoiceTriggers(System.Type type)
{
List<string> triggers = new List<string>();