<commit_before>﻿using Microsoft.Deployment.WindowsInstaller;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TGServiceInterface;

namespace TGInstallerWrapper
{
	public partial class Main : Form
	{
		const string InstallDir = "TG Station Server";  //keep this in sync with the msi installer
		bool installing = false;
		bool cancelled = false;
		bool pathIsDefault = true;
		public Main()
		{
			InitializeComponent();
			FormClosing += Main_FormClosing;
			PathTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + Path.DirectorySeparatorChar + InstallDir;
			if (Server.VerifyConnection() == null)
				try
				{
					VersionLabel.Text = Server.GetComponent<ITGSService>().Version();
				}
				catch
				{
					VersionLabel.Text = "< v192.168.3.11 (Missing ITGService.Version())";
				}
			TargetVersionLabel.Text += " v" + FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion;
		}

		private void Main_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = installing;  //not allowed, nununu
		}

		enum PKillType
		{
			Killed,
			Aborted,
			NoneFound,
		}

		PKillType PromptKillProcess(string name)
		{
			foreach (var P in Process.GetProcessesByName(name))
			{
				if (!P.HasExited && MessageBox.Show(String.Format("Found running {0}.exe! Shall I terminate it?", name), "Warning", MessageBoxButtons.YesNo) != DialogResult.Yes)
					return PKillType.Aborted;
				if (!P.HasExited)
				{
					P.Kill();
					P.WaitForExit();
				}
				P.Dispose();
				return PKillType.Killed;
			}
			return PKillType.NoneFound;
		}

