Guest User

Untitled

a guest
Jul 30th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #region using directives
  2.  
  3. using System;
  4. using System.IO;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Converters;
  7. using System.Collections.Generic;
  8.  
  9. #endregion
  10.  
  11. namespace PoGo.LocationFeeder.Settings
  12. {
  13. public class GlobalSettings
  14. {
  15. //public ulong ServerId = 206065054846681088;
  16. public List<string> ServerChannels = new List<string> { "coord-bot", "coords_bot", "coordsbots", "90_plus_iv", "90plus_ivonly", "rare_spottings", "high_iv_pokemon", "rare_pokemon" };
  17. public string DiscordToken = "";
  18. public int Port = 16969;
  19. public bool useToken = false;
  20. public string DiscordUser = "blahhh@hotmail.com";
  21. public string DiscordPassword = "123456";
  22. public bool usePokeSnipers = false;
  23.  
  24. public static GlobalSettings Default => new GlobalSettings();
  25.  
  26. public static GlobalSettings Load()
  27. {
  28. GlobalSettings settings;
  29. var configFile = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
  30.  
  31. if (File.Exists(configFile))
  32. {
  33. //if the file exists, load the settings
  34. var input = File.ReadAllText(configFile);
  35.  
  36. var jsonSettings = new JsonSerializerSettings();
  37. jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
  38. jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
  39. jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;
  40.  
  41. settings = JsonConvert.DeserializeObject<GlobalSettings>(input, jsonSettings);
  42. }
  43. else
  44. {
  45. settings = new GlobalSettings();
  46. }
  47.  
  48. var firstRun = !File.Exists(configFile);
  49.  
  50. settings.Save(configFile);
  51.  
  52. if (firstRun
  53. || settings.Port == 0
  54. || settings.ServerChannels == null
  55. || (settings.useToken && string.IsNullOrEmpty(settings.DiscordToken))
  56. || (!settings.useToken && string.IsNullOrEmpty(settings.DiscordUser))
  57. )
  58. {
  59. Console.WriteLine($"Invalid configuration detected. \nPlease edit {configFile} and try again");
  60. return null;
  61. }
  62.  
  63. return settings;
  64. }
  65.  
  66. public void Save(string fullPath)
  67. {
  68. var output = JsonConvert.SerializeObject(this, Formatting.Indented,
  69. new StringEnumConverter { CamelCaseText = true });
  70.  
  71. var folder = Path.GetDirectoryName(fullPath);
  72. if (folder != null && !Directory.Exists(folder))
  73. {
  74. Directory.CreateDirectory(folder);
  75. }
  76.  
  77. File.WriteAllText(fullPath, output);
  78. }
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment