public CSVTable UtilCsv(FileInfo file) { //针对ANSI编码的csv 不要用Unicode编码 StreamReader sr = new StreamReader(file.FullName, Encoding.Default); CSVTable table = new CSVTable(file.Name); string attachmsg = string.Empty; bool isSort = true; //字段 string tile = sr.ReadLine(); if (string.IsNullOrEmpty(tile.TrimEnd(eof))) { attachmsg = "标题为null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } //注释 string comment = sr.ReadLine(); if (string.IsNullOrEmpty(comment.TrimEnd(eof))) { attachmsg = "注释为null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } //client使用还是server使用 string mode = sr.ReadLine(); if (string.IsNullOrEmpty(mode.TrimEnd(eof))) { attachmsg = "读表地方null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } string[] modes = UtilLine(mode); bool[] useList = new bool[modes.Length]; int useColumeCnt = 0; for (int i = 0; i < modes.Length; i++) { useList[i] = modes[i].Equals("A") || modes[i].Equals("C"); if (useList[i]) { useColumeCnt++; } } //类型 string tp = sr.ReadLine(); if (string.IsNullOrEmpty(tp.TrimEnd(eof))) { attachmsg = "类型为null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } string[] titles = RemoveUnuseless(UtilLine(tile.TrimEnd(eof)), useList); if (titles.Length > 0) { isSort &= titles[0].Contains("ID"); } string[] comments = RemoveUnuseless(UtilLine(comment), useList); string[] tps = RemoveUnuseless(UtilLine(tp), useList); ValueParse[] parses = new ValueParse[tps.Length]; for (int i = 0, max = parses.Length; i < max; i++) { parses[i] = TransParse(tps[i], file.Name); } if (tps.Length > 0) { isSort &= (tps[0].Equals("int") || tps[0].Equals("uint")); } int lineCnt = 0; table.sortlist = new List <CVSSortRow>(); while (true) { string line = sr.ReadLine(); if (string.IsNullOrEmpty(line)) { break; } string[] colums = RemoveUnuseless(UtilLine(line), useList); if (colums == null || tp == null) { attachmsg = "内容有null"; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } else if (colums.Length != tps.Length || colums.Length != titles.Length) { attachmsg = "字段不等长 内容:" + colums.Length + " tpye:" + tps.Length + "\n" + line; throw new Exception("非法的表格:" + file.Name + " " + attachmsg); } CVSSortRow sortRow = new CVSSortRow(); sortRow.row = new CSVStruct[useColumeCnt]; for (int i = 0, max = colums.Length; i < max; i++) { CSVStruct sct = new CSVStruct(); sct.title = titles[i]; sct.comment = comments[i]; sct.parse = TransParse(tps[i], file.Name); sct.content = string.Intern(colums[i]); if (i == 0) { sortRow.sortid = isSort ? int.Parse(colums[i]) : 0; } sortRow.row[i] = sct; } table.sortlist.Add(sortRow); lineCnt++; } table.isSort = isSort; table.rowCnt = lineCnt; table.colCnt = titles.Length; table.titles = titles; table.comments = comments; table.types = tps; table.parses = parses; if (isSort) { table.sortlist.Sort(table.Sort); } return(table); }
private void GenerateTable(FileInfo file) { CSVTable tb = CSVUtil.sington.UtilCsv(file); string name = file.Name.Substring(0, file.Name.LastIndexOf('.')); //声明代码的部分 CodeCompileUnit compunit = new CodeCompileUnit(); CodeNamespace sample = new CodeNamespace("GameCore"); //引用命名空间 sample.Imports.Add(new CodeNamespaceImport("System.Collections.Generic")); sample.Imports.Add(new CodeNamespaceImport("System.Runtime.InteropServices")); compunit.Namespaces.Add(sample); //在命名空间下添加一个类 CodeTypeDeclaration wrapClass = new CodeTypeDeclaration("C" + name); sample.Types.Add(wrapClass); //加一个标记 用来替换 CodeMemberField field = new CodeMemberField(typeof(int), "replace"); field.Attributes = MemberAttributes.Public; wrapClass.Members.Add(field); CodeMemberField field2 = new CodeMemberField("RowData", "m_data"); field2.Attributes = MemberAttributes.Static | MemberAttributes.Private; wrapClass.Members.Add(field2); //属性 public static int length { get { return iGetEquipSuitLength(); } } CodeMemberProperty prop = new CodeMemberProperty(); prop.Name = "length"; prop.HasGet = true; prop.HasSet = false; prop.Attributes = MemberAttributes.Public | MemberAttributes.Static; prop.Type = new CodeTypeReference(typeof(int)); prop.GetStatements.Add(new CodeSnippetStatement("\t\t\t\treturn iGet" + name + "Length();")); wrapClass.Members.Add(prop); // public static RowData GetRow(int val) CodeMemberMethod method = new CodeMemberMethod(); method.Name = "GetRow"; method.Attributes = MemberAttributes.Static | MemberAttributes.Public; method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "idx")); method.ReturnType = new CodeTypeReference("RowData");//返回值 method.Statements.Add(new CodeSnippetStatement("\t\t\tiGet" + name + "Row(idx, ref m_data);")); method.Statements.Add(new CodeSnippetStatement("\t\t\treturn m_data;")); wrapClass.Members.Add(method); StringBuilder fileContent = new StringBuilder(); using (StringWriter sw = new StringWriter(fileContent)) { CodeDomProvider.CreateProvider("CSharp").GenerateCodeFromCompileUnit(compunit, sw, new CodeGeneratorOptions()); } StringBuilder sb = new StringBuilder(); sb.Append("[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]"); sb.Append("\n\t\tpublic struct RowData {"); for (int i = 0, max = tb.types.Length; i < max; i++) { if (tb.types[i].Contains("<>")) //Seq { sb.Append("\n\t\t\t[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]"); sb.Append("\n\t\t\t" + tb.types[i].Replace("<>", "[] ") + tb.titles[i].ToLower() + ";"); } else if (tb.types[i].ToLower().Equals("string[]")) //stringarray { sb.Append("\n\t\t\t[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]"); sb.Append("\n\t\t\tbyte[] " + tb.titles[i].ToLower() + ";"); } else if (tb.types[i].Contains("[]")) { sb.Append("\n\t\t\t[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]"); sb.Append("\n\t\t\t" + tb.types[i] + " " + tb.titles[i].ToLower() + ";"); } else if (tb.types[i].ToLower().Equals("string")) { sb.Append("\n\t\t\t[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]"); sb.Append("\n\t\t\tstring " + tb.titles[i].ToLower() + ";"); } else { sb.Append("\n\t\t\t" + tb.types[i] + " " + tb.titles[i].ToLower() + ";"); } } for (int i = 0, max = tb.types.Length; i < max; i++) { if (tb.types[i].Contains("<>")) //Seq { string ctype = "CSeq< " + tb.types[i].Replace("<", ""); sb.Append("\n\n\t\t\tpublic " + ctype + " " + FirstUpperStr(tb.titles[i]) + "{ get { return new " + ctype + "(ref " + tb.titles[i].ToLower() + "); } }"); } else if (tb.types[i].ToLower().Equals("string[]")) //stringarray { string tn = tb.titles[i].ToLower(); string tp = tb.types[i].Replace("[", string.Empty).Replace("]", string.Empty); sb.Append("\n\n\t\t\tpublic string[] " + FirstUpperStr(tn) + " { "); sb.Append("\n\t\t\t\tget { "); sb.Append("\n\t\t\t\t\tint ptr = 0;"); sb.Append("\n\t\t\t\t\tstring[] ss = new string[16];"); sb.Append("\n\t\t\t\t\tfor (int i = 0; i < 16; i++)"); sb.Append("\n\t\t\t\t\t{"); sb.Append("\n\t\t\t\t\t\tbyte[] bytes = new byte[64];"); sb.Append("\n\t\t\t\t\t\tSystem.Array.Copy(hit_f, ptr, bytes, 0, 64);"); sb.Append("\n\t\t\t\t\t\tss[i] = System.Text.Encoding.Default.GetString(bytes);"); sb.Append("\n\t\t\t\t\t\tptr += 64;"); sb.Append("\n\t\t\t\t\t}"); sb.Append("\n\t\t\t\t\treturn ss;"); sb.Append("\n\t\t\t\t}"); sb.Append("\n\t\t\t}"); } else if (tb.types[i].Contains("[]")) { string tn = tb.titles[i].ToLower(); string tp = tb.types[i].Replace("[", string.Empty).Replace("]", string.Empty); sb.Append("\n\n\t\t\tpublic " + tp + "[] " + FirstUpperStr(tn) + " { "); sb.Append("\n\t\t\t\tget { "); sb.Append("\n\t\t\t\t\tif (" + tn + ".Length == 16) {"); sb.Append("\n\t\t\t\t\tList<" + tp + "> list = new List<" + tp + ">();"); sb.Append("\n\t\t\t\t\tfor (int i = " + tn + ".Length - 1; i >= 0; i--)"); sb.Append("\n\t\t\t\t\t{"); string invalid = "0"; if (tp == "string") { invalid = "\"0\""; } sb.Append("\n\t\t\t\t\t\tif (" + tn + "[i] != " + invalid + ") list.Add(" + tn + "[i]);"); sb.Append("\n\t\t\t\t\t}"); sb.Append("\n\t\t\t\t\t" + tn + " = list.ToArray();"); sb.Append("\n\t\t\t\t\t}"); sb.Append("\n\t\t\t\t\t return " + tn + ";"); sb.Append("\n\t\t\t\t}"); sb.Append("\n\t\t\t}"); } else { sb.Append("\n\n\t\t\tpublic " + tb.types[i] + " " + FirstUpperStr(tb.titles[i]) + " { get { return " + tb.titles[i].ToLower() + "; } }"); } } sb.Append("\r\n\t\t}\r\n"); AppendExtendAttr(sb); sb.Append("\r\n\t\tstatic extern void iGet" + name + "Row(int idx, ref RowData row);"); if (tb.isSort) { AppendExtendAttr(sb); sb.Append("\r\n\t\tstatic extern void iGet" + name + "RowByID(int idx, ref RowData row);"); } AppendExtendAttr(sb); sb.Append("\r\n\t\tstatic extern int iGet" + name + "Length();"); fileContent.Replace("public int replace;", sb.ToString()); string filePath = destdir + "C" + name + ".cs"; File.WriteAllText(filePath, fileContent.ToString()); MergeCsproj(name); }