OptionsHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace Optimizer {
  9. internal static class OptionsHelper {
  10. internal static Color ForegroundColor = Color.FromArgb(153, 102, 204);
  11. internal static Color ForegroundAccentColor = Color.FromArgb(134, 89, 179);
  12. internal static Color BackgroundColor = Color.FromArgb(10, 10, 10);
  13. internal static Color BackAccentColor = Color.FromArgb(40, 40, 40);
  14. internal static Color TextColor;
  15. internal readonly static string SettingsFile = CoreHelper.CoreFolder + "\\Optimizer.json";
  16. internal static Options CurrentOptions = new Options();
  17. internal static dynamic TranslationList;
  18. internal static Color GetContrastColor(Color c) {
  19. double brightness = c.R * 0.299 + c.G * 0.587 + c.B * 0.114;
  20. return brightness > Constants.CONTRAST_THRESHOLD ? Color.Black : Color.White;
  21. }
  22. internal static void ApplyTheme(Form f) {
  23. SetTheme(f, CurrentOptions.Theme, ColorHelper.ChangeColorBrightness(CurrentOptions.Theme, 0.7));
  24. }
  25. private static void SetTheme(Form f, Color c1, Color c2) {
  26. dynamic c;
  27. ForegroundColor = c1;
  28. ForegroundAccentColor = c2;
  29. TextColor = GetContrastColor(CurrentOptions.Theme);
  30. Utilities.GetSelfAndChildrenRecursive(f).ToList().ForEach(x => {
  31. c = x;
  32. if (x is Button) {
  33. c.ForeColor = TextColor;
  34. c.BackColor = c1;
  35. c.FlatAppearance.BorderColor = c1;
  36. c.FlatAppearance.MouseDownBackColor = c2;
  37. c.FlatAppearance.MouseOverBackColor = c2;
  38. c.FlatAppearance.BorderSize = 0;
  39. }
  40. if (x is LinkLabel) {
  41. if ((string)c.Tag == Constants.THEME_FLAG) {
  42. c.LinkColor = c1;
  43. c.VisitedLinkColor = c1;
  44. c.ActiveLinkColor = c2;
  45. }
  46. }
  47. if (x is CheckBox || x is RadioButton || x is Label) {
  48. if ((string)c.Tag == Constants.THEME_FLAG) {
  49. c.ForeColor = c1;
  50. }
  51. }
  52. c.Invalidate();
  53. });
  54. }
  55. internal static void LegacyCheck() {
  56. if (File.Exists(SettingsFile)) {
  57. if (File.ReadAllText(SettingsFile).Contains("FirstRun")) {
  58. File.Delete(SettingsFile);
  59. }
  60. }
  61. }
  62. internal static void SaveSettings() {
  63. if (File.Exists(SettingsFile)) {
  64. string jsonFile = File.ReadAllText(SettingsFile);
  65. string jsonMemory = JsonConvert.SerializeObject(CurrentOptions);
  66. // check to see if no changes have been made
  67. if (JToken.DeepEquals(JObject.Parse(jsonFile), JObject.Parse(jsonMemory))) return;
  68. File.Delete(SettingsFile);
  69. using (FileStream fs = File.Open(SettingsFile, FileMode.OpenOrCreate))
  70. using (StreamWriter sw = new StreamWriter(fs))
  71. using (JsonWriter jw = new JsonTextWriter(sw)) {
  72. jw.Formatting = Formatting.Indented;
  73. JsonSerializer serializer = new JsonSerializer();
  74. serializer.Serialize(jw, CurrentOptions);
  75. }
  76. }
  77. }
  78. internal static void LoadSettings() {
  79. if (!File.Exists(SettingsFile) || File.ReadAllText(SettingsFile).Contains("\"Color\":")) {
  80. // settings migration for new color picker
  81. if (File.Exists(SettingsFile) && File.ReadAllText(SettingsFile).Contains("\"Color\":")) {
  82. Options tmpJson = JsonConvert.DeserializeObject<Options>(File.ReadAllText(SettingsFile));
  83. tmpJson.Theme = Color.FromArgb(153, 102, 204);
  84. CurrentOptions = tmpJson;
  85. }
  86. else {
  87. // DEFAULT OPTIONS
  88. CurrentOptions.Theme = Color.FromArgb(153, 102, 204);
  89. CurrentOptions.AppsFolder = string.Empty;
  90. CurrentOptions.EnableTray = false;
  91. CurrentOptions.AutoStart = false;
  92. CurrentOptions.InternalDNS = Constants.INTERNAL_DNS;
  93. CurrentOptions.UpdateOnLaunch = true;
  94. CurrentOptions.DisableIndicium = false;
  95. CurrentOptions.DisableAppsTool = false;
  96. CurrentOptions.DisableHostsEditor = false;
  97. CurrentOptions.DisableUWPApps = false;
  98. CurrentOptions.DisableStartupTool = false;
  99. CurrentOptions.DisableCleaner = false;
  100. CurrentOptions.DisableIntegrator = false;
  101. CurrentOptions.DisablePinger = false;
  102. //CurrentOptions.TelemetryClientID = Guid.NewGuid().ToString().ToUpperInvariant();
  103. //CurrentOptions.DisableOptimizerTelemetry = false;
  104. CurrentOptions.LanguageCode = LanguageCode.EN;
  105. CurrentOptions.EnablePerformanceTweaks = false;
  106. CurrentOptions.DisableNetworkThrottling = false;
  107. CurrentOptions.DisableWindowsDefender = false;
  108. CurrentOptions.DisableSystemRestore = false;
  109. CurrentOptions.DisablePrintService = false;
  110. CurrentOptions.DisableMediaPlayerSharing = false;
  111. CurrentOptions.DisableErrorReporting = false;
  112. CurrentOptions.DisableHomeGroup = false;
  113. CurrentOptions.DisableSuperfetch = false;
  114. CurrentOptions.DisableTelemetryTasks = false;
  115. CurrentOptions.DisableOffice2016Telemetry = false;
  116. CurrentOptions.DisableCompatibilityAssistant = false;
  117. CurrentOptions.DisableFaxService = false;
  118. CurrentOptions.DisableSmartScreen = false;
  119. CurrentOptions.DisableStickyKeys = false;
  120. CurrentOptions.EnableGamingMode = false;
  121. CurrentOptions.EnableLegacyVolumeSlider = false;
  122. CurrentOptions.DisableQuickAccessHistory = false;
  123. CurrentOptions.DisableStartMenuAds = false;
  124. CurrentOptions.UninstallOneDrive = false;
  125. CurrentOptions.DisableMyPeople = false;
  126. CurrentOptions.DisableAutomaticUpdates = false;
  127. CurrentOptions.ExcludeDrivers = false;
  128. CurrentOptions.DisableTelemetryServices = false;
  129. CurrentOptions.DisablePrivacyOptions = false;
  130. CurrentOptions.DisableCortana = false;
  131. CurrentOptions.DisableSensorServices = false;
  132. CurrentOptions.DisableWindowsInk = false;
  133. CurrentOptions.DisableSpellingTyping = false;
  134. CurrentOptions.DisableXboxLive = false;
  135. CurrentOptions.DisableGameBar = false;
  136. CurrentOptions.DisableInsiderService = false;
  137. CurrentOptions.DisableStoreUpdates = false;
  138. CurrentOptions.DisableCloudClipboard = false;
  139. CurrentOptions.EnableLongPaths = false;
  140. CurrentOptions.RemoveCastToDevice = false;
  141. CurrentOptions.DisableHibernation = false;
  142. CurrentOptions.DisableSMB1 = false;
  143. CurrentOptions.DisableSMB2 = false;
  144. CurrentOptions.DisableNTFSTimeStamp = false;
  145. CurrentOptions.DisableSearch = false;
  146. CurrentOptions.RestoreClassicPhotoViewer = false;
  147. CurrentOptions.DisableVisualStudioTelemetry = false;
  148. CurrentOptions.DisableFirefoxTemeletry = false;
  149. CurrentOptions.DisableChromeTelemetry = false;
  150. CurrentOptions.DisableNVIDIATelemetry = false;
  151. CurrentOptions.DisableEdgeDiscoverBar = false;
  152. CurrentOptions.DisableEdgeTelemetry = false;
  153. CurrentOptions.DisableOneDrive = false;
  154. CurrentOptions.TaskbarToLeft = false;
  155. CurrentOptions.DisableSnapAssist = false;
  156. CurrentOptions.DisableWidgets = false;
  157. CurrentOptions.DisableChat = false;
  158. CurrentOptions.ClassicMenu = false;
  159. CurrentOptions.DisableTPMCheck = false;
  160. CurrentOptions.CompactMode = false;
  161. CurrentOptions.DisableStickers = false;
  162. CurrentOptions.DisableVBS = false;
  163. CurrentOptions.DisableCoPilotAI = false;
  164. CurrentOptions.DisableHPET = false;
  165. CurrentOptions.EnableLoginVerbose = false;
  166. using (FileStream fs = File.Open(SettingsFile, FileMode.CreateNew))
  167. using (StreamWriter sw = new StreamWriter(fs))
  168. using (JsonWriter jw = new JsonTextWriter(sw)) {
  169. jw.Formatting = Formatting.Indented;
  170. JsonSerializer serializer = new JsonSerializer();
  171. serializer.Serialize(jw, CurrentOptions);
  172. }
  173. }
  174. }
  175. else {
  176. CurrentOptions = JsonConvert.DeserializeObject<Options>(File.ReadAllText(SettingsFile));
  177. }
  178. // prevent options from corruption
  179. if (CurrentOptions.Theme == Color.Empty || CurrentOptions.Theme == Color.FromArgb(0, 0, 0, 0)) {
  180. CurrentOptions.Theme = Color.FromArgb(153, 102, 204);
  181. }
  182. // generate random telemetry ID if not present
  183. //if (string.IsNullOrEmpty(CurrentOptions.TelemetryClientID))
  184. //{
  185. // CurrentOptions.TelemetryClientID = Guid.NewGuid().ToString().ToUpperInvariant();
  186. // SaveSettings();
  187. //}
  188. LoadTranslation();
  189. }
  190. internal static void LoadTranslation() {
  191. // load proper translation list
  192. try {
  193. if (CurrentOptions.LanguageCode == LanguageCode.EN) TranslationList = JObject.Parse(Properties.Resources.EN);
  194. if (CurrentOptions.LanguageCode == LanguageCode.RU) TranslationList = JObject.Parse(Properties.Resources.RU);
  195. if (CurrentOptions.LanguageCode == LanguageCode.EL) TranslationList = JObject.Parse(Properties.Resources.EL);
  196. if (CurrentOptions.LanguageCode == LanguageCode.TR) TranslationList = JObject.Parse(Properties.Resources.TR);
  197. if (CurrentOptions.LanguageCode == LanguageCode.DE) TranslationList = JObject.Parse(Properties.Resources.DE);
  198. if (CurrentOptions.LanguageCode == LanguageCode.ES) TranslationList = JObject.Parse(Properties.Resources.ES);
  199. if (CurrentOptions.LanguageCode == LanguageCode.PT) TranslationList = JObject.Parse(Properties.Resources.PT);
  200. if (CurrentOptions.LanguageCode == LanguageCode.FR) TranslationList = JObject.Parse(Properties.Resources.FR);
  201. if (CurrentOptions.LanguageCode == LanguageCode.IT) TranslationList = JObject.Parse(Properties.Resources.IT);
  202. if (CurrentOptions.LanguageCode == LanguageCode.CN) TranslationList = JObject.Parse(Properties.Resources.CN);
  203. if (CurrentOptions.LanguageCode == LanguageCode.CZ) TranslationList = JObject.Parse(Properties.Resources.CZ);
  204. if (CurrentOptions.LanguageCode == LanguageCode.TW) TranslationList = JObject.Parse(Properties.Resources.TW);
  205. if (CurrentOptions.LanguageCode == LanguageCode.KO) TranslationList = JObject.Parse(Properties.Resources.KO);
  206. if (CurrentOptions.LanguageCode == LanguageCode.PL) TranslationList = JObject.Parse(Properties.Resources.PL);
  207. if (CurrentOptions.LanguageCode == LanguageCode.AR) TranslationList = JObject.Parse(Properties.Resources.AR);
  208. if (CurrentOptions.LanguageCode == LanguageCode.KU) TranslationList = JObject.Parse(Properties.Resources.KU);
  209. if (CurrentOptions.LanguageCode == LanguageCode.HU) TranslationList = JObject.Parse(Properties.Resources.HU);
  210. if (CurrentOptions.LanguageCode == LanguageCode.RO) TranslationList = JObject.Parse(Properties.Resources.RO);
  211. if (CurrentOptions.LanguageCode == LanguageCode.NL) TranslationList = JObject.Parse(Properties.Resources.NL);
  212. if (CurrentOptions.LanguageCode == LanguageCode.UA) TranslationList = JObject.Parse(Properties.Resources.UA);
  213. if (CurrentOptions.LanguageCode == LanguageCode.JA) TranslationList = JObject.Parse(Properties.Resources.JA);
  214. if (CurrentOptions.LanguageCode == LanguageCode.FA) TranslationList = JObject.Parse(Properties.Resources.FA);
  215. if (CurrentOptions.LanguageCode == LanguageCode.NE) TranslationList = JObject.Parse(Properties.Resources.NE);
  216. if (CurrentOptions.LanguageCode == LanguageCode.BG) TranslationList = JObject.Parse(Properties.Resources.BG);
  217. }
  218. catch (Exception ex) {
  219. Logger.LogError("Options.LoadTranslation", ex.Message, ex.StackTrace);
  220. TranslationList = JObject.Parse(Properties.Resources.EN);
  221. }
  222. }
  223. }
  224. }