/// <summary> /// 正在删除行的值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Row_RowDeleteing(object sender, DataRowChangeEventArgs e) { if (isLoadingData) { return; } SpellEditorForm.Instance.mLogicEditor.DeleteEffectOnLogic(Convert.ToInt32(e.Row[0].ToString())); uint id = Convert.ToUInt32(e.Row[0].ToString()); IDAllocator alloc = GetIDAllocator(id); if (alloc != null) { alloc.PushUnUsedId(id); } }
/// <summary> /// 新建一行时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Row_NewEvent(object sender, DataTableNewRowEventArgs e) { //为这一行的ID赋值 if (curIDRangeItem != null) { IDAllocator alloc = GetIDAllocator((uint)curIDRangeItem.minID); uint newID = alloc.AllocId(); if (newID <= (uint)curIDRangeItem.maxID) { e.Row[0] = newID; DataTable dt = (DataTable)sender; if (dt.Columns.Count <= 1)//只有一行的时候,直接应用改变 { dt.AcceptChanges(); } } else { Trace.Error("该ID组已满"); } } }
/// <summary> /// 加载bin的条件table /// </summary> /// <param name="tableArray">table数组</param> /// <param name="filePath">文件路径</param> /// <returns></returns> private bool LoadEffectTableFromBinImp(string filePath) { if (string.IsNullOrEmpty(filePath)) { return(false); } if (!File.Exists(filePath)) { Trace.Error("文件不存在:" + filePath); return(false); } FileStream _readObj = null; try { using (_readObj = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { foreach (KeyValuePair <string, DataTable> kv in mEffectSet) { BIN_FILE_HEADER header = new BIN_FILE_HEADER(); //已经读取完了,跳出 if (DataTableSerializer.ReadStruct(ref header, _readObj) <= 0) { break; } if (!mEffectSet.ContainsKey(header.szTypeName)) { Trace.Error("加载bin格式的效果时遇到无法识别的类型:" + header.szTypeName); continue; } DataTable table = mEffectSet[header.szTypeName]; Type structType = m_EffectType[header.szTypeName]; table.Clear(); FieldInfo[] fields = structType.GetFields(); // 逐个输入 for (int i = 0; i < header.nItemTotal; i++) { Object structTypeInstance = Activator.CreateInstance(structType); DataTableSerializer.ReadStructFromType(ref structTypeInstance, _readObj, structType); DataRow _row = table.NewRow(); for (int j = 0; j < fields.Length; j++) { _row[j] = fields[j].GetValue(structTypeInstance); //第一个是技能ID if (j == 0) { //取出最大的ID uint Id = Convert.ToUInt32(_row[j]); IDAllocator alloc = GetIDAllocator(Id); if (alloc != null) { alloc.PushUsedId(Id); } } } table.Rows.Add(_row); } } } } catch (Exception e) { Trace.Error(e.Message); return(false); } return(true); }
/// <summary> /// 加载csv到存储模式的table /// </summary> /// <param name="tableArray">table数组</param> /// <param name="tableName">table名字</param> /// <param name="filePath">文件路径</param> /// <param name="rowIndex">索引</param> /// <returns></returns> private bool LoadSaveFormatTableFromCsv(ref Dictionary <string, DataTable> tableSet, string filePath, int rowIndex = 2) { if (tableSet == null || string.IsNullOrEmpty(filePath)) { return(false); } if (rowIndex < 0) { Trace.Error("DataTableSerializer.LoadCSV,RowIndex参数小于0!"); return(false); } if (!File.Exists(filePath)) { Trace.Error("文件不存在:" + filePath); return(false); } int i = 0, j = 0; try { using (StreamReader reader = new StreamReader(filePath, Config.FileEncoding, false)) { reader.Peek(); string CurrentLineName = ""; //是否第一次读 bool bFirstRead = true; DataTable table = null; while (reader.Peek() > 0) { j = j + 1; string _line = reader.ReadLine(); if (j >= rowIndex + 1) { string[] _split = _line.Split(','); if (bFirstRead) { CurrentLineName = _split[0]; bFirstRead = false; if (!tableSet.ContainsKey(CurrentLineName)) { Trace.Warring("在参数tableSet中找不到类型:" + CurrentLineName + "数据已忽略"); continue; } table = tableSet[CurrentLineName]; table.Clear(); } //上一次的的名字和这次的名字不一样 if (!CurrentLineName.Equals(_split[0])) { if (!tableSet.ContainsKey(CurrentLineName)) { Trace.Warring("在参数tableSet中找不到类型:" + CurrentLineName + "数据已忽略"); continue; } CurrentLineName = _split[0]; table = tableSet[CurrentLineName]; table.Clear(); } if (table.Columns.Count != _split.Length) { Trace.Warring("文件:" + filePath + ",第" + (j).ToString() + "行格式与所需要的格式不一致,本行只有" + (_split.Length).ToString() + "列,所需要的格式为:" + table.Columns.Count + "列,已填充默认值,请注意检查!"); //table.Rows.Clear(); //return false; } DataRow _row = DataTableSerializer.FillDefaultNewRow(ref table); for (i = 0; i < table.Columns.Count; i++) { if (i > _split.Length - 1) { break; } if (string.IsNullOrEmpty(_split[i])) { Trace.Error("文件:" + filePath + ",[" + j.ToString() + "行," + i.ToString() + "列]为空!"); table.Rows.Clear(); return(false); } //第二个是技能ID if (i == 1) { //取出最大的ID uint Id = Convert.ToUInt32(_split[i]); IDAllocator alloc = GetIDAllocator(Id); if (alloc != null) { alloc.PushUsedId(Id); } } //枚举类型,特殊处理 if (DataTableSerializer.IsEnum(table.Columns[i].DataType)) { _row[i] = Enum.Parse(table.Columns[i].DataType, _split[i]); } else { _row[i] = _split[i]; } } table.Rows.Add(_row); } } } } catch (ArgumentException e) { string erroMsg; erroMsg = "[" + i.ToString() + "列," + j.ToString() + "行]参数错误,"; erroMsg += e.Message; Trace.Error(erroMsg); return(false); } return(true); }
//加载英雄ID范围配置 public bool LoadEffectIDRangeCSV(string filePath, int rowIndex = 2) { if (string.IsNullOrEmpty(filePath)) { Trace.Error("路径为空!"); return(false); } if (!File.Exists(filePath)) { Trace.Error("文件不存在:" + filePath); return(false); } try { using (StreamReader reader = new StreamReader(filePath, Config.FileEncoding, false)) { reader.ReadLine(); reader.ReadLine(); int max = 0; int lineNum = 1; while (reader.Peek() > 0) { string _line = reader.ReadLine(); string[] _split = _line.Split(','); HeroEffectIDRange item = new HeroEffectIDRange(); item.heroID = int.Parse(_split[0]); item.heroName = _split[1]; item.minID = int.Parse(_split[2]); item.maxID = int.Parse(_split[3]); string key = (_split[1]); string name = _split[1]; //做些ID范围冲突检测 if (item.minID > item.maxID) { Trace.Error("ID范围配置,第" + lineNum + "行数据出错:最小ID必须小于最大ID"); return(false); } if (max > item.minID) { Trace.Error("ID范围配置,第" + lineNum + "行数据出错:ID范围必须是从小到大排序"); return(false); } max = item.maxID; effectIDRangeDict.Add(key, item); IDAllocator alloc = new IDAllocator(IDAllocatorType.ID_MAX); alloc.IDStartIndex = (uint)item.minID; IDAllocatorList.Add(item, alloc); lineNum++; } reader.Close(); return(true); } } catch (ArgumentException e) { string erroMsg; //erroMsg = "[" + i.ToString() + "列," + j.ToString() + "行]参数错误,"; erroMsg = e.Message; Trace.Error(erroMsg); return(false); } }