OptionsHelper.cs 14 KB

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