Ejemplo n.º 1
0
        private void AddExtensionItems()
        {
            extensionsItem.Items.Clear();
            AddMenuChild(extensionsItem.Items, "LOCReloadScripts", mainModel.ReloadScriptsCommand);
            extensionsItem.Items.Add(new Separator());
            foreach (var function in mainModel.Extensions.ExportedFunctions)
            {
                var item = new MenuItem
                {
                    Header           = function.Name,
                    Command          = mainModel.InvokeExtensionFunctionCommand,
                    CommandParameter = function
                };

                extensionsItem.Items.Add(item);
            }

            var args  = new GetMainMenuItemsArgs();
            var toAdd = new List <MainMenuItem>();

            foreach (var plugin in mainModel.Extensions.Plugins.Values)
            {
                try
                {
                    var items = plugin.Plugin.GetMainMenuItems(args);
                    if (items.HasItems())
                    {
                        toAdd.AddRange(items);
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, $"Failed to get menu items from plugin {plugin.Description.Name}");
                }
            }

            foreach (var script in mainModel.Extensions.Scripts)
            {
                if (script.SupportedMenus.Contains(Scripting.SupportedMenuMethods.MainMenu))
                {
                    try
                    {
                        var items = script.GetMainMenuItems(args);
                        if (items.HasItems())
                        {
                            foreach (var item in items)
                            {
                                var newItem = MainMenuItem.FromScriptMainMenuItem(item);
                                newItem.Action = (a) =>
                                {
                                    script.InvokeFunction(item.FunctionName, new List <object>
                                    {
                                        new ScriptMainMenuItemActionArgs()
                                    });
                                };

                                toAdd.Add(newItem);
                            }
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to get menu items from script {script.Name}");
                    }
                }
            }

            if (toAdd.Count > 0)
            {
                var menuItems          = new Dictionary <string, MenuItem>();
                var menuExtensionItems = new Dictionary <string, MenuItem>();
                foreach (var item in toAdd)
                {
                    var newItem = new MenuItem()
                    {
                        Header = item.Description,
                        Icon   = MenuHelpers.GetIcon(item.Icon)
                    };

                    if (item.Action != null)
                    {
                        newItem.Click += (_, __) =>
                        {
                            try
                            {
                                item.Action(new MainMenuItemActionArgs());
                            }
                            catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                            {
                                logger.Error(e, "Main menu extension action failed.");
                                Dialogs.ShowErrorMessage(
                                    ResourceProvider.GetString("LOCMenuActionExecError") +
                                    Environment.NewLine + Environment.NewLine +
                                    e.Message, "");
                            }
                        };
                    }

                    var startIndex = Items.IndexOf(extensionsItem) + 1;
                    if (item.MenuSection.IsNullOrEmpty())
                    {
                        Items.Insert(startIndex, newItem);
                    }
                    else
                    {
                        if (item.MenuSection == "@")
                        {
                            extensionsItem.Items.Add(newItem);
                        }
                        else if (item.MenuSection.StartsWith("@"))
                        {
                            var parent = MenuHelpers.GenerateMenuParents(menuExtensionItems, item.MenuSection.Substring(1), extensionsItem.Items);
                            parent?.Items.Add(newItem);
                        }
                        else
                        {
                            var parent = MenuHelpers.GenerateMenuParents(menuItems, item.MenuSection, Items, startIndex);
                            parent?.Items.Add(newItem);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void AddExtensionItems()
        {
            var args  = new GetGameMenuItemsArgs();
            var toAdd = new List <GameMenuItem>();

            if (Games != null)
            {
                args.Games = Games;
            }
            else
            {
                args.Games = new List <Game>(1)
                {
                    Game
                };
            }

            foreach (var plugin in model.Extensions.Plugins.Values)
            {
                try
                {
                    var items = plugin.Plugin.GetGameMenuItems(args);
                    if (items.HasItems())
                    {
                        toAdd.AddRange(items);
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, $"Failed to get menu items from plugin {plugin.Description.Name}");
                }
            }

            foreach (var script in model.Extensions.Scripts)
            {
                if (script.SupportedMenus.Contains(Scripting.SupportedMenuMethods.GameMenu))
                {
                    try
                    {
                        var items = script.GetGameMenuItems(args);
                        if (items.HasItems())
                        {
                            foreach (var item in items)
                            {
                                var newItem = GameMenuItem.FromScriptGameMenuItem(item);
                                newItem.Action = (a) =>
                                {
                                    script.InvokeFunction(item.FunctionName, new List <object>
                                    {
                                        new ScriptGameMenuItemActionArgs
                                        {
                                            Games      = a.Games,
                                            SourceItem = item
                                        }
                                    });
                                };

                                toAdd.Add(newItem);
                            }
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to get menu items from script {script.Name}");
                    }
                }
            }

            if (toAdd.Count > 0)
            {
                Items.Add(new Separator());
                var menuItems = new Dictionary <string, MenuItem>();
                foreach (var item in toAdd)
                {
                    object newItem = null;
                    if (item.Description == "-")
                    {
                        newItem = new Separator();
                    }
                    else
                    {
                        newItem = new MenuItem()
                        {
                            Header = item.Description,
                            Icon   = MenuHelpers.GetIcon(item.Icon)
                        };

                        if (item.Action != null)
                        {
                            ((MenuItem)newItem).Click += (_, __) =>
                            {
                                try
                                {
                                    item.Action(new GameMenuItemActionArgs
                                    {
                                        Games      = args.Games,
                                        SourceItem = item
                                    });
                                }
                                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                                {
                                    logger.Error(e, "Game menu extension action failed.");
                                    Dialogs.ShowErrorMessage(
                                        ResourceProvider.GetString("LOCMenuActionExecError") +
                                        Environment.NewLine + Environment.NewLine +
                                        e.Message, "");
                                }
                            };
                        }
                    }

                    if (item.MenuSection.IsNullOrEmpty())
                    {
                        Items.Add(newItem);
                    }
                    else
                    {
                        var parent = MenuHelpers.GenerateMenuParents(menuItems, item.MenuSection, Items);
                        parent?.Items.Add(newItem);
                    }
                }
            }
        }