<commit_before>﻿using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.WindowsAPICodePack.Dialogs;
using Newtonsoft.Json;

namespace AutomationTool
{
	[Serializable]
	public partial class AutomationTool : Form
	{
		// Is this a debug build?
		public bool BUILD_DEBUG;

		// data_Files : Column names
		public const string STR_DATA_FILES_CHK_ENABLED = "col_Enabled";
		public const string STR_DATA_FILES_CHK_COPY = "col_Copy";
		public const string STR_DATA_FILES_TXT_FILE = "col_File";
		public const string STR_DATA_FILES_BTN_FILE_BROWSE = "col_FileBrowse";
		public const string STR_DATA_FILES_TXT_STAGING = "col_Staging";
		public const string STR_DATA_FILES_BTN_STAGING_BROWSE = "col_StagingBrowse";
		public const string STR_DATA_FILES_TXT_MUNGE_DIR = "col_MungeDir";
		public const string STR_DATA_FILES_TXT_MUNGED_FILES = "col_MungedFiles";
		public const string STR_DATA_FILES_BTN_MUNGED_FILES_EDIT = "col_MungedFilesEdit";
		public const string STR_DATA_FILES_CHK_IS_MUNGE_SCRIPT = "col_IsMungeScript";
		public const string STR_DATA_FILES_CHK_IS_VALID = "col_IsValid";

		// data_Files : Column indexes
		public const int INT_DATA_FILES_CHK_ENABLED = 0;
		public const int INT_DATA_FILES_CHK_COPY = 1;
		public const int INT_DATA_FILES_TXT_FILE = 2;
		public const int INT_DATA_FILES_BTN_FILE_BROWSE = 3;
		public const int INT_DATA_FILES_TXT_STAGING = 4;
		public const int INT_DATA_FILES_BTN_STAGING_BROWSE = 5;
		public const int INT_DATA_FILES_TXT_MUNGE_DIR = 6;
		public const int INT_DATA_FILES_TXT_MUNGED_FILES = 7;
		public const int INT_DATA_FILES_BTN_MUNGED_FILES_EDIT = 8;
		public const int INT_DATA_FILES_CHK_IS_MUNGE_SCRIPT = 9;
		public const int INT_DATA_FILES_CHK_IS_VALID = 10;
		
		// data_Files : Cell error color
		public Color errorRed = Color.FromArgb(251, 99, 99);

		// Updates
		public static UpdateInfo latestAppVersion = new UpdateInfo();
		public static bool updateAvailable = false;

		public enum CellChangeMethod
		{
			Button,     // Buttons outside the DataGridView
			Cell        // Cells inside the DataGridView
		};


		// This is the very first method called by the application. It initializes the UI controls and loads user settings.
		public AutomationTool()
		{
			InitializeComponent();

			// Set debug flag based on solution configuration
			#if (BUILD_DEBUG)
				BUILD_DEBUG = true;
			#elif (BUILD_RELEASE)
				BUILD_DEBUG = false;
			#endif
			Trace.WriteLine("BUILD_DEBUG: " + BUILD_DEBUG);

			latestAppVersion.BuildNum = 0;

			// Load any saved settings
			LoadSettings();
		}

		public Prefs prefs = new Prefs();

		// When the AutomationTool form is finished loading:
		// Create the tray icon, initialize some stuff with the file list, and start a new output log.
		private void AutomationTool_Load(object sender, EventArgs e)
		{
			// Set the tray icon if it's enabled
			if (prefs.ShowTrayIcon)
			{
				trayIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
				trayIcon.Text = "Zero Munge: Idle";
			}
			else
			{
				trayIcon.Visible = false;
			}

			// Set the visibility of the debug columns in the file list
			col_MungeDir.Visible = BUILD_DEBUG;
			col_IsMungeScript.Visible = BUILD_DEBUG;
			col_IsValid.Visible = BUILD_DEBUG;
			button2.Visible = BUILD_DEBUG;
			button3.Visible = BUILD_DEBUG;

			// Set the visibility of the DataGridView buttons
			col_FileBrowse.UseColumnTextForButtonValue = true;
			col_StagingBrowse.UseColumnTextForButtonValue = true;
			col_MungedFilesEdit.UseColumnTextForButtonValue = true;

			// Start a new log file
			string openMessage = "Opened logfile ZeroMunge_OutputLog.log  " + DateTime.Now.ToString("yyyy-MM-dd") +  " " + Utilities.GetTimestamp();
			File.WriteAllText(Directory.GetCurrentDirectory() + @"\ZeroMunge_OutputLog.log", openMessage + Environment.NewLine);
		}

