//################## Add games to collection ################## private List <GameDownload> getDownloads(GogDataStructures.GameDetails details) { if (details.downloads == null) { return(null); } List <GameDownload> downloads = new List <GameDownload>(); foreach (List <object> downloadObject in details.downloads) { string language = (string)downloadObject[0]; GogDataStructures.DownloadBySystem downloadsForLanguage = JsonConvert.DeserializeObject <GogDataStructures.DownloadBySystem>(((JObject)downloadObject[1]).ToString()); if (downloadsForLanguage == null) { return(null); } List <GogDataStructures.Download> downloadsForWindows = downloadsForLanguage.windows; if (downloadsForWindows == null) { return(null); } foreach (GogDataStructures.Download d in downloadsForWindows) { downloads.Add(new GameDownload { _language = language, _version = d.version, _link = d.manualUrl, _name = d.name }); } } return(downloads); }
private void GetGames_doWork(IProgress <int> progress, IProgress <String> msg) { string json = null; // get ids of agmes owned GogDataStructures.GamesOwned userGames = null; try { json = webClient.DownloadString("https://embed.gog.com/user/data/games"); userGames = JsonConvert.DeserializeObject <GogDataStructures.GamesOwned>(json); } catch (Exception e) { handleApiExceptions(e, json); return; } if (userGames == null || userGames.owned == null) { MessageBox.Show("Could not retrieve owned games."); return; } if (userGames.owned.Count == 0) { MessageBox.Show("No games found."); return; } // Debug Code: reduce number of games for faster testing #if DEBUG reduceResults(userGames, 10); #endif // get game info int noGamesOwned = userGames.owned.Count; int noGamesToScan = noGamesOwned - gamesFound.Count; int counter = 0; int skipped = 0; int failure = 0; string currentGameId = ""; string currentGame = "Unknown"; Dictionary <string, string> errorMessages = new Dictionary <string, string>(); JsonSerializerSettings settings = new JsonSerializerSettings { Error = (sender, e) => handleParsingError(sender, e, errorMessages, currentGame), }; foreach (string gameId in userGames.owned) { string messageText = "Game " + (counter + 1).ToString() + " of " + noGamesOwned.ToString(); // report progress if (progress != null) { progress.Report((counter * 100) / noGamesToScan); } currentGameId = gameId; // don't scan games already found GameImport gameFound = gamesFound.Find(x => x._gogId.Equals(gameId)); if (gameFound != null) { // if set to skip, also remove game from games already found if (skipGogId.Contains(gameId) || skipTitle.Contains(gameFound.title)) { gamesFound.Remove(gameFound); } messageText += ": " + gameFound.title; if (msg != null) { msg.Report(messageText); } continue; } counter++; // skip games by id if (skipGogId.Contains(gameId)) { skipped++; messageText = "Skipping " + messageText; if (msg != null) { msg.Report(messageText); } continue; } GogDataStructures.GameDetails details = null; try { json = webClient.DownloadString("https://embed.gog.com/account/gameDetails/" + gameId.ToString() + ".json"); if (json == null || json.Equals("[]")) { appendError(errorMessages, "Game with Id " + gameId.ToString(), "No game information found (skip game)."); failure++; messageText += ": Unknown"; if (msg != null) { msg.Report(messageText); } continue; } // try to get game title JsonTextReader reader = new JsonTextReader(new StringReader(json)); bool titleFound = false; while (!titleFound) { reader.Read(); if (reader.TokenType == JsonToken.PropertyName && reader.Value != null && reader.Value.Equals("title")) { titleFound = true; break; } } reader.Read(); if (reader.TokenType != JsonToken.String || reader.Value == null || reader.Value.Equals("")) { titleFound = false; } if (!titleFound) { appendError(errorMessages, "Game with Id " + gameId.ToString(), "Invalid game information: game has no title (skip game)."); failure++; messageText += ": Unknown"; if (msg != null) { msg.Report(messageText); } continue; } else { currentGame = reader.Value.ToString(); } // report curent game messageText += ": " + currentGame; if (msg != null) { msg.Report(messageText); } // skip duplicate games by title if (skipTitle.Contains(currentGame)) { skipped++; continue; } // parse game details details = JsonConvert.DeserializeObject <GogDataStructures.GameDetails>(json, settings); } catch (Exception e) { if (e is JsonReaderException || e is JsonSerializationException) { appendError(errorMessages, currentGame, "Error parsing game with id " + gameId + " (skip game): " + e.ToString()); failure++; continue; } else if (e is NullReferenceException) { appendError(errorMessages, currentGame, "Error parsing object " + e.Source + " (skip game)."); failure++; continue; } } if (details == null) { appendError(errorMessages, currentGame, "Could not retrieve game details (skip game)."); failure++; continue; } Debug.Assert(details.title.Equals(currentGame)); // get aditional details //json = webClient.DownloadString("http://api.gog.com/products/" // + gameId.ToString() + "?expand=downloads,expanded_dlcs,description,screenshots,videos,related_products,changelog"); //GogData.MoreGameDetails moreDetails = JsonConvert.DeserializeObject<GogData.MoreGameDetails>(json); // get Downloads List <GameDownload> downloads = null; if (mode.Equals(Mode.linkToDownload)) { downloads = getDownloads(details); if (downloads == null || downloads.Count == 0) { appendError(errorMessages, currentGame, "No Downloads found (skip game)."); failure++; continue; } } // add game to list of found games (stored during launchbox session) gamesFound.Add(new GameImport { _title = currentGame, _importTitle = details.title, _import = true, _gameDetails = details, _downloads = downloads, _selectedDownload = downloads != null ? downloads[0] : null, _gogId = gameId }); } // show errors if (errorMessages.Count > 0) { this.errorForm = new ErrorForm(userGames.owned.Count.ToString() + " games found, " + gamesFound.Count.ToString() + " ready for import, " + skipped.ToString() + " skipped, " + failure.ToString() + " failed to scan, " + (errorMessages.Count - failure).ToString() + " scanned with errors.", errorMessages); } else { MessageBox.Show(gamesFound.Count.ToString() + " games found, " + skipped.ToString() + " skipped."); } }