		async void DoInstall()
		{
			string path = null;
			string logfile = null;
			try
			{
				while (true)
				{
					var res = PromptKillProcess("TGCommandLine");
					if (res == PKillType.Aborted)
						return;
					else if (res == PKillType.Killed)
						continue;
					res = PromptKillProcess("TGControlPanel");
					if (res == PKillType.Aborted)
						return;
					else if (res == PKillType.Killed)
						continue;
					break;
				}
				
				var args = new List<string>();
				if (!pathIsDefault)
					args.Add(String.Format("INSTALLFOLDER=\"{0}\"", PathTextBox.Text));
				if (StartShortcutsCheckbox.Checked)
					args.Add("INSTALLSHORTCUTSTART=1");
				if (DeskShortcutsCheckbox.Checked)
					args.Add("INSTALLSHORTCUTDESK=1");

				args.Add("REBOOT=R");

				SelectPathButton.Enabled = false;
				PathTextBox.Enabled = false;
				DeskShortcutsCheckbox.Enabled = false;
				StartShortcutsCheckbox.Enabled = false;
				InstallButton.Enabled = false;
				ShowLogCheckbox.Enabled = false;
				InstallButton.Text = "Installing...";

				path = Path.GetTempFileName();
				File.Delete(path);  //we want a dir not a file
				Directory.CreateDirectory(path);
				var msipath = path + Path.DirectorySeparatorChar + "TGServiceInstaller.msi";
				File.WriteAllBytes(msipath, Properties.Resources.TGServiceInstaller);
				File.WriteAllBytes(path + Path.DirectorySeparatorChar + "cab1.cab", Properties.Resources.cab1);

				ProgressBar.Style = ProgressBarStyle.Marquee;

				if (Server.VerifyConnection() == null)
					try
					{
						Server.GetComponent<ITGSService>().PrepareForUpdate();
						Thread.Sleep(3000); //chat messages
					}
					catch
					{
						if (MessageBox.Show("ITGSService.PrepareForUpdate() threw an exception! Existing DreamDaemon instances will be terminated. Continue?", "Warning", MessageBoxButtons.YesNo) != DialogResult.Yes)
							return;
					}

				InstallCancelButton.Enabled = true;

				if (ShowLogCheckbox.Checked)
				{
					logfile = path + Path.DirectorySeparatorChar + "tgsinstall.log";
					Installer.EnableLog(InstallLogModes.Verbose | InstallLogModes.PropertyDump, logfile);
				}
				var cl = String.Join(" ", args);
				Installer.SetInternalUI(InstallUIOptions.Silent);
				Installer.SetExternalUI(OnUIUpdate, InstallLogModes.Progress);

				await Task.Factory.StartNew(() => Installer.InstallProduct(msipath, cl));

				if (cancelled)
				{
					MessageBox.Show("Operation cancelled!");<commit_msg>Fixes installer not calling PrepareForUpdate on versions < 3.1<commit_after>﻿using Microsoft.Deployment.WindowsInstaller;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TGServiceInterface;

namespace TGInstallerWrapper
{
	public partial class Main : Form
	{
		const string InstallDir = "TG Station Server";  //keep this in sync with the msi installer
		bool installing = false;
		bool cancelled = false;
		bool pathIsDefault = true;
		public Main()
		{
			InitializeComponent();
			FormClosing += Main_FormClosing;
			PathTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + Path.DirectorySeparatorChar + InstallDir;
			var verifiedConnection = Server.VerifyConnection() == null;
			try
			{
				VersionLabel.Text = Server.GetComponent<ITGSService>().Version();
			}
			catch
			{
				if(verifiedConnection)
					VersionLabel.Text = "< v192.168.3.11 (Missing ITGService.Version())";
			}
			TargetVersionLabel.Text += " v" + FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion;
		}

		private void Main_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = installing;  //not allowed, nununu
		}

		enum PKillType
		{
			Killed,
			Aborted,
			NoneFound,
		}

		PKillType PromptKillProcess(string name)
		{
			foreach (var P in Process.GetProcessesByName(name))
			{
				if (!P.HasExited && MessageBox.Show(String.Format("Found running {0}.exe! Shall I terminate it?", name), "Warning", MessageBoxButtons.YesNo) != DialogResult.Yes)
					return PKillType.Aborted;
				if (!P.HasExited)
				{
					P.Kill();
					P.WaitForExit();
				}
				P.Dispose();
				return PKillType.Killed;
			}
			return PKillType.NoneFound;
		}

		async void DoInstall()
		{
			string path = null;
			string logfile = null;
			try
			{
				while (true)
				{
					var res = PromptKillProcess("TGCommandLine");
					if (res == PKillType.Aborted)
						return;
					else if (res == PKillType.Killed)
						continue;
					res = PromptKillProcess("TGControlPanel");
					if (res == PKillType.Aborted)
						return;
					else if (res == PKillType.Killed)
						continue;
					break;
				}
				
				var args = new List<string>();
				if (!pathIsDefault)
					args.Add(String.Format("INSTALLFOLDER=\"{0}\"", PathTextBox.Text));
				if (StartShortcutsCheckbox.Checked)
					args.Add("INSTALLSHORTCUTSTART=1");
				if (DeskShortcutsCheckbox.Checked)
					args.Add("INSTALLSHORTCUTDESK=1");

				args.Add("REBOOT=R");

				SelectPathButton.Enabled = false;
				PathTextBox.Enabled = false;
				DeskShortcutsCheckbox.Enabled = false;
				StartShortcutsCheckbox.Enabled = false;
				InstallButton.Enabled = false;
				ShowLogCheckbox.Enabled = false;
				InstallButton.Text = "Installing...";

				path = Path.GetTempFileName();
				File.Delete(path);  //we want a dir not a file
				Directory.CreateDirectory(path);
				var msipath = path + Path.DirectorySeparatorChar + "TGServiceInstaller.msi";
				File.WriteAllBytes(msipath, Properties.Resources.TGServiceInstaller);
				File.WriteAllBytes(path + Path.DirectorySeparatorChar + "cab1.cab", Properties.Resources.cab1);

				ProgressBar.Style = ProgressBarStyle.Marquee;

				var connectionVerified = Server.VerifyConnection() == null;
				try
				{
					Server.GetComponent<ITGSService>().PrepareForUpdate();
					Thread.Sleep(3000); //chat messages
				}
				catch
				{
					if (connectionVerified && MessageBox.Show("ITGSService.PrepareForUpdate() threw an exception! Existing DreamDaemon instances will be terminated. Continue?", "Warning", MessageBoxButtons.YesNo) != DialogResult.Yes)
						return;
				}

				InstallCancelButton.Enabled = true;

				if (ShowLogCheckbox.Checked)
				{
					logfile = path + Path.DirectorySeparatorChar + "tgsinstall.log";
					Installer.EnableLog(InstallLogModes.Verbose | InstallLogModes.PropertyDump, logfile);
				}
				var cl = String.Join(" ", args);
				Installer.SetInternalUI(InstallUIOptions.Silent);
				Installer.SetExternalUI(OnUIUpdate, InstallLogModes.Progress);

				await Task.Factory.StartNew(() => Installer.InstallProduct(msipath, cl));

				if (cancelled)
				{
					MessageBox.Show("Operation cancelled!");
{'fraction_non_alphanumeric': 0.11402374515105207, 'fraction_numerical': 0.005172211120253909, 'mean_word_length': 3.623913043478261, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7027116', 'n_tokens_mistral': 3330, 'n_tokens_neox': 2611, 'n_words': 528}