DataTable.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Text;
  6. public class DataTable : Dictionary<string, SingleData>
  7. {
  8. const char c_split = '\t';
  9. const string c_newline = "\r\n";
  10. const string c_defaultValueTableTitle = "default";
  11. const string c_noteTableTitle = "note";
  12. const string c_fieldTypeTableTitle = "type";
  13. const char c_EnumSplit = '|';
  14. const char c_DataFieldAssetTypeSplit = '&';
  15. public string m_tableName;
  16. /// <summary>
  17. /// 默认值
  18. /// </summary>
  19. public Dictionary<string, string> m_defaultValue = new Dictionary<string,string>();
  20. /// <summary>
  21. /// 注释
  22. /// </summary>
  23. public Dictionary<string, string> m_noteValue = new Dictionary<string, string>();
  24. /// <summary>
  25. /// 储存每个字段是什么类型
  26. /// </summary>
  27. public Dictionary<string, FieldType> m_tableTypes = new Dictionary<string,FieldType>();
  28. /// <summary>
  29. /// 如果是枚举类型,这里储存二级类型
  30. /// </summary>
  31. public Dictionary<string, string> m_tableEnumTypes = new Dictionary<string, string>();
  32. /// <summary>
  33. /// 单条记录所拥有的字段名
  34. /// </summary>
  35. public List<string> TableKeys = new List<string>();
  36. /// <summary>
  37. /// 数据所有的Key
  38. /// </summary>
  39. public List<string> TableIDs = new List<string>();
  40. /// <summary>
  41. /// 字段的用途区分
  42. /// </summary>
  43. public Dictionary<string, DataFieldAssetType> m_fieldAssetTypes = new Dictionary<string, DataFieldAssetType>();
  44. /// <summary>
  45. /// 将文本解析为表单数据
  46. /// </summary>
  47. /// <param name="stringData">文本</param>
  48. /// <returns>表单数据</returns>
  49. public static DataTable Analysis(string stringData)
  50. {
  51. string debugContent = "";
  52. int debugLineCount = 0;
  53. int debugRowCount = 0;
  54. string debugKey = "";
  55. string debugProperty = "";
  56. try
  57. {
  58. int lineIndex = 0;
  59. DataTable data = new DataTable();
  60. string[] line = stringData.Split(c_newline.ToCharArray());
  61. //第一行作为Key
  62. debugContent = "解析Key";
  63. data.TableKeys = new List<string>();
  64. string[] rowKeys = ConvertStringArray(line[0]);
  65. for (int i = 0; i < rowKeys.Length; i++)
  66. {
  67. debugRowCount = i;
  68. if (!rowKeys[i].Equals(""))
  69. {
  70. data.TableKeys.Add(rowKeys[i]);
  71. }
  72. }
  73. string[] LineData;
  74. for (lineIndex = 1; lineIndex < line.Length; lineIndex++)
  75. {
  76. if (line[lineIndex] != "" && line[lineIndex] != null)
  77. {
  78. debugLineCount = lineIndex;
  79. LineData = ConvertStringArray(line[lineIndex]);
  80. //注释
  81. if (LineData[0].Equals(c_noteTableTitle))
  82. {
  83. debugContent = "解析注释";
  84. AnalysisNoteValue(data, LineData);
  85. }
  86. //默认值
  87. else if (LineData[0].Equals(c_defaultValueTableTitle))
  88. {
  89. debugContent = "解析默认值";
  90. AnalysisDefaultValue(data, LineData);
  91. }
  92. //数据类型
  93. else if (LineData[0].Equals(c_fieldTypeTableTitle))
  94. {
  95. debugContent = "解析类型";
  96. AnalysisFieldType(data, LineData);
  97. }
  98. //数据正文
  99. else
  100. {
  101. debugContent = "解析正文";
  102. break;
  103. }
  104. }
  105. }
  106. data.TableIDs = new List<string>();
  107. //开始解析数据
  108. for (int i = lineIndex; i < line.Length; i++)
  109. {
  110. debugLineCount = i;
  111. SingleData dataTmp = new SingleData();
  112. dataTmp.data = data;
  113. if (line[i] != "" && line[i] != null)
  114. {
  115. string[] row = ConvertStringArray(line[i]);
  116. for (int j = 0; j < data.TableKeys.Count; j++)
  117. {
  118. debugRowCount = j;
  119. debugKey = row[0];
  120. if (!row[j].Equals(""))
  121. {
  122. debugProperty = data.TableKeys[j];
  123. dataTmp.Add(data.TableKeys[j], row[j]);
  124. }
  125. }
  126. //第一个数据作为这一个记录的Key
  127. data.AddData(dataTmp);
  128. }
  129. }
  130. return data;
  131. }
  132. catch (Exception e)
  133. {
  134. throw new Exception("DataTable Analysis Error: 错误位置:" + debugContent + " 行:" + debugLineCount / 2 + " 列:" + debugRowCount + " key:->" + debugKey + "<- PropertyName:->" +debugProperty+ "<-\n" + e.ToString()); // throw
  135. }
  136. }
  137. /// <summary>
  138. /// 解析注释
  139. /// </summary>
  140. /// <param name="l_data"></param>
  141. /// <param name="l_lineData"></param>
  142. public static void AnalysisNoteValue(DataTable l_data, string[] l_lineData)
  143. {
  144. l_data.m_noteValue = new Dictionary<string, string>();
  145. for (int i = 0; i < l_lineData.Length && i < l_data.TableKeys.Count; i++)
  146. {
  147. if (!l_lineData[i].Equals(""))
  148. {
  149. l_data.m_noteValue.Add(l_data.TableKeys[i], l_lineData[i]);
  150. }
  151. }
  152. }
  153. public static void AnalysisDefaultValue(DataTable l_data,string[] l_lineData)
  154. {
  155. l_data.m_defaultValue = new Dictionary<string, string>();
  156. for (int i = 0; i < l_lineData.Length && i < l_data.TableKeys.Count; i++)
  157. {
  158. if (!l_lineData[i].Equals(""))
  159. {
  160. l_data.m_defaultValue.Add(l_data.TableKeys[i], l_lineData[i]);
  161. }
  162. }
  163. }
  164. public static void AnalysisFieldType(DataTable l_data, string[] l_lineData)
  165. {
  166. l_data.m_tableTypes = new Dictionary<string, FieldType>();
  167. for (int i = 1; i < l_lineData.Length && i < l_data.TableKeys.Count; i++)
  168. {
  169. if (!l_lineData[i].Equals(""))
  170. {
  171. string field = l_data.TableKeys[i];
  172. string[] tempType = l_lineData[i].Split(c_DataFieldAssetTypeSplit);
  173. string[] content = tempType[0].Split(c_EnumSplit);
  174. try
  175. {
  176. l_data.m_tableTypes.Add(field, (FieldType)Enum.Parse(typeof(FieldType), content[0]));
  177. if (content.Length > 1)
  178. {
  179. l_data.m_tableEnumTypes.Add(field, content[1]);
  180. }
  181. }
  182. catch(Exception e)
  183. {
  184. throw new Exception("AnalysisFieldType Exception: " + content + "\n" + e.ToString());
  185. }
  186. if (tempType.Length > 1)
  187. {
  188. l_data.m_fieldAssetTypes.Add(field, (DataFieldAssetType)Enum.Parse(typeof(DataFieldAssetType), tempType[1]));
  189. }
  190. else
  191. {
  192. l_data.m_fieldAssetTypes.Add(field, DataFieldAssetType.Data);
  193. }
  194. }
  195. }
  196. }
  197. public static string Serialize(DataTable data)
  198. {
  199. StringBuilder build = new StringBuilder();
  200. //key
  201. for (int i = 0; i < data.TableKeys.Count; i++)
  202. {
  203. build.Append(data.TableKeys[i]);
  204. if (i != data.TableKeys.Count - 1)
  205. {
  206. build.Append(c_split);
  207. }
  208. else
  209. {
  210. build.Append(c_newline);
  211. }
  212. }
  213. //type
  214. List<string> type = new List<string>(data.m_tableTypes.Keys);
  215. //Debug.Log("type count " + type.Count);
  216. build.Append(c_fieldTypeTableTitle);
  217. if (type.Count > 0)
  218. {
  219. build.Append(c_split);
  220. for (int i = 1; i < data.TableKeys.Count; i++)
  221. {
  222. string key = data.TableKeys[i];
  223. string typeString = "";
  224. if (data.m_tableTypes.ContainsKey(key))
  225. {
  226. typeString = data.m_tableTypes[key].ToString();
  227. if (data.m_tableEnumTypes.ContainsKey(key))
  228. {
  229. typeString += c_EnumSplit + data.m_tableEnumTypes[key];
  230. }
  231. }
  232. //默认字符类型
  233. else
  234. {
  235. typeString = FieldType.String.ToString();
  236. }
  237. if (data.m_fieldAssetTypes.ContainsKey(key))
  238. {
  239. if (data.m_fieldAssetTypes[key] != DataFieldAssetType.Data)
  240. typeString += "&" + data.m_fieldAssetTypes[key];
  241. }
  242. build.Append(typeString);
  243. if (i != data.TableKeys.Count - 1)
  244. {
  245. build.Append(c_split);
  246. }
  247. else
  248. {
  249. build.Append(c_newline);
  250. }
  251. }
  252. }
  253. else
  254. {
  255. build.Append(c_newline);
  256. }
  257. //note
  258. List<string> noteValue = new List<string>(data.m_noteValue.Keys);
  259. build.Append(c_noteTableTitle);
  260. if (noteValue.Count > 0)
  261. {
  262. build.Append(c_split);
  263. for (int i = 1; i < data.TableKeys.Count; i++)
  264. {
  265. string key = data.TableKeys[i];
  266. string defauleNoteTmp = "";
  267. if (data.m_noteValue.ContainsKey(key))
  268. {
  269. defauleNoteTmp = data.m_noteValue[key];
  270. }
  271. else
  272. {
  273. defauleNoteTmp = "";
  274. }
  275. build.Append(defauleNoteTmp);
  276. if (i != data.TableKeys.Count - 1)
  277. {
  278. build.Append(c_split);
  279. }
  280. else
  281. {
  282. build.Append(c_newline);
  283. }
  284. }
  285. }
  286. else
  287. {
  288. build.Append(c_newline);
  289. }
  290. //defauleValue
  291. List<string> defaultValue = new List<string>(data.m_defaultValue.Keys);
  292. build.Append(c_defaultValueTableTitle);
  293. if (defaultValue.Count >0)
  294. {
  295. build.Append(c_split);
  296. for (int i = 1; i < data.TableKeys.Count; i++)
  297. {
  298. string key = data.TableKeys[i];
  299. string defauleValueTmp = "";
  300. if (data.m_defaultValue.ContainsKey(key))
  301. {
  302. defauleValueTmp = data.m_defaultValue[key];
  303. }
  304. else
  305. {
  306. defauleValueTmp = "";
  307. }
  308. build.Append(defauleValueTmp);
  309. if (i != data.TableKeys.Count - 1)
  310. {
  311. build.Append(c_split);
  312. }
  313. else
  314. {
  315. build.Append(c_newline);
  316. }
  317. }
  318. }
  319. else
  320. {
  321. build.Append(c_newline);
  322. }
  323. //value
  324. foreach (string k in data.TableIDs)
  325. {
  326. SingleData dataTmp = data[k];
  327. for (int i = 0; i < data.TableKeys.Count; i++)
  328. {
  329. string valueTmp = "";
  330. string field = data.TableKeys[i];
  331. string defaultV="";
  332. if (data.m_defaultValue.ContainsKey(field))
  333. defaultV = data.m_defaultValue[field];
  334. if (dataTmp.ContainsKey(field) && dataTmp[field]!= defaultV)
  335. {
  336. valueTmp = dataTmp[field];
  337. }
  338. build.Append(valueTmp);
  339. if (i != data.TableKeys.Count - 1)
  340. {
  341. build.Append(c_split);
  342. }
  343. else
  344. {
  345. build.Append(c_newline);
  346. }
  347. }
  348. }
  349. return build.ToString();
  350. }
  351. public static string[] ConvertStringArray(string lineContent)
  352. {
  353. List<string> result = new List<string>();
  354. int startIndex = 0;
  355. bool state = true; //逗号状态和引号状态
  356. for (int i = 0; i < lineContent.Length; i++)
  357. {
  358. if (state)
  359. {
  360. if (lineContent[i] == c_split)
  361. {
  362. result.Add(lineContent.Substring(startIndex, i - startIndex));
  363. startIndex = i + 1;
  364. }
  365. else if (lineContent[i] == '\"')
  366. {
  367. //转为引号状态
  368. state = false;
  369. }
  370. }
  371. else
  372. {
  373. if (lineContent[i] == '\"')
  374. {
  375. //转为逗号状态
  376. state = true;
  377. }
  378. }
  379. }
  380. result.Add(lineContent.Substring(startIndex, lineContent.Length - startIndex));
  381. return result.ToArray();
  382. }
  383. public FieldType GetFieldType(string key)
  384. {
  385. //主键只能是String类型
  386. if (key == TableKeys[0])
  387. {
  388. return FieldType.String;
  389. }
  390. if(m_tableTypes.ContainsKey(key))
  391. {
  392. return m_tableTypes[key];
  393. }
  394. else
  395. {
  396. return FieldType.String;
  397. }
  398. }
  399. public void SetFieldType(string key,FieldType type ,string enumType)
  400. {
  401. //主键只能是String类型
  402. if (key == TableKeys[0])
  403. {
  404. return;
  405. }
  406. if (m_tableTypes.ContainsKey(key))
  407. {
  408. m_tableTypes[key] = type;
  409. }
  410. else
  411. {
  412. m_tableTypes.Add(key,type);
  413. }
  414. //存储二级类型
  415. if (enumType != null)
  416. {
  417. if (m_tableEnumTypes.ContainsKey(key))
  418. {
  419. m_tableEnumTypes[key] = enumType;
  420. }
  421. else
  422. {
  423. m_tableEnumTypes.Add(key, enumType);
  424. }
  425. }
  426. }
  427. public void SetAssetTypes(string key, DataFieldAssetType type)
  428. {
  429. //主键只能是String类型
  430. if (key == TableKeys[0])
  431. {
  432. return;
  433. }
  434. if (m_fieldAssetTypes.ContainsKey(key))
  435. {
  436. m_fieldAssetTypes[key] = type;
  437. }
  438. else
  439. {
  440. m_fieldAssetTypes.Add(key, type);
  441. }
  442. }
  443. public SingleData GetLineFromKey(string key)
  444. {
  445. //主键只能是String类型
  446. SingleData _value = null;
  447. if (ContainsKey(key))
  448. _value = this[key];
  449. return _value;
  450. }
  451. public string GetEnumType(string key)
  452. {
  453. if (m_tableEnumTypes.ContainsKey(key))
  454. {
  455. return m_tableEnumTypes[key];
  456. }
  457. else
  458. {
  459. return null;
  460. }
  461. }
  462. public string GetDefault(string key)
  463. {
  464. if(m_defaultValue.ContainsKey(key))
  465. {
  466. return m_defaultValue[key];
  467. }
  468. else
  469. {
  470. return null;
  471. }
  472. }
  473. public void SetDefault(string key,string value)
  474. {
  475. if (!m_defaultValue.ContainsKey(key))
  476. {
  477. m_defaultValue.Add(key, value);
  478. }
  479. else
  480. {
  481. m_defaultValue[key] = value;
  482. }
  483. }
  484. public void SetNote(string key, string note)
  485. {
  486. if (!m_noteValue.ContainsKey(key))
  487. {
  488. m_noteValue.Add(key, note);
  489. }
  490. else
  491. {
  492. m_noteValue[key] = note;
  493. }
  494. }
  495. public string GetNote(string key)
  496. {
  497. if (!m_noteValue.ContainsKey(key))
  498. {
  499. return null;
  500. }
  501. else
  502. {
  503. return m_noteValue[key];
  504. }
  505. }
  506. public void AddData(SingleData data)
  507. {
  508. if(data.ContainsKey(TableKeys[0]))
  509. {
  510. data.m_SingleDataKey = data[TableKeys[0]];
  511. Add(data[TableKeys[0]], data);
  512. TableIDs.Add(data[TableKeys[0]]);
  513. }
  514. else
  515. {
  516. throw new Exception("Add SingleData fail! The dataTable dont have MainKey!");
  517. }
  518. }
  519. public void SetData(SingleData data)
  520. {
  521. //主键
  522. string mainKey = TableKeys[0];
  523. if (data.ContainsKey(mainKey))
  524. {
  525. string key = data[mainKey];
  526. if (ContainsKey(key))
  527. {
  528. this[key] = data;
  529. }
  530. else
  531. {
  532. Add(key, data);
  533. TableIDs.Add(key);
  534. }
  535. }
  536. else
  537. {
  538. throw new Exception("Add SingleData fail! The dataTable dont have MainKeyKey!");
  539. }
  540. }
  541. public void RemoveData(string key)
  542. {
  543. if (ContainsKey(key))
  544. {
  545. Remove(key);
  546. TableIDs.Remove(key);
  547. }
  548. else
  549. {
  550. throw new Exception("Add SingleData fail!");
  551. }
  552. }
  553. }
  554. public class SingleData : Dictionary<string, string>
  555. {
  556. public DataTable data;
  557. /// <summary>
  558. /// 该记录的key
  559. /// </summary>
  560. public string m_SingleDataKey;
  561. public int GetInt(string key)
  562. {
  563. string content = null;
  564. try
  565. {
  566. if (this.ContainsKey(key))
  567. {
  568. content = this[key];
  569. return int.Parse(content);
  570. }
  571. if (data.m_defaultValue.ContainsKey(key))
  572. {
  573. content = data.m_defaultValue[key];
  574. return int.Parse(content);
  575. }
  576. }
  577. catch (Exception e)
  578. {
  579. throw new Exception("SingleData GetInt Error TableName is :->" + data.m_tableName + "<- key : ->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- content: ->" + content + "<- \n" + e.ToString());
  580. }
  581. throw new Exception("Don't Exist Value or DefaultValue TableName is :->" + data.m_tableName + "<- key : ->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<-");// throw
  582. }
  583. public int[] GetIntArray(string key)
  584. {
  585. string content = null;
  586. try
  587. {
  588. if (this.ContainsKey(key))
  589. {
  590. content = this[key];
  591. return ParseTool.String2IntArray(content);
  592. }
  593. if (data.m_defaultValue.ContainsKey(key))
  594. {
  595. content = data.m_defaultValue[key];
  596. return ParseTool.String2IntArray(content);
  597. }
  598. }
  599. catch (Exception e)
  600. {
  601. throw new Exception("SingleData GetIntArray Error TableName is :->" + data.m_tableName + "<- key : ->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- content: ->" + content + "<- \n" + e.ToString());
  602. }
  603. throw new Exception("Don't Exist Value or DefaultValue TableName is :->" + data.m_tableName + "<- key : ->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<-");// throw
  604. }
  605. public float GetFloat(string key)
  606. {
  607. try
  608. {
  609. if (this.ContainsKey(key))
  610. {
  611. return float.Parse(this[key]);
  612. }
  613. if (data.m_defaultValue.ContainsKey(key))
  614. {
  615. return float.Parse(data.m_defaultValue[key]);
  616. }
  617. }
  618. catch (Exception e)
  619. {
  620. throw new Exception("SingleData GetFloat Error TableName is :->" + data.m_tableName + "<- key :->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  621. }
  622. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  623. }
  624. public float[] GetFloatArray(string key)
  625. {
  626. try
  627. {
  628. if (this.ContainsKey(key))
  629. {
  630. return ParseTool.String2FloatArray(this[key]);
  631. }
  632. if (data.m_defaultValue.ContainsKey(key))
  633. {
  634. return ParseTool.String2FloatArray(data.m_defaultValue[key]);
  635. }
  636. }
  637. catch (Exception e)
  638. {
  639. throw new Exception("SingleData GetFloatArray Error TableName is :->" + data.m_tableName + "<- key :->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  640. }
  641. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  642. }
  643. public bool GetBool(string key)
  644. {
  645. string content = null;
  646. try
  647. {
  648. if (this.ContainsKey(key))
  649. {
  650. content = this[key];
  651. return bool.Parse(content);
  652. }
  653. if (data.m_defaultValue.ContainsKey(key))
  654. {
  655. content = data.m_defaultValue[key];
  656. return bool.Parse(content);
  657. }
  658. }
  659. catch (Exception e)
  660. {
  661. throw new Exception("SingleData GetBool Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- content: ->" + content + "<- \n" + e.ToString());
  662. }
  663. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  664. }
  665. public bool[] GetBoolArray(string key)
  666. {
  667. try
  668. {
  669. if (this.ContainsKey(key))
  670. {
  671. return ParseTool.String2BoolArray(this[key]);
  672. }
  673. if (data.m_defaultValue.ContainsKey(key))
  674. {
  675. return ParseTool.String2BoolArray(data.m_defaultValue[key]);
  676. }
  677. }
  678. catch (Exception e)
  679. {
  680. throw new Exception("SingleData GetBoolArray Error TableName is :->" + data.m_tableName + "<- key :->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  681. }
  682. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  683. }
  684. public string GetString(string key)
  685. {
  686. try
  687. {
  688. if (this.ContainsKey(key))
  689. {
  690. //String 读取null 的改进,兼容旧代码
  691. #if Compatibility
  692. return this[key];
  693. #else
  694. return StringFilter(this[key]);
  695. #endif
  696. }
  697. if (data.m_defaultValue.ContainsKey(key))
  698. {
  699. #if Compatibility
  700. return data.m_defaultValue[key];
  701. #else
  702. return StringFilter(data.m_defaultValue[key]);
  703. #endif
  704. }
  705. }
  706. catch (Exception e)
  707. {
  708. throw new Exception("SingleData GetString Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  709. }
  710. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-");// throw
  711. }
  712. string StringFilter(string content)
  713. {
  714. if(content == "Null"
  715. || content == "null"
  716. || content == "NULL"
  717. || content == "nu11"
  718. || content == "none"
  719. || content == "nil"
  720. || content == "")
  721. {
  722. return null;
  723. }
  724. else
  725. {
  726. return content;
  727. }
  728. }
  729. public Vector2 GetVector2(string key)
  730. {
  731. try
  732. {
  733. if (this.ContainsKey(key))
  734. {
  735. return ParseTool.String2Vector2(this[key]);
  736. }
  737. if (data.m_defaultValue.ContainsKey(key))
  738. {
  739. return ParseTool.String2Vector2(data.m_defaultValue[key]);
  740. }
  741. }
  742. catch (Exception e)
  743. {
  744. throw new Exception("SingleData GetVector2 Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  745. }
  746. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  747. }
  748. public Vector2[] GetVector2Array(string key)
  749. {
  750. try
  751. {
  752. if (this.ContainsKey(key))
  753. {
  754. return ParseTool.String2Vector2Array(this[key]);
  755. }
  756. if (data.m_defaultValue.ContainsKey(key))
  757. {
  758. return ParseTool.String2Vector2Array(data.m_defaultValue[key]);
  759. }
  760. }
  761. catch (Exception e)
  762. {
  763. throw new Exception("SingleData GetVector2 Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  764. }
  765. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  766. }
  767. public Vector3[] GetVector3Array(string key)
  768. {
  769. try
  770. {
  771. if (this.ContainsKey(key))
  772. {
  773. return ParseTool.String2Vector3Array(this[key]);
  774. }
  775. if (data.m_defaultValue.ContainsKey(key))
  776. {
  777. return ParseTool.String2Vector3Array(data.m_defaultValue[key]);
  778. }
  779. }
  780. catch (Exception e)
  781. {
  782. throw new Exception("SingleData GetVector3Array Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  783. }
  784. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  785. }
  786. public Vector3 GetVector3(string key)
  787. {
  788. try
  789. {
  790. if (this.ContainsKey(key))
  791. {
  792. return ParseTool.String2Vector3(this[key]);
  793. }
  794. if (data.m_defaultValue.ContainsKey(key))
  795. {
  796. return ParseTool.String2Vector3(data.m_defaultValue[key]);
  797. }
  798. }
  799. catch (Exception e)
  800. {
  801. throw new Exception("SingleData GetVector3 Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  802. }
  803. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  804. }
  805. public Color GetColor(string key)
  806. {
  807. try
  808. {
  809. if (this.ContainsKey(key))
  810. {
  811. return ParseTool.String2Color(this[key]);
  812. }
  813. if (data.m_defaultValue.ContainsKey(key))
  814. {
  815. return ParseTool.String2Color(data.m_defaultValue[key]);
  816. }
  817. }
  818. catch (Exception e)
  819. {
  820. throw new Exception("SingleData GetColor Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  821. }
  822. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  823. }
  824. public T GetEnum<T>(string key) where T : struct
  825. {
  826. try
  827. {
  828. if (this.ContainsKey(key))
  829. {
  830. return (T)Enum.Parse(typeof(T), this[key]);
  831. }
  832. if (data.m_defaultValue.ContainsKey(key))
  833. {
  834. return (T)Enum.Parse(typeof(T), data.m_defaultValue[key]);
  835. }
  836. }
  837. catch (Exception e)
  838. {
  839. throw new Exception("SingleData GetEnum Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  840. }
  841. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-"); // throw
  842. }
  843. public string[] GetStringArray(string key)
  844. {
  845. try
  846. {
  847. if (this.ContainsKey(key))
  848. {
  849. return ParseTool.String2StringArray(this[key]);
  850. }
  851. if (data.m_defaultValue.ContainsKey(key))
  852. {
  853. return ParseTool.String2StringArray(data.m_defaultValue[key]);
  854. }
  855. }
  856. catch (Exception e)
  857. {
  858. throw new Exception("SingleData GetStringArray Error TableName is :->" + data.m_tableName + "<- key->" + key + "<- singleDataName : ->" + m_SingleDataKey + "<- \n" + e.ToString());
  859. }
  860. throw new Exception("Don't Exist Value or DefaultValue by ->" + key + "<- TableName is : ->" + data.m_tableName + "<- singleDataName : ->" + m_SingleDataKey + "<-");// throw
  861. }
  862. }