private List <RA_MD5List> GetMD5List(string PluginUserDataPath, RA_Consoles rA_Consoles)
        {
            List <RA_MD5List> ListMD5 = new List <RA_MD5List>();

            // Cache
            string fileMD5List = PluginUserDataPath + "\\RA_MD5List.json";

            if (File.Exists(fileMD5List) && File.GetLastWriteTime(fileMD5List).AddDays(3) > DateTime.Now)
            {
                ListMD5 = JsonConvert.DeserializeObject <List <RA_MD5List> >(File.ReadAllText(fileMD5List));
                return(ListMD5);
            }

            // Web
            foreach (RA_Console rA_Console in rA_Consoles.ListConsoles)
            {
                int ConsoleId = rA_Console.ID;

                try
                {
                    string ResultWeb = Web.DownloadStringData(string.Format(BaseMD5List, ConsoleId)).GetAwaiter().GetResult();
                    if (!ResultWeb.Contains("\"MD5List\":[]"))
                    {
                        RA_MD5ListResponse ResultMD5List = JsonConvert.DeserializeObject <RA_MD5ListResponse>(ResultWeb);

                        foreach (var obj in ResultMD5List.MD5List)
                        {
                            ListMD5.Add(new RA_MD5List {
                                Id = (int)obj.Value, MD5 = obj.Key
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "SuccessStory", $"Error GetConsoleIDs({rA_Console.ID}, {rA_Console.Name})");
                }
            }

            // Save
            if (ListMD5.Count > 0)
            {
                try
                {
                    File.WriteAllText(fileMD5List, JsonConvert.SerializeObject(ListMD5));
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "SuccessStory", $"Failed to save ListMD5");
                }
            }

            return(ListMD5);
        }
        private RA_Consoles GetConsoleIDs(string PluginUserDataPath)
        {
            string Target = "API_GetConsoleIDs.php";
            string url    = string.Format(BaseUrl + Target + @"?z={0}&y={1}", User, Key);

            RA_Consoles resultObj = new RA_Consoles();

            string fileConsoles = PluginUserDataPath + "\\RA_Consoles.json";

            if (File.Exists(fileConsoles))
            {
                resultObj = JsonConvert.DeserializeObject <RA_Consoles>(File.ReadAllText(fileConsoles));
                return(resultObj);
            }

            string ResultWeb = string.Empty;

            try
            {
                ResultWeb = Web.DownloadStringData(url).GetAwaiter().GetResult();
            }
            catch (WebException ex)
            {
                Common.LogError(ex, "SuccessStory", $"Failed to load from {url}");
            }


            if (!ResultWeb.IsNullOrEmpty())
            {
                try
                {
                    resultObj.ListConsoles = JsonConvert.DeserializeObject <List <RA_Console> >(ResultWeb);
                    File.WriteAllText(fileConsoles, JsonConvert.SerializeObject(resultObj));
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "SuccessStory", $"Failed to parse {ResultWeb}");
                }
            }

            return(resultObj);
        }
        public override GameAchievements GetAchievements(Game game)
        {
            List <Achievements> AllAchievements = new List <Achievements>();
            string GameName = game.Name;
            string ClientId = game.PlayAction.EmulatorId.ToString();

            GameAchievements Result = SuccessStory.PluginDatabase.GetDefault(game);

            Result.Items = AllAchievements;


            if (User == string.Empty || Key == string.Empty)
            {
                logger.Error($"SuccessStory - No RetroAchievement configuration.");
                SuccessStoryDatabase.ListErrors.Add($"Error on RetroAchievement: no RetroAchievement configuration in settings menu of plugin.");
                return(null);
            }

            // Load list console
            RA_Consoles ra_Consoles = GetConsoleIDs(_PluginUserDataPath);

            if (ra_Consoles != null && ra_Consoles != new RA_Consoles())
            {
                ra_Consoles.ListConsoles.Sort((x, y) => (y.Name).CompareTo(x.Name));
            }
            else
            {
                logger.Warn($"SuccessStory - No ra_Consoles find");
            }

            // List MD5
            List <RA_MD5List> ListMD5 = GetMD5List(_PluginUserDataPath, ra_Consoles);


            // Game Id
            int gameID = 0;

            gameID = GetGameIdByHash(game, ListMD5);
            if (gameID == 0)
            {
                gameID = GetGameIdByName(game, ra_Consoles);
            }

            // Get achievements
            if (gameID != 0)
            {
                AllAchievements = GetGameInfoAndUserProgress(gameID);
            }
            else
            {
                return(Result);
            }

            Result.HaveAchivements = (AllAchievements.Count > 0);
            Result.Items           = AllAchievements;
            Result.Total           = AllAchievements.Count;
            Result.Unlocked        = AllAchievements.FindAll(x => x.DateUnlocked != default(DateTime)).Count;
            Result.Locked          = Result.Total - Result.Unlocked;
            Result.Progression     = (Result.Total != 0) ? (int)Math.Ceiling((double)(Result.Unlocked * 100 / Result.Total)) : 0;


            return(Result);
        }
        private int GetGameIdByName(Game game, RA_Consoles ra_Consoles)
        {
            string GameName = game.Name;

            // Search id console for the game
            string PlatformName = game.Platform.Name;
            int    consoleID    = 0;

            var FindConsole = ra_Consoles.ListConsoles.Find(x => PlatformName.ToLower().Contains(x.Name.ToLower()));

            if (FindConsole != null)
            {
                consoleID = FindConsole.ID;
            }

            if (consoleID != 0)
            {
                foreach (RA_Console ra_Console in ra_Consoles.ListConsoles)
                {
                    string NameConsole = ra_Console.Name.ToLower();
                    if (NameConsole == "snes")
                    {
                        NameConsole = "super nintendo";
                    }
                    if (NameConsole == "nes")
                    {
                        NameConsole = "nintendo";
                    }
                    if (NameConsole == "mega drive")
                    {
                        NameConsole = "sega genesis";
                    }

                    if (PlatformName.ToLower().IndexOf(NameConsole) > -1)
                    {
                        consoleID = ra_Console.ID;
                        break;
                    }
                }
            }

            // Search game id
            int gameID = 0;

            if (consoleID != 0)
            {
                RA_Games ra_Games = GetGameList(consoleID, _PluginUserDataPath);
                ra_Games.ListGames.Sort((x, y) => (y.Title).CompareTo(x.Title));
                foreach (RA_Game ra_Game in ra_Games.ListGames)
                {
                    string Title = ra_Game.Title.Trim().ToLower();
                    if (GameName.Trim().ToLower() == Title && gameID == 0)
                    {
                        logger.Info($"SuccessStory - Find for {GameName.Trim().ToLower()} / {Title} with {PlatformName} in {consoleID}");
                        gameID = ra_Game.ID;
                        break;
                    }

                    string[] TitleSplits = Title.Split('|');
                    if (TitleSplits.Length > 1)
                    {
                        foreach (string TitleSplit in TitleSplits)
                        {
                            if (GameName.Trim().ToLower() == TitleSplit.Trim() && gameID == 0)
                            {
                                logger.Info($"SuccessStory - Find for {GameName.Trim().ToLower()} / {TitleSplit.Trim()} with {PlatformName} in {consoleID}");
                                gameID = ra_Game.ID;
                                break;
                            }
                        }
                    }

                    TitleSplits = Title.Split('-');
                    if (TitleSplits.Length > 1)
                    {
                        foreach (string TitleSplit in TitleSplits)
                        {
                            if (GameName.Trim().ToLower() == TitleSplit.Trim() && gameID == 0)
                            {
                                logger.Info($"SuccessStory - Find for {GameName.Trim().ToLower()} / {TitleSplit.Trim()} with {PlatformName} in {consoleID}");
                                gameID = ra_Game.ID;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                logger.Warn($"SuccessStory - No console find for {GameName} with {PlatformName}");
            }

            if (gameID == 0)
            {
                logger.Warn($"SuccessStory - No game find for {GameName} with {PlatformName} in {consoleID}");
            }

            return(gameID);
        }
Ejemplo n.º 5
0
        public GameAchievements GetAchievements(IPlayniteAPI PlayniteApi, Guid Id, string PluginUserDataPath)
        {
            List <Achievements> Achievements = new List <Achievements>();
            Game   game            = PlayniteApi.Database.Games.Get(Id);
            string GameName        = game.Name;
            string ClientId        = game.PlayAction.EmulatorId.ToString();
            bool   HaveAchivements = false;
            int    Total           = 0;
            int    Unlocked        = 0;
            int    Locked          = 0;

            GameAchievements Result = new GameAchievements
            {
                Name            = GameName,
                HaveAchivements = HaveAchivements,
                IsEmulators     = true,
                Total           = Total,
                Unlocked        = Unlocked,
                Locked          = Locked,
                Progression     = 0,
                Achievements    = Achievements
            };


            if (User == "" || Key == "")
            {
                logger.Error($"SuccessStory - No RetroAchievement configuration.");
                AchievementsDatabase.ListErrors.Add($"Error on RetroAchievement: no RetroAchievement configuration in settings menu of plugin.");
                return(null);
            }

            // Load list console
            RA_Consoles ra_Consoles = GetConsoleIDs(PluginUserDataPath);

            ra_Consoles.ListConsoles.Sort((x, y) => (y.Name).CompareTo(x.Name));

            // Search id console for the game
            string PlatformName = game.Platform.Name;
            int    consoleID    = 0;

            foreach (RA_Console ra_Console in ra_Consoles.ListConsoles)
            {
                string NameConsole = ra_Console.Name.ToLower();
                if (NameConsole == "snes")
                {
                    NameConsole = "super nintendo";
                }
                if (NameConsole == "nes")
                {
                    NameConsole = "nintendo";
                }
                if (NameConsole == "mega drive")
                {
                    NameConsole = "sega genesis";
                }

                if (PlatformName.ToLower().IndexOf(NameConsole) > -1)
                {
                    consoleID = ra_Console.ID;
                    break;
                }
            }

            // Search game id
            int gameID = 0;

            if (consoleID != 0)
            {
                RA_Games ra_Games = GetGameList(consoleID, PluginUserDataPath);
                ra_Games.ListGames.Sort((x, y) => (y.Title).CompareTo(x.Title));
                foreach (RA_Game ra_Game in ra_Games.ListGames)
                {
                    string Title = ra_Game.Title.Trim().ToLower();
                    //logger.Debug($"SuccessStory - {GameName.Trim().ToLower()} / {Title} / {gameID}");
                    if (GameName.Trim().ToLower() == Title && gameID == 0)
                    {
                        logger.Info($"SuccessStory - Find for {GameName.Trim().ToLower()} / {Title} with {PlatformName} in {consoleID}");
                        gameID = ra_Game.ID;
                        break;
                    }

                    string[] TitleSplits = Title.Split('|');
                    if (TitleSplits.Length > 1)
                    {
                        foreach (string TitleSplit in TitleSplits)
                        {
                            //logger.Debug($"SuccessStory - {GameName.Trim().ToLower()} / {TitleSplit.Trim()} / {gameID}");
                            if (GameName.Trim().ToLower() == TitleSplit.Trim() && gameID == 0)
                            {
                                logger.Info($"SuccessStory - Find for {GameName.Trim().ToLower()} / {TitleSplit.Trim()} with {PlatformName} in {consoleID}");
                                gameID = ra_Game.ID;
                                break;
                            }
                        }
                    }

                    TitleSplits = Title.Split('-');
                    if (TitleSplits.Length > 1)
                    {
                        foreach (string TitleSplit in TitleSplits)
                        {
                            //logger.Debug($"SuccessStory - {GameName.Trim().ToLower()} / {TitleSplit.Trim()} / {gameID}");
                            if (GameName.Trim().ToLower() == TitleSplit.Trim() && gameID == 0)
                            {
                                logger.Info($"SuccessStory - Find for {GameName.Trim().ToLower()} / {TitleSplit.Trim()} with {PlatformName} in {consoleID}");
                                gameID = ra_Game.ID;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                logger.Info($"SuccessStory - No console find for {GameName} with {PlatformName}");
                return(Result);
            }

            // Get achievements
            if (gameID != 0)
            {
                Achievements = GetGameInfoAndUserProgress(gameID);
            }
            else
            {
                logger.Info($"SuccessStory - No game find for {GameName} with {PlatformName} in {consoleID}");
                return(Result);
            }

            Result.HaveAchivements = (Achievements.Count > 0);
            Result.Achievements    = Achievements;
            Result.Total           = Achievements.Count;
            Result.Unlocked        = Achievements.FindAll(x => x.DateUnlocked != default(DateTime)).Count;
            Result.Locked          = Result.Total - Result.Unlocked;
            Result.Progression     = (Result.Total != 0) ? (int)Math.Ceiling((double)(Result.Unlocked * 100 / Result.Total)) : 0;


            return(Result);
        }