using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CSVTable : IEnumerable { /// /// 获取表名 /// public string Name { get { return _name; } } private string _name; /// /// 获取表中的所有属性键 /// public List AtrributeKeys { get { return _atrributeKeys; } } private List _atrributeKeys; /// /// 存储表中所有数据对象 /// private Dictionary _dataObjDic; /// /// 构造方法 /// /// 表名 public CSVTable(string tableName, string[] attributeKeys) { _name = tableName; // init _atrributeKeys = new List(attributeKeys); _dataObjDic = new Dictionary(); } /// /// 获取数据表对象的签名,用于比较是否与数据对象的签名一致 /// /// 数据表对象的签名 public string GetFormat() { string format = string.Empty; foreach (string key in _atrributeKeys) { format += (key + "-"); } return format; } /// /// 提供类似于键值对的访问方式便捷获取和设置数据对象 /// /// 数据对象主键 /// 数据对象 public CSVDataObject this[string dataMajorKey] { get { return GetDataObject(dataMajorKey); } set { AddDataObject(dataMajorKey, value); } } /// /// 添加数据对象, 并将数据对象主键添加到主键集合中 /// /// 数据对象主键 /// 数据对象 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); } /// /// 通过数据对象主键获取数据对象 /// /// 数据对象主键 /// 数据对象 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; } /// /// 根据数据对象主键删除对应数据对象 /// /// 数据对象主键 public void DeleteDataObject(string dataMajorKey) { if (_dataObjDic.ContainsKey(dataMajorKey)) _dataObjDic.Remove(dataMajorKey); else Debug.LogError("The table not include the key."); } /// /// 删除所有所有数据对象 /// public void DeleteAllDataObject() { _dataObjDic.Clear(); } /// /// 获取数据表对象的文本内容 /// /// 数据表文本内容 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 item in data) { content += (item.Value + ",").Trim(); } content = content.Remove(content.Length - 1); } return content; } /// /// 迭代表中所有数据对象 /// /// 数据对象 public IEnumerator GetEnumerator() { if (_dataObjDic == null) { Debug.LogWarning("The table is empty."); yield break; } foreach (var data in _dataObjDic.Values) { yield return data; } } /// /// 获得数据表内容 /// /// 数据表内容 public override string ToString() { string content = string.Empty; foreach (var data in _dataObjDic.Values) { content += data.ToString() + "\n"; } return content; } /// /// 通过数据表名字和数据表文本内容构造一个数据表对象 /// /// 数据表名字 /// 数据表文本内容 /// 数据表对象 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 tempAttributeDic = new Dictionary(); 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; } }