Script for automating a large assortment of AME related actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.5 KiB

9 months ago
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using Ameliorated.ConsoleUtils;
  9. using System.Drawing;
  10. using System.Security.AccessControl;
  11. using System.Security.Principal;
  12. namespace amecs.Actions
  13. {
  14. public class Profile
  15. {
  16. public static bool ChangeImage()
  17. {
  18. ConsoleTUI.OpenFrame.WriteCenteredLine("Select an image");
  19. Thread.Sleep(1000);
  20. var dialog = new System.Windows.Forms.OpenFileDialog();
  21. dialog.Filter = "Image Files (*.jpg; *.jpeg; *.png; *.bmp; *.jfif)| *.jpg; *.jpeg; *.png; *.bmp; *.jfif"; // Filter files by extension
  22. dialog.Multiselect = false;
  23. dialog.InitialDirectory = Globals.UserFolder;
  24. NativeWindow window = new NativeWindow();
  25. window.AssignHandle(Process.GetCurrentProcess().MainWindowHandle);
  26. if (dialog.ShowDialog(window) == DialogResult.OK)
  27. {
  28. string file;
  29. try
  30. {
  31. file = dialog.FileName;
  32. }
  33. catch (SecurityException e)
  34. {
  35. Console.WriteLine();
  36. ConsoleTUI.OpenFrame.Close("Security error: " + e.Message, ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  37. return false;
  38. }
  39. ConsoleTUI.OpenFrame.WriteCentered("\r\nSetting profile image");
  40. using (new ConsoleUtils.LoadingIndicator(true))
  41. {
  42. var pfpDir = Path.Combine(Environment.ExpandEnvironmentVariables("%PUBLIC%\\AccountPictures"), Globals.UserSID);
  43. if (Directory.Exists(pfpDir))
  44. {
  45. try
  46. {
  47. Directory.Delete(pfpDir, true);
  48. } catch (Exception e)
  49. {
  50. var logdi = new DirectoryInfo(pfpDir) { Attributes = FileAttributes.Normal };
  51. try
  52. {
  53. NSudo.GetOwnershipPrivilege();
  54. var logdirsec = logdi.GetAccessControl();
  55. logdirsec.SetOwner(WindowsIdentity.GetCurrent().User);
  56. logdi.SetAccessControl(logdirsec);
  57. logdirsec = new DirectorySecurity();
  58. logdirsec.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().User, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
  59. logdi.SetAccessControl(logdirsec);
  60. } catch (Exception exception)
  61. {
  62. }
  63. foreach (var info in logdi.GetFileSystemInfos("*", SearchOption.AllDirectories))
  64. {
  65. info.Attributes = FileAttributes.Normal;
  66. }
  67. Directory.Delete(pfpDir, true);
  68. }
  69. }
  70. Directory.CreateDirectory(pfpDir);
  71. var image = Image.FromFile(file);
  72. var pfpKey = @"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\AccountPicture\Users\" + Globals.UserSID;
  73. new Reg.Key() { KeyName = pfpKey, Operation = RegistryOperation.Delete }.Apply();
  74. foreach (var res in new [] { 32, 40, 48, 64, 96, 192, 208, 240, 424, 448, 1080 })
  75. {
  76. var bitmap = new Bitmap(res, res);
  77. var graph = Graphics.FromImage(bitmap);
  78. graph.DrawImage(image, 0, 0, res, res);
  79. var saveLoc = Path.Combine(pfpDir, $"{res}x{res}.png");
  80. bitmap.Save(saveLoc);
  81. new Reg.Value() { KeyName = pfpKey, ValueName = "Image" + res, Type = Reg.RegistryValueType.REG_SZ, Data = saveLoc }.Apply();
  82. }
  83. new Reg.Value() { KeyName = pfpKey, ValueName = "UserPicturePath", Type = Reg.RegistryValueType.REG_SZ, Data = Path.Combine(pfpDir, $"448x448.png") }.Apply();
  84. try
  85. {
  86. Process proc = new Process();
  87. proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  88. proc.StartInfo.FileName = "gpupdate.exe";
  89. proc.StartInfo.Arguments = "/force";
  90. proc.Start();
  91. proc.WaitForExit(20000);
  92. } catch { }
  93. }
  94. Console.WriteLine();
  95. ConsoleTUI.OpenFrame.Close("Profile image changed successfully", ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  96. return true;
  97. }
  98. else
  99. {
  100. Console.WriteLine();
  101. ConsoleTUI.OpenFrame.Close("You must select an image.", new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  102. return true;
  103. }
  104. }
  105. }
  106. }