/// <summary> /// Convert a Beat Saber Zip file into a Minecraft Resourcepack and Datapack /// </summary> /// <param name="zipPath">file path to beat saber zip file</param> /// <param name="datapackOutputPath">folder path that minecraft zips will be generated</param> /// <param name="uuid">Unique number that determines song value</param> /// <param name="cancellationToken">Token that allows async function to be canceled</param> /// <returns>-1 on Success</returns> public static async Task <ConversionError> ConvertAsync(string zipPath, string datapackOutputPath, int uuid, IProgress <ConversionProgress> progress, CancellationToken cancellationToken) { if (!File.Exists(zipPath) || !Directory.Exists(datapackOutputPath)) { return(ConversionError.MissingInfo); } progress.Report(new ConversionProgress(0.1f, "Loading beat map file")); var beatSaberMap = await MapLoader.GetDataFromMapZip(zipPath, ProcessManager.temporaryPath, cancellationToken); if (beatSaberMap == null) { return(ConversionError.InvalidBeatMap); } cancellationToken.ThrowIfCancellationRequested(); var tempFolder = beatSaberMap.ExtractedFilePath; try { beatSaberMap = ConvertFilesEggToOgg(beatSaberMap); beatSaberMap = ConvertFilesJpgToPng(beatSaberMap); if (beatSaberMap.InfoData.DifficultyBeatmapSets.Length == 0) { return(ConversionError.NoMapData); } cancellationToken.ThrowIfCancellationRequested(); // Generating Resource pack progress.Report(new ConversionProgress(0.2f, "Generating resource pack")); var resourcepackError = await ResourcePack.FromBeatSaberData(datapackOutputPath, beatSaberMap); if (resourcepackError != ConversionError.None) { return(resourcepackError); } cancellationToken.ThrowIfCancellationRequested(); // Generating Data pack progress.Report(new ConversionProgress(0.3f, "Generating datapack")); var datapackError = await DataPack.FromBeatSaberData(datapackOutputPath, beatSaberMap, progress, cancellationToken); if (datapackError != ConversionError.None) { return(datapackError); } } catch (OperationCanceledException e) { SafeFileManagement.DeleteDirectory(tempFolder); throw (e); } catch (ObjectDisposedException) { SafeFileManagement.DeleteDirectory(tempFolder); } // Successfully converted map SafeFileManagement.DeleteDirectory(tempFolder); return(ConversionError.None); }
/// <summary> /// Get all data from beatsaber map zip file /// </summary> /// <param name="fileToUnzip">path to beat saber map zip</param> /// <param name="pathToUnzip">path to unzip map data</param> /// <param name="cancellationToken">token to cancel action</param> /// <returns></returns> public static async Task <BeatSaberMap> GetDataFromMapZip(string fileToUnzip, string pathToUnzip, CancellationToken cancellationToken) { string tempUnZipPath = Path.Combine(pathToUnzip, "MapLoader", SafeFileManagement.GetFolderName(fileToUnzip)); if (Directory.Exists(tempUnZipPath)) { SafeFileManagement.DeleteDirectory(tempUnZipPath); } Directory.CreateDirectory(tempUnZipPath); await Archive.DecompressAsync(fileToUnzip, tempUnZipPath, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); return(await Task.Run(() => { string infoPath = Path.Combine(tempUnZipPath, "info.dat"); Info info = JsonUtility.FromJson <Info>(SafeFileManagement.GetFileContents(infoPath)); if (info == null) { return null; } info = ConvertSoundFile(tempUnZipPath, info); if (info == null) { return null; } info = ConvertImageFiles(tempUnZipPath, info); if (info == null) { return null; } cancellationToken.ThrowIfCancellationRequested(); Dictionary <string, MapDataInfo> mapDataInfos = new Dictionary <string, MapDataInfo>(); foreach (var beatMapSets in info.DifficultyBeatmapSets) { foreach (var beatMap in beatMapSets.DifficultyBeatmaps) { string mapPath = Path.Combine(tempUnZipPath, beatMap.BeatmapFilename); MapData mapData = JsonUtility.FromJson <MapData>(SafeFileManagement.GetFileContents(mapPath)); mapDataInfos.Add(beatMap.BeatmapFilename, new MapDataInfo(beatMap, mapData)); } } return new BeatSaberMap(info, mapDataInfos, tempUnZipPath); })); }