		private void AutomationTool_Shown(object sender, EventArgs e)
		{
			// Check for application updates
			updateAvailable = CheckForUpdates();
			if (updateAvailable)
			{
				if (prefs.ShowUpdatePromptOnStartup)
				{
					Trace.WriteLine("Update is available. Pushing update prompt.");
					StartUpdateFlow();
				}
				else
				{
					Trace.WriteLine("Update is available, but user has specified to not show the update prompt on startup.");
				}
			}

			SetUpdateStatusBar(updateAvailable);
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="available"></param>
		public void SetUpdateStatusBar(bool available)
		{

		}


		/// <summary>
		/// Loads and initializes saved user settings.
		/// </summary>
		public void LoadSettings()
		{
			// Load the saved user settings into our prefs object
			prefs = Utilities.LoadPrefs();

			Debug.WriteLine("Loading GameDirectory: " + prefs.GameDirectory);
			Log("Loading GameDirectory: " + prefs.GameDirectory);
		}


		public static bool CheckForUpdates()
		{
			Trace.WriteLine("Checking for application updates...");

			if (Utilities.CheckForInternetConnection())
			{
				latestAppVersion = Utilities.GetLatestVersion();
				int curBuild = Properties.Settings.Default.Info_BuildNum;

				Debug.WriteLine("Current build, latest build:    {0}, {1}", curBuild, latestAppVersion.BuildNum);

				// Prompt for update if one is available
				if (curBuild < latestAppVersion.BuildNum)
				{
					Trace.WriteLine("Update is available.");
					return true;
				}
				else
				{
					Trace.WriteLine("No updates available!");
					return false;
				}
			}
			else
			{
				Trace.WriteLine("There is no internet connection!");
				return false;
			}
		}


		public static void StartUpdateFlow()<commit_msg>- Add thread-safe starting and stopping of listener server; - Add functionality for preferences to be saved and loaded by the user;<commit_after>﻿using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.WindowsAPICodePack.Dialogs;
using Newtonsoft.Json;

namespace AutomationTool
{
	[Serializable]
	public partial class AutomationTool : Form
	{
		// Is this a debug build?
		public bool BUILD_DEBUG;

		// data_Files : Column names
		public const string STR_DATA_FILES_CHK_ENABLED = "col_Enabled";
		public const string STR_DATA_FILES_CHK_COPY = "col_Copy";
		public const string STR_DATA_FILES_TXT_FILE = "col_File";
		public const string STR_DATA_FILES_BTN_FILE_BROWSE = "col_FileBrowse";
		public const string STR_DATA_FILES_TXT_STAGING = "col_Staging";
		public const string STR_DATA_FILES_BTN_STAGING_BROWSE = "col_StagingBrowse";
		public const string STR_DATA_FILES_TXT_MUNGE_DIR = "col_MungeDir";
		public const string STR_DATA_FILES_TXT_MUNGED_FILES = "col_MungedFiles";
		public const string STR_DATA_FILES_BTN_MUNGED_FILES_EDIT = "col_MungedFilesEdit";
		public const string STR_DATA_FILES_CHK_IS_MUNGE_SCRIPT = "col_IsMungeScript";
		public const string STR_DATA_FILES_CHK_IS_VALID = "col_IsValid";

		// data_Files : Column indexes
		public const int INT_DATA_FILES_CHK_ENABLED = 0;
		public const int INT_DATA_FILES_CHK_COPY = 1;
		public const int INT_DATA_FILES_TXT_FILE = 2;
		public const int INT_DATA_FILES_BTN_FILE_BROWSE = 3;
		public const int INT_DATA_FILES_TXT_STAGING = 4;
		public const int INT_DATA_FILES_BTN_STAGING_BROWSE = 5;
		public const int INT_DATA_FILES_TXT_MUNGE_DIR = 6;
		public const int INT_DATA_FILES_TXT_MUNGED_FILES = 7;
		public const int INT_DATA_FILES_BTN_MUNGED_FILES_EDIT = 8;
		public const int INT_DATA_FILES_CHK_IS_MUNGE_SCRIPT = 9;
		public const int INT_DATA_FILES_CHK_IS_VALID = 10;
		
		// data_Files : Cell error color
		public Color errorRed = Color.FromArgb(251, 99, 99);

		// Updates
		public static UpdateInfo latestAppVersion = new UpdateInfo();
		public static bool updateAvailable = false;

		public enum CellChangeMethod
		{
			Button,     // Buttons outside the DataGridView
			Cell        // Cells inside the DataGridView
		};


		// This is the very first method called by the application. It initializes the UI controls and loads user settings.
		public AutomationTool()
		{
			InitializeComponent();

			// Set debug flag based on solution configuration
			#if (BUILD_DEBUG)
				BUILD_DEBUG = true;
			#elif (BUILD_RELEASE)
				BUILD_DEBUG = false;
			#endif
			Trace.WriteLine("BUILD_DEBUG: " + BUILD_DEBUG);

			latestAppVersion.BuildNum = 0;

			// Load any saved settings
			LoadSettings();
		}

		public Prefs prefs = new Prefs();

		// When the AutomationTool form is finished loading:
		// Create the tray icon, initialize some stuff with the file list, and start a new output log.
		private void AutomationTool_Load(object sender, EventArgs e)
		{
			// Set the tray icon if it's enabled
			if (prefs.ShowTrayIcon)
			{
				trayIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
				trayIcon.Text = "Zero Munge: Idle";
			}
			else
			{
				trayIcon.Visible = false;
			}

			// Set the visibility of the debug columns in the file list
			col_MungeDir.Visible = BUILD_DEBUG;
			col_IsMungeScript.Visible = BUILD_DEBUG;
			col_IsValid.Visible = BUILD_DEBUG;
			button2.Visible = BUILD_DEBUG;
			button3.Visible = BUILD_DEBUG;

			// Set the visibility of the DataGridView buttons
			col_FileBrowse.UseColumnTextForButtonValue = true;
			col_StagingBrowse.UseColumnTextForButtonValue = true;
			col_MungedFilesEdit.UseColumnTextForButtonValue = true;

			// Start a new log file
			string openMessage = "Opened logfile ZeroMunge_OutputLog.log  " + DateTime.Now.ToString("yyyy-MM-dd") +  " " + Utilities.GetTimestamp();
			File.WriteAllText(Directory.GetCurrentDirectory() + @"\ZeroMunge_OutputLog.log", openMessage + Environment.NewLine);
		}

		private void AutomationTool_Shown(object sender, EventArgs e)
		{
			// Check for application updates
			updateAvailable = CheckForUpdates();
			if (updateAvailable)
			{
				if (prefs.ShowUpdatePromptOnStartup)
				{
					Trace.WriteLine("Update is available. Pushing update prompt.");
					StartUpdateFlow();
				}
				else
				{
					Trace.WriteLine("Update is available, but user has specified to not show the update prompt on startup.");
				}
			}

			
		}
		

		public void SetUpdateStatusBar(bool available)
		{
			this.SetUpdateStatusBar(available, null);
		}
		public void SetUpdateStatusBar(bool available, string downloadUrl)
		{
			// do stuff
		}


		/// <summary>
		/// Loads and initializes saved user settings.
		/// </summary>
		public void LoadSettings()
		{
			// Load the saved user settings into our prefs object
			prefs = Utilities.LoadPrefs();

			Debug.WriteLine("Loading GameDirectory: " + prefs.GameDirectory);
			Log("Loading GameDirectory: " + prefs.GameDirectory);
		}


		/// <summary>
		/// If there is an internet connection, checks for updates and returns true if an update is available.
		/// </summary>
		/// <returns>True if an update is available, false if not.</returns>
		public static bool CheckForUpdates()
		{
			Trace.WriteLine("Checking for application updates...");

			if (Utilities.CheckForInternetConnection())
			{
				latestAppVersion = Utilities.GetLatestVersion();
				int curBuild = Properties.Settings.Default.Info_BuildNum;

				Debug.WriteLine("Current build, latest build:    {0}, {1}", curBuild, latestAppVersion.BuildNum);

				// Prompt for update if one is available
				if (curBuild < latestAppVersion.BuildNum)
				{
					Trace.WriteLine("Update is available.");
					return true;
				}
				else
				{
					Trace.WriteLine("No updates available!");
					return false;
				}
			}
			else
			{
				Trace.WriteLine("There is no internet connection!");
				return false;
			}
		}


		/// <summary>
		/// Starts the update flow (duh).
		/// </summary>
		public static void StartUpdateFlow()
{'fraction_non_alphanumeric': 0.08471720818291216, 'fraction_numerical': 0.0038507821901323704, 'mean_word_length': 3.9233807266982623, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '107654', 'n_tokens_mistral': 4519, 'n_tokens_neox': 3841, 'n_words': 1037}