using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;

namespace DecalUtil;

public class DecalUtilMain : Form, IFormStatus
{
	private enum KEY_INFORMATION_CLASS
	{
		KeyBasicInformation,
		KeyNodeInformation,
		KeyFullInformation,
		KeyNameInformation,
		KeyCachedInformation,
		KeyFlagsInformation,
		KeyVirtualizationInformation,
		MaxKeyInfoClass
	}

	private enum CONTROL_FLAGS
	{
		RegKeyClearFlags = 0,
		RegKeyDontVirtualize = 2,
		RegKeyDontSilentFail = 4,
		RegKeyRecurseFlag = 8
	}

	private enum KEY_SET_INFORMATION_CLASS
	{
		KeyWriteTimeInformation,
		KeyWow64FlagsInformation,
		KeyControlFlagsInformation,
		KeySetVirtualizationInformation,
		KeySetDebugInformation,
		MaxKeySetInfoClass
	}

	private struct KEY_CONTROL_FLAGS_INFORMATION
	{
		public uint ControlFlags;
	}

	private struct KEY_FLAGS_INFO
	{
		public uint _unk1;

		public uint _unk2;

		public uint _ControlFlags;
	}

	private const int KEY_ALL_ACCESS = 983103;

	private const int KEY_CREATE_LINK = 32;

	private const int KEY_CREATE_SUB_KEY = 4;

	private const int KEY_ENUMERATE_SUB_KEYS = 8;

	private const int KEY_EXECUTE = 131097;

	private const int KEY_NOTIFY = 16;

	private const int KEY_QUERY_VALUE = 1;

	private const int KEY_READ = 131097;

	private const int KEY_SET_VALUE = 2;

	private const int KEY_WRITE = 131078;

