public void ArchiveOldReplays() { try { if (!settings.AutoArchiveOldReplays) { return; } var replayDirectory = new DirectoryInfo(settings.ReplayDirectoryPath); var lastReplay = new FileInfo(Path.Combine(replayDirectory.FullName, Constants.LastReplayFileName)); if (!lastReplay.Exists) { return; } var currentVersion = replayParser.GetVersion(lastReplay.FullName); if (string.IsNullOrEmpty(currentVersion)) { return; } foreach (var replayFile in replayDirectory.GetFiles("*.xml", SearchOption.TopDirectoryOnly)) { var fileVersion = replayParser.GetVersion(replayFile.FullName); if (CompareSemanticVersions(fileVersion, currentVersion) < 0) { ArchiveFile(replayFile.FullName, fileVersion); } } } catch (Exception) { // TODO: Log. For now: suppress. } }
public string GenerateFilename(FileInfo replayFile, ISettings settingsOverride = null) { settingsOverride = settingsOverride ?? settings; if (!replayFile.Exists) { return(null); } var replayContents = File.ReadAllText(replayFile.FullName); var replayXml = XDocument.Parse(replayContents); var dateValue = DateTime.Now; var playerNamesValue = string.Join( " vs ", replayXml.Descendants(PlayerIdentityNodeName) .Select(node => node.Attribute(PlayerNameAttributeName).Value) .OrderBy(playerName => string.Compare(settingsOverride.PlayerUsername, playerName, true)) ); float.TryParse(replayXml.Descendants(DurationNodeName).Select(node => node.Value).FirstOrDefault() ?? string.Empty, out float durationSeconds); var versionValue = replayParser.GetVersion(replayFile.FullName); var filenameTokens = filenameTokenParser.ParseTokens(settingsOverride.FileNamingPattern); var filenameBuilder = new StringBuilder(); foreach (var token in filenameTokens) { switch (token.Item1) { case FilenameToken.StringLiteral: filenameBuilder.Append(token.Item2); continue; case FilenameToken.DateTime: filenameBuilder.Append(dateValue.ToString(token.Item2)); continue; case FilenameToken.PlayerNames: filenameBuilder.Append(playerNamesValue); continue; case FilenameToken.Duration: filenameBuilder.Append(SecondsToHumanReadableTime(durationSeconds)); continue; case FilenameToken.Version: filenameBuilder.Append(versionValue); continue; default: throw new ArgumentOutOfRangeException($"Unknown filename token: {token.Item1}"); } } var filename = filenameBuilder.ToString(); foreach (var c in Path.GetInvalidFileNameChars()) { filename = filename.Replace(c, '_'); } return($"{filename}.xml"); }