Exemplo n.º 1
0
        private ModInstallationState ExecuteVerifyTask()
        {
            UpdateStatus(TaskExecutionStatus.Verify);

            // Get files affected by install
            UpdateStatus(TaskExecutionStatus.VerifyGetAffectedFiles);
            var affectedFiles = _persistenceService.GetAffectedFiles(Task.ModID);

            if (!affectedFiles.AnySafe())
            {
                return(ModInstallationState.NotInstalled);
            }

            // Normalize affected files
            if (affectedFiles.Length > 1)
            {
                affectedFiles = affectedFiles.StripCommonStart().ToArray();
            }
            affectedFiles = affectedFiles.Select(f => f.Replace("\\", "/")).ToArray();

            // Get verification pairs
            UpdateStatus(TaskExecutionStatus.VerifyGetVerificationPairs);
            var verificationPairs = _apiService.GetVerificationPairs(Task.ModID);

            // Verify files
            UpdateStatus(TaskExecutionStatus.VerifyExecute);
            foreach (var verificationPair in verificationPairs)
            {
                if (IsAbortPending)
                {
                    TaskAborted?.Invoke(this, new TaskEventArgs(Task));
                    return(ModInstallationState.Unknown);
                }

                // Get file in question
                string file = affectedFiles
                              .Where(f => f.ContainsInvariant(verificationPair.File))
                              .OrderBy(f => f.Length)
                              .FirstOrDefault();
                if (file.IsBlank())
                {
                    return(ModInstallationState.Corrupt);
                }

                // Check if exists
                if (!File.Exists(file))
                {
                    return(ModInstallationState.Corrupt);
                }

                // Compare hashes
                if (!Ext.CompareFileHash(file, verificationPair.Hash))
                {
                    return(ModInstallationState.Corrupt);
                }

                UpdateStatus(Progress + 100.0 / verificationPairs.Count);
            }

            // All checks passed => installed
            return(ModInstallationState.Installed);
        }