Skip to content

Commit

Permalink
Made config more robust.
Browse files Browse the repository at this point in the history
0.0.0.4
  • Loading branch information
krisdb2009 committed Jun 28, 2019
1 parent 78bb4e9 commit c9aa4db
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 58 deletions.
84 changes: 39 additions & 45 deletions SuperGrate/Classes/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ namespace SuperGrate
{
class Config
{
public static string MigrationStorePath = null;
public static string ScanStateParameters = null;
public static string LoadStateParameters = null;

public static Dictionary<string, string> Data = new Dictionary<string, string>() { //Make config more automagic
"MigrationStorePath",
"ScanStateParameters",
"LoadStateParameters"
public static Dictionary<string, string> Settings = new Dictionary<string, string>() {
{"XComment1", @"The UNC or Direct path to the USMT Migration Store E.g: \\ba-share\s$ or .\STORE."},
{"MigrationStorePath", @".\STORE"},
{"XComment2", "ScanState.exe & LoadState.exe CLI Parameters: https://docs.microsoft.com/en-us/windows/deployment/usmt/usmt-command-line-syntax "},
{"ScanStateParameters", "/config:Config_SettingsOnly.xml /i:MigUser.xml /r:3 /o"},
{"LoadStateParameters", "/config:Config_SettingsOnly.xml /i:MigUser.xml /r:3"}
};

public static void GenerateConfig()
{
Logger.Warning("Generating new SuperGrate.xml config.");
new XDocument(new XElement("SuperGrate",
new XComment(@"The UNC or Direct path to the USMT Migration Store E.g: \\ba-share\s$ or .\STORE."),
new XElement("MigrationStorePath", @".\STORE"),
new XComment("ScanState.exe & LoadState.exe CLI Parameters: https://docs.microsoft.com/en-us/windows/deployment/usmt/usmt-command-line-syntax "),
new XElement("ScanStateParameters", "/config:Config_SettingsOnly.xml /i:MigUser.xml /r:3 /o"),
new XElement("LoadStateParameters", "/config:Config_SettingsOnly.xml /i:MigUser.xml /r:3")
)).Save(@".\SuperGrate.xml");
XElement root = new XElement("SuperGrate");
foreach(KeyValuePair<string, string> setting in Settings)
{
if(setting.Key.StartsWith("XComment"))
{
root.Add(new XComment(setting.Value));
}
else
{
root.Add(new XElement(setting.Key, setting.Value));
}
}
new XDocument(root).Save(@".\SuperGrate.xml");
}
public static void LoadConfig()
{
Expand All @@ -38,42 +41,33 @@ public static void LoadConfig()
{
XDocument config = XDocument.Load(@".\SuperGrate.xml");
XElement root = config.Element("SuperGrate");


new Dictionary<string, string> schema = new Dictionary<string, string>([
"",
""
]);


XElement XMigrationStorePath = root.Element("MigrationStorePath");
XElement XScanStateParameters = root.Element("ScanStateParameters");
XElement XLoadStateParameters = root.Element("LoadStateParameters");
if (XMigrationStorePath == null)
{

}
else
{
MigrationStorePath = XMigrationStorePath.Value;
}
if (XScanStateParameters == null)
{

}
else
bool success = true;
Dictionary<string, string> xmlSettings = new Dictionary<string, string>();
foreach(KeyValuePair<string, string> setting in Settings)
{
ScanStateParameters = XScanStateParameters.Value;
if (!setting.Key.StartsWith("XComment"))
{
XElement element = root.Element(setting.Key);
if (element == null)
{
success = false;
Logger.Warning("SuperGrate.xml is missing: " + setting.Key + "!");
}
else
{
xmlSettings[setting.Key] = element.Value;
}
}
}
if (XLoadStateParameters == null)
Settings = xmlSettings;
if(success)
{

Logger.Success("Config loaded!");
}
else
{
LoadStateParameters = XLoadStateParameters.Value;
Logger.Warning("Config loaded, but is using default values for the missing elements.");
}
Logger.Success("Config loaded!");
}
catch(Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion SuperGrate/Classes/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static Task DeleteFromStore(string SID)
Logger.Information("Deleting '" + name + "' from the Store...");
try
{
Directory.Delete(Path.Combine(Config.MigrationStorePath, SID), true);
Directory.Delete(Path.Combine(Config.Settings["MigrationStorePath"], SID), true);
Logger.Information("'" + name + "' successfully deleted from the Store.");
}
catch(Exception e)
Expand Down
16 changes: 8 additions & 8 deletions SuperGrate/Classes/USMT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public static Task<bool> Do(USMTMode Mode, string[] SIDs)
if(Mode == USMTMode.LoadState)
{
exec = "loadstate.exe";
configParams = Config.LoadStateParameters;
configParams = Config.Settings["LoadStateParameters"];
CurrentTarget = Main.DestinationComputer;
}
if(Mode == USMTMode.ScanState)
{
exec = "scanstate.exe";
configParams = Config.ScanStateParameters;
configParams = Config.Settings["ScanStateParameters"];
CurrentTarget = Main.SourceComputer;
}
return Task.Run(async () => {
Expand Down Expand Up @@ -171,7 +171,7 @@ private static Task<bool> UploadToStore(string SID)
{
return Task.Run(() => {
Logger.Information("Uploading user state to the Store...");
string Destination = Path.Combine(Config.MigrationStorePath, SID);
string Destination = Path.Combine(Config.Settings["MigrationStorePath"], SID);
try
{
Directory.CreateDirectory(Destination);
Expand All @@ -194,11 +194,11 @@ private static Task<bool> DownloadFromStore(string SID)
return Task.Run(() => {
Logger.Information("Downloading user state to: " + Main.DestinationComputer + "...");
string Destination = Path.Combine(@"\\", Main.DestinationComputer, @"C$\SuperGrate\USMT\");
try
{
Directory.CreateDirectory(Destination);
Copy.CopyFile(
Path.Combine(Config.MigrationStorePath, SID, "USMT.MIG"),
try
{
Directory.CreateDirectory(Destination);
Copy.CopyFile(
Path.Combine(Config.Settings["MigrationStorePath"], SID, "USMT.MIG"),
Path.Combine(Destination, "USMT.MIG")
);
Logger.Success("User state successfully transferred.");
Expand Down
2 changes: 1 addition & 1 deletion SuperGrate/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private async void BtnListStore_Click(object sender, EventArgs e)
Running = true;
lbxUsers.Items.Clear();
lblUserList.Text = "Users in Migration Store:";
Dictionary<string, string> results = await Misc.GetUsersFromStore(Config.MigrationStorePath);
Dictionary<string, string> results = await Misc.GetUsersFromStore(Config.Settings["MigrationStorePath"]);
if(results != null)
{
lbxUsers.Tag = results.Keys.ToArray();
Expand Down
5 changes: 2 additions & 3 deletions SuperGrate/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down Expand Up @@ -32,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.0.3")]
[assembly: AssemblyFileVersion("0.0.0.3")]
[assembly: AssemblyVersion("0.0.0.4")]
[assembly: AssemblyFileVersion("0.0.0.4")]

0 comments on commit c9aa4db

Please sign in to comment.