Options.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. {
  10. [Serializable]
  11. public sealed class SettingsJson
  12. {
  13. public Color Theme { get; set; }
  14. public string AppsFolder { get; set; }
  15. public bool EnableTray { get; set; }
  16. public bool AutoStart { get; set; }
  17. //public string TelemetryClientID { get; set; }
  18. //public bool DisableOptimizerTelemetry { get; set; }
  19. public LanguageCode LanguageCode { get; set; }
  20. // universal
  21. public bool EnablePerformanceTweaks { get; set; }
  22. public bool DisableNetworkThrottling { get; set; }
  23. public bool DisableWindowsDefender { get; set; }
  24. public bool DisableSystemRestore { get; set; }
  25. public bool DisablePrintService { get; set; }
  26. public bool DisableMediaPlayerSharing { get; set; }
  27. public bool DisableErrorReporting { get; set; }
  28. public bool DisableHomeGroup { get; set; }
  29. public bool DisableSuperfetch { get; set; }
  30. public bool DisableTelemetryTasks { get; set; }
  31. public bool DisableCompatibilityAssistant { get; set; }
  32. public bool DisableFaxService { get; set; }
  33. public bool DisableSmartScreen { get; set; }
  34. public bool DisableCloudClipboard { get; set; }
  35. public bool DisableStickyKeys { get; set; }
  36. public bool DisableHibernation { get; set; }
  37. public bool DisableSMB1 { get; set; }
  38. public bool DisableSMB2 { get; set; }
  39. public bool DisableNTFSTimeStamp { get; set; }
  40. public bool DisableSearch { get; set; }
  41. // apps telemetry
  42. public bool DisableOffice2016Telemetry { get; set; }
  43. public bool DisableVisualStudioTelemetry { get; set; }
  44. public bool DisableFirefoxTemeletry { get; set; }
  45. public bool DisableChromeTelemetry { get; set; }
  46. public bool DisableNVIDIATelemetry { get; set; }
  47. // edge
  48. public bool DisableEdgeDiscoverBar { get; set; }
  49. public bool DisableEdgeTelemetry { get; set; }
  50. // windows 8
  51. public bool DisableOneDrive { get; set; }
  52. // windows 10
  53. public bool EnableLegacyVolumeSlider { get; set; }
  54. public bool DisableQuickAccessHistory { get; set; }
  55. public bool DisableStartMenuAds { get; set; }
  56. public bool UninstallOneDrive { get; set; }
  57. public bool DisableMyPeople { get; set; }
  58. public bool DisableAutomaticUpdates { get; set; }
  59. public bool ExcludeDrivers { get; set; }
  60. public bool DisableTelemetryServices { get; set; }
  61. public bool DisablePrivacyOptions { get; set; }
  62. public bool DisableCortana { get; set; }
  63. public bool DisableSensorServices { get; set; }
  64. public bool DisableWindowsInk { get; set; }
  65. public bool DisableSpellingTyping { get; set; }
  66. public bool DisableXboxLive { get; set; }
  67. public bool DisableGameBar { get; set; }
  68. public bool DisableInsiderService { get; set; }
  69. public bool DisableStoreUpdates { get; set; }
  70. public bool EnableLongPaths { get; set; }
  71. public bool RemoveCastToDevice { get; set; }
  72. public bool EnableGamingMode { get; set; }
  73. // windows 11
  74. public bool TaskbarToLeft { get; set; }
  75. public bool DisableSnapAssist { get; set; }
  76. public bool DisableWidgets { get; set; }
  77. public bool DisableChat { get; set; }
  78. public bool ClassicMenu { get; set; }
  79. public bool DisableTPMCheck { get; set; }
  80. public bool CompactMode { get; set; }
  81. public bool DisableStickers { get; set; }
  82. public bool DisableVBS { get; set; }
  83. // advanced
  84. public bool DisableHPET { get; set; }
  85. public bool EnableLoginVerbose { get; set; }
  86. }
  87. internal static class Options
  88. {
  89. const int CONTRAST_THRESHOLD = 149;
  90. internal static Color ForegroundColor = Color.FromArgb(153, 102, 204);
  91. internal static Color ForegroundAccentColor = Color.FromArgb(134, 89, 179);
  92. internal static Color BackgroundColor = Color.FromArgb(10, 10, 10);
  93. internal static Color BackAccentColor = Color.FromArgb(40, 40, 40);
  94. internal static Color TextColor;
  95. readonly static string _themeFlag = "themeable";
  96. internal readonly static string SettingsFile = Required.CoreFolder + "\\Optimizer.json";
  97. internal static SettingsJson CurrentOptions = new SettingsJson();
  98. internal static dynamic TranslationList;
  99. internal static Color GetContrastColor(Color c)
  100. {
  101. double brightness = c.R * 0.299 + c.G * 0.587 + c.B * 0.114;
  102. return brightness > CONTRAST_THRESHOLD ? Color.Black : Color.White;
  103. }
  104. internal static void ApplyTheme(Form f)
  105. {
  106. SetTheme(f, CurrentOptions.Theme, ColorHelper.ChangeColorBrightness(CurrentOptions.Theme, 0.7));
  107. }
  108. private static void SetTheme(Form f, Color c1, Color c2)
  109. {
  110. dynamic c;
  111. ForegroundColor = c1;
  112. ForegroundAccentColor = c2;
  113. TextColor = GetContrastColor(CurrentOptions.Theme);
  114. Utilities.GetSelfAndChildrenRecursive(f).ToList().ForEach(x =>
  115. {
  116. c = x;
  117. if (x is Button)
  118. {
  119. c.ForeColor = TextColor;
  120. c.BackColor = c1;
  121. c.FlatAppearance.BorderColor = c1;
  122. c.FlatAppearance.MouseDownBackColor = c2;
  123. c.FlatAppearance.MouseOverBackColor = c2;
  124. c.FlatAppearance.BorderSize = 0;
  125. }
  126. if (x is LinkLabel)
  127. {
  128. if ((string)c.Tag == _themeFlag)
  129. {
  130. c.LinkColor = c1;
  131. c.VisitedLinkColor = c1;
  132. c.ActiveLinkColor = c2;
  133. }
  134. }
  135. if (x is CheckBox || x is RadioButton || x is Label)
  136. {
  137. if ((string)c.Tag == _themeFlag)
  138. {
  139. c.ForeColor = c1;
  140. }
  141. }
  142. if (x is MoonComboBox)
  143. {
  144. if ((string)c.Tag == _themeFlag)
  145. {
  146. c.SelectColor = c1;
  147. }
  148. }
  149. c.Invalidate();
  150. });
  151. }
  152. internal static void LegacyCheck()
  153. {
  154. if (File.Exists(SettingsFile))
  155. {
  156. if (File.ReadAllText(SettingsFile).Contains("FirstRun"))
  157. {
  158. File.Delete(SettingsFile);
  159. }
  160. }
  161. }
  162. internal static void SaveSettings()
  163. {
  164. if (File.Exists(SettingsFile))
  165. {
  166. string jsonFile = File.ReadAllText(SettingsFile);
  167. string jsonMemory = JsonConvert.SerializeObject(CurrentOptions);
  168. // check to see if no changes have been made
  169. if (JToken.DeepEquals(JObject.Parse(jsonFile), JObject.Parse(jsonMemory))) return;
  170. File.Delete(SettingsFile);
  171. using (FileStream fs = File.Open(SettingsFile, FileMode.OpenOrCreate))
  172. using (StreamWriter sw = new StreamWriter(fs))
  173. using (JsonWriter jw = new JsonTextWriter(sw))
  174. {
  175. jw.Formatting = Formatting.Indented;
  176. JsonSerializer serializer = new JsonSerializer();
  177. serializer.Serialize(jw, CurrentOptions);
  178. }
  179. }
  180. }
  181. internal static void LoadSettings()
  182. {
  183. if (!File.Exists(SettingsFile) || File.ReadAllText(SettingsFile).Contains("\"Color\":"))
  184. {
  185. // settings migration for new color picker
  186. if (File.Exists(SettingsFile) && File.ReadAllText(SettingsFile).Contains("\"Color\":"))
  187. {
  188. SettingsJson tmpJson = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));
  189. tmpJson.Theme = Color.FromArgb(153, 102, 204);
  190. CurrentOptions = tmpJson;
  191. }
  192. else
  193. {
  194. // DEFAULT OPTIONS
  195. CurrentOptions.Theme = Color.FromArgb(153, 102, 204);
  196. CurrentOptions.AppsFolder = Path.Combine(Application.StartupPath, "OptimizerDownloads");
  197. Directory.CreateDirectory(Options.CurrentOptions.AppsFolder);
  198. CurrentOptions.EnableTray = false;
  199. CurrentOptions.AutoStart = false;
  200. //CurrentOptions.TelemetryClientID = Guid.NewGuid().ToString().ToUpperInvariant();
  201. //CurrentOptions.DisableOptimizerTelemetry = false;
  202. CurrentOptions.LanguageCode = LanguageCode.EN;
  203. CurrentOptions.EnablePerformanceTweaks = false;
  204. CurrentOptions.DisableNetworkThrottling = false;
  205. CurrentOptions.DisableWindowsDefender = false;
  206. CurrentOptions.DisableSystemRestore = false;
  207. CurrentOptions.DisablePrintService = false;
  208. CurrentOptions.DisableMediaPlayerSharing = false;
  209. CurrentOptions.DisableErrorReporting = false;
  210. CurrentOptions.DisableHomeGroup = false;
  211. CurrentOptions.DisableSuperfetch = false;
  212. CurrentOptions.DisableTelemetryTasks = false;
  213. CurrentOptions.DisableOffice2016Telemetry = false;
  214. CurrentOptions.DisableCompatibilityAssistant = false;
  215. CurrentOptions.DisableFaxService = false;
  216. CurrentOptions.DisableSmartScreen = false;
  217. CurrentOptions.DisableStickyKeys = false;
  218. CurrentOptions.EnableGamingMode = false;
  219. CurrentOptions.EnableLegacyVolumeSlider = false;
  220. CurrentOptions.DisableQuickAccessHistory = false;
  221. CurrentOptions.DisableStartMenuAds = false;
  222. CurrentOptions.UninstallOneDrive = false;
  223. CurrentOptions.DisableMyPeople = false;
  224. CurrentOptions.DisableAutomaticUpdates = false;
  225. CurrentOptions.ExcludeDrivers = false;
  226. CurrentOptions.DisableTelemetryServices = false;
  227. CurrentOptions.DisablePrivacyOptions = false;
  228. CurrentOptions.DisableCortana = false;
  229. CurrentOptions.DisableSensorServices = false;
  230. CurrentOptions.DisableWindowsInk = false;
  231. CurrentOptions.DisableSpellingTyping = false;
  232. CurrentOptions.DisableXboxLive = false;
  233. CurrentOptions.DisableGameBar = false;
  234. CurrentOptions.DisableInsiderService = false;
  235. CurrentOptions.DisableStoreUpdates = false;
  236. CurrentOptions.DisableCloudClipboard = false;
  237. CurrentOptions.EnableLongPaths = false;
  238. CurrentOptions.RemoveCastToDevice = false;
  239. CurrentOptions.DisableHibernation = false;
  240. CurrentOptions.DisableSMB1 = false;
  241. CurrentOptions.DisableSMB2 = false;
  242. CurrentOptions.DisableNTFSTimeStamp = false;
  243. CurrentOptions.DisableSearch = false;
  244. CurrentOptions.DisableVisualStudioTelemetry = false;
  245. CurrentOptions.DisableFirefoxTemeletry = false;
  246. CurrentOptions.DisableChromeTelemetry = false;
  247. CurrentOptions.DisableNVIDIATelemetry = false;
  248. CurrentOptions.DisableEdgeDiscoverBar = false;
  249. CurrentOptions.DisableEdgeTelemetry = false;
  250. CurrentOptions.DisableOneDrive = false;
  251. CurrentOptions.TaskbarToLeft = false;
  252. CurrentOptions.DisableSnapAssist = false;
  253. CurrentOptions.DisableWidgets = false;
  254. CurrentOptions.DisableChat = false;
  255. CurrentOptions.ClassicMenu = false;
  256. CurrentOptions.DisableTPMCheck = false;
  257. CurrentOptions.CompactMode = false;
  258. CurrentOptions.DisableStickers = false;
  259. CurrentOptions.DisableVBS = false;
  260. CurrentOptions.DisableHPET = false;
  261. CurrentOptions.EnableLoginVerbose = false;
  262. using (FileStream fs = File.Open(SettingsFile, FileMode.CreateNew))
  263. using (StreamWriter sw = new StreamWriter(fs))
  264. using (JsonWriter jw = new JsonTextWriter(sw))
  265. {
  266. jw.Formatting = Formatting.Indented;
  267. JsonSerializer serializer = new JsonSerializer();
  268. serializer.Serialize(jw, CurrentOptions);
  269. }
  270. }
  271. }
  272. else
  273. {
  274. CurrentOptions = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));
  275. }
  276. // prevent options from corruption
  277. if (CurrentOptions.Theme == Color.Empty || CurrentOptions.Theme == Color.FromArgb(0, 0, 0, 0))
  278. {
  279. CurrentOptions.Theme = Color.FromArgb(153, 102, 204);
  280. }
  281. // generate random telemetry ID if not present
  282. //if (string.IsNullOrEmpty(CurrentOptions.TelemetryClientID))
  283. //{
  284. // CurrentOptions.TelemetryClientID = Guid.NewGuid().ToString().ToUpperInvariant();
  285. // SaveSettings();
  286. //}
  287. LoadTranslation();
  288. }
  289. internal static void LoadTranslation()
  290. {
  291. // load proper translation list
  292. try
  293. {
  294. if (CurrentOptions.LanguageCode == LanguageCode.EN) TranslationList = JObject.Parse(Properties.Resources.EN);
  295. if (CurrentOptions.LanguageCode == LanguageCode.RU) TranslationList = JObject.Parse(Properties.Resources.RU);
  296. if (CurrentOptions.LanguageCode == LanguageCode.EL) TranslationList = JObject.Parse(Properties.Resources.EL);
  297. if (CurrentOptions.LanguageCode == LanguageCode.TR) TranslationList = JObject.Parse(Properties.Resources.TR);
  298. if (CurrentOptions.LanguageCode == LanguageCode.DE) TranslationList = JObject.Parse(Properties.Resources.DE);
  299. if (CurrentOptions.LanguageCode == LanguageCode.ES) TranslationList = JObject.Parse(Properties.Resources.ES);
  300. if (CurrentOptions.LanguageCode == LanguageCode.PT) TranslationList = JObject.Parse(Properties.Resources.PT);
  301. if (CurrentOptions.LanguageCode == LanguageCode.FR) TranslationList = JObject.Parse(Properties.Resources.FR);
  302. if (CurrentOptions.LanguageCode == LanguageCode.IT) TranslationList = JObject.Parse(Properties.Resources.IT);
  303. if (CurrentOptions.LanguageCode == LanguageCode.CN) TranslationList = JObject.Parse(Properties.Resources.CN);
  304. if (CurrentOptions.LanguageCode == LanguageCode.CZ) TranslationList = JObject.Parse(Properties.Resources.CZ);
  305. if (CurrentOptions.LanguageCode == LanguageCode.TW) TranslationList = JObject.Parse(Properties.Resources.TW);
  306. if (CurrentOptions.LanguageCode == LanguageCode.KO) TranslationList = JObject.Parse(Properties.Resources.KO);
  307. if (CurrentOptions.LanguageCode == LanguageCode.PL) TranslationList = JObject.Parse(Properties.Resources.PL);
  308. if (CurrentOptions.LanguageCode == LanguageCode.AR) TranslationList = JObject.Parse(Properties.Resources.AR);
  309. if (CurrentOptions.LanguageCode == LanguageCode.KU) TranslationList = JObject.Parse(Properties.Resources.KU);
  310. if (CurrentOptions.LanguageCode == LanguageCode.HU) TranslationList = JObject.Parse(Properties.Resources.HU);
  311. if (CurrentOptions.LanguageCode == LanguageCode.RO) TranslationList = JObject.Parse(Properties.Resources.RO);
  312. if (CurrentOptions.LanguageCode == LanguageCode.NL) TranslationList = JObject.Parse(Properties.Resources.NL);
  313. if (CurrentOptions.LanguageCode == LanguageCode.UA) TranslationList = JObject.Parse(Properties.Resources.UA);
  314. if (CurrentOptions.LanguageCode == LanguageCode.JA) TranslationList = JObject.Parse(Properties.Resources.JA);
  315. }
  316. catch (Exception ex)
  317. {
  318. ErrorLogger.LogError("Options.LoadTranslation", ex.Message, ex.StackTrace);
  319. TranslationList = JObject.Parse(Properties.Resources.EN);
  320. }
  321. }
  322. }
  323. }