//Download console information public async Task <DownloadInfoConsole> DownloadInfoConsole(string nameConsole, int imageWidth) { try { //Filter the name string nameConsoleSave = FilterNameRom(nameConsole, true, false, false, 0); //Show the text input popup string nameConsoleDownload = await Popup_ShowHide_TextInput("Console search", nameConsoleSave, "Search information for the console", true); if (string.IsNullOrWhiteSpace(nameConsoleDownload)) { Debug.WriteLine("No search term entered."); return(null); } nameConsoleDownload = FilterNameRom(nameConsoleDownload, false, true, false, 0); //Search for consoles IEnumerable <ApiIGDBPlatforms> iGDBPlatforms = vApiIGDBPlatforms.Where(x => FilterNameRom(x.name, false, true, false, 0).Contains(nameConsoleDownload) || (x.alternative_name != null && FilterNameRom(x.alternative_name, false, true, false, 0).Contains(nameConsoleDownload))); if (iGDBPlatforms == null || !iGDBPlatforms.Any()) { Debug.WriteLine("No consoles found"); await Notification_Send_Status("Close", "No consoles found"); return(null); } //Ask user which console to download List <DataBindString> Answers = new List <DataBindString>(); BitmapImage imageAnswer = FileToBitmapImage(new string[] { "Assets/Default/Icons/Emulator.png" }, vImageSourceFolders, vImageBackupSource, IntPtr.Zero, -1, 0); foreach (ApiIGDBPlatforms infoPlatforms in iGDBPlatforms) { DataBindString answerDownload = new DataBindString(); answerDownload.ImageBitmap = imageAnswer; answerDownload.Name = infoPlatforms.name; answerDownload.NameSub = infoPlatforms.alternative_name; answerDownload.Data1 = infoPlatforms; Answers.Add(answerDownload); } //Get selected result DataBindString messageResult = await Popup_Show_MessageBox("Select a found console (" + Answers.Count() + ")", "* Information will be saved in the \"Assets\\User\\Games\\Downloaded\" folder as:\n" + nameConsoleSave, "Download image and description for the console:", Answers); if (messageResult == null) { Debug.WriteLine("No console selected"); return(null); } //Create downloaded directory AVFiles.Directory_Create("Assets/User/Games/Downloaded", false); //Convert result back to json ApiIGDBPlatforms selectedConsole = (ApiIGDBPlatforms)messageResult.Data1; await Notification_Send_Status("Download", "Downloading information"); Debug.WriteLine("Downloading information for: " + nameConsole); //Get the platform versions id string firstPlatformId = selectedConsole.versions.FirstOrDefault().ToString(); ApiIGDBPlatformVersions[] iGDBPlatformVersions = await ApiIGDBDownloadPlatformVersions(firstPlatformId); if (iGDBPlatformVersions == null || !iGDBPlatformVersions.Any()) { Debug.WriteLine("No information found"); await Notification_Send_Status("Close", "No information found"); return(null); } ApiIGDBPlatformVersions targetPlatformVersions = iGDBPlatformVersions.FirstOrDefault(); await Notification_Send_Status("Download", "Downloading image"); Debug.WriteLine("Downloading image for: " + nameConsole); //Get the image url BitmapImage downloadedBitmapImage = null; string downloadImageId = targetPlatformVersions.platform_logo.ToString(); if (downloadImageId != "0") { ApiIGDBImage[] iGDBImages = await ApiIGDB_DownloadImage(downloadImageId, "platform_logos"); 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 File.WriteAllBytes("Assets/User/Games/Downloaded/" + nameConsoleSave + ".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(targetPlatformVersions, jsonSettings); //Save json information File.WriteAllText("Assets/User/Games/Downloaded/" + nameConsoleSave + ".json", serializedObject); await Notification_Send_Status("Download", "Downloaded information"); Debug.WriteLine("Downloaded and saved information for: " + nameConsole); //Return the information DownloadInfoConsole downloadInfo = new DownloadInfoConsole(); downloadInfo.ImageBitmap = downloadedBitmapImage; downloadInfo.Details = targetPlatformVersions; return(downloadInfo); } catch (Exception ex) { Debug.WriteLine("Failed downloading console information: " + ex.Message); await Notification_Send_Status("Close", "Failed downloading"); return(null); } }
//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); } }