InstallWizard.cs 9.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Install.Core.Common;
namespace Update.Core
{
    public class InstallWizard : INotifyPropertyChanged
    {

        public string Msg { get; private set; }

        /// <summary>
        /// 进度 100% 满
        /// </summary>
        public double Progress { get; private set; }

        /// <summary>
        /// 总安装列表
        /// </summary>
        [PropertyChanged.DoNotNotify]
        public InstallPackCollection InstallPacks { get; private set; }

        /// <summary>
        /// //已经安装的软件列表
        /// </summary>
        [PropertyChanged.DoNotNotify]
        public List<InstallInfo> HasInstalled { get; } = new List<InstallInfo>();


        /// <summary>
        /// 图标列表
        /// </summary>
        public Dictionary<string, BitmapSource> Icons { get; } = new Dictionary<string, BitmapSource>();

        /// <summary>
        /// 有更加新的安装包
        /// </summary>
        public bool HasNewestInstallZip { get; private set; }

48 49 50 51
        /// <summary>
        /// 从网络检测信息中
        /// </summary>
        public bool IsCheckingNetwork { get; private set; }
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        /// <summary>
        /// 最新的安装包版本
        /// </summary>
        public string NewestInstallZipVersion { get; private set; }
        /// <summary>
        /// 当前的安装包版本
        /// </summary>
        public string CurrentInstallZipVersion { get; private set; }

        public NewestInstallZipVersionInfo NewestInstallZipVersionInfo { get; private set; }

        #region 下载包
        public double DownloadInfo_Progress => DownloadInfo_TotalSize > 0 ? 1.0 * DownloadInfo_CurrSize / DownloadInfo_TotalSize : 0;
        public long DownloadInfo_TotalSize { get; private set; }
        public long DownloadInfo_CurrSize { get; private set; }


        public string DownloadInfo_TotalSizeStr => fileSize2Str(DownloadInfo_TotalSize);
        public string DownloadInfo_CurrSizeStr => fileSize2Str(DownloadInfo_CurrSize);

        public string DownloadInfo_SpeedStr { get; private set; }
        public string DownloadInfo_ElapsedTimeStr { get; private set; }
        public string DownloadInfo_RemainingTimeStr { get; private set; }

        public string DownloadInfo_ErrMsg { get; private set; }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        public InstallWizard()
        {
            
        }

        void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }



        public bool Init()
        {
            if(!LoadInstallPacks())
                return false;

            UpdateHasInstalled();

            UpdateIcons();
101
            
102 103 104 105 106 107 108 109 110
            DownloadNewestInstallZipInfo();
            return true;
        }

        /// <summary>
        /// 下载最新的安装包信息
        /// </summary>
        void DownloadNewestInstallZipInfo() 
        {
111
            IsCheckingNetwork = true;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            //新建线程
            Task.Factory.StartNew(() =>
            {
                var bytes = HttpExt.HttpDownload(InstallPacks.NewestInstallZipVersionInfoPath);
                if (bytes == null)
                    return;

                //bytes 转 string
                string json = System.Text.Encoding.UTF8.GetString(bytes);

                NewestInstallZipVersionInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<NewestInstallZipVersionInfo>(json);

                NewestInstallZipVersion = NewestInstallZipVersionInfo.InstallZipVersion;

                int ret = VerExt.VersionCompare(InstallPacks.InstallZipVersion, NewestInstallZipVersionInfo.InstallZipVersion);
127
                IsCheckingNetwork = false;
128
                HasNewestInstallZip = (ret < 0);
129
                
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
            });
        }

        string fileSize2Str(long fileSizeByte) 
        {
            double fileSize = fileSizeByte;
            int i = 0;
            while (fileSize > 1024) 
            {
                i++;
                fileSize /= 1024;

                if (i >= 2)
                    break;
            }
            switch (i) {
                case 0:
                    return $"{fileSize:F0}B";
                case 1:
                    return $"{fileSize:F3}KB";
                default://case 2:
                    return $"{fileSize:F3}MB";
            }
        }
        string fileSpeed2Str(long fileSpeedBytePerSec)
        {
            double fileSpeed = fileSpeedBytePerSec;
            int i = 0;
            while (fileSpeed > 1024)
            {
                i++;
                fileSpeed /= 1024;

                if (i >= 2)
                    break;
            }
            switch (i)
            {
                case 0:
                    return $"{ fileSpeed:F0}B/s";
                case 1:
                    return $"{ fileSpeed:F1}KB/s";
                default://case 2:
                    return $"{ fileSpeed:F1}MB/s";
            }
        }

