/// <summary> /// Instantanously replaces the Options singleton instance with the data provided. /// </summary> /// <param name="io"></param> public static void ReplaceCurr(OptionsInformation io) { lock (padlock) { instance = io; } }
/// <summary> /// Will read options from a file into the given instance. /// </summary> public static void ReadJson(string fn, OptionsInformation optionsInformation) { try { var jsonStr = File.ReadAllText(fn); JsonConvert.PopulateObject(jsonStr, optionsInformation); } catch (Exception ex) { AasxPackageExplorer.Log.Singleton.Error(ex, "When reading options JSON file"); } }
/// <summary> /// Will save options to a file. Catches exceptions. /// </summary> public static void WriteJson(OptionsInformation optionsInformation, string filename) { // execute in-line, in order to represent to correct order to the human operator try { var jsonStr = JsonConvert.SerializeObject(optionsInformation, Formatting.Indented); File.WriteAllText(filename, jsonStr); } catch (Exception ex) { AasxPackageExplorer.Log.Singleton.Error(ex, $"When writing options to a JSON file: {filename}"); } }
/// <summary> /// Takes the <c>opt</c> and tries to write them to the associated plugin. /// </summary> public static void TrySetOptionsForPlugins(OptionsInformation opt) { TryForAllLoadedPlugins(opt, "Trying set json options to plugins", (dllinfo, lpi) => { if (dllinfo.Options != null) { var jsonStr = dllinfo.Options.ToString(Formatting.None); lpi.InvokeAction("set-json-options", jsonStr); } return(true); }); }
public static void TryReadOptionsFile(string filename, OptionsInformation optionsInformation) { try { var optionsTxt = File.ReadAllText(filename); var argsFromFile = optionsTxt.Split( new[] { '\r', '\n', '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries); OptionsInformation.ParseArgs(argsFromFile, optionsInformation); } catch (Exception ex) { AasxPackageExplorer.Log.Singleton.Error(ex, "Reading options file: " + filename); } }
/// <summary> /// Takes the <c>opt</c> and tries to write them to the associated plugin. /// </summary> public static void TrGetDefaultOptionsForPlugins(OptionsInformation opt) { TryForAllLoadedPlugins(opt, "Trying get json options from Plugins", (dllinfo, lpi) => { var popt = lpi.InvokeAction("get-json-options") as AasxPluginResultBaseObject; if (popt != null && popt.obj != null && popt.obj is string) { dllinfo.DefaultOptions = Newtonsoft.Json.Linq.JValue.Parse((popt.obj as string)); } ; return(true); }); }
/// <summary> /// Execute lambda for all loaded plugins and correlate with source plugin-dll-information. /// Returns a list of results of the lambda. /// </summary> public static List <object> TryForAllLoadedPlugins( OptionsInformation opt, string exceptionWhere, Func <OptionsInformation.PluginDllInfo, PluginInstance, object> lambda) { // access var res = new List <object>(); if (opt == null || lambda == null) { return(res); } // try to find matching plugins according to options for (int sourceIndex = 0; sourceIndex < opt.PluginDll.Count; sourceIndex++) { // options var dllinfo = opt.PluginDll[sourceIndex]; // loaded plug in? PluginInstance piFound = null; foreach (var lpi in LoadedPlugins.Values) { if (lpi.SourceIndex == sourceIndex) { piFound = lpi; } } // yes? if (piFound == null) { continue; } // yes! try { var res2 = lambda(dllinfo, piFound); res.Add(res2); } catch (Exception ex) { Log.Error(ex, exceptionWhere); } } // OK return(res); }
/// <summary> /// Infers application options based on the command-line arguments. /// </summary> /// <param name="exePath">path to AasxPackageExplorer.exe</param> /// <param name="args">command-line arguments</param> /// <returns>inferred options</returns> public static OptionsInformation InferOptions(string exePath, string[] args) { var optionsInformation = new OptionsInformation(); // Load the default command-line options from a file with a conventional file name var pathToDefaultOptions = System.IO.Path.Combine( System.IO.Path.GetDirectoryName(exePath), System.IO.Path.GetFileNameWithoutExtension(exePath) + ".options.json"); Log.Singleton.Info( "The default options are expected in the JSON file: {0}", pathToDefaultOptions); if (File.Exists(pathToDefaultOptions)) { Log.Singleton.Info( "Loading the default options from: {0}", pathToDefaultOptions); OptionsInformation.ReadJson(pathToDefaultOptions, optionsInformation); } else { Log.Singleton.Info( "The JSON file with the default options does not exist;" + "no default options were loaded: {0}", pathToDefaultOptions); } // Cover the special case for having a single positional command-line option if (args.Length == 1 && !args[0].StartsWith("-")) { string directAasx = args[0]; Log.Singleton.Info("Direct request to load AASX {0} ..", directAasx); optionsInformation.AasxToLoad = directAasx; } // Parse options from the command-line and execute the directives on the fly (such as parsing and // overruling given in the additional option files, *e.g.*, through "-read-json" and "-options") Log.Singleton.Info($"Parsing {args.Length} command-line option(s)..."); for (var i = 0; i < args.Length; i++) { Log.Singleton.Info($"Command-line option: {i}: {args[i]}"); } OptionsInformation.ParseArgs(args, optionsInformation); return(optionsInformation); }
private void Application_Startup(object sender, StartupEventArgs e) { // allow long term logging (for report box) Log.Singleton.EnableLongTermStore(); // catch unhandled exceptions SetupExceptionHandling(); // Build up of options Log.Singleton.Info("Application startup."); var exePath = System.Reflection.Assembly.GetEntryAssembly()?.Location; Options.ReplaceCurr(InferOptions(exePath, e.Args)); // search for plugins? if (Options.Curr.PluginDir != null) { var searchDir = System.IO.Path.Combine( System.IO.Path.GetDirectoryName(exePath), Options.Curr.PluginDir); Log.Singleton.Info( "Searching for the plugins in the plugin directory: {0}", searchDir); var pluginDllInfos = Plugins.TrySearchPlugins(searchDir); Log.Singleton.Info( $"Found {pluginDllInfos.Count} plugin(s) in the plugin directory: {searchDir}"); Options.Curr.PluginDll.AddRange(pluginDllInfos); } Log.Singleton.Info( $"Loading and activating {Options.Curr.PluginDll.Count} plugin(s)..."); Plugins.LoadedPlugins = LoadAndActivatePlugins(Options.Curr.PluginDll); // at end, write all default options to JSON? if (Options.Curr.WriteDefaultOptionsFN != null) { // info var fullFilename = System.IO.Path.GetFullPath(Options.Curr.WriteDefaultOptionsFN); Log.Singleton.Info($"Writing resulting options to a JSON file: {fullFilename}"); // retrieve Plugins.TryGetDefaultOptionsForPlugins(Options.Curr.PluginDll, Plugins.LoadedPlugins); OptionsInformation.WriteJson(Options.Curr, fullFilename); } // colors if (true) { var resNames = new[] { "LightAccentColor", "DarkAccentColor", "DarkestAccentColor", "FocusErrorBrush" }; for (int i = 0; i < resNames.Length; i++) { var x = this.FindResource(resNames[i]); if (x != null && x is System.Windows.Media.SolidColorBrush && Options.Curr.AccentColors.ContainsKey((OptionsInformation.ColorNames)i)) { this.Resources[resNames[i]] = AnyUiDisplayContextWpf.GetWpfBrush( Options.Curr.GetColor((OptionsInformation.ColorNames)i)); } } resNames = new[] { "FocusErrorColor" }; for (int i = 0; i < resNames.Length; i++) { var x = this.FindResource(resNames[i]); if (x != null && x is System.Windows.Media.Color && Options.Curr.AccentColors.ContainsKey((OptionsInformation.ColorNames)(3 + i))) { this.Resources[resNames[i]] = AnyUiDisplayContextWpf.GetWpfColor( Options.Curr.GetColor((OptionsInformation.ColorNames)(3 + i))); } } } Pref pref = Pref.Read(); // show splash (required for licenses of open source) if (Options.Curr.SplashTime != 0) { var splash = new CustomSplashScreenNew(pref); splash.Show(); } // show main window MainWindow wnd = new MainWindow(pref); wnd.Show(); }
public static void ParseArgs(string[] args, OptionsInformation optionsInformation) { // This is a sweep line for plugin arguments. var pluginArgs = new List <string>(); for (int index = 0; index < args.Length; index++) { var arg = args[index].Trim().ToLower(); var morearg = (args.Length - 1) - index; // flags if (arg == "-maximized") { optionsInformation.WindowMaximized = true; continue; } if (arg == "-noflyouts") { optionsInformation.UseFlyovers = false; continue; } if (arg == "-intbrowse") { optionsInformation.InternalBrowser = true; continue; } if (arg == "-twopass") { optionsInformation.EclassTwoPass = true; continue; } if (arg == "-indirect-load-save") { optionsInformation.IndirectLoadSave = true; continue; } if (arg == "-load-without-prompt") { optionsInformation.LoadWithoutPrompt = true; continue; } // commands, which are executed on the fly .. if (arg == "-read-json" && morearg > 0) { // parse var fn = System.IO.Path.GetFullPath(args[index + 1]); index++; // execute in-line, in order to represent to correct order to the human operator OptionsInformation.ReadJson(fn, optionsInformation); // next arg continue; } if (arg == "-write-json" && morearg > 0) { // parse var filename = System.IO.Path.GetFullPath(args[index + 1]); index++; // do OptionsInformation.WriteJson(optionsInformation, filename); // next arg continue; } // options if (arg == "-left" && morearg > 0) { if (Int32.TryParse(args[index + 1], out int i)) { optionsInformation.WindowLeft = i; } index++; continue; } if (arg == "-top" && morearg > 0) { if (Int32.TryParse(args[index + 1], out int i)) { optionsInformation.WindowTop = i; } index++; continue; } if (arg == "-width" && morearg > 0) { if (Int32.TryParse(args[index + 1], out int i)) { optionsInformation.WindowWidth = i; } index++; continue; } if (arg == "-height" && morearg > 0) { if (Int32.TryParse(args[index + 1], out int i)) { optionsInformation.WindowHeight = i; } index++; continue; } if (arg == "-id-aas" && morearg > 0) { optionsInformation.TemplateIdAas = args[index + 1]; index++; continue; } if (arg == "-id-asset" && morearg > 0) { optionsInformation.TemplateIdAsset = args[index + 1]; index++; continue; } if (arg == "-id-sm-template" && morearg > 0) { optionsInformation.TemplateIdSubmodelTemplate = args[index + 1]; index++; continue; } if (arg == "-id-sm-instance" && morearg > 0) { optionsInformation.TemplateIdSubmodelInstance = args[index + 1]; index++; continue; } if (arg == "-id-cd" && morearg > 0) { optionsInformation.TemplateIdConceptDescription = args[index + 1]; index++; continue; } if (arg == "-eclass" && morearg > 0) { optionsInformation.EclassDir = args[index + 1]; index++; continue; } if (arg == "-qualifiers" && morearg > 0) { optionsInformation.QualifiersFile = args[index + 1]; index++; continue; } if (arg == "-logo" && morearg > 0) { optionsInformation.LogoFile = args[index + 1]; index++; continue; } if (arg == "-aasxrepo" && morearg > 0) { optionsInformation.AasxRepositoryFn = args[index + 1]; index++; continue; } if (arg == "-contenthome" && morearg > 0) { optionsInformation.ContentHome = args[index + 1]; index++; continue; } if (arg == "-splash" && morearg > 0) { if (Int32.TryParse(args[index + 1], out int i)) { optionsInformation.SplashTime = i; } index++; continue; } if (arg == "-options" && morearg > 0) { string pathToOptions = args[index + 1]; AasxPackageExplorer.Log.Singleton.Info( $"Parsing options from a non-default options file: {pathToOptions}"); var fullFilename = System.IO.Path.GetFullPath(pathToOptions); OptionsInformation.TryReadOptionsFile(fullFilename, optionsInformation); index++; continue; } if (arg == "-backupdir" && morearg > 0) { optionsInformation.BackupDir = args[index + 1]; index++; continue; } if (arg == "-resthost" && morearg > 0) { optionsInformation.RestServerHost = args[index + 1]; index++; continue; } if (arg == "-restport" && morearg > 0) { optionsInformation.RestServerPort = args[index + 1]; index++; continue; } if (arg == "-rem" && morearg > 0) { // Add one argument to the plugin list optionsInformation.Remarks.Add(args[index + 1]); index++; continue; } if (arg == "-write-all-json" && morearg > 0) { // will be executed very late! optionsInformation.WriteDefaultOptionsFN = args[index + 1]; index++; continue; } if (arg == "-plugin-dir" && morearg > 0) { optionsInformation.PluginDir = args[index + 1]; index++; continue; } // Sweep-line options for plugins and DLL path if (arg == "-p" && morearg > 0) { // Add exactly one following argument to the sweep line of plugin arguments pluginArgs.Add(args[index + 1]); index += 1; continue; } if (arg == "-dll" && morearg > 0) { // Process and reset the sweep line optionsInformation.PluginDll.Add( new PluginDllInfo(args[index + 1], pluginArgs.ToArray())); pluginArgs.Clear(); index++; continue; } // Colors { var found = false; for (int i = 0; i < 10; i++) { if (arg == $"-c{i:0}" && morearg > 0) { // ReSharper disable PossibleNullReferenceException try { var c = (Color)ColorConverter.ConvertFromString(args[index + 1]); optionsInformation.AccentColors.Add(i, c); } catch (Exception ex) { AdminShellNS.LogInternally.That.SilentlyIgnoredError(ex); } // ReSharper enable PossibleNullReferenceException index++; found = true; } } if (found) { continue; } } // if come to this point and obviously not an option, take this as load argument // allow for more options to come (motivation: allow "-write-json options.json" to be the last argument) if (!arg.StartsWith("-")) { if (System.IO.File.Exists(args[index])) { optionsInformation.AasxToLoad = args[index]; } } } }