示例#1
0
        public override void OnGameStopped(Game game, long elapsedSeconds)
        {
            // Add code to be executed when game is preparing to be started.

            // Refresh Achievements database for game played.
            achievementsDatabase.Remove(game);
            achievementsDatabase.Add(game, settings);

            // Refresh integration interface
            SuccessStory.isFirstLoad = true;
            Integration();
        }
示例#2
0
        public override void OnGameStopped(Game game, long elapsedSeconds)
        {
            // Add code to be executed when game is preparing to be started.

            // Refresh Achievements database for game played.
            AchievementsDatabase AchievementsDatabase = new AchievementsDatabase(PlayniteApi, this.GetPluginUserDataPath());

            AchievementsDatabase.Remove(game);
            AchievementsDatabase.Add(game, settings);

            // REfresh integration interface
            Integration();
        }
示例#3
0
        public override void OnLibraryUpdated()
        {
            // Add code to be executed when library is updated.

            // Get achievements for the new game added int he library.
            AchievementsDatabase AchievementsDatabase = new AchievementsDatabase(PlayniteApi, this.GetPluginUserDataPath());

            foreach (var game in PlayniteApi.Database.Games)
            {
                if (game.Added == null && ((DateTime)game.Added).ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"))
                {
                    AchievementsDatabase.Remove(game);
                    AchievementsDatabase.Add(game, settings);
                }
            }
        }
示例#4
0
        private void Integration()
        {
            bool noAchievements = false;

            try
            {
                AchievementsDatabase achievementsDatabase = new AchievementsDatabase(PlayniteApi, this.GetPluginUserDataPath());
                achievementsDatabase.Initialize();

                GameAchievements SelectedGameAchievements = achievementsDatabase.Get(GameSelected.Id);

                // Download Achievements if not exist in database.
                if (SelectedGameAchievements == null)
                {
                    logger.Info("SuccesStory - Download achievements for " + GameSelected.Name);
                    achievementsDatabase.Add(GameSelected, settings);
                    achievementsDatabase.Initialize();
                    SelectedGameAchievements = achievementsDatabase.Get(GameSelected.Id);
                }

                if (SelectedGameAchievements == null || !SelectedGameAchievements.HaveAchivements)
                {
                    logger.Info("SuccessStory - No achievement for " + GameSelected.Name);

                    if (settings.EnableIntegrationInDescription || settings.EnableIntegrationInDescriptionWithToggle)
                    {
                        Button PART_ScButton = (Button)LogicalTreeHelper.FindLogicalNode(PART_ActionButtons, "PART_ScButton");
                        // Delete old ButtonDetails
                        if (settings.EnableIntegrationButtonDetails)
                        {
                            PART_ActionButtons.Children.Remove(PART_ScButton);
                            PART_ScButton = null;
                        }

                        ToggleButton PART_ScToggleButton = (ToggleButton)LogicalTreeHelper.FindLogicalNode(PART_ActionButtons, "PART_ScToggleButton");
                        // Delete old ToggleDetails
                        if (settings.IntegrationToggleDetails)
                        {
                            PART_ActionButtons.Children.Remove(PART_ScToggleButton);
                            PART_ScToggleButton = null;
                        }

                        // Delete old
                        string     NameControl       = "PART_Achievements";
                        StackPanel PART_Achievements = (StackPanel)LogicalTreeHelper.FindLogicalNode(PART_ElemDescription, NameControl);
                        if (PART_Achievements != null)
                        {
                            PART_ElemDescription.Children.Remove(PART_Achievements);
                        }
                    }

                    noAchievements = true;
                }

                // Auto integration
                if (settings.EnableIntegrationInDescription || settings.EnableIntegrationInDescriptionWithToggle)
                {
                    // Search parent action buttons
                    if (PART_ActionButtons == null)
                    {
                        foreach (Button bt in Tools.FindVisualChildren <Button>(Application.Current.MainWindow))
                        {
                            if (bt.Name == "PART_ButtonEditGame")
                            {
                                PART_ActionButtons = (StackPanel)bt.Parent;
                                break;
                            }
                        }
                    }

                    //Adding togglebutton
                    if (settings.EnableIntegrationInDescriptionWithToggle && PART_ActionButtons != null)
                    {
                        ToggleButton PART_ScToggleButton = (ToggleButton)LogicalTreeHelper.FindLogicalNode(PART_ActionButtons, "PART_ScToggleButton");

                        // Delete old ToggleDetails
                        if (settings.IntegrationToggleDetails)
                        {
                            PART_ActionButtons.Children.Remove(PART_ScToggleButton);
                            PART_ScToggleButton = null;
                        }

                        if (PART_ScToggleButton == null && !noAchievements)
                        {
                            ToggleButton tb = new ToggleButton();
                            if (settings.IntegrationToggleDetails)
                            {
                                tb = new SuccessStoryToggleButtonDetails(SelectedGameAchievements.Unlocked, SelectedGameAchievements.Total);
                            }
                            else
                            {
                                tb.Content = resources.GetString("LOCSucessStoryAchievements");
                            }

                            tb.IsChecked           = false;
                            tb.Name                = "PART_ScToggleButton";
                            tb.Width               = 150;
                            tb.Height              = 40;
                            tb.HorizontalAlignment = HorizontalAlignment.Right;
                            tb.VerticalAlignment   = VerticalAlignment.Stretch;
                            tb.Margin              = new Thickness(10, 0, 0, 0);
                            tb.Click              += ScToggleButton_Click;

                            PART_ActionButtons.Children.Add(tb);
                            PART_ActionButtons.UpdateLayout();
                        }
                    }

                    // Search game description
                    if (PART_ElemDescription == null)
                    {
                        foreach (StackPanel sp in Tools.FindVisualChildren <StackPanel>(Application.Current.MainWindow))
                        {
                            if (sp.Name == "PART_ElemDescription")
                            {
                                PART_ElemDescription = sp;
                                break;
                            }
                        }
                    }

                    // Adding control
                    if (PART_ElemDescription != null)
                    {
                        // Delete old
                        string     NameControl       = "PART_Achievements";
                        StackPanel PART_Achievements = (StackPanel)LogicalTreeHelper.FindLogicalNode(PART_ElemDescription, NameControl);
                        if (PART_Achievements != null)
                        {
                            if (settings.EnableIntegrationInDescription)
                            {
                                PART_ElemDescription.Children.Remove(PART_Achievements);
                            }
                            if (settings.EnableIntegrationInDescriptionWithToggle)
                            {
                                PART_ElemDescription.Children.Remove(PART_Achievements);
                            }
                        }
                        else
                        {
                            logger.Error($"SuccessStory - {NameControl} not found in Integration()");
                        }

                        if (SelectedGameAchievements != null && SelectedGameAchievements.HaveAchivements)
                        {
                            StackPanel ScA = CreateSc(achievementsDatabase, SelectedGameAchievements, settings.IntegrationShowTitle, settings.IntegrationShowGraphic, settings.IntegrationShowAchievements, false);

                            if (settings.EnableIntegrationInDescription)
                            {
                                // Add
                                if (settings.IntegrationTopGameDetails)
                                {
                                    PART_ElemDescription.Children.Insert(0, ScA);
                                }
                                else
                                {
                                    PART_ElemDescription.Children.Add(ScA);
                                }

                                PART_ElemDescription.UpdateLayout();
                            }

                            if (settings.EnableIntegrationInDescriptionWithToggle)
                            {
                                ScA.Visibility = Visibility.Collapsed;
                                PART_ElemDescription.Children.Add(ScA);
                                PART_ElemDescription.UpdateLayout();
                            }
                        }
                    }
                    else
                    {
                        logger.Error($"SuccessStory - PART_ElemDescription not found in Integration()");
                    }
                }

                // Auto adding button
                if (settings.EnableIntegrationButton || settings.EnableIntegrationButtonDetails)
                {
                    // Search parent action buttons
                    if (PART_ActionButtons == null)
                    {
                        foreach (Button bt in Tools.FindVisualChildren <Button>(Application.Current.MainWindow))
                        {
                            if (bt.Name == "PART_ButtonEditGame")
                            {
                                PART_ActionButtons = (StackPanel)bt.Parent;
                                break;
                            }
                        }
                    }

                    // Adding button
                    if (PART_ActionButtons != null)
                    {
                        Button PART_ScButton = (Button)LogicalTreeHelper.FindLogicalNode(PART_ActionButtons, "PART_ScButton");

                        // Delete old ButtonDetails
                        if (settings.EnableIntegrationButtonDetails)
                        {
                            PART_ActionButtons.Children.Remove(PART_ScButton);
                            PART_ScButton = null;
                        }

                        if (PART_ScButton == null)
                        {
                            Button bt = new Button();

                            if (settings.EnableIntegrationButton)
                            {
                                bt.Content = resources.GetString("LOCSucessStoryAchievements");
                            }

                            if (settings.EnableIntegrationButtonDetails)
                            {
                                bt = new SuccessStoryButtonDetails(SelectedGameAchievements.Unlocked, SelectedGameAchievements.Total);
                            }

                            bt.Name   = "PART_ScButton";
                            bt.Width  = 150;
                            bt.Height = 40;
                            bt.HorizontalAlignment = HorizontalAlignment.Right;
                            bt.VerticalAlignment   = VerticalAlignment.Stretch;
                            bt.Margin = new Thickness(10, 0, 0, 0);
                            bt.Click += ScButton_Click;

                            PART_ActionButtons.Children.Add(bt);
                            PART_ActionButtons.UpdateLayout();
                        }
                    }
                }


                // Custom theme
                if (settings.EnableIntegrationInCustomTheme)
                {
                    // Search custom element
                    foreach (StackPanel sp in Tools.FindVisualChildren <StackPanel>(Application.Current.MainWindow))
                    {
                        if (sp.Name == "PART_Achievements_Graphics")
                        {
                            if (SelectedGameAchievements != null && SelectedGameAchievements.HaveAchivements)
                            {
                                // Create
                                StackPanel scAG = CreateSc(achievementsDatabase, SelectedGameAchievements, false, true, false, true);

                                // Clear & add
                                sp.Children.Clear();
                                sp.Children.Add(scAG);
                                sp.UpdateLayout();
                            }
                            else
                            {
                                sp.Children.Clear();
                                sp.UpdateLayout();
                            }
                        }

                        if (sp.Name == "PART_Achievements_List")
                        {
                            if (SelectedGameAchievements != null && SelectedGameAchievements.HaveAchivements)
                            {
                                // Create
                                StackPanel scAL = CreateSc(achievementsDatabase, SelectedGameAchievements, false, false, true, true);

                                // Clear & add
                                sp.Children.Clear();
                                sp.Children.Add(scAL);
                                sp.UpdateLayout();
                            }
                            else
                            {
                                sp.Children.Clear();
                                sp.UpdateLayout();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var    LineNumber = new StackTrace(ex, true).GetFrame(0).GetFileLineNumber();
                string FileName   = new StackTrace(ex, true).GetFrame(0).GetFileName();
                logger.Error(ex, $"SuccessStory [{FileName} {LineNumber}] - Impossible integration ");
            }
        }
示例#5
0
        private void RefreshData(string SourceName, bool IsGet = false)
        {
#if DEBUG
            logger.Info($"SuccessStory - RefreshData() - Start");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
#endif

            SuccessStorySettings.IsEnabled = false;

            pbDataLoad.IsIndeterminate = false;
            pbDataLoad.Minimum         = 0;
            pbDataLoad.Value           = 0;

            DataLoad.Visibility   = Visibility.Visible;
            tcSettings.Visibility = Visibility.Hidden;

            tokenSource = new CancellationTokenSource();
            ct          = tokenSource.Token;

            bool IsFirstLoop = true;

            var taskSystem = Task.Run(() =>
            {
                try
                {
                    // filter games
                    IEnumerable <Game> FilterDatabaseGame = null;
                    switch (SourceName.ToLower())
                    {
                    case "all":
                        FilterDatabaseGame = PlayniteApi.Database.Games;
                        break;

                    case "allrecent":
                        FilterDatabaseGame = PlayniteApi.Database.Games.Where(
                            x => x.LastActivity > DateTime.Now.AddMonths(-2) || (x.Added != null && x.Added > DateTime.Now.AddMonths(-2))
                            );
                        break;

                    case "allinstalled":
                        FilterDatabaseGame = PlayniteApi.Database.Games.Where(x => x.IsInstalled);
                        break;

                    default:
                        FilterDatabaseGame = PlayniteApi.Database.Games.Where(
                            x => PlayniteTools.GetSourceName(x, PlayniteApi).ToLower() == SourceName.ToLower()
                            );
                        break;
                    }

                    Application.Current.Dispatcher.BeginInvoke((Action) delegate { pbDataLoad.Maximum = FilterDatabaseGame.Count(); });
#if DEBUG
                    logger.Debug($"SuccessStory - FilterDatabaseGame: {FilterDatabaseGame.Count()}");
#endif
                    foreach (var game in FilterDatabaseGame)
                    {
                        try
                        {
                            if (SourceName.ToLower() == "steam" && IsFirstLoop)
                            {
#if DEBUG
                                logger.Debug($"SuccessStory - Check Steam profil with {game.GameId}");
#endif

                                SteamAchievements steamAPI = new SteamAchievements(PlayniteApi, settings, PluginUserDataPath);
                                int AppId = 0;
                                int.TryParse(game.GameId, out AppId);
                                if (!steamAPI.CheckIsPublic(AppId))
                                {
                                    AchievementsDatabase.ListErrors.Add(resources.GetString("LOCSuccessStoryNotificationsSteamPrivate"));
                                    break;
                                }
                                IsFirstLoop = false;
                            }

                            // Respect API limitation
                            Thread.Sleep(1000);

                            if (IsGet)
                            {
                                // Add only it's not loaded
                                if (!achievementsDatabase.VerifAchievementsLoad(game.Id))
                                {
                                    achievementsDatabase.Add(game, settings);
                                }
                            }
                            else
                            {
                                achievementsDatabase.Remove(game);
                                achievementsDatabase.Add(game, settings);
                            }

                            Application.Current.Dispatcher.BeginInvoke((Action) delegate { pbDataLoad.Value += 1; });
                        }
                        catch (Exception ex)
                        {
                            Common.LogError(ex, "SuccessStory", $"Error on RefreshData({SourceName}, {IsGet}) for {game.Name}");
                        }

                        if (ct.IsCancellationRequested)
                        {
                            logger.Info($"IsCancellationRequested for RefreshData({ SourceName}, { IsGet})");
                            break;
                        }
                    }

                    achievementsDatabase.Initialize();
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "SuccessStory", $"Error on RefreshData({SourceName}, {IsGet})");
                }
            }, tokenSource.Token)
                             .ContinueWith(antecedent =>
            {
                Application.Current.Dispatcher.BeginInvoke((Action) delegate
                {
                    DataLoad.Visibility   = Visibility.Collapsed;
                    tcSettings.Visibility = Visibility.Visible;

                    if (!WithoutMessage)
                    {
                        if (AchievementsDatabase.ListErrors.Get() != string.Empty)
                        {
                            PlayniteApi.Dialogs.ShowErrorMessage(AchievementsDatabase.ListErrors.Get(), "SuccessStory errors");
                        }
                        else
                        {
                            PlayniteApi.Dialogs.ShowMessage((string)ResourceProvider.GetResource("LOCSuccessStoryRefreshDataMessage"), "Success Story");
                        }
                    }

                    SetTotal();

                    SuccessStorySettings.IsEnabled = true;
#if DEBUG
                    stopwatch.Stop();
                    logger.Debug($"SuccessStory - RefreshData() - End - {stopwatch.Elapsed}");
#endif
                });
            });
        }
示例#6
0
        internal void RefreshData(string SourceName, bool IsGet = false)
        {
            // ProgressBar
            SuccessStoryLoad.Visibility = Visibility.Visible;
            SuccessStoryLoad.Value      = 0;
            SuccessStoryLoad.Maximum    = PlayniteApi.Database.Games.Count;

            SuccessStorySettings.IsEnabled = false;


            foreach (var game in PlayniteApi.Database.Games)
            {
                string GameSourceName = "";
                if (game.SourceId != Guid.Parse("00000000-0000-0000-0000-000000000000"))
                {
                    GameSourceName = game.Source.Name;
                }
                else
                {
                    GameSourceName = "Playnite";
                }

                if (GameSourceName.ToLower() == SourceName.ToLower() || SourceName.ToLower() == "all" || SourceName.ToLower() == "allrecent" || SourceName.ToLower() == "allinstalled")
                {
                    bool isOK = true;
                    if (SourceName.ToLower() == "allrecent")
                    {
                        if ((game.LastActivity != null && game.LastActivity > DateTime.Now.AddMonths(-1)) || (game.Added != null && game.Added > DateTime.Now.AddMonths(-1)))
                        {
                            isOK = true;
                        }
                        else
                        {
                            isOK = false;
                        }
                    }
                    if (SourceName.ToLower() == "allinstalled")
                    {
                        if (game.IsInstalled)
                        {
                            isOK = true;
                        }
                        else
                        {
                            isOK = false;
                        }
                    }

                    if (isOK)
                    {
                        Dispatcher.Invoke(new Action(() =>
                        {
                            // Prevent HTTP 429 with limit request per minutes.
                            Thread.Sleep(1000);

                            if (IsGet)
                            {
                                // Add only it's not loaded
                                if (!AchievementsDatabase.VerifAchievementsLoad(game.Id))
                                {
                                    AchievementsDatabase.Add(game, settings);
                                }
                            }
                            else
                            {
                                AchievementsDatabase.Remove(game);
                                AchievementsDatabase.Add(game, settings);
                            }
                        }), DispatcherPriority.ContextIdle, null);
                    }
                }
                SuccessStoryLoad.Value += 1;
            }


            if (AchievementsDatabase.ListErrors.Get() != "")
            {
                PlayniteApi.Dialogs.ShowErrorMessage(AchievementsDatabase.ListErrors.Get(), "SuccesStory errors");
            }
            else
            {
                PlayniteApi.Dialogs.ShowMessage((string)ResourceProvider.GetResource("LOCSucessStoryRefreshDataMessage"), "Success Story");
            }


            SuccessStoryLoad.Visibility    = Visibility.Hidden;
            SuccessStorySettings.IsEnabled = true;
        }