ConfigFile.cs 11 KB

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