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); }
public static async Task <string> InstallMap(BeatSaverApiResponseMap Map, bool showNotification = true) { string zip = Path.Combine(Utils.BeatSaberPath, CustomSongsFolder, Map.hash) + ".zip"; string mapName = string.Concat(($"{Map.key} ({Map.metadata.songName} - {Map.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(BeatSaverURLPrefix + Map.directDownload, CustomSongsFolder, Map.hash + ".zip", mapName, showNotification, true); } else { await Utils.DownloadAsset(BeatSaverURLPrefix + Map.downloadURL, CustomSongsFolder, Map.hash + ".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."); } // Verify and patch separate audio file if needed var hasFingerprintFile = File.Exists(Path.Combine(directory, "fingerprint.bin")); if (hasFingerprintFile) { var noAudioFile = !Directory.EnumerateFiles(directory).Any(file => { var ext = Path.GetExtension(file); return(ext == ".egg" || ext == ".ogg" || ext == ".wav"); }); if (noAudioFile) { var patchSucccess = await SongPatcher.PromptAndPatchSongFromDisk(directory); if (!patchSucccess) { if (showNotification) { MessageBox.Show($"{Application.Current.FindResource("OneClick:PatchSong:Failed")}"); } throw new Exception("Verification and patching failed."); } } } return(mapName); }