Exemplo n.º 1
0
        private bool ExecuteInstallTask()
        {
            UpdateStatus(TaskExecutionStatus.Install);

            // Get mod info
            UpdateStatus(TaskExecutionStatus.InstallGetModInfo);
            var modInfo = _apiService.GetModInfo(Task.Identifier);

            if (modInfo == null)
            {
                return(false);
            }

            // Reset everything
            _fileChanges.Clear();

            // Retrieve file info
            var fileInfo = _apiService.GetFileInfo(modInfo.FileId);

            // Download archive
            UpdateStatus(TaskExecutionStatus.InstallDownload);
            var downloadedFileContainer = _webService.Download(fileInfo.DownloadUrl, FileSystem.CreateTempFile($"Mod_{modInfo.FileId}"));

            if (downloadedFileContainer == null)
            {
                _windowService.ShowErrorWindowAsync(Localization.Current.Task_Install_Download_Failed).GetResult();
                return(false);
            }
            var contentType    = downloadedFileContainer.ResponseHeaders[HttpResponseHeader.ContentType];
            var downloadedFile = downloadedFileContainer.FileInfo;

            if (IsAbortPending)
            {
                TaskAborted?.Invoke(this, new TaskEventArgs(Task));
                return(false);
            }

            // Unpack archive
            UpdateStatus(TaskExecutionStatus.InstallUnpack);
            string unpackedDir = FileSystem.CreateTempDirectory($"Mod_{modInfo.FileId}");

            _aliasService.Set(new InternalAlias(InternalAliasKeyword.ArchiveExtractedDirectory, unpackedDir));
            var extractSuccess = _archivingService.ExtractFiles(downloadedFile.FullName, unpackedDir);

            if (!extractSuccess || !Directory.Exists(unpackedDir))
            {
                // Note: RAR5 supported now, but might be useful for future formats
                //if ("application/x-rar-compressed".Equals(contentType))
                //{
                //    _windowService.ShowErrorWindowAsync(Localization.Current.Task_Install_Unpack_Failed_RAR5).GetResult();
                //}
                //else
                //{
                _windowService.ShowErrorWindowAsync(Localization.Current.Task_Install_Unpack_Failed).GetResult();
                //}
                return(false);
            }
            if (IsAbortPending)
            {
                TaskAborted?.Invoke(this, new TaskEventArgs(Task));
                return(false);
            }

            // Get commands
            UpdateStatus(TaskExecutionStatus.InstallExecute);
            var    commands         = fileInfo.InstallationCommands;
            string commandContextID = modInfo.FileId; // can be improved later

            // If we aren't copying files, we probably don't have an installation scheme for this type of submission
            if (!commands.Any(c => CommandType.Copy.Equals(c.Type)))
            {
                _windowService.ShowErrorWindowAsync(Localization.Current.Task_UnknownInstallationProcess).GetResult();
                return(false);
            }

            // Execute commands
            foreach (var command in commands)
            {
                if (IsAbortPending)
                {
                    TaskAborted?.Invoke(this, new TaskEventArgs(Task));
                    return(false);
                }

                bool success = _commandExecutionService.ExecuteCommand(command, commandContextID);
                if (!success)
                {
#if !DEBUG
                    _windowService.ShowErrorWindowAsync(Localization.Current.Task_CommandExecutionFailed).GetResult();
                    return(false);
#endif
                }

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

            // Clear local aliases
            _aliasService.Clear(commandContextID);

            // Store results
            UpdateStatus(TaskExecutionStatus.InstallStoreResults);
            _persistenceService.RecordInstall(modInfo.Identifier, _fileChanges.ToArray());

            return(true);
        }
Exemplo n.º 2
0
        private bool ExecuteInstallTask()
        {
            UpdateStatus(TaskExecutionStatus.Install);

            // Get mod info
            UpdateStatus(TaskExecutionStatus.InstallGetModInfo);
            var modInfo = _apiService.GetModInfo(Task.ModID);

            if (modInfo == null)
            {
                return(false);
            }

            // Reset everything
            _fileChanges.Clear();

            // Download archive
            UpdateStatus(TaskExecutionStatus.InstallDownload);
            var downloadedFile = _webService.Download(modInfo.DownloadURL, FileSystem.CreateTempFile($"Mod_{modInfo.ModID}"));

            if (downloadedFile == null)
            {
                return(false);
            }
            if (IsAbortPending)
            {
                TaskAborted?.Invoke(this, new TaskEventArgs(Task));
                return(false);
            }

            // Unpack archive
            UpdateStatus(TaskExecutionStatus.InstallUnpack);
            string unpackedDir = FileSystem.CreateTempDirectory($"Mod_{modInfo.ModID}");

            _aliasService.Set(new InternalAlias(InternalAliasKeyword.ArchiveExtractedDirectory, unpackedDir));
            _archivingService.ExtractFiles(downloadedFile.FullName, unpackedDir);
            if (!Directory.Exists(unpackedDir))
            {
                return(false);
            }
            if (IsAbortPending)
            {
                TaskAborted?.Invoke(this, new TaskEventArgs(Task));
                return(false);
            }

            // Get commands
            UpdateStatus(TaskExecutionStatus.InstallExecute);
            var    commands         = _apiService.GetInstallationCommands(modInfo.ModID);
            string commandContextID = modInfo.ModID; // can be improved later

            // Execute commands
            foreach (var command in commands)
            {
                if (IsAbortPending)
                {
                    TaskAborted?.Invoke(this, new TaskEventArgs(Task));
                    return(false);
                }

                bool success = _commandExecutionService.ExecuteCommand(command, commandContextID);
                if (!success)
                {
#if !DEBUG
                    _windowService.ShowErrorWindowAsync(Localization.Current.Task_CommandExecutionFailed).GetResult();
                    return(false);
#endif
                }

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

            // Clear local aliases
            _aliasService.Clear(commandContextID);

            // Store results
            UpdateStatus(TaskExecutionStatus.InstallStoreResults);
            _persistenceService.RecordInstall(commandContextID, _fileChanges.ToArray());

#if !DEBUG
            // Submit results
            UpdateStatus(TaskExecutionStatus.InstallSubmitResults);
            _apiService.ReportSuccessfulExecution(Task.ModID, TaskType.Install);
#endif

            return(true);
        }
Exemplo n.º 3
0
        private bool ExecutePromptPathAlias(bool isGlobal, bool isFile, string aliasKeyword, string aliasDisplayName,
                                            string[] defaultPaths)
        {
            // Parameter checks
            if (aliasKeyword.IsBlank())
            {
                return(false);
            }
            if (aliasDisplayName.IsBlank())
            {
                aliasDisplayName = aliasKeyword;
            }

            // Check if alias is already known
            if (_aliasService.IsKnown(aliasKeyword))
            {
                return(true);
            }

            // Check if any of the default paths exist
            if (defaultPaths.AnySafe())
            {
                // Find the first default path that exists
                string validDefaultPath = null;
                foreach (string defaultPath in defaultPaths)
                {
                    string absolutePath = Ext.GetPossibleAbsolutePaths(defaultPath).FirstOrDefault();
                    if (absolutePath.IsNotBlank())
                    {
                        validDefaultPath = absolutePath;
                        break;
                    }
                }

                // If found - set alias and return
                if (validDefaultPath.IsNotBlank())
                {
                    _aliasService.Set(new GlobalAlias(aliasKeyword, validDefaultPath));
                    return(true);
                }
            }

            // Prompt the user and set alias
            var    type   = isFile ? AliasPromptType.File : AliasPromptType.Directory;
            string result = _windowService.ShowAliasPromptWindowAsync(type, aliasDisplayName).GetResult();

            if (result.IsNotBlank())
            {
                if (isGlobal)
                {
                    _aliasService.Set(new GlobalAlias(aliasKeyword, result));
                }
                else
                {
                    _aliasService.Set(new LocalAlias(_contextID, aliasKeyword, result));
                }
                return(true);
            }

            return(false);
        }