/// <summary> /// The initialize plugin. /// </summary> /// <param name="name"> /// The name. /// </param> /// <returns> /// The <see cref="BasePlugIn" />. /// </returns> private static BasePlugIn InitializePlugin(string name) { if (pluginMappings.ContainsKey(name.ToLower(CultureInfo.InvariantCulture).Trim())) { if (pluginMappings[name.ToLower(CultureInfo.InvariantCulture).Trim()] is string) { string path = (string)pluginMappings[name.ToLower(CultureInfo.InvariantCulture).Trim()]; try { //Assembly assembly = Assembly.LoadFrom(path); Assembly assembly = Assembly.UnsafeLoadFrom(path); Type pluginType = assembly.GetType( "TranslationAssistant.AutomationToolkit.TranslationPlugins." + name, true, true); var plugin = (BasePlugIn) assembly.CreateInstance( pluginType.FullName, true, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.InvokeMethod, null, new object[] { Logger }, null, null); if (plugin != null) { pluginMappings[name.ToLower(CultureInfo.InvariantCulture).Trim()] = plugin; return(plugin); } Logger.WriteLine(LogLevel.Error, "ERROR: Failed to load plugin {0}.", name); Logger.WriteLine(LogLevel.Error, "CreateInstance( ) return null."); } catch (DivideByZeroException fe) { Logger.WriteLine(LogLevel.Error, "ERROR: Failed to load plugin {0}.", name); Logger.WriteException(fe); } catch (Exception e) { Logger.WriteLine(LogLevel.Error, "ERROR: Failed to load plugin {0}.", name); Logger.WriteException(e); } } else { BasePlugIn basePlugIn = pluginMappings[name.ToLower(CultureInfo.InvariantCulture).Trim()] as BasePlugIn; if (basePlugIn != null) { // plugin is already initialized. return(basePlugIn); } } } return(null); }
/// <summary> /// The main. /// </summary> /// <param name="args"> /// The args. /// </param> private static void Main(string[] args) { bool returnValue = false; if (!GetPluginHash()) { return; } if (args.Length <= 0) { Logger.WriteLine(LogLevel.Error, "ERROR: At least 1 argument is required."); Usage(); } else if (args.Length == 1 && (args[0].ToLower(CultureInfo.InvariantCulture).Trim().Equals("/?") || args[0].ToLower(CultureInfo.InvariantCulture).Trim().Equals("/h") || args[0].ToLower(CultureInfo.InvariantCulture).Trim().Equals("/help"))) { Usage(); } else { BasePlugIn plugin = InitializePlugin(args[0]); if (plugin == null) { Logger.WriteLine(LogLevel.Error, "ERROR: Unknown functionality."); Usage(); } else { var functionalArguments = new string[args.Length - 1]; bool helpInvoked = false; for (int i = 1; i < args.Length; i++) { string arg = args[i].ToLower(CultureInfo.InvariantCulture).Trim(); if (arg != "/h" && arg != "-h" && arg != "\\h" && arg != "/?" && arg != "-?" && arg != "\\?" && arg != "/help" && arg != "-help" && arg != "\\help") { functionalArguments[i - 1] = args[i]; } else { Logger.WriteLine(LogLevel.Debug, "User activated plug-in help menu."); Logger.WriteLine(LogLevel.Msg, "Usage details for [{0}]", plugin.Name); Logger.WriteLine(LogLevel.Msg, string.Empty); plugin.Usage(); Logger.WriteLine(LogLevel.Msg, string.Empty); // Usage(); helpInvoked = true; break; } } if (!helpInvoked) { if (plugin.Parse(functionalArguments)) { returnValue = plugin.Execute(); } else { Logger.WriteLine(LogLevel.Error, "ERROR: Error in arguments."); } } } } Environment.ExitCode = returnValue ? 0 : 1; }