private void LoadCrafts()
        {
            this.ClearCrafts();

            List <BackupCraft> crafts = BackupCraft.LoadList(this.XwaDirectory, this.IncludeNewFiles);

            foreach (var craft in crafts.OrderByDescending(t => t.CreationDate))
            {
                this.BackupCrafts.Add(craft);
            }
        }
Exemple #2
0
        public static List <BackupCraft> LoadList(string xwaDirectory, bool includeNewFiles)
        {
            var crafts = new List <BackupCraft>();

            string backupDirectory = Path.Combine(xwaDirectory, "Backup");

            if (!Directory.Exists(backupDirectory))
            {
                return(crafts);
            }

            foreach (string directoryName in Directory.EnumerateDirectories(backupDirectory))
            {
                string item  = System.IO.Path.GetFileName(directoryName);
                Match  match = _craftFolderRegex.Match(item);

                if (!match.Success)
                {
                    continue;
                }

                var craft = new BackupCraft
                {
                    FolderName   = item,
                    CraftName    = match.Groups[2].Value,
                    CreationDate = DateTime.ParseExact(match.Groups[1].Value, "yyyy-MM-dd_H.mm.ss", CultureInfo.InvariantCulture)
                };

                craft.ReadmeFileName = Path.GetFileName(Directory.EnumerateFiles(backupDirectory, string.Concat(craft.CraftName, "v*Readme.*")).FirstOrDefault());

                foreach (string fileName in Directory.EnumerateFiles(directoryName, "*.*", SearchOption.AllDirectories))
                {
                    craft.FileNames.Add(fileName.Replace(directoryName, "Backup"));
                }

                crafts.Add(craft);
            }

            crafts.Sort((x, y) => x.CreationDate.CompareTo(y.CreationDate));

            if (includeNewFiles)
            {
                DateTime now = DateTime.Now.AddMilliseconds(1);

                for (int craftIndex = 0; craftIndex < crafts.Count; craftIndex++)
                {
                    BackupCraft craft = crafts[craftIndex];

                    DateTime from = craft.CreationDate;
                    DateTime to   = craftIndex + 1 < crafts.Count ? crafts[craftIndex + 1].CreationDate : now;

                    foreach (string fileName in Directory.EnumerateFiles(xwaDirectory, "*.*", SearchOption.AllDirectories))
                    {
                        if (fileName.StartsWith(backupDirectory, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        string name = Path.GetFileName(fileName);

                        if (name.StartsWith("flightscreen", StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        if (name.StartsWith("frontscreen", StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        string fileNameBackup = fileName.Replace(xwaDirectory, "Backup");
                        if (craft.FileNames.Any(t => t.Equals(fileNameBackup, StringComparison.OrdinalIgnoreCase)))
                        {
                            continue;
                        }

                        DateTime creationDateTime = File.GetCreationTime(fileName);

                        if (creationDateTime >= from && creationDateTime < to)
                        {
                            craft.FileNames.Add(fileName.Replace(xwaDirectory, "New"));
                        }
                    }
                }
            }

            return(crafts);
        }