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); }
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); }