	private UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)2147483650u;

	private IContainer components;

	private Button doUpdate;

	private StatusStrip statusStrip1;

	private ToolStripStatusLabel status;

	private TextBox history;

	private Button diagnostics;

	private CheckBox AdapterDebug;

	private ToolStripProgressBar downloadProgress;

	private Button RegVirt;

	private CheckBox crashDumpsEnable;

	public int Max
	{
		get
		{
			return downloadProgress.Maximum;
		}
		set
		{
			downloadProgress.Maximum = value;
		}
	}

	public int Current
	{
		get
		{
			return downloadProgress.Value;
		}
		set
		{
			downloadProgress.Value = value;
		}
	}

	public bool ProgressEnabled
	{
		get
		{
			return ((ToolStripItem)downloadProgress).Visible;
		}
		set
		{
			((ToolStripItem)downloadProgress).Visible = value;
		}
	}

	[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
	private static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult);

	[DllImport("advapi32.dll", SetLastError = true)]
	public static extern int RegCloseKey(UIntPtr hKey);

	[DllImport("NTDLL.dll")]
	private static extern uint NtQueryKey(UIntPtr KeyHandle, KEY_INFORMATION_CLASS KeyInformationClass, ref KEY_FLAGS_INFO KeyInformation, uint Length, out IntPtr ResultLength);

	[DllImport("NTDLL.dll")]
	private static extern uint NtSetInformationKey(UIntPtr KeyHandle, KEY_SET_INFORMATION_CLASS InformationClass, ref KEY_CONTROL_FLAGS_INFORMATION KeyInformationData, uint DataLength);

	public DecalUtilMain()
	{
		InitializeComponent();
		((ToolStripItem)status).Text = "";
		RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Decal\\Decal.Adapter");
		using (registryKey)
		{
			bool flag = false;
			try
			{
				flag = (int)registryKey.GetValue("Tracing", 0) > 0;
			}
			catch
			{
			}
			AdapterDebug.Checked = flag;
		}
		crashDumpsEnable.Checked = CrashDumpsEnabled();
	}

	private void doUpdate_Click(object sender, EventArgs e)
	{
		((Control)doUpdate).Enabled = false;
		UriBuilder uriBuilder = new UriBuilder((string)Registry.LocalMachine.OpenSubKey("SOFTWARE\\Decal\\Agent").GetValue("DecalDirectory", "http://update.decaldev.com"));
		if (!uriBuilder.Path.EndsWith("/"))
		{
			uriBuilder.Path += "/";
		}
		uriBuilder.Path += "updatelist.xml";
		ReportStatus($"Start Download at: {DateTime.Now:hh:mm:ss}");
		Update update = new Update(uriBuilder.ToString());
		if (!update.FetchUpdateList(this))
		{
			ReportStatus($"Failed to download updatelist.xml");
		}
		else
		{
			update.DownloadFiles(this);
		}
		((Control)doUpdate).Enabled = true;
	}

	private void status_Click(object sender, EventArgs e)
	{
	}

	public void ReportStatus(string statusData)
	{
		((ToolStripItem)status).Text = statusData;
		TextBox obj = history;
		((Control)obj).Text = ((Control)obj).Text + statusData + "\r\n";
		Application.DoEvents();
	}

	private void diagnostics_Click(object sender, EventArgs e)
	{
	}

	private void AdapterDebug_CheckedChanged(object sender, EventArgs e)
	{
		RegistryKey registryKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Decal\\Decal.Adapter");
		using (registryKey)
		{
			bool flag = false;
			try
			{
				flag = (int)registryKey.GetValue("Tracing", 0) > 0;
			}
			catch
			{
			}
			if (flag != AdapterDebug.Checked)
			{
				if (AdapterDebug.Checked)
				{
					registryKey.SetValue("Tracing", 1, RegistryValueKind.DWord);
				}
				else
				{
					registryKey.SetValue("Tracing", 0, RegistryValueKind.DWord);
				}
			}
		}
	}

	private void DecalUtilMain_Load(object sender, EventArgs e)
	{
	}

	private void RegVirt_Click(object sender, EventArgs e)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		if (Environment.OSVersion.Version.Major < 6)
		{
			MessageBox.Show("This only works on Vista and above.");
		}
		else if (Environment.OSVersion.Version.Major >= 6)
		{
			KEY_FLAGS_INFO KeyInformation = new KEY_FLAGS_INFO
			{
				_unk1 = 0u,
				_unk2 = 0u,
				_ControlFlags = 0u
			};
			UIntPtr hkResult = UIntPtr.Zero;
			int num = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Decal\\Plugins", 0, 983103, out hkResult);
			IntPtr ResultLength = IntPtr.Zero;
			uint num2 = NtQueryKey(hkResult, KEY_INFORMATION_CLASS.KeyFlagsInformation, ref KeyInformation, 12u, out ResultLength);
			RegCloseKey(hkResult);
			ReportStatus($"OpenKeyEx Return is {num}, NTQueryKey Return is {num2}, flags mask for HKLM\\Software\\Decal\\Plugins is {KeyInformation._ControlFlags}");
		}
	}

	private bool CrashDumpsEnabled()
	{
		bool result = false;
		try
		{
			RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps\\DenAgent.exe");
			if (registryKey != null)
			{
				registryKey.Close();
				result = true;
			}
		}
		catch
		{
		}
		return result;
	}

	private void EnableCrashDumps(string appName)
	{
		string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
		folderPath = Path.Combine(folderPath, "Decal\\Dumps");
		Directory.CreateDirectory(folderPath);
		using RegistryKey registryKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps\\" + appName);
		registryKey.SetValue("DumpFolder", folderPath, RegistryValueKind.String);
		registryKey.SetValue("DumpCount", 5, RegistryValueKind.DWord);
		registryKey.SetValue("DumpType", 2, RegistryValueKind.DWord);
	}

	private void DisableCrashDumps(string appName)
	{
		Registry.LocalMachine.DeleteSubKey("SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps\\" + appName);
	}

	private void crashDumpsEnable_CheckedChanged(object sender, EventArgs e)
	{
		if (crashDumpsEnable.Checked)
		{
			EnableCrashDumps("DenAgent.exe");
			EnableCrashDumps("acclient.exe");
			EnableCrashDumps("aclauncher.exe");
		}
		else
		{
			DisableCrashDumps("DenAgent.exe");
			DisableCrashDumps("acclient.exe");
			DisableCrashDumps("aclauncher.exe");
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Expected O, but got Unknown
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Expected O, but got Unknown
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Expected O, but got Unknown
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Expected O, but got Unknown
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Expected O, but got Unknown
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Expected O, but got Unknown
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Expected O, but got Unknown
		//IL_0545: Unknown result type (might be due to invalid IL or missing references)
		//IL_054f: Expected O, but got Unknown
		ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(DecalUtilMain));
		doUpdate = new Button();
		statusStrip1 = new StatusStrip();
		status = new ToolStripStatusLabel();
		downloadProgress = new ToolStripProgressBar();
		history = new TextBox();
		diagnostics = new Button();
		AdapterDebug = new CheckBox();
		RegVirt = new Button();
		crashDumpsEnable = new CheckBox();
		((Control)statusStrip1).SuspendLayout();
		((Control)this).SuspendLayout();
		((Control)doUpdate).Location = new Point(269, 12);
		((Control)doUpdate).Name = "doUpdate";
		((Control)doUpdate).Size = new Size(92, 41);
		((Control)doUpdate).TabIndex = 0;
		((Control)doUpdate).Text = "Auto-Update (XML etc)";
		((ButtonBase)doUpdate).UseVisualStyleBackColor = true;
		((Control)doUpdate).Click += doUpdate_Click;
		((ToolStrip)statusStrip1).Items.AddRange((ToolStripItem[])(object)new ToolStripItem[2]
		{
			(ToolStripItem)status,
			(ToolStripItem)downloadProgress
		});
		((Control)statusStrip1).Location = new Point(0, 244);
		((Control)statusStrip1).Name = "statusStrip1";
		((ToolStrip)statusStrip1).RenderMode = (ToolStripRenderMode)2;
		((Control)statusStrip1).Size = new Size(562, 22);
		statusStrip1.SizingGrip = false;
		((Control)statusStrip1).TabIndex = 1;
		((ToolStripItem)status).Name = "status";
		((ToolStripItem)status).Size = new Size(547, 17);
		status.Spring = true;
		((ToolStripItem)status).Text = "status";
		((ToolStripItem)status).Click += status_Click;
		((ToolStripItem)downloadProgress).Name = "downloadProgress";
		((ToolStripItem)downloadProgress).Size = new Size(100, 16);
		((ToolStripItem)downloadProgress).Visible = false;
		history.AcceptsReturn = true;
		((Control)history).Location = new Point(12, 110);
		((TextBoxBase)history).Multiline = true;
		((Control)history).Name = "history";
		((TextBoxBase)history).ReadOnly = true;
		history.ScrollBars = (ScrollBars)2;
		((Control)history).Size = new Size(446, 122);
		((Control)history).TabIndex = 2;
		((Control)diagnostics).Enabled = false;
		((Control)diagnostics).Location = new Point(367, 12);
		((Control)diagnostics).Name = "diagnostics";
		((Control)diagnostics).Size = new Size(91, 41);
		((Control)diagnostics).TabIndex = 3;
		((Control)diagnostics).Text = "Decal.Adapter Diagnostics";
		((ButtonBase)diagnostics).UseVisualStyleBackColor = true;
		((Control)diagnostics).Click += diagnostics_Click;
		((Control)AdapterDebug).AutoSize = true;
		((Control)AdapterDebug).Location = new Point(12, 24);
		((Control)AdapterDebug).Name = "AdapterDebug";
		((Control)AdapterDebug).Size = new Size(198, 17);
		((Control)AdapterDebug).TabIndex = 4;
		((Control)AdapterDebug).Text = "Enable Decal.Adapter Debug output";
		((ButtonBase)AdapterDebug).UseVisualStyleBackColor = true;
		AdapterDebug.CheckedChanged += AdapterDebug_CheckedChanged;
		((Control)RegVirt).Location = new Point(464, 12);
		((Control)RegVirt).Name = "RegVirt";
		((Control)RegVirt).Size = new Size(102, 41);
		((Control)RegVirt).TabIndex = 5;
		((Control)RegVirt).Text = "Registry Virt. Settings";
		((ButtonBase)RegVirt).UseVisualStyleBackColor = true;
		((Control)RegVirt).Click += RegVirt_Click;
		((Control)crashDumpsEnable).AutoSize = true;
		((Control)crashDumpsEnable).Location = new Point(12, 47);
		((Control)crashDumpsEnable).Name = "crashDumpsEnable";
		((Control)crashDumpsEnable).Size = new Size(125, 17);
		((Control)crashDumpsEnable).TabIndex = 6;
		((Control)crashDumpsEnable).Text = "Enable Crash Dumps";
		((ButtonBase)crashDumpsEnable).UseVisualStyleBackColor = true;
		crashDumpsEnable.CheckedChanged += crashDumpsEnable_CheckedChanged;
		((ContainerControl)this).AutoScaleDimensions = new SizeF(6f, 13f);
		((ContainerControl)this).AutoScaleMode = (AutoScaleMode)1;
		((Form)this).ClientSize = new Size(562, 266);
		((Control)this).Controls.Add((Control)(object)crashDumpsEnable);
		((Control)this).Controls.Add((Control)(object)RegVirt);
		((Control)this).Controls.Add((Control)(object)AdapterDebug);
		((Control)this).Controls.Add((Control)(object)diagnostics);
		((Control)this).Controls.Add((Control)(object)history);
		((Control)this).Controls.Add((Control)(object)statusStrip1);
		((Control)this).Controls.Add((Control)(object)doUpdate);
		((Form)this).Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
		((Form)this).MaximizeBox = false;
		((Control)this).MaximumSize = new Size(578, 304);
		((Control)this).MinimumSize = new Size(478, 304);
		((Control)this).Name = "DecalUtilMain";
		((Form)this).SizeGripStyle = (SizeGripStyle)2;
		((Control)this).Text = "Decal 3.0 Utilities";
		((Form)this).Load += DecalUtilMain_Load;
		((Control)statusStrip1).ResumeLayout(false);
		((Control)statusStrip1).PerformLayout();
		((Control)this).ResumeLayout(false);
		((Control)this).PerformLayout();
	}
}
