/Source/PacketParser/WowPacketParser/Misc/Settings.cs

http://github.com/Strawberry-Pr0jcts/StrawberryTools · C# · 110 lines · 95 code · 15 blank · 0 comment · 13 complexity · 3ef27bd91c4630b555fe3660134a031d MD5 · raw file

  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Configuration;
  4. using System.Globalization;
  5. using WowPacketParser.Enums;
  6. namespace WowPacketParser.Misc
  7. {
  8. public static class Settings
  9. {
  10. private static readonly NameValueCollection SettingsCollection = ConfigurationManager.AppSettings;
  11. public static readonly string[] Filters = GetStringList("Filters", new string[0]);
  12. public static readonly string[] IgnoreFilters = GetStringList("IgnoreFilters", new string[0]);
  13. public static readonly string[] IgnoreByEntryFilters = GetStringList("IgnoreByEntryFilters", new string[0]);
  14. public static readonly string[] AreaFilters = GetStringList("AreaFilters", new string[0]);
  15. public static readonly int FilterPacketNumLow = GetInt32("FilterPacketNumLow", 0);
  16. public static readonly int FilterPacketNumHigh = GetInt32("FilterPacketNumHigh", 0);
  17. public static readonly int FilterPacketsNum = GetInt32("FilterPacketsNum", 0);
  18. public static readonly ClientVersionBuild ClientBuild = GetEnum("ClientBuild", ClientVersionBuild.Zero);
  19. public static readonly int ThreadsRead = GetInt32("Threads.Read", 0);
  20. public static readonly int ThreadsParse = GetInt32("Threads.Parse", 0);
  21. public static readonly DumpFormatType DumpFormat = GetEnum("DumpFormat", DumpFormatType.Text);
  22. public static readonly StatsOutputFlags StatsOutput = GetEnum("StatsOutput", StatsOutputFlags.Local);
  23. public static readonly SQLOutputFlags SQLOutput = GetEnum("SQLOutput", SQLOutputFlags.None);
  24. public static readonly string SQLFileName = GetString("SQLFileName", string.Empty);
  25. public static readonly bool ShowEndPrompt = GetBoolean("ShowEndPrompt", false);
  26. public static readonly bool LogErrors = GetBoolean("LogErrors", false);
  27. public static readonly bool DebugReads = GetBoolean("DebugReads", false);
  28. public static readonly bool SplitOutput = GetBoolean("SplitOutput", false);
  29. public static readonly bool ParsingLog = GetBoolean("ParsingLog", false);
  30. public static readonly bool SSHEnabled = GetBoolean("SSHEnabled", false);
  31. public static readonly string SSHHost = GetString("SSHHost", "localhost");
  32. public static readonly string SSHUsername = GetString("SSHUsername", string.Empty);
  33. public static readonly string SSHPassword = GetString("SSHPassword", string.Empty);
  34. public static readonly int SSHPort = GetInt32("SSHPort", 22);
  35. public static readonly int SSHLocalPort = GetInt32("SSHLocalPort", 3307);
  36. public static readonly bool DBEnabled = GetBoolean("DBEnabled", false);
  37. public static readonly string Server = GetString("Server", "localhost");
  38. public static readonly string Port = GetString("Port", "3306");
  39. public static readonly string Username = GetString("Username", "root");
  40. public static readonly string Password = GetString("Password", string.Empty);
  41. public static readonly string WPPDatabase = GetString("WPPDatabase", "WPP");
  42. public static readonly string TDBDatabase = GetString("TDBDatabase", "world");
  43. public static readonly string CharacterSet = GetString("CharacterSet", "utf8");
  44. private static string GetString(string key, string defValue)
  45. {
  46. var aux = SettingsCollection[key];
  47. return aux ?? defValue;
  48. }
  49. private static string[] GetStringList(string key, string[] defValue)
  50. {
  51. var s = SettingsCollection[key];
  52. return s == null ? defValue : s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  53. }
  54. private static bool GetBoolean(string key, bool defValue)
  55. {
  56. bool aux;
  57. var s = SettingsCollection[key];
  58. if (s == null || !bool.TryParse(s, out aux))
  59. aux = defValue;
  60. return aux;
  61. }
  62. private static int GetInt32(string key, int defValue)
  63. {
  64. int aux;
  65. var s = SettingsCollection[key];
  66. if (s == null || !int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out aux))
  67. aux = defValue;
  68. return aux;
  69. }
  70. public static float GetFloat(string key, float defValue)
  71. {
  72. float aux;
  73. var s = SettingsCollection[key];
  74. if (s == null || !float.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out aux))
  75. aux = defValue;
  76. return aux;
  77. }
  78. private static T GetEnum<T>(string key, T defValue)
  79. {
  80. object aux;
  81. var s = SettingsCollection[key];
  82. if (s == null)
  83. aux = defValue;
  84. else
  85. {
  86. int value;
  87. if (!int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
  88. aux = defValue;
  89. else
  90. aux = value;
  91. }
  92. return (T)aux;
  93. }
  94. }
  95. }