<commit_before>﻿/*
* Copyright 2015 <NAME>
* This file is a part of the RedirectLauncher by <NAME>.
* All code contained in here is licensed under the MIT license.
* Please fill issue report on https://github.com/ripxfrostbite/RedirectLauncher
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace RedirectLauncherMk2_WPF
{
	class ModUpdater
	{
		private Game client;
		private ProgressBar progressBar;
		private TextBlock clientVersionBlock;
		public bool isUpdateInProgress = false;
		public bool hasUserSkippedUpdate = false;
		private TextBlock statusBlock;
		private TextBlock statusPercentBlock;
		private string serverModpack;


		public ModUpdater(Game client)
		{
			this.client = client;
		}

		public bool startModUpdate(ProgressBar progressBar, TextBlock clientVersionBlock, TextBlock statusBlock, TextBlock statusPercentBlock)
		{
			this.statusBlock = statusBlock;
			this.statusPercentBlock = statusPercentBlock;
			this.progressBar = progressBar;
			this.clientVersionBlock = clientVersionBlock;
			isUpdateInProgress = true;
			serverModpack = client.selectedServer.name.Replace(" ", "_");
			if (client.remoteClientModVersion > 0 && ((client.clientModVersion < client.remoteClientModVersion || !doesModpackFileExist(client.clientModVersion)) && !client.offlineMode))
			{
				if (MessageBox.Show("It appears your client's mod package file is outdated or missing.\nWould you like to download the latest?", "Update", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
				{
					statusBlock.Text = "Updating modpack to version " + client.remoteClientModVersion;
					if (doesModpackFileExist(client.clientModVersion))
						File.Delete(client.clientDirectory + "\\modpacks\\zzz" + serverModpack + "-" + client.clientModVersion.ToString() + ".pack");
					downloadFileFromWeb("package/modpack-" + client.remoteClientModVersion, client.clientDirectory + "\\modpacks\\zzz" + serverModpack + "-" + client.remoteClientModVersion + ".pack", client.launcherModRepo);
					return false;
				}
				else
				{
					isUpdateInProgress = false;
					hasUserSkippedUpdate = true;
					MessageBox.Show("You may experience issues without the modpack, some content may be missing or some features may not function.\nYou may now launch the client.");
					return true;
				}
			}
			else
			{
				isUpdateInProgress = false;
			}
			return true;
		}

		public bool doesModpackFileExist(int modVersion)
		{
			return File.Exists(client.clientDirectory + "\\modpacks\\zzz" + serverModpack + "-" + modVersion.ToString() + ".pack"); ;
		}

		private void downloadFileFromWeb(String pathToFile, String pathToSave, String host)
		{
			WebClient w = new WebClient();
			w.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadComplete);
			w.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progressUpdate);
			w.DownloadFileAsync(new Uri(host + pathToFile), pathToSave);
		}
		//Download async functions
		private void progressUpdate(object sender, DownloadProgressChangedEventArgs e)
		{
			progressBar.Value = e.ProgressPercentage;
			statusPercentBlock.Text = "(" + e.ProgressPercentage.ToString() + "%)";
		}
		private void downloadComplete(object sender, AsyncCompletedEventArgs e)
		{
			client.writeModVersionData(client.tryGetModpackVersion(client.packDirectory.FullName, serverModpack), clientVersionBlock);
			isUpdateInProgress = false;
			MessageBox.Show("Download of the latest mod package is done!\nYou may now launch the client.");
			statusBlock.Text = "Ready to launch!";
			statusPercentBlock.Text = "";
		}<commit_msg>Fixed the modpack exists check<commit_after>﻿/*
* Copyright 2015 <NAME>
* This file is a part of the RedirectLauncher by <NAME>.
* All code contained in here is licensed under the MIT license.
* Please fill issue report on https://github.com/ripxfrostbite/RedirectLauncher
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace RedirectLauncherMk2_WPF
{
	class ModUpdater
	{
		private Game client;
		private ProgressBar progressBar;
		private TextBlock clientVersionBlock;
		public bool isUpdateInProgress = false;
		public bool hasUserSkippedUpdate = false;
		private TextBlock statusBlock;
		private TextBlock statusPercentBlock;
		private string serverModpack;


		public ModUpdater(Game client)
		{
			this.client = client;
		}

		public bool startModUpdate(ProgressBar progressBar, TextBlock clientVersionBlock, TextBlock statusBlock, TextBlock statusPercentBlock)
		{
			this.statusBlock = statusBlock;
			this.statusPercentBlock = statusPercentBlock;
			this.progressBar = progressBar;
			this.clientVersionBlock = clientVersionBlock;
			isUpdateInProgress = true;
			serverModpack = client.selectedServer.name.Replace(" ", "_");
			if (client.remoteClientModVersion > 0 && ((client.clientModVersion < client.remoteClientModVersion || !doesModpackFileExist(client.clientModVersion)) && !client.offlineMode))
			{
				if (MessageBox.Show("It appears your client's mod package file is outdated or missing.\nWould you like to download the latest?", "Update", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
				{
					statusBlock.Text = "Updating modpack to version " + client.remoteClientModVersion;
					if (doesModpackFileExist(client.clientModVersion))
						File.Delete(client.clientDirectory + "\\modpacks\\zzz" + serverModpack + "-" + client.clientModVersion.ToString() + ".pack");
					downloadFileFromWeb("package/modpack-" + client.remoteClientModVersion, client.clientDirectory + "\\modpacks\\zzz" + serverModpack + "-" + client.remoteClientModVersion + ".pack", client.launcherModRepo);
					return false;
				}
				else
				{
					isUpdateInProgress = false;
					hasUserSkippedUpdate = true;
					MessageBox.Show("You may experience issues without the modpack, some content may be missing or some features may not function.\nYou may now launch the client.");
					return true;
				}
			}
			else
			{
				isUpdateInProgress = false;
			}
			return true;
		}

		public bool doesModpackFileExist(int modVersion)
		{
			if(modVersion > 0)
				return File.Exists(client.clientDirectory + "\\modpacks\\zzz" + serverModpack + "-" + modVersion.ToString() + ".pack");
			else
				return true;
		}

		private void downloadFileFromWeb(String pathToFile, String pathToSave, String host)
		{
			WebClient w = new WebClient();
			w.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadComplete);
			w.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progressUpdate);
			w.DownloadFileAsync(new Uri(host + pathToFile), pathToSave);
		}
		//Download async functions
		private void progressUpdate(object sender, DownloadProgressChangedEventArgs e)
		{
			progressBar.Value = e.ProgressPercentage;
			statusPercentBlock.Text = "(" + e.ProgressPercentage.ToString() + "%)";
		}
		private void downloadComplete(object sender, AsyncCompletedEventArgs e)
		{
			client.writeModVersionData(client.tryGetModpackVersion(client.packDirectory.FullName, serverModpack), clientVersionBlock);
			isUpdateInProgress = false;
			MessageBox.Show("Download of the latest mod package is done!\nYou may now launch the client.");
			statusBlock.Text = "Ready to launch!";
			statusPercentBlock.Text = "";
		}
{'fraction_non_alphanumeric': 0.09559110417479516, 'fraction_numerical': 0.0016907270126154247, 'mean_word_length': 4.675276752767528, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5705407', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2134, 'n_words': 551}