Пример #1
0
        /// <summary>
        /// Loads the tool dialog T (T must implement <see cref="IToolForm"/>) , if it does not exist it will be created, if it is already open, it will be focused
        /// </summary>
        /// <typeparam name="T">Type of tool you want to load</typeparam>
        /// <param name="toolPath">Path to the .dll of the external tool</param>
        /// <param name="focus">Define if the tool form has to get the focus or not (Default is true)</param>
        /// <returns>An instantiated <see cref="IToolForm"/></returns>
        public T Load <T>(string toolPath, bool focus = true)
            where T : class, IToolForm
        {
            bool isExternal = typeof(T) == typeof(IExternalToolForm);

            if (!IsAvailable <T>() && !isExternal)
            {
                return(null);
            }

            T existingTool;

            if (isExternal)
            {
                existingTool = (T)_tools.FirstOrDefault(t => t is T && t.GetType().Assembly.Location == toolPath);
            }
            else
            {
                existingTool = (T)_tools.FirstOrDefault(t => t is T);
            }

            if (existingTool != null)
            {
                if (existingTool.IsDisposed)
                {
                    _tools.Remove(existingTool);
                }
                else
                {
                    if (focus)
                    {
                        existingTool.Show();
                        existingTool.Focus();
                    }

                    return(existingTool);
                }
            }

            IToolForm newTool = CreateInstance <T>(toolPath);

            if (newTool == null)
            {
                return(null);
            }

            if (newTool is Form)
            {
                (newTool as Form).Owner = GlobalWin.MainForm;
            }

            if (isExternal)
            {
                ApiInjector.UpdateApis(GlobalWin.ApiProvider, newTool);
            }

            ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool);
            string toolType = typeof(T).ToString();

            // auto settings
            if (newTool is IToolFormAutoConfig)
            {
                ToolDialogSettings settings;
                if (!Global.Config.CommonToolSettings.TryGetValue(toolType, out settings))
                {
                    settings = new ToolDialogSettings();
                    Global.Config.CommonToolSettings[toolType] = settings;
                }

                AttachSettingHooks(newTool as IToolFormAutoConfig, settings);
            }

            // custom settings
            if (HasCustomConfig(newTool))
            {
                if (!Global.Config.CustomToolSettings.TryGetValue(toolType, out var settings))
                {
                    settings = new Dictionary <string, object>();
                    Global.Config.CustomToolSettings[toolType] = settings;
                }

                InstallCustomConfig(newTool, settings);
            }

            newTool.Restart();
            if (!OSTailoredCode.IsWindows() && newTool is RamSearch)
            {
                // the mono winforms implementation is buggy, skip to the return statement and call Show in MainForm instead
            }
            else
            {
                newTool.Show();
            }
            return((T)newTool);
        }