Exemplo n.º 1
0
        public static string GetSteamData(uint appId, string PlayniteLanguage)
        {
            string url = string.Empty;

            try
            {
                string SteamLangCode = CodeLang.GetSteamLang(PlayniteLanguage);
                url = $"https://store.steampowered.com/api/appdetails?appids={appId}&l={SteamLangCode}";
                return(Web.DownloadStringData(url).GetAwaiter().GetResult());
            }
            catch (Exception ex)
            {
                Common.LogError(ex, "MetadataLocal", $"Failed to load {url}");
                return(string.Empty);
            }
        }
 public SteamAchievements(IPlayniteAPI PlayniteApi, SuccessStorySettings settings, string PluginUserDataPath) : base(PlayniteApi, settings, PluginUserDataPath)
 {
     LocalLang = CodeLang.GetSteamLang(_PlayniteApi.ApplicationSettings.Language);
 }
        /// <summary>
        /// Get all achievements for a Steam game.
        /// </summary>
        /// <param name="PlayniteApi"></param>
        /// <param name="Id"></param>
        /// <param name="PluginUserDataPath"></param>
        /// <returns></returns>
        public GameAchievements GetAchievements(IPlayniteAPI PlayniteApi, Guid Id, string PluginUserDataPath, bool isLocal = false)
        {
            List <Achievements> Achievements = new List <Achievements>();
            string GameName        = PlayniteApi.Database.Games.Get(Id).Name;
            string ClientId        = PlayniteApi.Database.Games.Get(Id).GameId;
            bool   HaveAchivements = false;
            int    Total           = 0;
            int    Unlocked        = 0;
            int    Locked          = 0;

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

            var    url       = "";
            string ResultWeb = "";

            // Get Steam configuration if exist.
            string userId    = "";
            string apiKey    = "";
            string SteamUser = "";

            try
            {
                JObject SteamConfig = JObject.Parse(File.ReadAllText(PluginUserDataPath + "\\..\\CB91DFC9-B977-43BF-8E70-55F46E410FAB\\config.json"));
                userId    = (string)SteamConfig["UserId"];
                apiKey    = (string)SteamConfig["ApiKey"];
                SteamUser = (string)SteamConfig["UserName"];
            }
            catch
            {
            }

            if (userId == "" || apiKey == "" || SteamUser == "")
            {
                logger.Error($"SuccessStory - No Steam configuration.");
                AchievementsDatabase.ListErrors.Add($"Error on SteamAchievements: no Steam configuration and/or API key in settings menu for Steam Library.");
                return(null);
            }

            if (!isLocal)
            {
                string lang = CodeLang.GetSteamLang(Localization.GetPlayniteLanguageConfiguration(PlayniteApi.Paths.ConfigurationPath));

                // List acheviements (default return in english)
                url = string.Format(@"https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid={0}&key={1}&steamid={2}&l={3}",
                                    ClientId, apiKey, userId, lang);

                ResultWeb = "";
                try
                {
                    ResultWeb = HttpDownloader.DownloadString(url, Encoding.UTF8);
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                    {
                        var resp = (HttpWebResponse)ex.Response;
                        switch (resp.StatusCode)
                        {
                        case HttpStatusCode.BadRequest:     // HTTP 400
                            break;

                        case HttpStatusCode.ServiceUnavailable:     // HTTP 503
                            break;

                        default:
                            Common.LogError(ex, "SuccessStory", $"Failed to load from {url}. ");
                            break;
                        }
                        return(Result);
                    }
                }

                if (ResultWeb != "")
                {
                    JObject resultObj   = new JObject();
                    JArray  resultItems = new JArray();

                    try
                    {
                        resultObj = JObject.Parse(ResultWeb);

                        if ((bool)resultObj["playerstats"]["success"])
                        {
                            resultItems = (JArray)resultObj["playerstats"]["achievements"];
                            if (resultItems.Count > 0)
                            {
                                HaveAchivements = true;

                                for (int i = 0; i < resultItems.Count; i++)
                                {
                                    Achievements temp = new Achievements
                                    {
                                        Name         = (string)resultItems[i]["name"],
                                        ApiName      = (string)resultItems[i]["apiname"],
                                        Description  = (string)resultItems[i]["description"],
                                        UrlUnlocked  = "",
                                        UrlLocked    = "",
                                        DateUnlocked = ((int)resultItems[i]["unlocktime"] == 0) ? default(DateTime) : new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds((int)resultItems[i]["unlocktime"])
                                    };

                                    // Achievement without unlocktime but achieved = 1
                                    if ((int)resultItems[i]["achieved"] == 1 && temp.DateUnlocked == new DateTime(1970, 1, 1, 0, 0, 0, 0))
                                    {
                                        temp.DateUnlocked = new DateTime(1982, 12, 15, 0, 0, 0, 0);
                                    }

                                    Total += 1;
                                    if ((int)resultItems[i]["unlocktime"] == 0)
                                    {
                                        Locked += 1;
                                    }
                                    else
                                    {
                                        Unlocked += 1;
                                    }

                                    Achievements.Add(temp);
                                }
                            }
                            else
                            {
                                logger.Info($"SuccessStory - No list achievement for {ClientId}. ");
                                return(Result);
                            }
                        }
                        else
                        {
                            logger.Info($"SuccessStory - No Succes for {ClientId}. ");
                            return(Result);
                        }
                    }
                    catch (Exception ex)
                    {
                        Common.LogError(ex, "SuccessStory", $"[{ClientId}] Failed to parse {ResultWeb}");
                        return(Result);
                    }


                    // List details acheviements
                    url = string.Format(@"https://api.steampowered.com/ISteamUserStats/GetSchemaForGame/v2/?key={0}&appid={1}&l={2}",
                                        apiKey, ClientId, lang);

                    ResultWeb = "";
                    try
                    {
                        ResultWeb = HttpDownloader.DownloadString(url, Encoding.UTF8);
                    }
                    catch (WebException ex)
                    {
                        if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                        {
                            var resp = (HttpWebResponse)ex.Response;
                            switch (resp.StatusCode)
                            {
                            case HttpStatusCode.BadRequest:     // HTTP 400
                                break;

                            case HttpStatusCode.ServiceUnavailable:     // HTTP 503
                                break;

                            default:
                                Common.LogError(ex, "SuccessStory", $"Failed to load from {url}");
                                break;
                            }
                            return(Result);
                        }
                    }

                    if (ResultWeb != "")
                    {
                        resultObj = JObject.Parse(ResultWeb);

                        try
                        {
                            resultItems = (JArray)resultObj["game"]["availableGameStats"]["achievements"];

                            for (int i = 0; i < resultItems.Count; i++)
                            {
                                for (int j = 0; j < Achievements.Count; j++)
                                {
                                    if (Achievements[j].ApiName.ToLower() == ((string)resultItems[i]["name"]).ToLower())
                                    {
                                        Achievements temp = new Achievements
                                        {
                                            Name         = (string)resultItems[i]["displayName"],
                                            ApiName      = Achievements[j].ApiName,
                                            Description  = Achievements[j].Description,
                                            UrlUnlocked  = (string)resultItems[i]["icon"],
                                            UrlLocked    = (string)resultItems[i]["icongray"],
                                            DateUnlocked = Achievements[j].DateUnlocked
                                        };

                                        if ((int)resultItems[i]["hidden"] == 1)
                                        {
                                            temp.Description = FindHiddenDescription(SteamUser, userId, ClientId, temp.Name, lang);
                                        }

                                        Achievements[j] = temp;
                                        j = Achievements.Count;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Common.LogError(ex, "SuccessStory", $"Failed to parse");
                            return(Result);
                        }
                    }
                }

                //logger.Info($"SuccessStory - No data for {ClientId}. ");

                Result = new GameAchievements
                {
                    Name            = GameName,
                    HaveAchivements = HaveAchivements,
                    Total           = Total,
                    Unlocked        = Unlocked,
                    Locked          = Locked,
                    Progression     = (Total != 0) ? (int)Math.Ceiling((double)(Unlocked * 100 / Total)) : 0,
                    Achievements    = Achievements
                };
            }
            else
            {
                SteamEmulators se = new SteamEmulators(PlayniteApi, PluginUserDataPath);
                ClientId = se.GetSteamId().ToString();

                var temp = se.GetAchievementsLocal(GameName, apiKey);

                if (temp.Achievements.Count > 0)
                {
                    Result.HaveAchivements = true;
                    Result.Total           = temp.Total;
                    Result.Locked          = temp.Locked;
                    Result.Unlocked        = temp.Unlocked;
                    Result.Progression     = temp.Progression;

                    for (int i = 0; i < temp.Achievements.Count; i++)
                    {
                        Result.Achievements.Add(new Achievements
                        {
                            Name         = temp.Achievements[i].Name,
                            Description  = temp.Achievements[i].Description,
                            UrlUnlocked  = temp.Achievements[i].UrlUnlocked,
                            UrlLocked    = temp.Achievements[i].UrlLocked,
                            DateUnlocked = temp.Achievements[i].DateUnlocked
                        });
                    }
                }
            }

            // Percentages
            url = string.Format(@"http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid={0}&format=json",
                                ClientId);
            ResultWeb = "";
            try
            {
                ResultWeb = HttpDownloader.DownloadString(url, Encoding.UTF8);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    switch (resp.StatusCode)
                    {
                    case HttpStatusCode.BadRequest:     // HTTP 400
                        break;

                    case HttpStatusCode.ServiceUnavailable:     // HTTP 503
                        break;

                    default:
                        Common.LogError(ex, "SuccessStory", $"Failed to load from {url}. ");
                        break;
                    }
                    return(Result);
                }
            }

            if (ResultWeb != "")
            {
                JObject resultObj   = new JObject();
                JArray  resultItems = new JArray();

                try
                {
                    resultObj = JObject.Parse(ResultWeb);

                    if (resultObj["achievementpercentages"]["achievements"] != null)
                    {
                        foreach (JObject data in resultObj["achievementpercentages"]["achievements"])
                        {
                            for (int i = 0; i < Result.Achievements.Count; i++)
                            {
                                if (Result.Achievements[i].ApiName == (string)data["name"])
                                {
                                    Result.Achievements[i].Percent = (float)data["percent"];
                                    i = Result.Achievements.Count;
                                }
                            }
                        }
                    }
                    else
                    {
                        logger.Info($"SuccessStory - No percentages for {ClientId}. ");
                        return(Result);
                    }
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "SuccessStory", $"[{ClientId}] Failed to parse {ResultWeb}");
                    return(Result);
                }
            }


            return(Result);
        }