}// ----------------------------------------- void check_parameters() { // Check for input files // -------------------- if (!CDCRUSH.check_file_(p.inputFile, CDCRUSH.CDCRUSH_EXTENSIONS)) { fail(msg: CDCRUSH.ERROR); return; } if (string.IsNullOrEmpty(p.outputDir)) { p.outputDir = Path.GetDirectoryName(p.inputFile); } // -- Output folder check if (p.flag_folder) { p.outputDir = CDCRUSH.checkCreateUniqueOutput(p.outputDir, Path.GetFileNameWithoutExtension(p.inputFile)); if (p.outputDir == null) { fail("Output Dir Error " + p.outputDir); return; } } else { if (!FileTools.createDirectory(p.outputDir)) { fail(msg: "Can't create Output Dir " + p.outputDir); return; } } // -- p.tempDir = CDCRUSH.getSubTempDir(); if (!FileTools.createDirectory(p.tempDir)) { fail(msg: "Can't create TEMP dir"); return; } }// ------------------
// -- public JobRestore(RestoreParams p) : base("Restore CD") { // Check for input files // -------------------- if (!CDCRUSH.check_file_(p.inputFile, CDCRUSH.CDCRUSH_EXTENSION)) { fail(msg: CDCRUSH.ERROR); return; } if (string.IsNullOrEmpty(p.outputDir)) { p.outputDir = Path.GetDirectoryName(p.inputFile); } // -- Output folder check if (p.flag_folder) { p.outputDir = CDCRUSH.checkCreateUniqueOutput(p.outputDir, Path.GetFileNameWithoutExtension(p.inputFile)); if (p.outputDir == null) { fail("Output Dir Error " + p.outputDir); return; } } else { if (!FileTools.createDirectory(p.outputDir)) { fail(msg: "Can't create Output Dir " + p.outputDir); return; } } // -- p.tempDir = Path.Combine(CDCRUSH.TEMP_FOLDER, Guid.NewGuid().ToString().Substring(0, 12)); if (!FileTools.createDirectory(p.tempDir)) { fail(msg: "Can't create TEMP dir"); return; } // Safeguard, even if the GUI doesn't allow it if (p.flag_encCue) { p.flag_forceSingle = false; } // IMPORTANT!! sharedData gets set by value, NOT A POINTER, do not make changes to p after this jobData = p; // -- hack_setExpectedProgTracks(p.expectedTracks + 3); // - Extract the Archive // ----------------------- add(new CTask((t) => { var arc = new FreeArc(CDCRUSH.TOOLS_PATH); t.handleCliReport(arc); arc.extractAll(p.inputFile, p.tempDir); arc.onProgress = (pr) => t.PROGRESS = pr; // In case the operation is aborted t.killExtra = () => { arc.kill(); }; }, "Extracting", "Extracting the archive to temp folder")); // - Read JSON data // - Restore tracks // - JOIN if it has to // ----------------------- add(new CTask((t) => { var cd = new CueReader(); jobData.cd = cd; // -- if (!cd.loadJson(Path.Combine(p.tempDir, CDCRUSH.CDCRUSH_SETTINGS))) { t.fail(msg: cd.ERROR); return; } LOG.log("== Detailed CD INFOS =="); LOG.log(cd.getDetailedInfo()); // - Push TASK RESTORE tasks right after this one foreach (CueTrack tr in cd.tracks) { addNextAsync(new TaskRestoreTrack(tr)); // Note: Task will take care of encoded cue case } //-- t.complete(); }, "-Preparing to Restore", "Reading stored CD info and preparing track restore tasks")); // - Join Tracks, but only when not creating .Cue/Enc Audio // ----------------------- if (!p.flag_encCue) { add(new CTask((t) => { CueReader cd = jobData.cd; // -- Join tracks if (p.flag_forceSingle || !cd.MULTIFILE) { // The task will read data from the shared job data var // Will join all tracks in place into track01.bin // Note: Sets track.workingFile to null to moved track addNext(new TaskJoinTrackFiles()); } //-- t.complete(); }, "-Preparing to Join")); } // - Prepare tracks `trackfile` which is the track written to the CUE // - Convert tracks // - Move files to final destination // - Create CUE files // - Delete Temp Files // ----------------------- add(new CTask((t) => { CueReader cd = jobData.cd; int progressStep = (int)Math.Round(100.0f / cd.tracks.Count); // -- foreach (var track in cd.tracks) { if (p.flag_encCue) { string ext = Path.GetExtension(track.workingFile); if (cd.tracks.Count == 1) { track.trackFile = $"{cd.CD_TITLE}{ext}"; } else { track.trackFile = $"{cd.CD_TITLE} (track {track.trackNo}){ext}"; } if (!cd.MULTIFILE) { track.setNewTimesReset(); // :: CONVERTS SINGLE TO MULTI } } else { if (p.flag_forceSingle && cd.MULTIFILE) // :: CONVERT MULTI TO SINGLE { track.setNewTimesBasedOnSector(); } if (cd.MULTIFILE && !p.flag_forceSingle) { track.trackFile = cd.CD_TITLE + " " + track.getFilenameRaw(); } if (!cd.MULTIFILE || p.flag_forceSingle) { if (track.trackNo == 1) { track.trackFile = cd.CD_TITLE + ".bin"; } else { track.trackFile = null; } } } // -- // Move ALL files to final output folder // NULL workingFile means that is has been deleted if (track.workingFile != null) { FileTools.tryMove(track.workingFile, Path.Combine(p.outputDir, track.trackFile)); t.PROGRESS += progressStep; } } // -- end track processing // -- // Create CUE File if (!cd.saveCUE(Path.Combine(p.outputDir, cd.CD_TITLE + ".cue"))) { t.fail(cd.ERROR); return; } t.complete(); }, "Moving, Finalizing", "Calculating track data and creating .CUE")); // - Complete - }// -----------------------------------------
// -- public JobConvertCue(CrushParams p) : base("Convert CD") { // Check for input files // :: -------------------- if (!CDCRUSH.check_file_(p.inputFile, ".cue")) { fail(msg: CDCRUSH.ERROR); return; } if (string.IsNullOrEmpty(p.outputDir)) { p.outputDir = Path.GetDirectoryName(p.inputFile); } // : NEW : // : ALWAYS Create a subfolder to avoid overwriting the source files p.outputDir = CDCRUSH.checkCreateUniqueOutput(p.outputDir, p.cdTitle + CDCRUSH.RESTORED_FOLDER_SUFFIX); if (p.outputDir == null) { fail("Output Dir Error " + p.outputDir); return; } // - p.tempDir = Path.Combine(CDCRUSH.TEMP_FOLDER, Guid.NewGuid().ToString().Substring(0, 12)); if (!FileTools.createDirectory(p.tempDir)) { fail(msg: "Can't create TEMP dir"); return; } // Useful to know. p.flag_convert_only = true; // IMPORTANT!! sharedData gets set by value, NOT A POINTER, do not make changes to p after this jobData = p; hack_setExpectedProgTracks(p.expectedTracks + 2); // -- // - Read the CUE file :: add(new CTask((t) => { var cd = new CueReader(); jobData.cd = cd; if (!cd.load(p.inputFile)) { t.fail(msg: cd.ERROR); return; } // -- if (cd.tracks.Count == 1) { t.fail(msg: "No point in converting. No audio tracks on the cd."); return; } // Meaning the tracks are going to be extracted in the temp folder jobData.flag_sourceTracksOnTemp = (!cd.MULTIFILE && cd.tracks.Count > 1); // In case user named the CD, otherwise it's going to be the same if (!string.IsNullOrWhiteSpace(p.cdTitle)) { cd.CD_TITLE = FileTools.sanitizeFilename(p.cdTitle); } // Real quality to string name cd.CD_AUDIO_QUALITY = CDCRUSH.getAudioQualityString(p.audioQuality); t.complete(); }, "-Reading")); // - Cut tracks // --------------------------- add(new TaskCutTrackFiles()); // - Compress tracks // --------------------- add(new CTask((t) => { // Only encode the audio tracks foreach (CueTrack tr in (jobData.cd as CueReader).tracks) { if (!tr.isData) { addNextAsync(new TaskCompressTrack(tr)); } } t.complete(); }, "-Preparing")); // - Create new CUE file // -------------------- add(new CTask((t) => { CueReader cd = jobData.cd; // DEV: So far : // track.trackFile is UNSET. cd.saveCue needs it to be set. // track.workingFile points to a valid file, some might be in TEMP folder and some in input folder (data tracks) int stepProgress = (int)Math.Round(100.0f / cd.tracks.Count); // -- Move files to output folder foreach (var track in cd.tracks) { if (!cd.MULTIFILE) { // Fix the index times to start with 00:00:00 track.setNewTimesReset(); } string ext = Path.GetExtension(track.workingFile); track.trackFile = $"{cd.CD_TITLE} (track {track.trackNo}){ext}"; // Data track was not cut or encoded. // It's in the input folder, don't move it if (track.isData && cd.MULTIFILE) { FileTools.tryCopy(track.workingFile, Path.Combine(p.outputDir, track.trackFile)); } else { // TaskCompress already put the audio files on the output folder // But it's no big deal calling it again // This is for the data tracks that are on the temp folder FileTools.tryMove(track.workingFile, Path.Combine(p.outputDir, track.trackFile)); } t.PROGRESS += stepProgress; } //-- Create the new CUE file if (!cd.saveCUE(Path.Combine(p.outputDir, cd.CD_TITLE + ".cue"))) { t.fail(cd.ERROR); return; } t.complete(); }, "Finalizing")); // -- COMPLETE -- }// -----------------------------------------
// -- public JobCrush(CrushParams par) : base("Compress CD") { p = par; // Hack to fix progress hack_setExpectedProgTracks(p.expectedTracks + 3); // - Read CUE and some init // --------------------------- add(new CTask((t) => { var cd = new cd.CDInfos(); p.cd = cd; try{ cd.cueLoad(p.inputFile); }catch (haxe.lang.HaxeException e) { t.fail(msg: e.Message); return; } // Meaning the tracks are going to be CUT in the temp folder, so they are safe to be removed p.flag_sourceTracksOnTemp = (!cd.MULTIFILE && cd.tracks.length > 1); // In case user named the CD, otherwise it's going to be the same if (!string.IsNullOrWhiteSpace(p.cdTitle)) { cd.CD_TITLE = FileTools.sanitizeFilename(p.cdTitle); } // Real quality to string name cd.CD_AUDIO_QUALITY = AudioMaster.getCodecSettingsInfo(p.audioQuality); if (p.mode != 1) // Convert only does not require an archive { // Generate the final ARCHIVE path now that I have the CD TITLE p.finalArcPath = Path.Combine(p.outputDir, cd.CD_TITLE + p.archiver_ext); // Try to create a new archive in case it exists? while (File.Exists(p.finalArcPath)) { LOG.log("{0} already exists, adding (_) until unique", p.finalArcPath); // S is entire path without (.ext) string S = p.finalArcPath.Substring(0, p.finalArcPath.Length - p.archiver_ext.Length); p.finalArcPath = S + "_" + p.archiver_ext; } LOG.log("- Destination Archive : {0}", p.finalArcPath); } if (p.mode == 1) { // : ALWAYS Create a subfolder (when converting) to avoid overwriting the source files p.outputDir = CDCRUSH.checkCreateUniqueOutput(p.outputDir, p.cdTitle + CDCRUSH.RESTORED_FOLDER_SUFFIX); if (p.outputDir == null) { fail("Output Dir Error " + p.outputDir); return; } } jobData = p; // Some TASKS read jobData t.complete(); }, "-Reading", "Reading CUE data and preparing")); // - Cut tracks // --------------------------- add(new TaskCutTrackFiles()); // - Encode tracks // --------------------- add(new CTask((t) => { for (int i = 0; i < p.cd.tracks.length; i++) { cd.CDTrack tr = p.cd.tracks[i] as cd.CDTrack; // Do not encode DATA TRACKS to ECM when converting. if (p.mode > 0 && tr.isData) { continue; } addNextAsync(new TaskCompressTrack(tr)); } t.complete(); }, "-Preparing", "Preparing to compress tracks")); // - Prepare Tracks on CONVERT modes // - Needed for the new .CUE to be created // - if CONVERT MODE, move all files to output if (p.mode > 0) { add(new CTask((t) => { // DEV: So far : // track.trackFile is UNSET. cd.saveCue needs it to be set. // track.workingFile points to a valid file, some might be in TEMP folder and some in input folder (data tracks) int stepProgress = (int)Math.Round(100.0f / p.cd.tracks.length); // -- Move files to output folder for (int i = 0; i < p.cd.tracks.length; i++) { cd.CDTrack track = p.cd.tracks[i] as cd.CDTrack; if (!p.cd.MULTIFILE) { // Fix the index times to start with 00:00:00 track.rewriteIndexes_forMultiFile(); } string ext = Path.GetExtension(track.workingFile); // This tells what the files should be named in the `.cue` file: track.trackFile = $"{p.cd.CD_TITLE} (track {track.trackNo}){ext}"; // Data track was not cut or encoded. // It's in the input folder, don't move it if (track.isData && (p.cd.MULTIFILE || p.cd.tracks.length == 1)) { if (p.mode == 1) { FileTools.tryCopy(track.workingFile, Path.Combine(p.outputDir, track.trackFile)); track.workingFile = Path.Combine(p.outputDir, track.trackFile); } else { // I need to copy all files to TEMP, so that they can be renamed and archived FileTools.tryCopy(track.workingFile, Path.Combine(p.tempDir, track.trackFile)); track.workingFile = Path.Combine(p.tempDir, track.trackFile); } } else // encoded file that is on TEMP or OUTPUT { if (p.mode == 1) { // TaskCompress already put the audio files on the output folder // But it's no big deal calling it again // This is for the data tracks that are on the temp folder FileTools.tryMove(track.workingFile, Path.Combine(p.outputDir, track.trackFile)); track.workingFile = Path.Combine(p.outputDir, track.trackFile); } else { // Track that has been encoded and is on TEMP // It is currently named as "track_xx.xx" so rename it FileTools.tryMove(track.workingFile, Path.Combine(p.tempDir, track.trackFile)); track.workingFile = Path.Combine(p.tempDir, track.trackFile); } } t.PROGRESS += stepProgress; } // -- end processing tracks if (p.mode == 1) { p.new_cue_path = Path.Combine(p.outputDir, p.cd.CD_TITLE + ".cue"); } else { p.new_cue_path = Path.Combine(p.tempDir, p.cd.CD_TITLE + ".cue"); } //. Create the new CUE file try{ p.cd.cueSave( p.new_cue_path, new haxe.root.Array <object>(new [] { "CDCRUSH (dotNet) version : " + CDCRUSH.PROGRAM_VERSION, CDCRUSH.LINK_SOURCE })); }catch (haxe.lang.HaxeException e) { t.fail(msg: e.Message); return; } t.complete(); }, "Converting")); } // - Create an Archive // Add all tracks to the final archive // --------------------- if (p.mode != 1) { add(new CTask((t) => { // -- Get list of files to compress // . Tracks System.Collections.ArrayList files = new System.Collections.ArrayList(); for (var i = 0; i < p.cd.tracks.length; i++) { files.Add((p.cd.tracks[i] as cd.CDTrack).workingFile); // Working file is valid, was set earlier } if (p.mode == 0) // Only on CDCRUSH add cover and json data { // . Settings string path_settings = Path.Combine(p.tempDir, CDCRUSH.CDCRUSH_SETTINGS); try{ p.cd.jsonSave(path_settings); files.Add(path_settings); }catch (haxe.lang.HaxeException e) { t.fail(msg: e.Message); return; } // . Cover Image string path_cover; if (p.cover != null) { path_cover = Path.Combine(p.tempDir, CDCRUSH.CDCRUSH_COVER); File.Copy(p.cover, path_cover); files.Add(path_cover); } else { path_cover = null; } } else { // It must be CONVERT + ARCHIVE files.Add(p.new_cue_path); } // -. Compress whatever files are on var arc = ArchiveMaster.getArchiver(p.finalArcPath); string arcStr = ArchiveMaster.getCompressionSettings(p.archiveSettingsInd).Item2; arc.compress((string[])files.ToArray(typeof(string)), jobData.finalArcPath, -1, arcStr); arc.onProgress = (pr) => t.PROGRESS = pr; arc.onComplete = (s) => { if (s) { // NOTE: This var is autowritten whenever a compress operation is complete p.crushedSize = (int)arc.COMPRESSED_SIZE; // IMPORTANT to write to jobdata, because it is not a pointer and this needs to be read externally jobData = p; t.complete(); } else { fail(arc.ERROR); } }; t.killExtra = () => arc.kill(); }, "Compressing", "Compressing everything into an archive")); } // -- COMPLETE -- add(new CTask((t) => { LOG.log("== Detailed CD INFOS =="); LOG.log(p.cd.getDetailedInfo()); t.complete(); }, "-complete")); }// -----------------------------------------