// Decompiled with JetBrains decompiler
// Type: ik.PowerShell.PS2EXEHostUI
// Assembly: BitLocker_Encryption, Version=0.3.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 26A91D44-64C1-496F-B0CE-A0E68A4652B0
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Reflection;
using System.Security;
using System.Windows.Forms;
namespace ik.PowerShell
{
internal class PS2EXEHostUI : PSHostUserInterface
{
public ConsoleColor ErrorForegroundColor = ConsoleColor.Red;
public ConsoleColor WarningForegroundColor = ConsoleColor.Yellow;
public ConsoleColor DebugForegroundColor = ConsoleColor.Yellow;
public ConsoleColor VerboseForegroundColor = ConsoleColor.Yellow;
public ConsoleColor ProgressForegroundColor = ConsoleColor.DarkCyan;
public ConsoleColor ProgressBackgroundColor = ConsoleColor.DarkCyan;
private PS2EXEHostRawUI rawUI;
public ConsoleColor ErrorBackgroundColor;
public ConsoleColor WarningBackgroundColor;
public ConsoleColor DebugBackgroundColor;
public ConsoleColor VerboseBackgroundColor;
private string ibcaption;
private string ibmessage;
public ProgressForm pf;
public PS2EXEHostUI()
{
this.rawUI = new PS2EXEHostRawUI();
}
public override Dictionary<string, PSObject> Prompt(
string caption,
string message,
Collection<FieldDescription> descriptions)
{
if (!string.IsNullOrEmpty(caption) || !string.IsNullOrEmpty(message))
{
string caption1 = AppDomain.CurrentDomain.FriendlyName;
string text = "";
if (!string.IsNullOrEmpty(caption))
caption1 = caption;
if (!string.IsNullOrEmpty(message))
text = message;
int num = (int) MessageBox.Show(text, caption1);
}
this.ibcaption = "";
this.ibmessage = "";
Dictionary<string, PSObject> dictionary = new Dictionary<string, PSObject>();
foreach (FieldDescription description in descriptions)
{
System.Type conversionType = !string.IsNullOrEmpty(description.ParameterAssemblyFullName) ? System.Type.GetType(description.ParameterAssemblyFullName) : typeof (string);
if (conversionType.IsArray)
{
System.Type elementType = conversionType.GetElementType();
System.Type type = System.Type.GetType("System.Collections.Generic.List" + '`'.ToString() + "1").MakeGenericType(elementType);
object target = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, (Binder) null, System.Type.EmptyTypes, (ParameterModifier[]) null).Invoke((object[]) null);
int num = 0;
while (true)
{
try
{
if (!string.IsNullOrEmpty(description.Name))
this.ibmessage = string.Format("{0}[{1}]: ", (object) description.Name, (object) num);
string str = this.ReadLine();
if (!string.IsNullOrEmpty(str))
{
object obj = Convert.ChangeType((object) str, elementType);
type.InvokeMember("Add", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, (Binder) null, target, new object[1]
{
obj
});
}
else
break;
}
catch (Exception ex)
{
throw ex;
}
++num;
}
Array array = (Array) type.InvokeMember("ToArray", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, (Binder) null, target, (object[]) null);
dictionary.Add(description.Name, new PSObject((object) array));
}
else
{
object obj = (object) null;
try
{
if (conversionType != typeof (SecureString))
{
if (conversionType != typeof (PSCredential))
{
if (!string.IsNullOrEmpty(description.Name))
this.ibmessage = string.Format("{0}: ", (object) description.Name);
if (!string.IsNullOrEmpty(description.HelpMessage))
this.ibmessage += "\n(Type !? for help.)";
string str;
do
{
str = this.ReadLine();
if (str == "!?")
{
this.WriteLine(description.HelpMessage);
}
else
{
if (string.IsNullOrEmpty(str))
obj = (object) description.DefaultValue;
if (obj == null)
{
try
{
obj = Convert.ChangeType((object) str, conversionType);
}
catch
{
this.Write("Wrong format, please repeat input: ");
str = "!?";
}
}
}
}
while (str == "!?");
}
else
obj = (object) this.PromptForCredential("", "", "", "");
}
else
{
if (!string.IsNullOrEmpty(description.Name))
this.ibmessage = string.Format("{0}: ", (object) description.Name);
obj = (object) this.ReadLineAsSecureString();
}
dictionary.Add(description.Name, new PSObject(obj));
}
catch (Exception ex)
{
throw ex;
}
}
}
this.ibcaption = "";
this.ibmessage = "";
return dictionary;
}
public override int PromptForChoice(
string caption,
string message,
Collection<ChoiceDescription> choices,
int defaultChoice)
{
int num = ChoiceBox.Show(choices, defaultChoice, caption, message);
if (num == -1)
num = defaultChoice;
return num;
}
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName,
PSCredentialTypes allowedCredentialTypes,
PSCredentialUIOptions options)
{
CredentialForm.UserPwd userPwd = CredentialForm.PromptForPassword(caption, message, targetName, userName, allowedCredentialTypes, options);
if (userPwd == null)
return (PSCredential) null;
SecureString password = new SecureString();
foreach (char c in userPwd.Password.ToCharArray())
password.AppendChar(c);
return new PSCredential(userPwd.User, password);
}
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName)
{
CredentialForm.UserPwd userPwd = CredentialForm.PromptForPassword(caption, message, targetName, userName, PSCredentialTypes.Default, PSCredentialUIOptions.Default);
if (userPwd == null)
return (PSCredential) null;
SecureString password = new SecureString();
foreach (char c in userPwd.Password.ToCharArray())
password.AppendChar(c);
return new PSCredential(userPwd.User, password);
}
public override PSHostRawUserInterface RawUI
{
get
{
return (PSHostRawUserInterface) this.rawUI;
}
}
public override string ReadLine()
{
string sValue = "";
if (InputBox.Show(this.ibcaption, this.ibmessage, ref sValue) == DialogResult.OK)
return sValue;
return "";
}
private SecureString getPassword()
{
SecureString secureString = new SecureString();
while (true)
{
ConsoleKeyInfo consoleKeyInfo;
do
{
consoleKeyInfo = Console.ReadKey(true);
if (consoleKeyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
return secureString;
}
if (consoleKeyInfo.Key != ConsoleKey.Backspace)
goto label_6;
}
while (secureString.Length <= 0);
secureString.RemoveAt(secureString.Length - 1);
Console.Write("\b \b");
continue;
label_6:
secureString.AppendChar(consoleKeyInfo.KeyChar);
Console.Write("*");
}
}
public override SecureString ReadLineAsSecureString()
{
SecureString secureString = new SecureString();
string sValue = "";
if (InputBox.Show(this.ibcaption, this.ibmessage, ref sValue, true) == DialogResult.OK)
{
foreach (char c in sValue)
secureString.AppendChar(c);
}
return secureString;
}
public override void Write(
ConsoleColor foregroundColor,
ConsoleColor backgroundColor,
string value)
{
}
public override void Write(string value)
{
}
public override void WriteDebugLine(string message)
{
int num = (int) MessageBox.Show(message, AppDomain.CurrentDomain.FriendlyName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
public override void WriteErrorLine(string value)
{
int num = (int) MessageBox.Show(value, AppDomain.CurrentDomain.FriendlyName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
public override void WriteLine()
{
}
public override void WriteLine(
ConsoleColor foregroundColor,
ConsoleColor backgroundColor,
string value)
{
}
public override void WriteLine(string value)
{
}
public override void WriteProgress(long sourceId, ProgressRecord record)
{
if (this.pf == null)
{
this.pf = new ProgressForm(this.ProgressForegroundColor);
this.pf.Show();
}
this.pf.Update(record);
if (record.RecordType != ProgressRecordType.Completed)
return;
this.pf = (ProgressForm) null;
}
public override void WriteVerboseLine(string message)
{
}
public override void WriteWarningLine(string message)
{
int num = (int) MessageBox.Show(message, AppDomain.CurrentDomain.FriendlyName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}