Exemplo n.º 1
0
        private async void RequestData(DisplayRootRegistry displayRootRegistry, DataUpdaterViewModel dataUpdater)
        {
            var net          = new Net();
            var dataLauncher = new LauncherInfoSerializer();
            var proc         = new ProcessHelper();

            var settingHelp = new SettingHelper();
            LauncherSettingLastSerializer lastSettingLauncher = null;


            try
            {
                lastSettingLauncher  = settingHelp.Read <LauncherSettingLastSerializer>(CommonConstant.KeySettings, CommonConstant.FileSettingsLauncher);
                dataLauncher.version = lastSettingLauncher.Info.Version;
            }
            catch (Exception ex)
            {
                if (ex is DirectoryNotFoundException || ex is FileNotFoundException)
                {
                    MessageBox.Show(CommonConstant.NotFoundFileSettings);
                }
                else
                {
                    MessageBox.Show(CommonConstant.ErrorReadOrWriteSettings);
                }
                proc.CloseProcess(Constant.PidLauncher);
                Application.Current.Shutdown();
                return;
            }



            var programLauncher = await net.RequestAsync(Constant.UrlUpdaterLauncherCheckVersion, Net.Post, dataLauncher);

            if (programLauncher.detail != null)
            {
                MessageBox.Show(ErrorParser.Parse(programLauncher.detail));
                return;
            }

            if (!programLauncher.is_updated)
            {
                var message = MessageBox.Show(Constant.DescriptionMessBoxUpdate, "Обновление", MessageBoxButton.OKCancel);
                if (message == MessageBoxResult.Cancel)
                {
                    return;
                }


                _dataMainViewModel.TimerUpdater.Stop();

                displayRootRegistry.ShowPresentation(dataUpdater);

                programLauncher = await net.RequestAsync <LauncherInfoSerializer>(
                    Constant.UrlUpdaterLauncherInfo,
                    Net.Post,
                    null,
                    null,
                    null,
                    (s, e) =>
                {
                    dataUpdater.ProgressValue = (e.BytesReceived * 100 / e.TotalBytesToReceive);
                });

                if (programLauncher.detail != null)
                {
                    displayRootRegistry.HidePresentation(dataUpdater);
                    MessageBox.Show(ErrorParser.Parse(programLauncher.detail));
                    return;
                }

                var cryptBytesLauncher = Convert.FromBase64String(programLauncher.data);


                if (Hash.Sha256Bytes(cryptBytesLauncher) == programLauncher.hash)
                {
                    var crypt = new Crypt(Encoding.UTF8.GetBytes(CommonConstant.Key));
                    crypt.RemoveAndSetIv(ref cryptBytesLauncher);

                    var bytesLauncher = crypt.Decode(cryptBytesLauncher);


                    proc.CloseProcess(Constant.PidLauncher);


                    var zip = new ArchiveHelper();

                    var bytesLauncherSetting = zip.Extract(bytesLauncher, programLauncher.hash, Path.GetTempPath());



                    settingHelp.RemoveProgram(@".\", false);

                    settingHelp.RemoveDumps <SettingLastSerializer, IRemoveData>(lastSettingLauncher.Info);



                    settingHelp.CopyFilesProgram(Path.GetTempPath() + programLauncher.hash, ".\\");

                    var getLauncherSetting = settingHelp.Read <LauncherSettingSerializer>(bytesLauncherSetting, CommonConstant.KeySettings);



                    settingHelp.RewriteLastToNewSettingsLauncher(getLauncherSetting, lastSettingLauncher, CommonConstant.KeySettings, CommonConstant.FileSettingsLauncher);

                    displayRootRegistry.HidePresentation(dataUpdater);


                    try
                    {
                        proc.StartProcess(getLauncherSetting.Info.StartApp, new string[1] {
                            programLauncher.hash
                        });
                    }
                    catch (System.ComponentModel.Win32Exception)
                    {
                        MessageBox.Show(Constant.NotFoundStartAppLauncher);
                    }

                    Application.Current.Shutdown();
                    return;
                }
                else
                {
                    MessageBox.Show(Constant.NoHashEqual);
                }


                displayRootRegistry.HidePresentation(dataUpdater);

                _dataMainViewModel.TimerUpdater.Start();
            }
        }
Exemplo n.º 2
0
        protected override async void OnStartup(StartupEventArgs e)
        {
            var proc        = new ProcessHelper();
            var settingHelp = new SettingHelper();

            if (proc.CheckProcessByName(CommonConstant.NameAppUpdater))
            {
                proc.CloseProcess(CommonConstant.NameAppUpdater);
            }

            var getArgs = Environment.GetCommandLineArgs();

            try
            {
                if (getArgs.Length == 2)
                {
                    settingHelp.RemoveProgram(CommonConstant.PathUpdater, true);
                    settingHelp.CopyFilesProgram(Path.GetTempPath() + getArgs[1] + "\\" + CommonConstant.PathUpdater, CommonConstant.PathUpdater, true);
                    settingHelp.RemoveProgram(Path.GetTempPath() + getArgs[1], true);
                }
            }
            catch (Exception)
            {
            }


            if (!File.Exists(CommonConstant.PathUpdater + CommonConstant.StartAppUpdater))
            {
                MessageBox.Show(Constant.NotFoundUpdater);
                Shutdown();
                return;
            }

            if (!File.Exists(CommonConstant.FileSettingsLauncher))
            {
                MessageBox.Show(Constant.DescriptionMessBoxNotFoundSettings);
                Shutdown();
                return;
            }

            var token = proc.GetPublicKeyTokenFromAssembly(Assembly.GetCallingAssembly());

            if (string.IsNullOrEmpty(token))
            {
                MessageBox.Show(Constant.CrashLauncher);
                Shutdown();
                return;
            }

            var mut = new Mutex(true, token, out instance);

            if (!instance)
            {
                MessageBox.Show(Constant.AlreadyStartLauncher);
                Shutdown();
                return;
            }

            var args = new string[1]
            {
                Process.GetCurrentProcess().Id.ToString()
            };

            var setHelp = new SettingHelper();
            var set     = setHelp.Read <LauncherSettingSerializer>(CommonConstant.KeySettings, CommonConstant.FileSettingsLauncher);


            Constant.PidUpdater = proc.StartProcess(CommonConstant.PathUpdater + CommonConstant.StartAppUpdater, args);

            base.OnStartup(e);

            _mainWindowViewModel = new CollectionsDataViewModel();

            await DisplayRootRegistry.ShowModalPresentation(_mainWindowViewModel);

            Shutdown();
        }