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.

131 lines
5.2 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Security;
  8. using System.Text;
  9. using System.Threading;
  10. using amecs.Misc;
  11. using Ameliorated.ConsoleUtils;
  12. using Microsoft.Dism;
  13. namespace amecs.Actions
  14. {
  15. public class _NET
  16. {
  17. private static string _mountedPath;
  18. private static string _isoPath;
  19. private static void Unmount()
  20. {
  21. if (_isoPath == "none")
  22. return;
  23. SelectWindowsImage.DismountIso(_isoPath);
  24. }
  25. public static bool Install()
  26. {
  27. (_mountedPath, _isoPath, _, _, _) = SelectWindowsImage.GetMediaPath(true);
  28. if (_mountedPath == null) return false;
  29. if (!Directory.Exists(_mountedPath + @"\sources\sxs") || !Directory.GetFiles(_mountedPath + @"\sources\sxs", "*netfx3*").Any())
  30. {
  31. Unmount();
  32. Console.WriteLine();
  33. ConsoleTUI.OpenFrame.Close("ISO/USB/folder does not contain the required files.",
  34. ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " });
  35. return false;
  36. }
  37. ConsoleTUI.OpenFrame.WriteCentered("\r\nInstalling .NET 3.5");
  38. var topCache = Console.CursorTop;
  39. var leftCache = Console.CursorLeft;
  40. Console.WriteLine();
  41. var inProgress = false;
  42. try
  43. {
  44. using var indicator = new ConsoleUtils.LoadingIndicator();
  45. DismApi.Initialize(DismLogLevel.LogErrors);
  46. using (var session = DismApi.OpenOnlineSession())
  47. {
  48. var stdout = GetStdHandle(-11);
  49. var indicatorStopped = false;
  50. var maxHashTags = (ConsoleTUI.OpenFrame.DisplayWidth - 5);
  51. DismApi.EnableFeatureByPackagePath(session, "NetFX3", null, true, true, new List<string>() { _mountedPath + @"\sources\sxs" }, delegate(DismProgress progress)
  52. {
  53. inProgress = true;
  54. if (!indicatorStopped)
  55. {
  56. indicator.Stop();
  57. Console.SetCursorPosition(leftCache, topCache);
  58. Console.WriteLine(" ");
  59. }
  60. indicatorStopped = true;
  61. var progressPercentage = progress.Current / 10;
  62. var currentHashTags = (int)Math.Ceiling(Math.Min(((double)progressPercentage / 100) * maxHashTags, maxHashTags));
  63. var spaces = maxHashTags - currentHashTags + (4 - progressPercentage.ToString().Length);
  64. var sb = new StringBuilder(new string('#', currentHashTags) + new string(' ', spaces) + progressPercentage + "%");
  65. WriteConsoleOutputCharacter(stdout, sb, (uint)sb.Length, new Languages.COORD((short)ConsoleTUI.OpenFrame.DisplayOffset, (short)Console.CursorTop), out _);
  66. inProgress = false;
  67. });
  68. session.Close();
  69. Thread.Sleep(100);
  70. var sb = new StringBuilder(new string('#', maxHashTags) + " 100%");
  71. uint throwaway;
  72. WriteConsoleOutputCharacter(stdout, sb, (uint)sb.Length, new Languages.COORD((short)ConsoleTUI.OpenFrame.DisplayOffset, (short)Console.CursorTop), out throwaway);
  73. }
  74. DismApi.Shutdown();
  75. Unmount();
  76. } catch (Exception e)
  77. {
  78. while (inProgress)
  79. {
  80. Thread.Sleep(50);
  81. }
  82. Unmount();
  83. Console.WriteLine();
  84. Console.WriteLine();
  85. ConsoleTUI.OpenFrame.Close("DISM error: " + e.Message, ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt()
  86. {
  87. AnyKey = true,
  88. Text = "Press any key to return to the Menu: "
  89. });
  90. return false;
  91. }
  92. Console.WriteLine();
  93. Console.WriteLine();
  94. ConsoleTUI.OpenFrame.Close(".NET 3.5 installed successfully", ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt()
  95. {
  96. AnyKey = true,
  97. Text = "Press any key to return to the Menu: "
  98. });
  99. return true;
  100. }
  101. [StructLayout(LayoutKind.Sequential)]
  102. public struct COORD
  103. {
  104. public short X;
  105. public short Y;
  106. public COORD(short X, short Y)
  107. {
  108. this.X = X;
  109. this.Y = Y;
  110. }
  111. };
  112. [DllImport("kernel32.dll", SetLastError = true)]
  113. internal static extern bool WriteConsoleOutputCharacter(IntPtr hConsoleOutput, StringBuilder lpCharacter, uint nLength, Languages.COORD dwWriteCoord, out uint lpNumberOfCharsWritten);
  114. [DllImport("kernel32.dll", SetLastError = true)]
  115. static extern IntPtr GetStdHandle(int nStdHandle);
  116. }
  117. }