123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CSVTable : IEnumerable
- {
- /// <summary>
- /// 获取表名
- /// </summary>
- public string Name { get { return _name; } }
- private string _name;
- /// <summary>
- /// 获取表中的所有属性键
- /// </summary>
- public List<string> AtrributeKeys { get { return _atrributeKeys; } }
- private List<string> _atrributeKeys;
- /// <summary>
- /// 存储表中所有数据对象
- /// </summary>
- private Dictionary<string, CSVDataObject> _dataObjDic;
- /// <summary>
- /// 构造方法
- /// </summary>
- /// <param name="tableName"> 表名 </param>
- public CSVTable(string tableName, string[] attributeKeys)
- {
- _name = tableName;
- // init
- _atrributeKeys = new List<string>(attributeKeys);
- _dataObjDic = new Dictionary<string, CSVDataObject>();
- }
- /// <summary>
- /// 获取数据表对象的签名,用于比较是否与数据对象的签名一致
- /// </summary>
- /// <returns> 数据表对象的签名 </returns>
- public string GetFormat()
- {
- string format = string.Empty;
- foreach (string key in _atrributeKeys)
- {
- format += (key + "-");
- }
- return format;
- }
- /// <summary>
- /// 提供类似于键值对的访问方式便捷获取和设置数据对象
- /// </summary>
- /// <param name="key"> 数据对象主键 </param>
- /// <returns> 数据对象 </returns>
- public CSVDataObject this[string dataMajorKey]
- {
- get { return GetDataObject(dataMajorKey); }
- set { AddDataObject(dataMajorKey, value); }
- }
- /// <summary>
- /// 添加数据对象, 并将数据对象主键添加到主键集合中
- /// </summary>
- /// <param name="dataMajorKey"> 数据对象主键 </param>
- /// <param name="value"> 数据对象 </param>
- private void AddDataObject(string dataMajorKey, CSVDataObject value)
- {
- if (dataMajorKey != value.ID)
- {
- Debug.LogError("所设对象的主键值与给定主键值不同!设置失败!");
- return;
- }
- if (value.GetFormat() != GetFormat())
- {
- Debug.LogError("所设对象的的签名与表的签名不同!设置失败!");
- return;
- }
- if (_dataObjDic.ContainsKey(dataMajorKey))
- {
- Debug.LogError("表中已经存在主键为 '" + dataMajorKey + "' 的对象!设置失败!");
- return;
- }
- _dataObjDic.Add(dataMajorKey, value);
- }
- /// <summary>
- /// 通过数据对象主键获取数据对象
- /// </summary>
- /// <param name="dataMajorKey"> 数据对象主键 </param>
- /// <returns> 数据对象 </returns>
- private CSVDataObject GetDataObject(string dataMajorKey)
- {
- CSVDataObject data = null;
- if (_dataObjDic.ContainsKey(dataMajorKey))
- data = _dataObjDic[dataMajorKey];
- else
- Debug.LogError("The table not include data of this key.");
- return data;
- }
- /// <summary>
- /// 根据数据对象主键删除对应数据对象
- /// </summary>
- /// <param name="dataMajorKey"> 数据对象主键 </param>
- public void DeleteDataObject(string dataMajorKey)
- {
- if (_dataObjDic.ContainsKey(dataMajorKey))
- _dataObjDic.Remove(dataMajorKey);
- else
- Debug.LogError("The table not include the key.");
- }
- /// <summary>
- /// 删除所有所有数据对象
- /// </summary>
- public void DeleteAllDataObject()
- {
- _dataObjDic.Clear();
- }
- /// <summary>
- /// 获取数据表对象的文本内容
- /// </summary>
- /// <returns> 数据表文本内容 </returns>
- public string GetContent()
- {
- string content = string.Empty;
- foreach (string key in _atrributeKeys)
- {
- content += (key + ",").Trim();
- }
- content = content.Remove(content.Length - 1);
- if (_dataObjDic.Count == 0)
- {
- Debug.LogWarning("The table is empty, fuction named 'GetContent()' will just retrun key's list.");
- return content;
- }
- foreach (CSVDataObject data in _dataObjDic.Values)
- {
- content += "\n" + data.ID + ",";
- foreach (KeyValuePair<string, string> item in data)
- {
- content += (item.Value + ",").Trim();
- }
- content = content.Remove(content.Length - 1);
- }
- return content;
- }
- /// <summary>
- /// 迭代表中所有数据对象
- /// </summary>
- /// <returns> 数据对象 </returns>
- public IEnumerator GetEnumerator()
- {
- if (_dataObjDic == null)
- {
- Debug.LogWarning("The table is empty.");
- yield break;
- }
- foreach (var data in _dataObjDic.Values)
- {
- yield return data;
- }
- }
- /// <summary>
- /// 获得数据表内容
- /// </summary>
- /// <returns> 数据表内容 </returns>
- public override string ToString()
- {
- string content = string.Empty;
- foreach (var data in _dataObjDic.Values)
- {
- content += data.ToString() + "\n";
- }
- return content;
- }
- /// <summary>
- /// 通过数据表名字和数据表文本内容构造一个数据表对象
- /// </summary>
- /// <param name="tableName"> 数据表名字 </param>
- /// <param name="tableContent"> 数据表文本内容 </param>
- /// <returns> 数据表对象 </returns>
- public static CSVTable CreateTable(string tableName, string tableContent)
- {
- string content = tableContent.Replace("\r", "");
- string[] lines = content.Split('\n');
- if (lines.Length < 2)
- {
- Debug.LogError("The csv file is not csv table format.");
- return null;
- }
- string keyLine = lines[0];
- string[] keys = keyLine.Split(',');
- CSVTable table = new CSVTable(tableName, keys);
- for (int i = 1; i < lines.Length; i++)
- {
- string[] values = lines[i].Split(',');
- string major = values[0].Trim();
- Dictionary<string, string> tempAttributeDic = new Dictionary<string, string>();
- for (int j = 1; j < values.Length; j++)
- {
- string key = keys[j].Trim();
-
- string value = values[j].Trim();
- tempAttributeDic.Add(key, value);
- }
- CSVDataObject dataObj = new CSVDataObject(major, tempAttributeDic, keys);
- table[dataObj.ID] = dataObj;
- }
- return table;
- }
- }
|