<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Ionic.Zip;

namespace RelhaxModpack.Windows
{
    /// <summary>
    /// Interaction logic for Diagnostics.xaml
    /// </summary>
    public partial class Diagnostics : RelhaxWindow
    {
        public Diagnostics()
        {
            InitializeComponent();
        }

        private void RelhaxWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //check to make sure a selected tanks installation is selected
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                ToggleUIOptions(false);
            else
                ToggleUIOptions(true);
        }

        private void ChangeInstall_Click(object sender, RoutedEventArgs e)
        {
            //show a standard WoT selection window from manual fine WoT.exe
            Microsoft.Win32.OpenFileDialog manualWoTFind = new Microsoft.Win32.OpenFileDialog()
            {
                InitialDirectory = string.IsNullOrWhiteSpace(Settings.WoTDirectory) ? Settings.ApplicationStartupPath : Settings.WoTDirectory,
                AddExtension = true,
                CheckFileExists = true,
                CheckPathExists = true,
                Filter = "WorldOfTanks.exe|WorldOfTanks.exe",
                Multiselect = false,
                RestoreDirectory = true,
                ValidateNames = true
            };
            if ((bool)manualWoTFind.ShowDialog())
            {
                Settings.WoTDirectory = manualWoTFind.FileName;
            }
            else
            {
                Logging.WriteToLog("User Canceled installation");
            }

            //check to make sure a selected tanks installation is selected
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                ToggleUIOptions(false);
            else
                ToggleUIOptions(true);
        }

        private void LaunchWoTLauncher_Click(object sender, RoutedEventArgs e)
        {
            //just to make sure
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                return;
            //TODO: this code needs to be checked, see if it actually works
            Logging.Debug("Starting WoTLauncher with argument \"-integrity_default_client\"",nameof(Diagnostics));
            DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("startingLauncherRepairMode");
            string filename = Path.Combine(Settings.WoTDirectory, "WoTLauncher.exe");
            string formattedArguement = "-integrity_default_client";
            Logging.Info("Complete command: {0} {1}", nameof(Diagnostics), filename, formattedArguement);
            try
            {
                Process.Start(filename, formattedArguement);
            }
            catch (Exception ex)
            {
                Logging.Exception("LaunchWoTLauncher_Click", ex);
                DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("failedStartLauncherRepairMode");
                return;
            }
            DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("launcherRepairModeStarted");
        }

