//Generate summary string from ApiIGDB string ApiIGDB_GameSummaryString(ApiIGDBGames infoGames) { string summaryString = string.Empty; try { //Release date if (infoGames.first_release_date != 0) { string gameReleaseDate = string.Empty; string gameReleaseYear = string.Empty; ApiIGDB_ReleaseDateToString(infoGames, out gameReleaseDate, out gameReleaseYear); summaryString += "\nReleased: " + gameReleaseDate; } //Genres string gameGenres = string.Empty; if (infoGames.genres != null) { foreach (int genreId in infoGames.genres) { ApiIGDBGenres apiIGDBGenres = vApiIGDBGenres.Where(x => x.id == genreId).FirstOrDefault(); gameGenres = AVFunctions.StringAdd(gameGenres, apiIGDBGenres.name, ","); } if (!string.IsNullOrWhiteSpace(gameGenres)) { summaryString += "\nGenres: " + gameGenres; } } //Summary if (string.IsNullOrWhiteSpace(infoGames.summary)) { summaryString += "\n\nThere is no description available."; } else { summaryString += "\n\n" + infoGames.summary; } //Remove first linebreak summaryString = AVFunctions.StringRemoveStart(summaryString, "\n"); } catch { } if (string.IsNullOrWhiteSpace(summaryString)) { summaryString = "There is no description available."; } return(summaryString); }
//ApiIGDB ReleaseDate to string void ApiIGDB_ReleaseDateToString(ApiIGDBGames infoGames, out string gameReleaseDate, out string gameReleaseYear) { gameReleaseDate = "Unknown"; gameReleaseYear = "N/A"; try { if (infoGames.first_release_date != 0) { DateTime epochDateTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(infoGames.first_release_date); gameReleaseDate = epochDateTime.ToString("d MMMM yyyy", vAppCultureInfo); gameReleaseYear = epochDateTime.ToString("yyyy", vAppCultureInfo); } } catch { } }
//ApiIGDB Platforms to string void ApiIGDB_PlatformsToString(ApiIGDBGames infoGames, out string gamePlatforms) { gamePlatforms = string.Empty; try { foreach (int platformId in infoGames.platforms) { ApiIGDBPlatforms apiIGDBPlatforms = vApiIGDBPlatforms.Where(x => x.id == platformId).FirstOrDefault(); if (apiIGDBPlatforms != null) { gamePlatforms = AVFunctions.StringAdd(gamePlatforms, apiIGDBPlatforms.name, ","); } } } catch { } if (string.IsNullOrWhiteSpace(gamePlatforms)) { gamePlatforms = "Unknown"; } }
//Get rom details image and description void GetRomDetails(string listName, string listPath, FileInfo[] directoryRomImages, FileInfo[] directoryRomDescription, ref BitmapImage listImage, ref string listDescription) { try { //Set rom file names string romPathImage = string.Empty; string romPathDescription = string.Empty; string romNameFiltered = string.Empty; //Create sub directory image paths string subPathImagePng = string.Empty; string subPathImageJpg = string.Empty; string subPathDescription = string.Empty; if (!string.IsNullOrWhiteSpace(listPath)) { romNameFiltered = FilterNameRom(listName, false, true, false, 0); subPathImagePng = Path.Combine(listPath, listName + ".png"); subPathImageJpg = Path.Combine(listPath, listName + ".jpg"); subPathDescription = Path.Combine(listPath, listName + ".txt"); } else { romNameFiltered = FilterNameRom(listName, true, true, false, 0); } //Check if rom directory has image foreach (FileInfo foundImage in directoryRomImages) { try { string imageNameFiltered = FilterNameRom(foundImage.Name, true, true, false, 0); //Debug.WriteLine(imageNameFiltered + " / " + romNameFiltered); if (romNameFiltered.Contains(imageNameFiltered)) { romPathImage = foundImage.FullName; break; } } catch { } } //Check if rom directory has description foreach (FileInfo foundDesc in directoryRomDescription) { try { string descNameFiltered = FilterNameRom(foundDesc.Name, true, true, false, 0); //Debug.WriteLine(descNameFiltered + " / " + romNameFiltered); if (romNameFiltered.Contains(descNameFiltered)) { romPathDescription = foundDesc.FullName; break; } } catch { } } //Update description and image listImage = FileToBitmapImage(new string[] { romPathImage, subPathImagePng, subPathImageJpg, "Rom" }, vImageSourceFolders, vImageBackupSource, IntPtr.Zero, 210, 0); string jsonFile = FileToString(new string[] { romPathDescription, subPathDescription }); if (jsonFile.Contains("platform_logo")) { ApiIGDBPlatformVersions platformVersionsJson = JsonConvert.DeserializeObject <ApiIGDBPlatformVersions>(jsonFile); listDescription = ApiIGDB_ConsoleSummaryString(platformVersionsJson); } else { ApiIGDBGames gamesJson = JsonConvert.DeserializeObject <ApiIGDBGames>(jsonFile); listDescription = ApiIGDB_GameSummaryString(gamesJson); } } catch { } }
//Download game information public async Task <DownloadInfoGame> DownloadInfoGame(string nameRom, int imageWidth, bool saveOriginalName) { try { //Filter the game name string nameRomSaveOriginal = FilterNameFile(nameRom); string nameRomSaveFilter = FilterNameRom(nameRom, true, false, true, 0); //Show the text input popup string nameRomDownload = await Popup_ShowHide_TextInput("Game search", nameRomSaveFilter, "Search information for the game", true); if (string.IsNullOrWhiteSpace(nameRomDownload)) { Debug.WriteLine("No search term entered."); return(null); } nameRomDownload = nameRomDownload.ToLower(); await Notification_Send_Status("Download", "Downloading information"); Debug.WriteLine("Downloading information for: " + nameRom); //Download available games IEnumerable <ApiIGDBGames> iGDBGames = await ApiIGDB_DownloadGames(nameRomDownload); if (iGDBGames == null || !iGDBGames.Any()) { Debug.WriteLine("No games found"); await Notification_Send_Status("Close", "No games found"); return(null); } //Ask user which game to download List <DataBindString> Answers = new List <DataBindString>(); BitmapImage imageAnswer = FileToBitmapImage(new string[] { "Assets/Default/Icons/Game.png" }, vImageSourceFolders, vImageBackupSource, IntPtr.Zero, -1, 0); foreach (ApiIGDBGames infoGames in iGDBGames) { //Check if information is available if (infoGames.cover == 0 && string.IsNullOrWhiteSpace(infoGames.summary)) { continue; } //Release date string gameReleaseDate = string.Empty; string gameReleaseYear = string.Empty; ApiIGDB_ReleaseDateToString(infoGames, out gameReleaseDate, out gameReleaseYear); //Game platforms string gamePlatforms = string.Empty; if (infoGames.platforms != null) { foreach (int platformId in infoGames.platforms) { ApiIGDBPlatforms apiIGDBPlatforms = vApiIGDBPlatforms.Where(x => x.id == platformId).FirstOrDefault(); gamePlatforms = AVFunctions.StringAdd(gamePlatforms, apiIGDBPlatforms.name, ","); } } DataBindString answerDownload = new DataBindString(); answerDownload.ImageBitmap = imageAnswer; answerDownload.Name = infoGames.name; answerDownload.NameSub = gamePlatforms; answerDownload.NameDetail = gameReleaseYear; answerDownload.Data1 = infoGames; Answers.Add(answerDownload); } //Get selected result DataBindString messageResult = await Popup_Show_MessageBox("Select a found game (" + Answers.Count() + ")", "* Information will be saved in the \"Assets\\User\\Games\\Downloaded\" folder as:\n" + nameRomSaveFilter, "Download image and description for the game:", Answers); if (messageResult == null) { Debug.WriteLine("No game selected"); return(null); } //Create downloaded directory AVFiles.Directory_Create("Assets/User/Games/Downloaded", false); //Convert result back to json ApiIGDBGames selectedGame = (ApiIGDBGames)messageResult.Data1; await Notification_Send_Status("Download", "Downloading image"); Debug.WriteLine("Downloading image for: " + nameRom); //Get the image url BitmapImage downloadedBitmapImage = null; string downloadImageId = selectedGame.cover.ToString(); if (downloadImageId != "0") { ApiIGDBImage[] iGDBImages = await ApiIGDB_DownloadImage(downloadImageId, "covers"); if (iGDBImages == null || !iGDBImages.Any()) { Debug.WriteLine("No images found"); await Notification_Send_Status("Close", "No images found"); return(null); } //Download and save image ApiIGDBImage infoImages = iGDBImages.FirstOrDefault(); Uri imageUri = new Uri("https://images.igdb.com/igdb/image/upload/t_720p/" + infoImages.image_id + ".png"); byte[] imageBytes = await AVDownloader.DownloadByteAsync(5000, "CtrlUI", null, imageUri); if (imageBytes != null && imageBytes.Length > 256) { try { //Convert bytes to a BitmapImage downloadedBitmapImage = BytesToBitmapImage(imageBytes, imageWidth); //Save bytes to image file if (saveOriginalName) { File.WriteAllBytes("Assets/User/Games/Downloaded/" + nameRomSaveOriginal + ".png", imageBytes); } else { File.WriteAllBytes("Assets/User/Games/Downloaded/" + nameRomSaveFilter + ".png", imageBytes); } Debug.WriteLine("Saved image: " + imageBytes.Length + "bytes/" + imageUri); } catch { } } } //Json settings JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); jsonSettings.NullValueHandling = NullValueHandling.Ignore; //Json serialize string serializedObject = JsonConvert.SerializeObject(selectedGame, jsonSettings); //Save json information if (saveOriginalName) { File.WriteAllText("Assets/User/Games/Downloaded/" + nameRomSaveOriginal + ".json", serializedObject); } else { File.WriteAllText("Assets/User/Games/Downloaded/" + nameRomSaveFilter + ".json", serializedObject); } await Notification_Send_Status("Download", "Downloaded information"); Debug.WriteLine("Downloaded and saved information for: " + nameRom); //Return the information DownloadInfoGame downloadInfo = new DownloadInfoGame(); downloadInfo.ImageBitmap = downloadedBitmapImage; downloadInfo.Details = selectedGame; return(downloadInfo); } catch (Exception ex) { Debug.WriteLine("Failed downloading game information: " + ex.Message); await Notification_Send_Status("Close", "Failed downloading"); return(null); } }