/WowPacketParser/Misc/Settings.cs

https://bitbucket.org/ZaxarPal/wowpacketparser · C# · 164 lines · 136 code · 23 blank · 5 comment · 31 complexity · 55bb62715079bb9f5c998d6cf25275f0 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  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 KeyValueConfigurationCollection SettingsCollection = GetConfiguration();
  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 DumpFormatType DumpFormat = GetEnum("DumpFormat", DumpFormatType.Text);
  20. public static readonly SQLOutputFlags SQLOutput = GetEnum("SQLOutput", SQLOutputFlags.None);
  21. public static readonly string SQLFileName = GetString("SQLFileName", string.Empty);
  22. public static readonly bool ShowEndPrompt = GetBoolean("ShowEndPrompt", false);
  23. public static readonly bool LogErrors = GetBoolean("LogErrors", false);
  24. public static readonly bool LogPacketErrors = GetBoolean("LogPacketErrors", false);
  25. public static readonly bool DebugReads = GetBoolean("DebugReads", false);
  26. public static readonly bool SplitOutput = GetBoolean("SplitOutput", false);
  27. public static readonly bool ParsingLog = GetBoolean("ParsingLog", false);
  28. public static readonly bool SSHEnabled = GetBoolean("SSHEnabled", false);
  29. public static readonly string SSHHost = GetString("SSHHost", "localhost");
  30. public static readonly string SSHUsername = GetString("SSHUsername", string.Empty);
  31. public static readonly string SSHPassword = GetString("SSHPassword", string.Empty);
  32. public static readonly int SSHPort = GetInt32("SSHPort", 22);
  33. public static readonly int SSHLocalPort = GetInt32("SSHLocalPort", 3307);
  34. public static readonly bool DBEnabled = GetBoolean("DBEnabled", false);
  35. public static readonly string Server = GetString("Server", "localhost");
  36. public static readonly string Port = GetString("Port", "3306");
  37. public static readonly string Username = GetString("Username", "root");
  38. public static readonly string Password = GetString("Password", string.Empty);
  39. public static readonly string WPPDatabase = GetString("WPPDatabase", "WPP");
  40. public static readonly string TDBDatabase = GetString("TDBDatabase", "world");
  41. public static readonly string CharacterSet = GetString("CharacterSet", "utf8");
  42. private static KeyValueConfigurationCollection GetConfiguration()
  43. {
  44. var args = Environment.GetCommandLineArgs();
  45. var opts = new Dictionary<string, string>();
  46. string configFile = null;
  47. KeyValueConfigurationCollection settings = null;
  48. for (var i = 1; i < args.Length - 1; ++i)
  49. {
  50. var opt = args[i];
  51. if (opt[0] != '/')
  52. break;
  53. // analyze options
  54. var optname = opt.Substring(1);
  55. switch (optname)
  56. {
  57. case "ConfigFile":
  58. configFile = args[i + 1];
  59. break;
  60. default:
  61. opts.Add(optname, args[i + 1]);
  62. break;
  63. }
  64. ++i;
  65. }
  66. // load different config file
  67. if (configFile != null)
  68. {
  69. string configPath = System.IO.Path.Combine(Environment.CurrentDirectory, configFile);
  70. try
  71. {
  72. // Map the new configuration file.
  73. var configFileMap = new ExeConfigurationFileMap {ExeConfigFilename = configPath};
  74. // Get the mapped configuration file
  75. var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
  76. settings = ((AppSettingsSection)config.GetSection("appSettings")).Settings;
  77. }
  78. catch (Exception ex)
  79. {
  80. Console.WriteLine("Could not load config file {0}, reason: {1}", configPath, ex.Message);
  81. }
  82. }
  83. if (settings == null)
  84. settings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings;
  85. // override config options with options from command line
  86. foreach(var pair in opts)
  87. {
  88. settings.Remove(pair.Key);
  89. settings.Add(pair.Key, pair.Value);
  90. }
  91. return settings;
  92. }
  93. private static string GetString(string key, string defValue)
  94. {
  95. var s = SettingsCollection[key];
  96. return (s == null || s.Value == null) ? defValue : s.Value;
  97. }
  98. private static string[] GetStringList(string key, string[] defValue)
  99. {
  100. var s = SettingsCollection[key];
  101. if (s == null || s.Value == null)
  102. return defValue;
  103. var arr = s.Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  104. for (var i = 0; i < arr.Length; i++)
  105. arr[i] = arr[i].Trim();
  106. return arr;
  107. }
  108. private static bool GetBoolean(string key, bool defValue)
  109. {
  110. bool aux;
  111. var s = SettingsCollection[key];
  112. if ((s == null || s.Value == null) || !bool.TryParse(s.Value, out aux))
  113. aux = defValue;
  114. return aux;
  115. }
  116. private static int GetInt32(string key, int defValue)
  117. {
  118. int aux;
  119. var s = SettingsCollection[key];
  120. if ((s == null || s.Value == null) || !int.TryParse(s.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out aux))
  121. aux = defValue;
  122. return aux;
  123. }
  124. private static T GetEnum<T>(string key, T defValue)
  125. {
  126. object aux;
  127. var s = SettingsCollection[key];
  128. if ((s == null || s.Value == null))
  129. aux = defValue;
  130. else
  131. {
  132. int value;
  133. if (!int.TryParse(s.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
  134. aux = defValue;
  135. else
  136. aux = value;
  137. }
  138. return (T)aux;
  139. }
  140. }
  141. }