ConfigFile.cs 9.9 KB

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