Settings.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using log4net;
  32. namespace OpenSim.GridLaunch
  33. {
  34. internal class Settings
  35. {
  36. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  37. private Dictionary<string, string> Config = new Dictionary<string, string>();
  38. public Dictionary<string, bool> Components = new Dictionary<string, bool>();
  39. public static string[] defaultComponents = new string[]
  40. {
  41. "OpenSim.Grid.UserServer.exe",
  42. "OpenSim.Grid.GridServer.exe",
  43. "OpenSim.Grid.AssetServer.exe",
  44. "OpenSim.Grid.InventoryServer.exe",
  45. "OpenSim.Grid.MessagingServer.exe",
  46. "OpenSim.32BitLaunch.exe"
  47. };
  48. private static readonly char[] confSplit = new char[] { '=' };
  49. private static readonly char[] comaSplit = new char[] { ',' };
  50. private static readonly char[] colonSplit = new char[] { ';' };
  51. private string configFile = "";
  52. public Settings()
  53. {
  54. }
  55. public Settings(string ConfigFile)
  56. {
  57. LoadConfig(ConfigFile);
  58. }
  59. public void LoadConfig(string ConfigFile)
  60. {
  61. configFile = ConfigFile;
  62. m_log.Info("Reading config file: " + ConfigFile);
  63. try
  64. {
  65. // Read config file
  66. foreach (string line in System.IO.File.ReadAllLines(ConfigFile))
  67. {
  68. string[] s = line.Split(confSplit, 2);
  69. if (s.Length >= 2)
  70. Config.Add(s[0], s[1]);
  71. }
  72. // Process Components section
  73. string cmp = Config["Components"];
  74. Config.Remove("Components");
  75. foreach (string c in cmp.Split(comaSplit))
  76. {
  77. string[] cs = c.Split(colonSplit);
  78. if (cs.Length >= 2)
  79. {
  80. bool status = false;
  81. bool.TryParse(cs[1], out status);
  82. Components.Add(cs[0], status);
  83. }
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. m_log.Error("Exception reading config file: " + ex.ToString());
  89. }
  90. // No components? Add default components
  91. if (Components.Count == 0)
  92. foreach (string c in defaultComponents)
  93. {
  94. Components.Add(c, true);
  95. }
  96. }
  97. public void SaveConfig(string ConfigFile)
  98. {
  99. configFile = ConfigFile;
  100. SaveConfig();
  101. }
  102. public void SaveConfig()
  103. {
  104. m_log.Info("Writing config file: " + configFile);
  105. try
  106. {
  107. System.IO.File.WriteAllText(configFile, ToString());
  108. }
  109. catch (Exception ex)
  110. {
  111. m_log.Error("Exception writing config file: " + ex.ToString());
  112. }
  113. }
  114. public new string ToString()
  115. {
  116. StringBuilder ret = new StringBuilder();
  117. Dictionary<string, string> config = new Dictionary<string, string>(Config);
  118. // Add Components key
  119. StringBuilder _Components = new StringBuilder();
  120. foreach (string c in Components.Keys)
  121. {
  122. if (_Components.Length > 0)
  123. _Components.Append(",");
  124. _Components.Append(c + ";" + Components[c].ToString());
  125. }
  126. config["Components"] = _Components.ToString();
  127. // Make return string
  128. foreach (string key in config.Keys)
  129. {
  130. ret.AppendLine(key + "=" + config[key]);
  131. }
  132. // Return it
  133. return ret.ToString();
  134. }
  135. public string this[string Key]
  136. {
  137. get
  138. {
  139. if (Config.ContainsKey(Key))
  140. return Config[Key];
  141. return "";
  142. }
  143. set { Config[Key] = value; }
  144. }
  145. public void ParseCommandArguments(string[] args)
  146. {
  147. string key = null;
  148. foreach (string a in args)
  149. {
  150. if (a.StartsWith("--"))
  151. key = a.Remove(0, 2);
  152. else
  153. {
  154. if (key != null)
  155. Config[key] = a;
  156. key = null;
  157. }
  158. }
  159. }
  160. }
  161. }