2
0

Options.cs 17 KB

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