        private void CollectLogInfo_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                return;
            //setup UI
            Logging.Info("started collection of log files");
            DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("collectionLogInfo");
            //create the list of files to collect (should always collect these)
            List<string> filesToCollect = new List<string>()
            {
                //stuff in application startup path
                Settings.RelhaxLogFilepath,
                Settings.RelhaxSettingsFilepath,
                Settings.LastInstalledConfigFilepath,
                //stuff in the tanks location (need to be combined here cause it can change from installation)
                Path.Combine(Settings.WoTDirectory,"logs",Logging.InstallLogFilename),
                Path.Combine(Settings.WoTDirectory, "logs", Logging.UninstallLogFilename),
                Path.Combine(Settings.WoTDirectory, "python.log"),
                Path.Combine(Settings.WoTDirectory, "xvm.log"),
                Path.Combine(Settings.WoTDirectory, "pmod.log")
            };
            //use a nice diagnostic window to check if the user wants to include any other files
            AddPicturesZip apz = new AddPicturesZip();
            if((bool)apz.ShowDialog())
            {
                foreach (string s in apz.FilesToAddList.Items)
                    filesToCollect.Add(s);
            }
            apz = null;
            //check in the list to make sure that the entries are valid and paths exist
            Logging.Debug("Filtering list of files to collect",nameof(Diagnostics));
            filesToCollect = filesToCollect.Where(fileEntry => !string.IsNullOrWhiteSpace(fileEntry) && File.Exists(fileEntry)).ToList();
            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    foreach (string s in filesToCollect)
                    {
                        int duplicate = 0;
                        string fileNameToAdd = Path.GetFileName(s);
                        Logging.Debug("Attempting to add filename {0} in zip entry", nameof(Diagnostics), fileNameToAdd);
                        while (zip.ContainsEntry(fileNameToAdd))
                        {
                            fileNameToAdd = string.Format("{0}_{1}.{2}",
                                Path.GetFileNameWithoutExtension(fileNameToAdd), duplicate++, Path.GetExtension(fileNameToAdd));
                            Logging.Debug("exists, using filename {0}", nameof(Diagnostics), fileNameToAdd);
                        }
                        zip.AddFile(fileNameToAdd);
                        Logging.Debug("filename {0} added", nameof(Diagnostics), fileNameToAdd);
                    }
                    string zipSavePath = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                        string.Format("RelhaxModpackLogs_{0}.zip", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")));
                    zip.Save(zipSavePath);
                    Logging.Info("zip file saved to {0}", nameof(Diagnostics), zipSavePath);
                    DiagnosticsStatusTextBox.Text = string.Format("zip file saved to {0}", zipSavePath);
                }
            }
            catch(ZipException zex)
            {
                Logging.Exception(zex.ToString());
                DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("failedCreateZipfile");
            }
        }

        private void ToggleUIOptions(bool toggle)
        {
            LaunchWoTLauncher.IsEnabled = toggle;<commit_msg>fixed issues with making the diagnostic zip file<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Ionic.Zip;

namespace RelhaxModpack.Windows
{
    /// <summary>
    /// Interaction logic for Diagnostics.xaml
    /// </summary>
    public partial class Diagnostics : RelhaxWindow
    {
        public Diagnostics()
        {
            InitializeComponent();
        }

        private void RelhaxWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //check to make sure a selected tanks installation is selected
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                ToggleUIOptions(false);
            else
                ToggleUIOptions(true);
        }

        private void ChangeInstall_Click(object sender, RoutedEventArgs e)
        {
            //show a standard WoT selection window from manual fine WoT.exe
            Microsoft.Win32.OpenFileDialog manualWoTFind = new Microsoft.Win32.OpenFileDialog()
            {
                InitialDirectory = string.IsNullOrWhiteSpace(Settings.WoTDirectory) ? Settings.ApplicationStartupPath : Settings.WoTDirectory,
                AddExtension = true,
                CheckFileExists = true,
                CheckPathExists = true,
                Filter = "WorldOfTanks.exe|WorldOfTanks.exe",
                Multiselect = false,
                RestoreDirectory = true,
                ValidateNames = true
            };
            if ((bool)manualWoTFind.ShowDialog())
            {
                Settings.WoTDirectory = Path.GetDirectoryName(manualWoTFind.FileName);
            }
            else
            {
                Logging.WriteToLog("User Canceled installation");
            }

            //check to make sure a selected tanks installation is selected
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                ToggleUIOptions(false);
            else
                ToggleUIOptions(true);
        }

        private void LaunchWoTLauncher_Click(object sender, RoutedEventArgs e)
        {
            //just to make sure
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                return;

            Logging.Debug("Starting WoTLauncher with argument \"-integrity_default_client\"");
            DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("startingLauncherRepairMode");
            string filename = Path.Combine(Settings.WoTDirectory, "WoTLauncher.exe");
            string formattedArguement = "-integrity_default_client";
            Logging.Info("Complete command: {0} {1}", filename, formattedArguement);
            try
            {
                Process.Start(filename, formattedArguement);
            }
            catch (Exception ex)
            {
                Logging.Exception("LaunchWoTLauncher_Click", ex);
                DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("failedStartLauncherRepairMode");
                return;
            }
            DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("launcherRepairModeStarted");
        }

        private void CollectLogInfo_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Settings.WoTDirectory))
                return;
            //setup UI
            Logging.Info("started collection of log files");
            DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("collectionLogInfo");
            //create the list of files to collect (should always collect these)
            List<string> filesToCollect = new List<string>()
            {
                //stuff in application startup path
                Settings.RelhaxLogFilepath,
                Settings.RelhaxSettingsFilepath,
                Settings.LastInstalledConfigFilepath,
                //stuff in the tanks location (need to be combined here cause it can change from installation)
                Path.Combine(Settings.WoTDirectory,"logs",Logging.InstallLogFilename),
                Path.Combine(Settings.WoTDirectory, "logs", Logging.UninstallLogFilename),
                Path.Combine(Settings.WoTDirectory, "python.log"),
                Path.Combine(Settings.WoTDirectory, "xvm.log"),
                Path.Combine(Settings.WoTDirectory, "pmod.log")
            };
            //use a nice diagnostic window to check if the user wants to include any other files
            AddPicturesZip apz = new AddPicturesZip();
            if((bool)apz.ShowDialog())
            {
                foreach (string s in apz.FilesToAddList.Items)
                    filesToCollect.Add(s);
            }
            apz = null;
            //check in the list to make sure that the entries are valid and paths exist
            Logging.Debug("Filtering list of files to collect");
            filesToCollect = filesToCollect.Where(fileEntry => !string.IsNullOrWhiteSpace(fileEntry) && File.Exists(fileEntry)).ToList();
            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    foreach (string s in filesToCollect)
                    {
                        int duplicate = 0;

                        //get just the filename (it will be added to the zip file as just the name)
                        string fileNameToAdd = Path.GetFileName(s);

                        //run a loop to check if the file already exists in the zip with the same name, if it does then pad it until it does not
                        Logging.Debug("Attempting to add filename {0} in zip entry", fileNameToAdd);
                        while (zip.ContainsEntry(fileNameToAdd))
                        {
                            fileNameToAdd = string.Format("{0}_{1}.{2}", Path.GetFileNameWithoutExtension(fileNameToAdd), duplicate++, Path.GetExtension(fileNameToAdd));
                            Logging.Debug("exists, using filename {0}", fileNameToAdd);
                        }

                        //after padding, put the full path back together
                        fileNameToAdd = Path.Combine(Path.GetDirectoryName(s), fileNameToAdd);

                        //one last check to make sure it exists
                        if(!File.Exists(fileNameToAdd))
                        {
                            Logging.Error("the file {0} was parsed to exist but after loop modification, it does not!", fileNameToAdd);
                            continue;
                        }
                        else
                        {
                            //and the file to the zip file and grab the entry refrence
                            ZipEntry entry = zip.AddFile(fileNameToAdd);
                            //then use it to modify the name of the entry in the zip file
                            entry.FileName = Path.GetFileName(fileNameToAdd);
                            Logging.Info("file {0} added, entry name in zip = {1}", fileNameToAdd, entry.FileName);
                        }
                    }
                    string zipSavePath = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                        string.Format("RelhaxModpackLogs_{0}.zip", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")));
                    zip.Save(zipSavePath);
                    Logging.Info("zip file saved to {0}", zipSavePath);
                    DiagnosticsStatusTextBox.Text = string.Format("zip file saved to {0}", zipSavePath);
                }
            }
            catch(ZipException zex)
            {
                Logging.Exception(zex.ToString());
                DiagnosticsStatusTextBox.Text = Translations.GetTranslatedString("failedCreateZipfile");
            }
        }

        private void ToggleUIOptions(bool toggle)
        {
            LaunchWoTLauncher.IsEnabled = toggle;
{'fraction_non_alphanumeric': 0.08000528960592436, 'fraction_numerical': 0.00224808251785242, 'mean_word_length': 1.7560131195335278, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4370170', 'n_tokens_mistral': 3745, 'n_tokens_neox': 3566, 'n_words': 853}