123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- namespace Update.Core.Entities
- {
-
-
-
- public class Packages
- {
- private const char TAIL_SPLIT_UNDERLINE = '_';
- private const char TAIL_SPLIT_DOT = '.';
- private static readonly char[] VERSION_SPLIT_CHARS = { '_' };
- private const string VERSION_SPLIT_STRING = "to";
-
-
-
- public DiffPackageCollection DiffPackages { get; private set; }
-
-
-
- public FullPackageCollection FullPackages { get; private set; }
-
-
-
-
- public Packages(string text)
- {
- if (text == null)
- throw new ArgumentNullException("text");
-
- List<DiffPackage> diffPackages = new List<DiffPackage>();
- List<FullPackage> fullPackages = new List<FullPackage>();
- string[] fileList = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (string fileName in fileList)
- {
-
- string shortName = StripTail(fileName.Trim());
-
- List<string> columns = shortName.Split(VERSION_SPLIT_CHARS, StringSplitOptions.RemoveEmptyEntries)
- .Where(col => !col.Equals(VERSION_SPLIT_STRING, StringComparison.OrdinalIgnoreCase))
- .ToList();
-
- try
- {
- switch (columns.Count)
- {
- case 0:
- break;
- case 1:
- fullPackages.Add(new FullPackage { To = new Version(columns[0]), FileName = fileName });
- break;
- default:
- diffPackages.Add(new DiffPackage { From = new Version(columns[0]), To = new Version(columns[1]), FileName = fileName });
- break;
- }
- }
- catch
- {
- throw new Exception(string.Format("更新包配置错误 {0}。", fileName));
- }
- }
-
- this.DiffPackages = new DiffPackageCollection(diffPackages);
- this.FullPackages = new FullPackageCollection(fullPackages);
- }
-
-
-
-
-
- public bool HasAvailable(Version current)
- {
-
- return this.FullPackages.Any(package => package.To != current) || this.DiffPackages.Contains(current);
- }
-
-
-
-
-
- public PackageCollection GetAvailables(Version current)
- {
- PackageCollection packages = new PackageCollection();
- FullPackage maxFullPackage = this.FullPackages.Max();
- if (maxFullPackage != null && maxFullPackage.To != current)
- {
- packages.Add(maxFullPackage);
- current = maxFullPackage.To;
- }
- DiffPackage diffPackage;
- while (this.DiffPackages.TryGetValue(current, out diffPackage))
- {
- packages.Add(diffPackage);
- current = diffPackage.To;
- }
- return packages;
- }
-
-
-
-
-
- private static string StripTail(string fileName)
- {
- uint temp;
- int start;
- int end = fileName.Length;
- for (int index = fileName.Length - 1; index >= 0; index--)
- {
- switch (fileName[index])
- {
- case TAIL_SPLIT_DOT:
- case TAIL_SPLIT_UNDERLINE:
- if (uint.TryParse(fileName.Substring(start = index + 1, end - start), out temp))
- return fileName.Substring(0, end);
- else
- end = index;
- break;
- }
- }
- return string.Empty;
- }
- }
- }
|