private static async Task <BeatSaverMap> GetMap(string id, string type, bool showNotification) { string urlSegment; switch (type) { case "hash": urlSegment = "/maps/hash/"; break; case "key": urlSegment = "/maps/id/"; break; default: return(null); } BeatSaverMap map = new BeatSaverMap { Success = false }; if (showNotification) { Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Installing"), id)}"); } try { BeatSaverApiResponse beatsaver = await GetResponse(BeatSaverURLPrefix + urlSegment + id); if (beatsaver != null && beatsaver.map != null) { map.response = beatsaver; if (type == "hash") { map.HashToDownload = id.ToLower(); } else { BeatSaverApiResponseMap.BeatsaverMapVersion mapVersion = null; foreach (var version in map.response.map.versions) { if (mapVersion == null || version.createdAt > mapVersion.createdAt) { mapVersion = version; } } map.HashToDownload = mapVersion.hash; } map.Name = await InstallMap(map, showNotification); map.Success = true; } } catch (Exception e) { ModAssistant.Utils.Log($"Failed downloading BeatSaver map: {id} | Error: {e.Message}", "ERROR"); Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Failed"), (map.Name ?? id))}"); App.CloseWindowOnFinish = false; } return(map); }
public static async Task <string> InstallMap(BeatSaverMap Map, bool showNotification = true) { BeatSaverApiResponseMap responseMap = Map.response.map; BeatSaverApiResponseMap.BeatsaverMapVersion mapVersion = responseMap.versions.Where(r => r.hash == Map.HashToDownload).First(); if (mapVersion == null) { throw new Exception("Could not find map version."); } string zip = Path.Combine(Utils.BeatSaberPath, CustomSongsFolder, Map.HashToDownload) + ".zip"; string mapName = string.Concat(($"{responseMap.id} ({responseMap.metadata.songName} - {responseMap.metadata.levelAuthorName})") .Split(ModAssistant.Utils.Constants.IllegalCharacters)); string directory = Path.Combine(Utils.BeatSaberPath, CustomSongsFolder, mapName); #pragma warning disable CS0162 // Unreachable code detected if (BypassDownloadCounter) { await Utils.DownloadAsset(mapVersion.downloadURL, CustomSongsFolder, Map.HashToDownload + ".zip", mapName, showNotification, true); } else { await Utils.DownloadAsset(mapVersion.downloadURL, CustomSongsFolder, Map.HashToDownload + ".zip", mapName, showNotification, true); } #pragma warning restore CS0162 // Unreachable code detected if (File.Exists(zip)) { string mimeType = MimeMapping.GetMimeMapping(zip); if (!mimeType.StartsWith("application/x-zip")) { ModAssistant.Utils.Log($"Failed extracting BeatSaver map: {zip} \n| Content: {string.Join("\n", File.ReadAllLines(zip))}", "ERROR"); throw new Exception("File not a zip."); } try { using (FileStream stream = new FileStream(zip, FileMode.Open)) using (ZipArchive archive = new ZipArchive(stream)) { foreach (ZipArchiveEntry file in archive.Entries) { string fileDirectory = Path.GetDirectoryName(Path.Combine(directory, file.FullName)); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } if (!string.IsNullOrEmpty(file.Name)) { file.ExtractToFile(Path.Combine(directory, file.FullName), true); } } } } catch (Exception e) { File.Delete(zip); ModAssistant.Utils.Log($"Failed extracting BeatSaver map: {zip} | Error: {e} \n| Content: {string.Join("\n", File.ReadAllLines(zip))}", "ERROR"); throw new Exception("File extraction failed."); } File.Delete(zip); } else { if (showNotification) { string line1 = (string)Application.Current.FindResource("OneClick:SongDownload:Failed"); string line2 = (string)Application.Current.FindResource("OneClick:SongDownload:NetworkIssues"); string title = (string)Application.Current.FindResource("OneClick:SongDownload:FailedTitle"); MessageBox.Show($"{line1}\n{line2}", title); } throw new Exception("Zip file not found."); } return(mapName); }