Packages.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. namespace Update.Core.Entities
  6. {
  7. /// <summary>
  8. /// 更新包信息
  9. /// </summary>
  10. public class Packages
  11. {
  12. private const char TAIL_SPLIT_UNDERLINE = '_'; //末尾分隔符
  13. private const char TAIL_SPLIT_DOT = '.'; //末尾分割符
  14. private static readonly char[] VERSION_SPLIT_CHARS = { '_' }; //版本分割符
  15. private const string VERSION_SPLIT_STRING = "to"; //版本分割字符串
  16. /// <summary>
  17. /// 增量更新包集合
  18. /// </summary>
  19. public DiffPackageCollection DiffPackages { get; private set; }
  20. /// <summary>
  21. /// 全量更新包信息
  22. /// </summary>
  23. public FullPackageCollection FullPackages { get; private set; }
  24. /// <summary>
  25. /// 构造函数
  26. /// </summary>
  27. /// <param name="text">服务端配置文件内容</param>
  28. public Packages(string text)
  29. {
  30. if (text == null)
  31. throw new ArgumentNullException("text");
  32. //解析
  33. List<DiffPackage> diffPackages = new List<DiffPackage>();
  34. List<FullPackage> fullPackages = new List<FullPackage>();
  35. string[] fileList = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  36. foreach (string fileName in fileList)
  37. {
  38. //删除扩展名
  39. string shortName = StripTail(fileName.Trim());
  40. //分割文件名
  41. List<string> columns = shortName.Split(VERSION_SPLIT_CHARS, StringSplitOptions.RemoveEmptyEntries)
  42. .Where(col => !col.Equals(VERSION_SPLIT_STRING, StringComparison.OrdinalIgnoreCase))
  43. .ToList();
  44. //转换
  45. try
  46. {
  47. switch (columns.Count)
  48. {
  49. case 0:
  50. break;
  51. case 1:
  52. fullPackages.Add(new FullPackage { To = new Version(columns[0]), FileName = fileName });
  53. break;
  54. default:
  55. diffPackages.Add(new DiffPackage { From = new Version(columns[0]), To = new Version(columns[1]), FileName = fileName });
  56. break;
  57. }
  58. }
  59. catch
  60. {
  61. throw new Exception(string.Format("更新包配置错误 {0}。", fileName));
  62. }
  63. }
  64. //赋值
  65. this.DiffPackages = new DiffPackageCollection(diffPackages);
  66. this.FullPackages = new FullPackageCollection(fullPackages);
  67. }
  68. /// <summary>
  69. /// 是否有可用更新
  70. /// </summary>
  71. /// <param name="current">当前版本</param>
  72. /// <returns>有可用更新返回true,否则返回false</returns>
  73. public bool HasAvailable(Version current)
  74. {
  75. //MessageBox.Show(FullPackages[0].To + "_" + current);
  76. return this.FullPackages.Any(package => package.To != current) || this.DiffPackages.Contains(current);
  77. }
  78. /// <summary>
  79. /// 获取可用更新包集合
  80. /// </summary>
  81. /// <param name="current">当前版本</param>
  82. /// <returns>更新包集合</returns>
  83. public PackageCollection GetAvailables(Version current)
  84. {
  85. PackageCollection packages = new PackageCollection();
  86. FullPackage maxFullPackage = this.FullPackages.Max();
  87. if (maxFullPackage != null && maxFullPackage.To != current)
  88. {
  89. packages.Add(maxFullPackage);
  90. current = maxFullPackage.To;
  91. }
  92. DiffPackage diffPackage;
  93. while (this.DiffPackages.TryGetValue(current, out diffPackage))
  94. {
  95. packages.Add(diffPackage);
  96. current = diffPackage.To;
  97. }
  98. return packages;
  99. }
  100. /// <summary>
  101. /// 去掉文件名的末尾(最后一个 '.' 或 '_' 后的字符串,如果为非 unit 则去掉
  102. /// </summary>
  103. /// <param name="fileName">文件名</param>
  104. /// <returns>处理后的字符串</returns>
  105. private static string StripTail(string fileName)
  106. {
  107. uint temp;
  108. int start;
  109. int end = fileName.Length;
  110. for (int index = fileName.Length - 1; index >= 0; index--)
  111. {
  112. switch (fileName[index])
  113. {
  114. case TAIL_SPLIT_DOT:
  115. case TAIL_SPLIT_UNDERLINE:
  116. if (uint.TryParse(fileName.Substring(start = index + 1, end - start), out temp))
  117. return fileName.Substring(0, end);
  118. else
  119. end = index;
  120. break;
  121. }
  122. }
  123. return string.Empty;
  124. }
  125. }
  126. }