// rename in place: // customreplayformat != null // output directory = empty // rename in place = true // restoreoriginalreplaynames = false // rename to output: // customreplayformat != null // outputdirectory != null/empty // renameinplace = false // restore = false // restore: // customreplayformat = null // outputdirectory = null // renameinplace = false // restore = true public static RenamingParameters Create(CustomReplayFormat customReplayFormat, string outputDirectory, bool?renameInPlace, bool?restoreOriginalReplayNames, bool?isPreview) { var renameInPlaceValue = renameInPlace.HasValue && renameInPlace.Value; var restoreOriginalReplayNamesValue = restoreOriginalReplayNames.HasValue && restoreOriginalReplayNames.Value; var isPreviewValue = isPreview.HasValue && isPreview.Value; if (customReplayFormat != null) { if (renameInPlaceValue) { if (string.IsNullOrWhiteSpace(outputDirectory)) { return(new RenamingParameters(customReplayFormat, string.Empty, true, false, isPreviewValue)); } return(null); } if (string.IsNullOrWhiteSpace(outputDirectory)) { return(null); } return(new RenamingParameters(customReplayFormat, outputDirectory, false, false, isPreviewValue)); } else { if (restoreOriginalReplayNamesValue) { return(new RenamingParameters(null, null, false, true, isPreviewValue)); } return(null); } }
private RenamingParameters(CustomReplayFormat customReplayFormat, string outputDirectory, bool renameInPlace, bool restoreOriginalReplayNames, bool isPreview) { _customReplayFormat = customReplayFormat; _outputDirectory = outputDirectory; _renameInPlace = renameInPlace; _restoreOriginalReplayNames = restoreOriginalReplayNames; _isPreview = isPreview; }
/// <summary> /// Renames a replay according to a custom replay format. /// </summary> /// <param name="replay">The replay to rename.</param> /// <param name="format">The custom replay format to use to rename the replay.</param> /// <param name="forward">Whether to move the file history pointer forward or not.</param> /// <exception cref="ArgumentNullException">Throws an ArgumentNullException in case replay or format is null.</exception> public static void RenameReplay(File <IReplay> replay, CustomReplayFormat format, bool forward = true) { var newName = GenerateReplayName(replay, format); replay.AddAfterCurrent(newName); if (forward) { replay.Forward(); } }
/// <summary> /// Generates a new replay name based on a replay and a custom replay format. /// </summary> /// <param name="replay">The replay to generate a new name for.</param> /// <param name="format">The custom replay format to use to generate a new replay name.</param> /// <returns></returns> public static string GenerateReplayName(File <IReplay> replay, CustomReplayFormat format) { if (replay == null) { throw new ArgumentNullException(nameof(replay)); } if (format == null) { throw new ArgumentNullException(nameof(format)); } var customReplayName = format.GenerateReplayName(replay); return(FileHandler.RemoveInvalidChars(customReplayName)); }
static void Main(string[] args) { // Try writing resource to disk C:\testreplays2 => success //if (!Directory.Exists(@"C:\testreplays2")) //{ // Directory.CreateDirectory(@"C:\testreplays2"); //} //File.WriteAllBytes(@"C:\testreplays2\_000204_Fighting_Spirit_1_3_.rep", ReplayParser.ReplaySorter.Replays._000204_Fighting_Spirit_1_3_); // Try writing all resources to disk C:\testreplays2 => success //if (!Directory.Exists(@"C:\testreplays2")) //{ // Directory.CreateDirectory(@"C:\testreplays2"); //} //var Resources = Assembly.GetExecutingAssembly().GetManifestResourceNames(); //foreach (var aResource in Resources) //{ // if (aResource.Substring(aResource.Length - 4) == ".rep") // { // string FilePath = @"C:\testreplays2\" + aResource.Substring(36); // using (Stream replayStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(aResource)) // { // using (var FileStream = new FileStream(FilePath, FileMode.OpenOrCreate)) // { // replayStream.CopyTo(FileStream); // } // } // } //} // ask directory, if none supplied, check the embedded resources directory //if (directory == string.Empty) //{ // directory = Directory.GetCurrentDirectory() + @"\" + "Resources"; //} // parse all replays in directory // for your UI, you should be able to put these things in a separate class and methods so you can make use of them instead of having to rewrite this... IReplaySorterConfiguration replaySorterConfiguration = new ReplaySorterAppConfiguration(); if (ErrorLogger.GetInstance(replaySorterConfiguration) == null) { Console.WriteLine("Issue intializing logger. Logging will be disabled."); } var searchDirectory = (SearchDirectory)User.AskDirectory(typeof(SearchDirectory)); List <File <IReplay> > ListReplays = new List <File <IReplay> >(); IEnumerable <string> files = Directory.EnumerateFiles(searchDirectory.Directory, "*.rep", searchDirectory.SearchOption); while (files.Count() == 0) { Console.WriteLine("No replays found in {0}", searchDirectory.Directory); searchDirectory = (SearchDirectory)User.AskDirectory(typeof(SearchDirectory)); files = Directory.EnumerateFiles(searchDirectory.Directory, "*.rep", searchDirectory.SearchOption); } // getting a lot of errors for replays, figure out why. Add possibility to rename the replays themselves. // Also figure out how to make this 1.16, 1.18, 1.19, ... compatible // could I put a try/catch inside a nother catch? Or should I just use a stringbuilder and write the entirety of it to a file in the end StringBuilder ErrorMessages = new StringBuilder(); List <string> ReplaysThrowingExceptions = new List <string>(); //var BadReplays = searchDirectory.Directory + @"\BadReplays"; Stopwatch sw = new Stopwatch(); sw.Start(); var ProgressBar = new ProgressBar(Console.CursorLeft, Console.CursorTop); var numberOfFiles = files.Count(); int currentPosition = 0; //try //{ foreach (var replay in files) { currentPosition++; ProgressBar.ShowAndUpdate(currentPosition, numberOfFiles); try { var ParsedReplay = ReplayLoader.LoadReplay(replay); ListReplays.Add(File <IReplay> .Create(ParsedReplay, replay)); //WriteUncompressedReplay(@"C:\testreplays\UncompressedReplays", replay); } catch (Exception ex) { //Console.WriteLine(ex.Message); Console.WriteLine("\nError with replay {0}", replay.ToString()); ErrorMessages.AppendLine(ex.Message + ":" + replay.ToString()); ReplaysThrowingExceptions.Add(replay); } } Console.WriteLine(); //} //catch(Exception ex) //{ // Console.WriteLine(ex.Message); //} sw.Stop(); //var ErrorMessagesFile = @"C:\testreplays\ErrorMessages.txt"; var ErrorMessagesFile = searchDirectory.Directory + @"\ErrorMessages.txt"; Console.WriteLine("It took {0} seconds to parse {1} replays. {2} replays encountered exceptions during parsing. Errors can be investigated in the textfile: {3}", sw.Elapsed, ListReplays.Count(), ReplaysThrowingExceptions.Count(), ErrorMessagesFile); try { //using (var StreamWriter = new StreamWriter(ErrorMessagesFile)) //{ //} File.WriteAllText(ErrorMessagesFile, ErrorMessages.ToString()); } catch (Exception ex) { Console.WriteLine("Error writing error messages." + ex.Message); } if (ReplaysThrowingExceptions.Count() > 0) { Console.WriteLine("Move bad replays to a designated folder? Y(es)/N(o)."); var MoveBadReplays = User.AskYesNo(); if (MoveBadReplays.Yes != null) { if ((bool)MoveBadReplays.Yes) { var BadReplaysOutputDirectory = (OutputDirectory)User.AskDirectory(typeof(OutputDirectory), "Specify a directory to put the bad replays."); bool CreateDirectorySuccess = false; while (!CreateDirectorySuccess) { try { Directory.CreateDirectory(BadReplaysOutputDirectory.Directory); CreateDirectorySuccess = true; } catch (Exception) { Console.WriteLine("Could not create directory. Invalid directory name or not enough privileges."); CreateDirectorySuccess = false; } } foreach (var replay in ReplaysThrowingExceptions) { ReplayHandler.RemoveBadReplay(BadReplaysOutputDirectory.Directory + @"\BadReplays", replay); } ReplayHandler.LogBadReplays(ReplaysThrowingExceptions, replaySorterConfiguration.LogDirectory, $"{DateTime.Now} - Error while parsing replay: {{0}}"); } } else { Console.WriteLine("Answer can not be null"); return; } // remove bad replay from files // not necessary anymore since I use ReplayFile now // files = files.Where(x => !ReplaysThrowingExceptions.Contains(x)); } // you might want to extract information and put everything into some sort of data structure, so you don't have to go through the list of replays for each // sort but instead can lookup in the datastructure/table? //Ask criteria to sort on, let's start with something simple... //Player name // use a library to parse command line arguments => then return some sort of Enum // then pass this enum to a Sorter object OutputDirectory SortResultOutputDirectory = (OutputDirectory)User.AskDirectory(typeof(OutputDirectory), "Specify a directory for the sort result. If it does not exist, it will be created."); while (!Directory.Exists(SortResultOutputDirectory.Directory)) { try { Directory.CreateDirectory(SortResultOutputDirectory.Directory); } catch (Exception) { Console.WriteLine("Could not create directory. Invalid directory name or not enough privileges."); Console.WriteLine("Retry?"); var TryAgain = User.AskYesNo(); if (TryAgain.Yes != null) { if ((bool)TryAgain.Yes) { continue; } else { SortResultOutputDirectory = (OutputDirectory)User.AskDirectory(typeof(OutputDirectory), "Specify a different directory for the sort result.If it does not exist, it will be created."); } } else { Console.WriteLine("Answer can not be null."); return; } } } Console.WriteLine("Please enter criteria to sort replays on."); Console.WriteLine("Provide a space separated list of criteria."); var SortCriteria = User.AskCriteria(); Sorter sorter = new Sorter(SortResultOutputDirectory.Directory, ListReplays); List <string> replaysThrowingExceptions = new List <string>(); while (SortCriteria.StopProgram != null) { if ((bool)!SortCriteria.StopProgram) { var CriteriaParameters = User.AskAdditionalQuestionsSortCriteria(SortCriteria.ChosenCriteria); Console.WriteLine("Keep original replay names?"); var KeepOriginalReplayNames = User.AskYesNo(); if (KeepOriginalReplayNames.Yes != null) { if ((bool)!KeepOriginalReplayNames.Yes) { CustomReplayFormat aCustomReplayFormat = null; while (aCustomReplayFormat == null) { Console.WriteLine("Custom format?"); string customformat = Console.ReadLine(); try { aCustomReplayFormat = CustomReplayFormat.Create(customformat, ListReplays.Count, true); sorter.CustomReplayFormat = aCustomReplayFormat; } catch (Exception) { Console.WriteLine("Invalid custom format."); } } } } else { Console.WriteLine("Answer can not be null"); return; } sorter.CurrentDirectory = SortResultOutputDirectory.Directory; sorter.SortCriteria = SortCriteria.ChosenCriteria; sorter.CriteriaStringOrder = SortCriteria.CriteriaStringOrder; try { // use SortCriteriaParameters sorter.ExecuteSort(CriteriaParameters.SortCriteriaParameters, (bool)KeepOriginalReplayNames.Yes, replaysThrowingExceptions); Console.WriteLine("Sort finished."); ReplayHandler.LogBadReplays(replaysThrowingExceptions, replaySorterConfiguration.LogDirectory, $"{DateTime.Now} - Error while sorting replay: {{0}} using arguments: {sorter.ToString()}"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Please enter criteria to sort replays on."); Console.WriteLine("Provide a space separated list of criteria."); SortCriteria = User.AskCriteria(); } } else { return; } } Console.WriteLine("Stop Program can not be null"); return; //string consoleInput = Console.ReadLine(); //Sorter sorter = new Sorter(); //while (consoleInput.ToUpper() != "STOP") //{ // string[] Criteria = Array.ConvertAll(consoleInput.ToUpper().Split(new char[] { ' ' }), x => x.Trim()); // Criteria ChosenCriteria = new Criteria(); // foreach (var criteria in Criteria) // { // if (!Enum.TryParse(criteria, out ChosenCriteria)) // { // Console.WriteLine("Invalid criteria: {0}", criteria); // } // } // // again have sort of mapping structure to ask additional questions bsed on the criteria // // for now keep it simple, just ask the questions // Console.WriteLine("Keep original replay names?"); // var KeepOriginalReplayNames = User.AskYesNo(); // if (KeepOriginalReplayNames.Yes != null) // { // if ((bool)!KeepOriginalReplayNames.Yes) // { // CustomReplayFormat aCustomReplayFormat = new CustomReplayFormat(); // while (aCustomReplayFormat.CustomFormat == null) // { // Console.WriteLine("Custom format?"); // string customformat = Console.ReadLine(); // try // { // aCustomReplayFormat.CustomFormat = customformat; // sorter.CustomReplayFormat = aCustomReplayFormat; // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } // } // } // } // else // { // Console.WriteLine("Answer can not be null"); // return; // } // var CriteriaParameters = User.AskAdditionalQuestionsSortCriteria(ChosenCriteria); // sorter.CurrentDirectory = searchDirectory.Directory; // sorter.Files = files; // sorter.ListReplays = ListReplays; // sorter.SortCriteria = ChosenCriteria; // try // { // sorter.ExecuteSort(CriteriaParameters.MakeFolderForWinner, CriteriaParameters.MakeFolderForLoser, (bool)KeepOriginalReplayNames.Yes, CriteriaParameters.ValidGameTypes, CriteriaParameters.Durations); // Console.WriteLine("Sort finished."); // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } // finally // { // Console.WriteLine("Please enter criteria to sort replays on."); // Console.WriteLine("Provide a space separated list of criteria."); // consoleInput = Console.ReadLine(); // } //} // switch not going to work unless you go through each value between 1 to 2^x and not just the values defined in the enum //switch((int)ChosenCriteria) //{ // case 1: // break; // case 2: // break; // default: // break; //} // use hasflag instead //if (ChosenCriteria.HasFlag(ReplaySorter.Criteria.PLAYERNAME)) //{ // List<string> PlayerNames = new List<string>(); // // sort on playername // foreach (var replay in ListReplays) // { // //List<string> Players = new List<string>(); // var parseplayers = replay.Players.ToList(); // foreach (var aplayer in parseplayers) // { // //Players.Add(aplayer.Name); // if (!PlayerNames.Contains(aplayer.Name)) // { // PlayerNames.Add(aplayer.Name); // } // } // //PlayerNames.AddRange(Players); // } // // create sort directory, and directories for each player // string sortDirectory = @directory + @"\" + ChosenCriteria.ToString(); // Directory.CreateDirectory(sortDirectory); // foreach(var player in PlayerNames.Distinct()) // { // Directory.CreateDirectory(sortDirectory + @"\" + player); // } // // now add all replays associated with player into the folder //foreach (var replay in ListReplays) // { // // get players per replay // var ParsePlayers = replay.Players.ToList(); // var index = ListReplays.IndexOf(replay); // var FilePath = files.ElementAt(index); // var DirectoryName = Directory.GetParent(FilePath); // var FileName = FilePath.Substring(DirectoryName.ToString().Length); // foreach (var aPlayer in ParsePlayers) // { // // for each player, get proper folder // // find the corresponding replay file // // add this file to that folder // var PlayerName = aPlayer.Name; // var DestinationFilePath = sortDirectory + @"\" + PlayerName + FileName; // File.Copy(FilePath, DestinationFilePath); // } // } //} }
public static void MoveReplay(File <IReplay> replay, string sortDirectory, string folderName, bool keepOriginalReplayNames, CustomReplayFormat customReplayFormat, bool isPreview = false) { if (replay == null) { throw new ArgumentNullException(nameof(replay)); } if (string.IsNullOrWhiteSpace(sortDirectory)) { throw new ArgumentException(nameof(sortDirectory)); } if (!keepOriginalReplayNames && customReplayFormat == null) { throw new InvalidOperationException($"{nameof(customReplayFormat)} cannot be null if {nameof(keepOriginalReplayNames)} is false!"); } var sourceFilePath = replay.FilePath; var fileName = FileHandler.GetFileName(replay.FilePath); var destinationFilePath = Path.Combine(sortDirectory, folderName, fileName); var renamedSuccessfully = true; Exception renameException = null; if (!keepOriginalReplayNames) { try { destinationFilePath = sortDirectory + @"\" + folderName + @"\" + GenerateReplayName(replay, customReplayFormat) + ".rep"; } catch (Exception ex) { renamedSuccessfully = false; renameException = ex; ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - Error while renaming replay: {replay.OriginalFilePath}", ex: ex); } } destinationFilePath = FileHandler.AdjustName(destinationFilePath, false); if (!isPreview) { File.Move(sourceFilePath, destinationFilePath); } replay.AddAfterCurrent(destinationFilePath); replay.Forward(); if (!renamedSuccessfully) { throw new RenameException(sourceFilePath, customReplayFormat, $"Something went wrong while renaming.", renameException); } }
public RenameException(string originalFilePath, CustomReplayFormat customReplayFormat, string message, Exception innerException) : base(message, innerException) { OriginalFilePath = originalFilePath; CustomReplayFormat = customReplayFormat; }