ConfigFile.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 
  2. using UnityEngine;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. namespace SC.XR.Unity {
  6. public class ConfigFile {
  7. private static readonly char[] TrimStart = new char[] { ' ', '\t' };
  8. private static readonly char[] TrimEnd = new char[] { ' ', '\t', '\r', '\n' };
  9. private const string DELEMITER = "=";
  10. private string strFilePath = null;
  11. private bool IsCaseSensitive = false;
  12. private Dictionary<string, Dictionary<string, string>> ConfigDic = new Dictionary<string, Dictionary<string, string>>();
  13. public ConfigFile(string path, bool isCaseSensitive = false) {
  14. strFilePath = path;
  15. IsCaseSensitive = isCaseSensitive;
  16. }
  17. public void ParseConfig() {
  18. if (!File.Exists(strFilePath)) {
  19. Debug.LogWarning("the Config file's path is error:" + strFilePath);
  20. return;
  21. }
  22. using (StreamReader reader = new StreamReader(strFilePath)) {
  23. string section = null;
  24. string key = null;
  25. string val = null;
  26. Dictionary<string, string> config = null;
  27. string strLine = null;
  28. while ((strLine = reader.ReadLine()) != null) {
  29. strLine = strLine.TrimStart(TrimStart);
  30. strLine = strLine.TrimEnd(TrimEnd);
  31. if (strLine.StartsWith("#") || strLine.StartsWith("//")) {
  32. continue;
  33. }
  34. if (strLine.Length <= 2) {
  35. continue;
  36. }
  37. if (TryParseSection(strLine, out section)) {
  38. if (!ConfigDic.ContainsKey(section)) {
  39. ConfigDic.Add(section, new Dictionary<string, string>());
  40. }
  41. config = ConfigDic[section];
  42. } else {
  43. if (config != null) {
  44. if (TryParseConfig(strLine, out key, out val)) {
  45. if (config.ContainsKey(key)) {
  46. config[key] = val;
  47. Debug.LogError("the Key[" + key + "] is appear repeat");
  48. } else {
  49. config.Add(key, val);
  50. Debug.Log(key + ":" + val);
  51. }
  52. }
  53. } else {
  54. Debug.LogWarning("the Config file's format is error,lost [Section]'s information");
  55. }
  56. }
  57. }
  58. }
  59. }
  60. public void SaveConfig() {
  61. if (string.IsNullOrEmpty(strFilePath)) {
  62. Debug.LogWarning("Empty file name for SaveConfig.");
  63. return;
  64. }
  65. string dirName = Path.GetDirectoryName(strFilePath);
  66. if (string.IsNullOrEmpty(dirName)) {
  67. Debug.LogWarning(string.Format("Empty directory for SaveConfig:{0}.", strFilePath));
  68. return;
  69. }
  70. if (!Directory.Exists(dirName)) {
  71. Directory.CreateDirectory(dirName);
  72. }
  73. using (StreamWriter sw = new StreamWriter(strFilePath)) {
  74. foreach (KeyValuePair<string, Dictionary<string, string>> pair in ConfigDic) {
  75. sw.WriteLine("[" + pair.Key + "]");
  76. foreach (KeyValuePair<string, string> cfg in pair.Value) {
  77. sw.WriteLine(cfg.Key + DELEMITER + cfg.Value);
  78. }
  79. }
  80. }
  81. }
  82. public string GetString(string section, string key, string defaultVal) {
  83. if (!IsCaseSensitive) {
  84. section = section.ToUpper();
  85. key = key.ToUpper();
  86. }
  87. Dictionary<string, string> config = null;
  88. if (ConfigDic.TryGetValue(section, out config)) {
  89. string ret = null;
  90. if (config.TryGetValue(key, out ret)) {
  91. return ret;
  92. }
  93. }
  94. return defaultVal;
  95. }
  96. public int GetInt(string section, string key, int defaultVal) {
  97. string val = GetString(section, key, null);
  98. if (val != null) {
  99. return int.Parse(val);
  100. }
  101. return defaultVal;
  102. }
  103. public void SetString(string section, string key, string val) {
  104. if (!string.IsNullOrEmpty(section) && !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(val)) {
  105. if (!IsCaseSensitive) {
  106. section = section.ToUpper();
  107. key = key.ToUpper();
  108. }
  109. Dictionary<string, string> config = null;
  110. if (!ConfigDic.TryGetValue(section, out config)) {
  111. config = new Dictionary<string, string>();
  112. ConfigDic[section] = config;
  113. }
  114. config[key] = val;
  115. }
  116. }
  117. public void SetInt(string section, string key, int val) {
  118. SetString(section, key, val.ToString());
  119. }
  120. public void AddString(string section, string key, string val) {
  121. if (!string.IsNullOrEmpty(section) && !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(val)) {
  122. if (!IsCaseSensitive) {
  123. section = section.ToUpper();
  124. key = key.ToUpper();
  125. }
  126. Dictionary<string, string> config = null;
  127. if (!ConfigDic.TryGetValue(section, out config)) {
  128. config = new Dictionary<string, string>();
  129. ConfigDic[section] = config;
  130. }
  131. if (!config.ContainsKey(key)) {
  132. config.Add(key, val);
  133. }
  134. }
  135. }
  136. public void AddInt(string section, string key, int val) {
  137. AddString(section, key, val.ToString());
  138. }
  139. public bool RemoveSection(string section) {
  140. if (ConfigDic.ContainsKey(section)) {
  141. ConfigDic.Remove(section);
  142. return true;
  143. }
  144. return false;
  145. }
  146. public bool RemoveConfig(string section, string key) {
  147. if (!IsCaseSensitive) {
  148. section = section.ToUpper();
  149. key = key.ToUpper();
  150. }
  151. Dictionary<string, string> config = null;
  152. if (ConfigDic.TryGetValue(section, out config)) {
  153. if (config.ContainsKey(key)) {
  154. config.Remove(key);
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. public Dictionary<string, string> GetSectionInfo(string section) {
  161. Dictionary<string, string> res = null;
  162. if (!IsCaseSensitive) {
  163. section = section.ToUpper();
  164. }
  165. ConfigDic.TryGetValue(section, out res);
  166. return res;
  167. }
  168. private bool TryParseSection(string strLine, out string section) {
  169. section = null;
  170. if (!string.IsNullOrEmpty(strLine)) {
  171. int len = strLine.Length;
  172. if (strLine[0] == '[' && strLine[len - 1] == ']') {
  173. section = strLine.Substring(1, len - 2);
  174. if (!IsCaseSensitive) {
  175. section = section.ToUpper();
  176. }
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. private bool TryParseConfig(string strLine, out string key, out string val) {
  183. if (strLine != null && strLine.Length >= 3) {
  184. string[] contents = strLine.Split(DELEMITER.ToCharArray());
  185. if (contents.Length == 2) {
  186. key = contents[0].TrimStart(TrimStart);
  187. key = key.TrimEnd(TrimEnd);
  188. val = contents[1].TrimStart(TrimStart);
  189. val = val.TrimEnd(TrimEnd);
  190. if (key.Length > 0 && val.Length > 0) {
  191. if (!IsCaseSensitive) {
  192. key = key.ToUpper();
  193. }
  194. return true;
  195. }
  196. }
  197. }
  198. key = null;
  199. val = null;
  200. return false;
  201. }
  202. }
  203. }