/// <summary> /// Convert Beat Saber data to a minecraft ResourcePack /// </summary> /// <param name="unzippedFolderPath">Path to unzipped beat saber pack</param> /// <param name="datapackOutputPath">Folder path that Resourcepack will be generated in</param> /// <param name="packInfo">Beat Saber Infomation</param> /// <returns>-1 if successful</returns> public static async Task <ConversionError> FromBeatSaberData(string datapackOutputPath, BeatSaberMap beatSaberMap) { return(await Task.Run(() => { var packInfo = beatSaberMap.InfoData; var unzippedFolderPath = beatSaberMap.ExtractedFilePath; if (!Directory.Exists(unzippedFolderPath) || packInfo == null) { return ConversionError.MissingInfo; } Dictionary <string, string> keyVars = new Dictionary <string, string>(); string folder_uuid = SafeFileManagement.GetFileName(Path.GetFileName(unzippedFolderPath)).MakeMinecraftSafe(); string packName = Globals.RESOURCEPACK + folder_uuid; // Paths string fullOutputPath = Path.Combine(datapackOutputPath, packName + Globals.ZIP); string rootFolderPath = Path.Combine(unzippedFolderPath, packName); string minecraftNamespace = Path.Combine(rootFolderPath, Globals.ASSETS, Globals.MINECRAFT); string mapSong = Path.Combine(unzippedFolderPath, packInfo.SongFilename); string packSong = Path.Combine(minecraftNamespace, Globals.SOUNDS, Globals.CUSTOM, folder_uuid + Globals.OGG); string mapIcon = Path.Combine(unzippedFolderPath, packInfo.CoverImageFilename); string packIcon = Path.Combine(rootFolderPath, Globals.PACK_ICON); // Replaced vars keyVars["SONGUUID"] = folder_uuid; keyVars["SONGNAME"] = packInfo.SongName + packInfo.SongSubName; keyVars["AUTHORNAME"] = packInfo.SongAuthorName; // Copying Template string copiedTemplatePath = Path.Combine(unzippedFolderPath, Globals.TEMPLATE_RESOURCES_PACK_NAME); if (SafeFileManagement.DirectoryCopy(Globals.pathOfResourcepackTemplate, unzippedFolderPath, true, Globals.excludeExtensions, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS)) { if (SafeFileManagement.MoveDirectory(copiedTemplatePath, rootFolderPath, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS)) { Filemanagement.UpdateAllCopiedFiles(rootFolderPath, keyVars); // Copying Image Icon SafeFileManagement.CopyFileTo(mapIcon, packIcon, true, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS); // Copying Song if (SafeFileManagement.CopyFileTo(mapSong, packSong, true, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS)) { if (!Filemanagement.UpdateFileWithKeys(Path.Combine(minecraftNamespace, Globals.SOUNDS_JSON), keyVars)) { return ConversionError.OtherFail; } } // Creating Zip Archive.Compress(rootFolderPath, fullOutputPath, true); return ConversionError.None; } } return ConversionError.FailedToCopyFile; })); }
/// <summary> /// Generate a minecraft datapack from Beat Saber data /// </summary> /// <param name="unzippedFolderPath">Path of unzipped Beat Saber data</param> /// <param name="datapackOutputPath">Path to output datapack</param> /// <param name="packInfo">Beat Saber Parsed info</param> /// <param name="beatMapSongList">List of Beat Saber song data</param> /// <param name="cancellationToken">Token that allows async function to be canceled</param> /// <returns></returns> public static async Task <ConversionError> FromBeatSaberData(string datapackOutputPath, BeatSaberMap beatSaberMap, IProgress <ConversionProgress> progress, CancellationToken cancellationToken) { return(await Task.Run(() => { var unzippedFolderPath = beatSaberMap.ExtractedFilePath; if (!Directory.Exists(unzippedFolderPath)) { return Task.FromResult(ConversionError.UnzipError); } DataPackData dataPackData = new DataPackData(unzippedFolderPath, datapackOutputPath, beatSaberMap); if (beatSaberMap.InfoData.DifficultyBeatmapSets.Length == 0) { return Task.FromResult(ConversionError.NoMapData); } // Copying Template string copiedTemplatePath = Path.Combine(unzippedFolderPath, Globals.TEMPLATE_DATA_PACK_NAME); if (!SafeFileManagement.DirectoryCopy(Globals.pathOfDatapackTemplate, unzippedFolderPath, true, Globals.excludeExtensions, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS)) { return Task.FromResult(ConversionError.FailedToCopyFile); } try { if (SafeFileManagement.MoveDirectory(copiedTemplatePath, dataPackData.datapackRootPath, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS)) { cancellationToken.ThrowIfCancellationRequested(); // Must change the folder names before searching for keys string songname_uuidFolder = Path.Combine(dataPackData.datapackRootPath, Globals.DATA, Globals.FOLDER_UUID); string newPath = Path.Combine(dataPackData.datapackRootPath, Globals.DATA, dataPackData.folder_uuid); SafeFileManagement.MoveDirectory(songname_uuidFolder, newPath, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS); // Updating Copied files Filemanagement.UpdateAllCopiedFiles(dataPackData.datapackRootPath, dataPackData.keyVars, true, Globals.excludeKeyVarExtensions); // Copying Image Icon string mapIcon = Path.Combine(unzippedFolderPath, beatSaberMap.InfoData.CoverImageFilename); string packIcon = Path.Combine(dataPackData.datapackRootPath, Globals.PACK_ICON); SafeFileManagement.CopyFileTo(mapIcon, packIcon, true, Globals.NUMBER_OF_IO_RETRY_ATTEMPTS); cancellationToken.ThrowIfCancellationRequested(); progress.Report(new ConversionProgress(0.4f, "Generating main datapack files")); var mcBeatDataError = GenerateMCBeatData(beatSaberMap, dataPackData, progress, cancellationToken); if (mcBeatDataError != ConversionError.None) { return Task.FromResult(mcBeatDataError); } cancellationToken.ThrowIfCancellationRequested(); progress.Report(new ConversionProgress(0.9f, "Zipping files")); Archive.Compress(dataPackData.datapackRootPath, dataPackData.fullOutputPath, true); return Task.FromResult(ConversionError.None); } } catch (OperationCanceledException wasCanceled) { throw wasCanceled; } catch (ObjectDisposedException wasAreadyCanceled) { throw wasAreadyCanceled; } return Task.FromResult(ConversionError.OtherFail); })); }