<commit_before>﻿using System;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using Duality;
using Duality.Editor.Properties;
using Duality.Editor.Forms;
using Duality.Editor.PackageManagement;

namespace Duality.Editor
{
	internal static class Program
	{
		private const string DualityMainLicenseUrl = @"https://github.com/AdamsLair/duality/raw/master/LICENSE";

		private static StreamWriter logfileWriter;
		private static TextWriterLogOutput logfileOutput;
		private static bool recoverFromPluginReload;


		[STAThread]
		private static void Main(string[] args)
		{
			// Parse command line arguments
			ParseCommandLineArguments(args);

			// Culture setup
			Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
			Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

			// Set up a text logfile
			ArchiveOldLogfile();
			CreateLogfile();

			// Winforms Setup
			PrepareWinFormsApplication();
			
			// Restore or remove packages to match package config
			VerifyPackageSetup();

			// Run the editor
			SplashScreen splashScreen = new SplashScreen(recoverFromPluginReload);
			splashScreen.Show();
			Application.Run();

			// Clean up the log file
			CloseLogfile();
		}

		private static void ParseCommandLineArguments(string[] args)
		{
			recoverFromPluginReload = false;
			foreach (string argument in args)
			{
				if (argument == "debug")
					System.Diagnostics.Debugger.Launch();
				else if (argument == "recover")
					recoverFromPluginReload = true;
			}
		}
		private static void PrepareWinFormsApplication()
		{
			Application.CurrentCulture = Thread.CurrentThread.CurrentCulture;
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.ThreadException += Application_ThreadException;
			AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
		}
		private static void VerifyPackageSetup()
		{
			PackageManager packageManager = new PackageManager();

			// On the first install startup, display a generic license agreement for Duality
			if (packageManager.IsFirstInstall)
			{
				LicenseAcceptDialog licenseDialog = new LicenseAcceptDialog
				{
					DescriptionText = GeneralRes.LicenseAcceptDialog_FirstStartGeneric,
					LicenseUrl = new Uri(DualityMainLicenseUrl)
				};
				DialogResult result = licenseDialog.ShowDialog();
				if (result != DialogResult.OK)
				{
					Application.Exit();
					return;
				}
			}

			// Perform the initial package update - even before initializing the editor
			if (packageManager.IsPackageSyncRequired)
			{
				Log.Editor.Write("Updating Packages...");
				Log.Editor.PushIndent();
				ProcessingBigTaskDialog setupDialog = new ProcessingBigTaskDialog(
					GeneralRes.TaskInstallPackages_Caption, 
					GeneralRes.TaskInstallPackages_Desc, 
					SynchronizePackages, 
					packageManager);
				setupDialog.ShowInTaskbar = true;
				setupDialog.MainThreadRequired = false;
				setupDialog.ShowDialog();
				Log.Editor.PopIndent();
			}
			// Restart to apply the update
			if (packageManager.ApplyUpdate())
			{
				Application.Exit();
				return;
			}
			// If we have nothing to apply, but still require a sync, something went wrong.
			// Should this happen on our first start, we'll remind the user that the install
			// requires an internet connection and refuse to start.
			else if (packageManager.IsPackageSyncRequired && packageManager.IsFirstInstall)
			{
				DialogResult result = MessageBox.Show( 
					GeneralRes.Msg_ErrorFirstDualityInstall_Desc, 
					GeneralRes.Msg_ErrorFirstDualityInstall_Caption, 
					MessageBoxButtons.OK, MessageBoxIcon.Information);
				Application.Exit();
				return;
			}
		}
		private static IEnumerable SynchronizePackages(ProcessingBigTaskDialog.WorkerInterface workerInterface)
		{
			PackageManager manager = workerInterface.Data as PackageManager;

			// Set the working state and yield, so the UI can update properly in case we're in the main thread
			workerInterface.Progress = 0.0f;<commit_msg>Startup Cancel Bugfix #FIX: Fixed a bug in startup cancellation code that would prevent Duality from shutting down when intending to do so.<commit_after>﻿using System;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using Duality;
using Duality.Editor.Properties;
using Duality.Editor.Forms;
using Duality.Editor.PackageManagement;

namespace Duality.Editor
{
	internal static class Program
	{
		private const string DualityMainLicenseUrl = @"https://github.com/AdamsLair/duality/raw/master/LICENSE";

		private static StreamWriter logfileWriter;
		private static TextWriterLogOutput logfileOutput;
		private static bool recoverFromPluginReload;


		[STAThread]
		private static void Main(string[] args)
		{
			// Parse command line arguments
			ParseCommandLineArguments(args);

			// Culture setup
			Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
			Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

			// Set up a text logfile
			ArchiveOldLogfile();
			CreateLogfile();

			// Winforms Setup
			PrepareWinFormsApplication();
			
			// Restore or remove packages to match package config
			if (!VerifyPackageSetup())
			{
				Application.Exit();
				return;
			}

			// Run the editor
			SplashScreen splashScreen = new SplashScreen(recoverFromPluginReload);
			splashScreen.Show();
			Application.Run();

			// Clean up the log file
			CloseLogfile();
		}

		private static void ParseCommandLineArguments(string[] args)
		{
			recoverFromPluginReload = false;
			foreach (string argument in args)
			{
				if (argument == "debug")
					System.Diagnostics.Debugger.Launch();
				else if (argument == "recover")
					recoverFromPluginReload = true;
			}
		}
		private static void PrepareWinFormsApplication()
		{
			Application.CurrentCulture = Thread.CurrentThread.CurrentCulture;
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.ThreadException += Application_ThreadException;
			AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
		}
		private static bool VerifyPackageSetup()
		{
			PackageManager packageManager = new PackageManager();

			// On the first install startup, display a generic license agreement for Duality
			if (packageManager.IsFirstInstall)
			{
				LicenseAcceptDialog licenseDialog = new LicenseAcceptDialog
				{
					DescriptionText = GeneralRes.LicenseAcceptDialog_FirstStartGeneric,
					LicenseUrl = new Uri(DualityMainLicenseUrl)
				};
				DialogResult result = licenseDialog.ShowDialog();
				if (result != DialogResult.OK)
					return false;
			}

			// Perform the initial package update - even before initializing the editor
			if (packageManager.IsPackageSyncRequired)
			{
				Log.Editor.Write("Updating Packages...");
				Log.Editor.PushIndent();
				ProcessingBigTaskDialog setupDialog = new ProcessingBigTaskDialog(
					GeneralRes.TaskInstallPackages_Caption, 
					GeneralRes.TaskInstallPackages_Desc, 
					SynchronizePackages, 
					packageManager);
				setupDialog.ShowInTaskbar = true;
				setupDialog.MainThreadRequired = false;
				setupDialog.ShowDialog();
				Log.Editor.PopIndent();
			}

			// Restart to apply the update
			if (packageManager.ApplyUpdate())
			{
				return false;
			}
			// If we have nothing to apply, but still require a sync, something went wrong.
			// Should this happen on our first start, we'll remind the user that the install
			// requires an internet connection and refuse to start.
			else if (packageManager.IsPackageSyncRequired && packageManager.IsFirstInstall)
			{
				DialogResult result = MessageBox.Show( 
					GeneralRes.Msg_ErrorFirstDualityInstall_Desc, 
					GeneralRes.Msg_ErrorFirstDualityInstall_Caption, 
					MessageBoxButtons.OK, MessageBoxIcon.Information);
				return false;
			}

			return true;
		}
		private static IEnumerable SynchronizePackages(ProcessingBigTaskDialog.WorkerInterface workerInterface)
		{
			PackageManager manager = workerInterface.Data as PackageManager;

			// Set the working state and yield, so the UI can update properly in case we're in the main thread
			workerInterface.Progress = 0.0f;
{'fraction_non_alphanumeric': 0.07736966824644549, 'fraction_numerical': 0.00047393364928909954, 'mean_word_length': 4.069669669669669, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '6597874', 'n_tokens_mistral': 2761, 'n_tokens_neox': 2239, 'n_words': 604}