private Properties(XlsxSheet sheet, DataSideEnum side, string name, Properties parent) : this() { mNamespace = name; mId = name; mParent = parent; List <int> cols = sheet.GetColumns(side); for (int i = 0; i < sheet.RowCount; ++i) { Properties rowProp = new Properties(sheet, i, cols, sheet.SheetName, this); mNamespaces.Add(rowProp); } }
public List <int> GetColumns(DataSideEnum side) { switch (side) { case DataSideEnum.C: return(mClientColumns); case DataSideEnum.S: return(mServerColumns); default: throw new Exception("wrong"); } }
private static List <XlsxRow> CleanRowAndCols(ExcelWorksheet tDS, XlsxSheet sheet, out string managerName, out string keys, out List <string> variableNames, out List <DataTypeEnum> variableTypes, out List <DataSideEnum> variableSides) { variableNames = new List <string>(); variableTypes = new List <DataTypeEnum>(); variableSides = new List <DataSideEnum>(); managerName = null; keys = null; ExcelRange range = tDS.Cells; object[,] values = (object[, ])range.Value; int rows = values.GetLength(0); int cols = values.GetLength(1); HashSet <int> skipCols = new HashSet <int>(); HashSet <int> skipRows = new HashSet <int>(); if (rows > 0 && cols > 0) { // List,Group<ColName>,Map<ColName>,Map<ColName1, ColName2> string value = (string)values[SHEET_DATA_MANAGER, 0]; if (value == null) { MainEntry.Log(string.Format("****************SheetName: {0} Wrong At [0, 0] End", tDS.Name)); return(null); } ParseManagerType(value, ref managerName, ref keys); if (managerName == null) { MainEntry.Log(string.Format("****************SheetName: {0} Wrong ManagerName {1} End", tDS.Name, value)); return(null); } } for (int i = 0; i < cols; ++i) { if (values[VARIABLE_SIDE_ROW, i] == null) { MainEntry.Log(string.Format("skip null col: [{0}, {1}], maybe last col", VARIABLE_SIDE_ROW, i)); skipCols.Add(i); continue; } string v = values[VARIABLE_SIDE_ROW, i].ToString().Trim().ToUpper(); DataSideEnum side = EnumUtils.StringToEnum <DataSideEnum>(v); if (side == DataSideEnum.SKIP) { MainEntry.Log(string.Format("skip flag col: [{0}, {1}]", VARIABLE_SIDE_ROW, i)); skipCols.Add(i); continue; } if (values[VARIABLE_NAME_ROW, i] == null) { // log MainEntry.Log(string.Format("Not Define Variable Name: [{0}, {1}]", VARIABLE_NAME_ROW, i)); return(null); } if (values[VARIABLE_TYPE_ROW, i] == null) { // log MainEntry.Log(string.Format("Not Define Variable Type: [{0}, {1}]", VARIABLE_TYPE_ROW, i)); return(null); } variableSides.Add(side); v = values[VARIABLE_NAME_ROW, i].ToString().Trim(); variableNames.Add(v); v = values[VARIABLE_TYPE_ROW, i].ToString().Trim(); try { DataTypeEnum dataType = EnumUtils.StringToEnum <DataTypeEnum>(v.ToUpper()); variableTypes.Add(dataType); } catch (Exception e) { MainEntry.Log(string.Format("Not Define Variable Side: [{0}, {1}] = {2}, Error: {3}", VARIABLE_TYPE_ROW, i, v, e.Message)); return(null); } } for (int i = VALUE_START_ROW; i < rows; ++i) { if (values[i, 0] != null) { string v = values[i, 0].ToString().Trim(); if (v.StartsWith(SKIP_ROW)) { MainEntry.Log(string.Format("skip flag row: [{0}, 0]", i)); skipRows.Add(i); } } else { MainEntry.Log(string.Format("skip null row: [{0}, 0], maybe last row", i)); skipRows.Add(i); } } List <XlsxRow> originalDatas = new List <XlsxRow>(); int rowIndex = 0; for (int i = VALUE_START_ROW; i < rows; ++i) { if (skipRows.Contains(i)) { continue; } List <string> rowData = new List <string>(); XlsxRow row = new XlsxRow(rowIndex++, rowData, sheet); originalDatas.Add(row); for (int j = 0; j < cols; ++j) { if (skipCols.Contains(j)) { continue; } rowData.Add(values[i, j] != null ? values[i, j].ToString() : null); } } return(originalDatas); }
public void ExportCSharp(StringBuilder builder, DataSideEnum side) { string tab = " "; string doubleTab = tab + tab; builder.Append(tab).Append(MakeClassDefine()); bool export_nullstream = MainEntry.Config.GetBool("export_nullstream", false); if (export_nullstream) { builder.Append(", INullStream"); } builder.AppendLine(); builder.Append(tab).AppendLine("{"); builder.Append(doubleTab).Append(string.Format("public static readonly string FileUrl = \"{0}#{1}s\";", Parent.FileName, SheetName)).AppendLine(); builder.Append(doubleTab).Append(string.Format("public static readonly bool IsDelayInitialized = {0};", Delay.ToString().ToLower())).AppendLine(); builder.Append(doubleTab).Append(string.Format("public static readonly List<string> KeyNameList = {0};", GetKeyNames())).AppendLine(); string dataTypeString = ""; string readString = ""; string writeString = ""; List <int> cols = GetColumns(side); int count = cols.Count; List <KeyValuePair <string, string> > listNames = new List <KeyValuePair <string, string> >(); for (int i = 0; i < count; ++i) { DataTypeEnum dt = mVariableTypes[cols[i]]; GetType(dt, ref dataTypeString, ref readString, ref writeString); builder.Append(doubleTab).Append(string.Format("public {0} {1} ", dataTypeString, mVariableNames[cols[i]])).Append("{ get; private set; }").AppendLine(); // List 需要初始化 if (dt > DataTypeEnum.LIST) { KeyValuePair <string, string> pair = new KeyValuePair <string, string>(mVariableNames[cols[i]], dataTypeString); listNames.Add(pair); } } builder.AppendLine(); if (listNames.Count > 0) { builder.Append(doubleTab).Append(string.Format("public {0}()", SheetName)).AppendLine(); builder.Append(doubleTab).Append("{").AppendLine(); foreach (KeyValuePair <string, string> name in listNames) { builder.Append(doubleTab).Append(tab).Append(string.Format("{0} = new {1}();", name.Key, name.Value)).AppendLine(); } builder.Append(doubleTab).Append("}").AppendLine(); } builder.AppendLine(); builder.Append(doubleTab).AppendLine("protected override void ConvertAll()"); builder.Append(doubleTab).AppendLine("{"); List <string> keys = null; if (Keys != null) { keys = new List <string>(Keys.Split(KeySplitChar)); int cnt = keys.Count; for (int i = 0; i < cnt; ++i) { keys[i] = keys[i].Trim(); } } for (int i = 0; i < count; ++i) { string name = mVariableNames[cols[i]]; if (keys != null && keys.Contains(name)) { continue; } DataTypeEnum dt = mVariableTypes[cols[i]]; GetType(dt, ref dataTypeString, ref readString, ref writeString); string func = "ToObject"; // List 需要初始化 if (dt > DataTypeEnum.LIST) { string l = StringUtils.StrTok(dataTypeString, "<"); dataTypeString = StringUtils.StrTok(null, ">"); func = "ToObjectList"; } builder.Append(doubleTab).Append(tab).Append(string.Format("{0} = DataUtils.{1}<{2}>(GetValue(\"{0}\"));", name, func, dataTypeString)).AppendLine(); } builder.Append(doubleTab).AppendLine("}"); if (export_nullstream) { builder.Append(doubleTab).Append("public int SaveToStream(NullMemoryStream stream)").AppendLine(); builder.Append(doubleTab).Append("{").AppendLine(); builder.Append(doubleTab).Append(tab).Append("int size = 0;").AppendLine(); for (int i = 0; i < count; ++i) { GetType(mVariableTypes[cols[i]], ref dataTypeString, ref readString, ref writeString); builder.Append(doubleTab).Append(tab).Append(string.Format("size += GameDataUtils.{0}(stream, {1})", writeString, mVariableNames[cols[i]])).AppendLine(); } builder.Append(doubleTab).Append(tab).Append("return size;").AppendLine(); builder.Append(doubleTab).Append("}").AppendLine(); builder.AppendLine(); builder.Append(doubleTab).Append("public bool LoadFromStream(NullMemoryStream stream)").AppendLine(); builder.Append(doubleTab).Append("{").AppendLine(); builder.Append(doubleTab).Append(tab).Append("bool res = true;").AppendLine(); for (int i = 0; i < count; ++i) { GetType(mVariableTypes[cols[i]], ref dataTypeString, ref readString, ref writeString); builder.Append(doubleTab).Append(tab).Append(string.Format("res &= GameDataUtils.{0}(stream, out {1})", readString, mVariableNames[cols[i]])).AppendLine(); } builder.Append(doubleTab).Append(tab).Append("return res;").AppendLine(); builder.Append(doubleTab).Append("}").AppendLine(); } builder.Append(tab).Append("}").AppendLine(); }