PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/PogoLocationFeeder/Config/Settings.cs

https://gitlab.com/HakaseShounen/PogoLocationFeeder
C# | 232 lines | 183 code | 27 blank | 22 comment | 17 complexity | a94d980b73658b2173ee6f8e4c82e719 MD5 | raw file
  1. /*
  2. PogoLocationFeeder gathers pokemon data from various sources and serves it to connected clients
  3. Copyright (C) 2016 PogoLocationFeeder Development Team <admin@pokefeeder.live>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as
  6. published by the Free Software Foundation, either version 3 of the
  7. License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #region using directives
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.IO;
  20. using System.Linq;
  21. using Microsoft.Win32;
  22. using Newtonsoft.Json;
  23. using Newtonsoft.Json.Converters;
  24. using PogoLocationFeeder.Common;
  25. using PogoLocationFeeder.Helper;
  26. using POGOProtos.Enums;
  27. #endregion
  28. namespace PogoLocationFeeder.Config
  29. {
  30. public class GlobalSettings
  31. {
  32. public static bool ThreadPause = false;
  33. public static GlobalSettings Settings;
  34. public static bool Gui = false;
  35. public static IOutput Output;
  36. public static int Port = 16969;
  37. public static bool UsePokeSnipers = true;
  38. public static bool UseTrackemon = false;
  39. public static bool UsePokewatchers = true;
  40. public static bool UsePokezz = true;
  41. public static bool UsePokemonGoIVClub = true;
  42. public static bool UseFilter = true;
  43. public static string AppTheme = "Dark";
  44. public static bool IsServer = true;
  45. public static bool IsManaged = true;
  46. public static string ServerHost = "";
  47. public static string PokeSnipers2Exe = "";
  48. public static int RemoveAfter = 15;
  49. public static int ShowLimit = 30;
  50. public static bool VerifyOnSkiplagged = true;
  51. public static List<string> PokekomsToFeedFilter;
  52. public static bool SniperVisibility => IsOneClickSnipeSupported();
  53. public static GlobalSettings Default => new GlobalSettings();
  54. public static string ConfigFile = Path.Combine(Directory.GetCurrentDirectory(), "Config", "config.json");
  55. public static string FilterPath = Path.Combine(Directory.GetCurrentDirectory(), "Config", "filter.json");
  56. public static GlobalSettings Load()
  57. {
  58. GlobalSettings settings;
  59. if (File.Exists(ConfigFile)) {
  60. SettingsToSave set;
  61. //if the file exists, load the Settings
  62. var input = File.ReadAllText(ConfigFile);
  63. var jsonSettings = new JsonSerializerSettings();
  64. jsonSettings.Converters.Add(new StringEnumConverter {CamelCaseText = true});
  65. jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
  66. jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;
  67. set = JsonConvert.DeserializeObject<SettingsToSave>(input, jsonSettings);
  68. settings = new GlobalSettings();
  69. Port = set.Port;
  70. //UseTrackemon = set.UseTrackemon;
  71. UsePokeSnipers = set.UsePokeSnipers;
  72. UsePokewatchers = set.UsePokewatchers;
  73. UsePokezz = set.UsePokezz;
  74. UsePokemonGoIVClub = set.UsePokemonGoIVClub;
  75. VerifyOnSkiplagged = set.VerifyOnSkiplagged;
  76. RemoveAfter = set.RemoveAfter;
  77. ShowLimit = Math.Max(set.ShowLimit, 1);
  78. PokeSnipers2Exe = set.PokeSnipers2Exe;
  79. UseFilter = set.UseFilter;
  80. AppTheme = set.AppTheme;
  81. IsServer = set.IsServer;
  82. IsManaged = set.IsManaged;
  83. ServerHost = set.ServerHost;
  84. }
  85. else
  86. {
  87. settings = new GlobalSettings();
  88. }
  89. PokekomsToFeedFilter = LoadFilter();
  90. var firstRun = !File.Exists(ConfigFile);
  91. Save();
  92. if (firstRun
  93. || Port == 0
  94. )
  95. {
  96. Log.Error($"Invalid configuration detected. \nPlease edit {ConfigFile} and try again");
  97. return null;
  98. }
  99. return settings;
  100. }
  101. public static bool IsOneClickSnipeSupported()
  102. {
  103. if (PokeSnipers2Exe != null && PokeSnipers2Exe.Contains(".exe"))
  104. {
  105. return true;
  106. }
  107. const string keyName = @"pokesniper2\Shell\Open\Command";
  108. //return Registry.GetValue(keyName, valueName, null) == null;
  109. using (var Key = Registry.ClassesRoot.OpenSubKey(keyName))
  110. {
  111. return Key != null;
  112. }
  113. }
  114. public static void Save()
  115. {
  116. var output = JsonConvert.SerializeObject(new SettingsToSave(), Formatting.Indented,
  117. new StringEnumConverter {CamelCaseText = true});
  118. var folder = Path.GetDirectoryName(ConfigFile);
  119. if (folder != null && !Directory.Exists(folder))
  120. {
  121. Directory.CreateDirectory(folder);
  122. }
  123. try {
  124. File.WriteAllText(ConfigFile, output);
  125. } catch (Exception) {
  126. //ignore
  127. }
  128. }
  129. public static List<string> DefaultPokemonsToFeed = new List<string>() {"Venusaur", "Charizard", "Blastoise","Beedrill","Raichu","Sandslash","Nidoking","Nidoqueen","Clefable","Ninetales",
  130. "Golbat","Vileplume","Golduck","Primeape","Arcanine","Poliwrath","Alakazam","Machamp","Golem","Rapidash","Slowbro","Farfetchd","Muk","Cloyster","Gengar","Exeggutor",
  131. "Marowak","Hitmonchan","Lickitung","Rhydon","Chansey","Kangaskhan","Starmie","MrMime","Scyther","Magmar","Electabuzz","Magmar","Jynx","Gyarados","Lapras","Ditto",
  132. "Vaporeon","Jolteon","Flareon","Porygon","Kabutops","Aerodactyl","Snorlax","Articuno","Zapdos","Moltres","Dragonite", "Mewtwo", "Mew"};
  133. public static List<string> LoadFilter() {
  134. if (File.Exists(FilterPath)) {
  135. var input = File.ReadAllText(FilterPath);
  136. var jsonSettings = new JsonSerializerSettings();
  137. jsonSettings.Converters.Add(new StringEnumConverter {CamelCaseText = true});
  138. jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
  139. jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;
  140. return JsonConvert.DeserializeObject<List<string>>(input, jsonSettings).
  141. Where(x => PokemonParser.ParsePokemon(x, true) != PokemonId.Missingno).
  142. GroupBy(x => PokemonParser.ParsePokemon(x)).
  143. Select(y => y.FirstOrDefault()).ToList();
  144. } else {
  145. var output = JsonConvert.SerializeObject(DefaultPokemonsToFeed, Formatting.Indented,
  146. new StringEnumConverter { CamelCaseText = true });
  147. var folder = Path.GetDirectoryName(FilterPath);
  148. if(folder != null && !Directory.Exists(folder)) {
  149. Directory.CreateDirectory(folder);
  150. }
  151. File.WriteAllText(FilterPath, output);
  152. return new List<string>();
  153. }
  154. }
  155. }
  156. public class SettingsToSave
  157. {
  158. [DefaultValue(16969)]
  159. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  160. public int Port = GlobalSettings.Port;
  161. [DefaultValue(true)]
  162. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  163. public bool UsePokeSnipers = GlobalSettings.UsePokeSnipers;
  164. //public bool UseTrackemon = GlobalSettings.UseTrackemon;
  165. [DefaultValue(true)]
  166. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  167. public bool UsePokezz = GlobalSettings.UsePokezz;
  168. [DefaultValue(true)]
  169. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  170. public bool UsePokewatchers = GlobalSettings.UsePokewatchers;
  171. [DefaultValue("")]
  172. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  173. public string PokeSnipers2Exe = GlobalSettings.PokeSnipers2Exe;
  174. [DefaultValue(5)]
  175. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  176. public int RemoveAfter = GlobalSettings.RemoveAfter;
  177. [DefaultValue(30)]
  178. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  179. public int ShowLimit = Math.Max(GlobalSettings.ShowLimit, 1);
  180. [DefaultValue(true)]
  181. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  182. public bool VerifyOnSkiplagged = GlobalSettings.VerifyOnSkiplagged;
  183. [DefaultValue(true)]
  184. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  185. public bool UsePokemonGoIVClub = GlobalSettings.UsePokemonGoIVClub;
  186. [DefaultValue(true)]
  187. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  188. public bool UseFilter = GlobalSettings.UseFilter;
  189. [DefaultValue("Dark")]
  190. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  191. public string AppTheme = GlobalSettings.AppTheme;
  192. [DefaultValue(false)]
  193. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  194. public bool IsServer = GlobalSettings.IsServer;
  195. [DefaultValue(false)]
  196. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  197. public bool IsManaged = GlobalSettings.IsManaged;
  198. [DefaultValue("")]
  199. [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
  200. public string ServerHost = GlobalSettings.ServerHost;
  201. }
  202. }