Skip to content

Instantly share code, notes, and snippets.

@Toyz
Created August 15, 2015 18:12
Show Gist options
  • Save Toyz/ade538f1ab9381e20547 to your computer and use it in GitHub Desktop.
Save Toyz/ade538f1ab9381e20547 to your computer and use it in GitHub Desktop.
using HelperLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using W10_Logon_BG_Changer___Command_Line.Tools;
using Color = System.Drawing.Color;
using Path = System.IO.Path;
using Rectangle = System.Drawing.Rectangle;
namespace W10_Logon_BG_Changer___Command_Line
{
internal class Program
{
private readonly static string NewPriLocation = Path.Combine(Path.GetTempPath(), "new_temp_pri.pri");
private readonly static string TempPriFile = Path.Combine(Path.GetTempPath(), "bak_temp_pri.pri");
private readonly static Dictionary<string, string> Commands = new Dictionary<string, string>()
{
{
"authors",
"Prints a list of the Authors of this Program."
},
{
"color",
"Sets the Logon Background to a color of your choosing." +
"\nYou will have to provide the color in Hex format." +
"\nTry this link if you have no clue what this means:" +
"\n http://www.yellowpipe.com/yis/tools/hex-to-rgb/" +
"\n color-converter.php"
},
{
"image",
"This will exchange the picture of your Logon Background." +
"\nThis is achieved by changing a system file and replacing" +
"\nthe bytes of the Hero image with your image." +
"\nYou will have to provide a picture by putting in the full" +
"\npath (meaning to start with Drive letter)." +
"\nOr you can just drag and drop the image to this window."
},
{
"restore",
"This will overwrite the new System file (if you have" +
"\nexchanged the picture once) with the previous version of" +
"\nthe file (which does not have to be the orginal file, for" +
"\nexample. when you ran the image command twice)."
},
{
"help",
"Prints this help text."
},
{
"exit",
"This will exit this Program (closing the Command Prompt)."
}
};
private readonly static string CommandHelpFormat = "{0,-12} | {1}";
private enum StartOrQuitType { Exit, NoClear, Clear, ClearAndHelp };
private static StartOrQuitType StartOrQuit()
{
Console.WriteLine("Type in your desired Action:");
string input = Console.ReadLine();
Console.WriteLine();
if(!Commands.ContainsKey(input))
{
Console.WriteLine("Command '" + input + "' unknown, printing Help...");
Console.WriteLine();
HelpCommand();
return StartOrQuitType.NoClear;
}
else
{
switch(input)
{
case "help":
return StartOrQuitType.ClearAndHelp;
case "authors":
AuthorsCommand();
break;
case "color":
Console.WriteLine("Type in the Color in a hex fromat:");
var hex = Console.ReadLine();
var isHex = TestIfHex(hex);
if(isHex == true)
{
var converter = new System.Drawing.ColorConverter();
Color color = (Color)converter.ConvertFromString(hex);
ChangeColor(color);
}
else
{
Console.WriteLine("");
Console.WriteLine(@"""{0}"" is not a valid hex color code!", hex);
}
break;
case "image":
Console.WriteLine("Put in the file path of the image to use:");
var filedir = Console.ReadLine();
if(File.Exists(filedir) && (Path.GetExtension(filedir) == ".jpg") ||
(Path.GetExtension(filedir) == ".png") || (Path.GetExtension(filedir) == ".bmp") ||
(Path.GetExtension(filedir) == ".tif") || (Path.GetExtension(filedir) == ".tiff"))
{
ChangeImage(filedir);
}
else
{
Console.WriteLine("");
Console.WriteLine(@"File ""{0}"" does not exist, or the file is not an image file!", filedir);
}
break;
case "restore":
RestoreCommand();
break;
case "exit":
return StartOrQuitType.Exit;
default:
Console.WriteLine("Command '" + input + "' unknown, printing Help...");
Console.WriteLine();
HelpCommand();
return StartOrQuitType.NoClear;
}
}
string again = "";
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Type in 'again' for another round, anything else will quit this program");
again = Console.ReadLine();
return again == "again" ? StartOrQuitType.Clear : StartOrQuitType.Exit;
}
public static bool TestIfHex(string hex)
{
return System.Text.RegularExpressions.Regex.IsMatch(hex, @"\A\b[0-9a-fA-F]+\b\Z");
//return Int32.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}
private static void Main(string[] args)
{
if(args == null || args.Length == 0)
{
HelpCommand();
var res = StartOrQuitType.ClearAndHelp;
while((res = StartOrQuit()) != StartOrQuitType.Exit)
{
switch(res)
{
case StartOrQuitType.Clear:
Console.Clear();
break;
case StartOrQuitType.ClearAndHelp:
Console.Clear();
HelpCommand();
break;
case StartOrQuitType.NoClear:
break;
case StartOrQuitType.Exit:
break;
default:
break;
}
}
}
else
{
switch(args[0])
{
case "help":
HelpCommand();
break;
case "authors":
AuthorsCommand();
break;
case "color":
ColorCommand(args[1]);
break;
case "image":
ImageCommand(args[1]);
break;
case "restore":
RestoreCommand();
break;
default:
HelpCommand();
break;
}
}
}
private static void HelpCommand(string command = null)
{
if(command == null)
{
foreach(string parameter in Commands.Keys)
{
HelpCommand(parameter);
}
}
else if(Commands.ContainsKey(command))
{
string[] lines = Commands[command].Split('\n');
bool firstLine = true;
foreach(string line in lines)
{
if(firstLine)
{
Console.WriteLine(String.Format(CommandHelpFormat, command, line));
firstLine = false;
}
else
{
Console.WriteLine(String.Format(CommandHelpFormat, "", " " + line));
}
}
}
else
{
Console.WriteLine(command + " is not a valid Command.");
}
Console.WriteLine();
}
private static void AuthorsCommand()
{
Console.WriteLine("");
Console.WriteLine(@"Syrexide | https://github.com/Syrexide/");
Console.WriteLine(@"Toyz | https://github.com/Toyz/");
}
private static void ColorCommand(string hex)
{
var isHex = TestIfHex(hex);
if(isHex == true)
{
var converter = new System.Drawing.ColorConverter();
Color color = (Color)converter.ConvertFromString(hex);
ChangeColor(color);
}
else
{
Console.WriteLine("");
Console.WriteLine(@"""{0}"" is not a valid hex color code!", hex);
}
}
private static void ImageCommand(string filedir)
{
if(File.Exists(filedir) && (Path.GetExtension(filedir) == ".jpg") ||
(Path.GetExtension(filedir) == ".png") || (Path.GetExtension(filedir) == ".bmp") ||
(Path.GetExtension(filedir) == ".tif") || (Path.GetExtension(filedir) == ".tiff"))
{
ChangeImage(filedir);
}
else
{
Console.WriteLine("");
Console.WriteLine(@"File ""{0}"" does not exist, or the file is not an image file!", filedir);
}
}
private static void RestoreCommand()
{
HelperLib.TakeOwnership(Config.PriFileLocation);
File.Copy(Config.BakPriFileLocation, Config.PriFileLocation, true);
}
private static void ChangeImage(string filedir)
{
HelperLib.TakeOwnership(Config.LogonFolder);
HelperLib.TakeOwnership(Config.PriFileLocation);
if(!File.Exists(Config.BakPriFileLocation))
{
File.Copy(Config.PriFileLocation, Config.BakPriFileLocation);
}
HelperLib.TakeOwnership(Config.BakPriFileLocation);
File.Copy(Config.BakPriFileLocation, TempPriFile, true);
if(File.Exists(NewPriLocation))
{
File.Delete(NewPriLocation);
}
File.Copy(Config.BakPriFileLocation, TempPriFile, true);
PriBuilder.CreatePri(TempPriFile, NewPriLocation, filedir);
File.Copy(NewPriLocation, Config.PriFileLocation, true);
}
private static string ChangeColor(Color color)
{
var image = Path.GetTempFileName();
DrawFilledRectangle(3840, 2160, new SolidBrush(color)).Save(image, ImageFormat.Jpeg);
return image;
}
private static Bitmap DrawFilledRectangle(int x, int y, System.Drawing.Brush b)
{
var bmp = new Bitmap(x, y);
using(var graph = Graphics.FromImage(bmp))
{
var imageSize = new Rectangle(0, 0, x, y);
graph.FillRectangle(b, imageSize);
}
return bmp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment