/// <summary> /// 通过数据对象主键获取数据对象 /// </summary> /// <param name="dataMajorKey"> 数据对象主键 </param> /// <returns> 数据对象 </returns> private CSVObject GetDataObject(string dataMajorKey) { CSVObject data = null; if (_dataObjDic != null) { if (_dataObjDic.ContainsKey(dataMajorKey)) { data = _dataObjDic[dataMajorKey]; } else { Debug.LogError("The table not include data of this key."); } } return(data); }
/// <summary> /// 将csv文本转化为CSVTable /// </summary> /// <param name="tableContent"></param> /// <returns></returns> public CSVTable Parse(string tableContent) { if (string.IsNullOrEmpty(tableContent)) { Debug.LogError("The csv file is error or empty."); return(this); } 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(this); } string keyLine = lines[0]; string[] keys = SplitStringByComma(keyLine); _atrributeKeys = new List <string>(keys); _dataObjDic = new Dictionary <string, CSVObject>(); for (int i = 1; i < lines.Length; i++) { string[] values = SplitStringByComma(lines[i]); string major = values[0].Trim(); Dictionary <string, CSVValue> tempAttributeDic = new Dictionary <string, CSVValue>(); for (int j = 1; j < values.Length; j++) { string key = keys[j].Trim(); string value = values[j].Trim().Trim('\"'); tempAttributeDic.Add(key, new CSVValue(value)); } CSVObject dataObj = new CSVObject(major, tempAttributeDic, keys); this[dataObj.ID] = dataObj; } return(this); }
/// <summary> /// 添加数据对象, 并将数据对象主键添加到主键集合中 /// </summary> /// <param name="dataMajorKey"> 数据对象主键 </param> /// <param name="value"> 数据对象 </param> private void AddDataObject(string dataMajorKey, CSVObject 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); }