src/TF2HUD.Editor/MainWindow.xaml.cs
/// <summary>
///     Invoke HUD installation or setting the tf/custom directory, if not already set.
/// </summary>
Invoke HUD installation or setting the tf/custom directory, if not already set.
Invoke HUD installation or setting the tf / custom directory if not already set .
private void BtnInstall_OnClick(object sender, RoutedEventArgs args)
        {
            try
            {
                BtnSwitch.IsEnabled = false;
                Json.SelectedHUD = Json.HighlightedHUD;
                Settings.Default.hud_selected = HudSelection = Json.SelectedHUD.Name;
                if (!Utilities.CheckUserPath(HudPath))
                    _hudDirectory.Setup(HudPath, true);
                if (Utilities.IsGameRunning())
                {
                    _notifier.ShowMessageBox(MessageBoxImage.Warning, Utilities.GetLocalizedString(Properties.Resources.info_game_running));
                    return;
                }
                var worker = new BackgroundWorker();
                worker.DoWork += (_, _) =>
                {
                    Dispatcher.Invoke(() =>
                    {
                        Logger.Info($"Start installing {HudSelection}.");
                        Json.SelectedHUD.Update();
                        Logger.Info("Preparing directories for extraction.");
                        foreach (var x in Json.HUDList)
                            if (Directory.Exists(HudPath + "\\" + x.Name?.ToLowerInvariant()))
                                Directory.Delete(HudPath + "\\" + x.Name?.ToLowerInvariant(), true);
                        var tempFile = $"{AppDomain.CurrentDomain.BaseDirectory}temp.zip";
                        if (!File.Exists(tempFile)) tempFile = "temp.zip";
                        using var archive = ZipFile.OpenRead(tempFile);
                        var hudName = archive.Entries.FirstOrDefault(entry =>
                            entry.FullName.EndsWith("/", StringComparison.OrdinalIgnoreCase) &&
                            string.IsNullOrWhiteSpace(entry.Name));
                        Logger.Info($"Extracting {HudSelection} to: {HudPath}");
                        ZipFile.ExtractToDirectory(tempFile, HudPath);
                        Directory.Move($"{HudPath}\\{hudName}", $"{HudPath}\\{HudSelection}");
                        if (string.IsNullOrWhiteSpace(HudSelection)) return;
                        Json.SelectedHUD.Settings.SaveSettings();
                        SetPageView(Json.SelectedHUD);
                        Json.SelectedHUD.ApplyCustomizations();
                    });
                };
                worker.RunWorkerCompleted += (_, _) =>
                {
                    LblStatus.Content = string.Format(Properties.Resources.status_installed_now,
                        Settings.Default.hud_selected, DateTime.Now);
                    Json.OnPropertyChanged("HighlightedHUDInstalled");
                    BtnSwitch.SetBinding(
                        IsEnabledProperty,
                        new Binding
                        {
                            Source = Json,
                            Path = new PropertyPath("SelectedHUD"),
                            Converter = new NullCheckConverter()
                        }
                    );
                    Json.OnPropertyChanged("SelectedHUD");
                    var tempPath = $"{Directory.GetCurrentDirectory()}\\temp.zip";
                    if (File.Exists(tempPath))
                        File.Delete(tempPath);
                };
                worker.RunWorkerAsync();
            }
            catch (Exception e)
            {
                _notifier.ShowMessageBox(MessageBoxImage.Error, $"{string.Format(Utilities.GetLocalizedString(Properties.Resources.error_hud_install), HudSelection)} {e.Message}");
            }
        }
private void BtnInstall_OnClick ( object sender , RoutedEventArgs args ) { try { BtnSwitch . IsEnabled = false ; Json . SelectedHUD = Json . HighlightedHUD ; Settings . Default . hud_selected = HudSelection = Json . SelectedHUD . Name ; if ( ! Utilities . CheckUserPath ( HudPath ) ) _hudDirectory . Setup ( HudPath , true ) ; if ( Utilities . IsGameRunning ( ) ) { _notifier . ShowMessageBox ( MessageBoxImage . Warning , Utilities . GetLocalizedString ( Properties . Resources . info_game_running ) ) ; return ; } var worker = new BackgroundWorker ( ) ; worker . DoWork += ( _ , _ ) => { Dispatcher . Invoke ( ( ) => { Logger . Info ( $" Start installing  { HudSelection } . " ) ; Json . SelectedHUD . Update ( ) ; Logger . Info ( " " ) ; foreach ( var x in Json . HUDList ) if ( Directory . Exists ( HudPath + " \\ " + x . Name ? . ToLowerInvariant ( ) ) ) Directory . Delete ( HudPath + " \\ " + x . Name ? . ToLowerInvariant ( ) , true ) ; var tempFile = $" { AppDomain . CurrentDomain . BaseDirectory } temp.zip " ; if ( ! File . Exists ( tempFile ) ) tempFile = " " ; using var archive = ZipFile . OpenRead ( tempFile ) ; var hudName = archive . Entries . FirstOrDefault ( entry => entry . FullName . EndsWith ( " " , StringComparison . OrdinalIgnoreCase ) && string . IsNullOrWhiteSpace ( entry . Name ) ) ; Logger . Info ( $" Extracting  { HudSelection }  to:  { HudPath } " ) ; ZipFile . ExtractToDirectory ( tempFile , HudPath ) ; Directory . Move ( $" { HudPath } \\ { hudName } " , $" { HudPath } \\ { HudSelection } " ) ; if ( string . IsNullOrWhiteSpace ( HudSelection ) ) return ; Json . SelectedHUD . Settings . SaveSettings ( ) ; SetPageView ( Json . SelectedHUD ) ; Json . SelectedHUD . ApplyCustomizations ( ) ; } ) ; } ; worker . RunWorkerCompleted += ( _ , _ ) => { LblStatus . Content = string . Format ( Properties . Resources . status_installed_now , Settings . Default . hud_selected , DateTime . Now ) ; Json . OnPropertyChanged ( " " ) ; BtnSwitch . SetBinding ( IsEnabledProperty , new Binding { Source = Json , Path = new PropertyPath ( " " ) , Converter = new NullCheckConverter ( ) } ) ; Json . OnPropertyChanged ( " " ) ; var tempPath = $" { Directory . GetCurrentDirectory ( ) } \\ temp.zip " ; if ( File . Exists ( tempPath ) ) File . Delete ( tempPath ) ; } ; worker . RunWorkerAsync ( ) ; } catch ( Exception e ) { _notifier . ShowMessageBox ( MessageBoxImage . Error , $" { string . Format ( Utilities . GetLocalizedString ( Properties . Resources . error_hud_install ) , HudSelection ) } { e . Message } " ) ; } }
Invoke HUD installation or setting the tf/custom directory, if not already set.
Invoke HUD installation or setting the tf / custom directory if not already set .
// Prevent switching HUD while installing to ensure that HighlightedHUD is the same as SelectedHUD at worker.RunWorkerCompleted // Force the user to set a directory before installing. // Stop the process if Team Fortress 2 is still running. // Step 1. Retrieve the HUD object, then download and extract it into the tf/custom directory. // Step 2. Clear tf/custom directory of other installed HUDs. // Step 3. Record the name of the HUD inside the downloaded folder. // Step 4. Extract the downloaded HUD and rename it to match the schema. // Step 5. Update the page view. // Update Install/Uninstall/Reset Buttons // Update Switch HUD Button // Clean the application directory.
sender
object
args
RoutedEventArgs