        string sec2Str(int sec) 
        {
            if (sec > 60)
                return $"{(int)(sec / 60)}{sec % 60}秒";
            else 
                return $"{sec % 60}秒";
        }
        public string NewestInstallZipPath { get; private set; }
        /// <summary>
        /// 下载最新的安装包
        /// </summary>
        public async Task<bool> DownloadNewestInstallZip() 
        {
            //新建线程
            var ret = await Task.Factory.StartNew(() =>
            {
                string path;
                if (string.IsNullOrEmpty(InstallPacks.DefaultNewestInstallZipPath))
                {
                    //下载到当前 exe 的上级目录
                    path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                    path = Path.GetDirectoryName(path);
                    path = Path.GetDirectoryName(path);
                }
                else {
                    path = InstallPacks.DefaultNewestInstallZipPath;
                }
                path = Path.Combine(path, NewestInstallZipVersionInfo.InstallZipName);
                NewestInstallZipPath = path;
                



                try
                {
                    HttpExt.BreakpointDownload(
                       NewestInstallZipVersionInfo.InstallZipUrl,
                       NewestInstallZipPath+".7z",
                       false,
                       (totalSize) => DownloadInfo_TotalSize = totalSize,
                       (currSize) => DownloadInfo_CurrSize = currSize,
                       (speed) => DownloadInfo_SpeedStr = fileSpeed2Str(speed),
                       (sec) => DownloadInfo_ElapsedTimeStr = sec2Str(sec),
                       (sec) => DownloadInfo_RemainingTimeStr = sec2Str(sec));
                    DownloadInfo_ErrMsg = null;
                    return true;
                }
                catch (Exception e) 
                {
                    DownloadInfo_ErrMsg = e.Message;
                    return false;
                }
            });
            return ret;
        }

        /// <summary>
        /// 加载安装包
        /// </summary>
        bool LoadInstallPacks()
        {
            string installpack_path = "install.json";
            InstallPackCollection installPackCollection;
            try
            {
                Msg = $"加载 {installpack_path}";
                string json = File.ReadAllText(installpack_path);
                installPackCollection = Newtonsoft.Json.JsonConvert.DeserializeObject<InstallPackCollection>(json);
            }
            catch
            {
                Msg = $"加载 {installpack_path} 失败";
                return false;
            }
            
            InstallPacks = installPackCollection;
            NotifyPropertyChanged(nameof(InstallPacks));

            CurrentInstallZipVersion = InstallPacks.InstallZipVersion;
            return true;
        }

       

        /// <summary>
        /// 更新已经安装的软件
        /// </summary>
        void UpdateHasInstalled() 
        {
            HasInstalled.Clear();
            var installInfos = InstallInfoHelper.GetInstallInfos();

            if (installInfos.Count() > 0)
            {
                HasInstalled.AddRange(installInfos);
            }
            
            //之前不是安装的,是直接复制到D盘运行的
            installInfos = InstallInfoHelper.GetInstallInfosFromPath(InstallPacks);
            if (installInfos.Count() > 0)
            {
                //一样的不复制进去
                foreach (var installInfo in installInfos)
                {
                    if (HasInstalled.Exists(ii => ii.InstallPath == installInfo.InstallPath))
                        continue;
                    HasInstalled.Add(installInfo);
                }
            }

            NotifyPropertyChanged(nameof(HasInstalled));
        }


        /// <summary>
        /// 更新在 安装包中的 图标列表
        /// </summary>
        void UpdateIcons() 
        {
            Icons.Clear();
            foreach (var installInfo in HasInstalled) 
            {
                string exePath = $@"{installInfo.InstallPath }\{installInfo.Exe }";
                var bitmapSource = IconExt.GetIcon(exePath);
                if (bitmapSource != null)
                {
                    Icons.Add(installInfo.ProcessName, bitmapSource);
                }
            }
        }

    }
}