712 lines
39 KiB
C#
712 lines
39 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Text;
|
||
|
||
namespace ExcelTool.Parser
|
||
{
|
||
public static class GenModels
|
||
{
|
||
/// <summary>
|
||
/// 生成对应的C#Model类
|
||
/// </summary>
|
||
/// <param name="fileName">传入的文件名</param>
|
||
/// <param name="outputDir">输出cs文件的路径</param>
|
||
/// <param name="nameSpace">cs文件要使用的命名空间</param>
|
||
/// <returns></returns>
|
||
public static bool GenCSharpModel(string fileName, string outputDir, string nameSpace = "")
|
||
{
|
||
if (string.IsNullOrEmpty(fileName))
|
||
{
|
||
"GenCSharpModel 参数传递有误".WriteErrorLine();
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
// 枚举类型映射
|
||
Dictionary<string, string> enumMap = new()
|
||
{
|
||
{ "killmode", "KillMode" },
|
||
{ "mixingtype", "MixingType" },
|
||
{ "containertype", "ContainerType" },
|
||
{ "blendcrossfadetype", "BlendCrossFadeType" }
|
||
};
|
||
FileInfo fileInfo = new(fileName);
|
||
if (string.IsNullOrEmpty(outputDir))
|
||
{
|
||
outputDir = fileInfo.DirectoryName;
|
||
}
|
||
for (int sheetNum = 0; ; sheetNum++)
|
||
{
|
||
var headers = ExcelHelper.ExcelHeaders(fileName, out string sheetName, out int sheetCount, sheetNum);
|
||
if (headers == null || headers.Count == 0 || sheetName.StartsWith("#") || sheetNum > sheetCount)
|
||
break;
|
||
StringBuilder sb = new();
|
||
sb.Append($"/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * {sheetName}\n */\n");
|
||
sb.Append("using System;\nusing System.IO;\nusing System.Collections.Generic;\nusing System.Text;\nusing WKMobile.Generated;\n\n");
|
||
|
||
|
||
if (!string.IsNullOrEmpty(nameSpace))
|
||
{
|
||
sb.Append($"namespace {nameSpace}\n{{\n");
|
||
}
|
||
|
||
sb.Append("[Serializable]\n");
|
||
sb.Append($"public partial class {sheetName} : IBinarySerializable\n");
|
||
sb.Append("{\n");
|
||
for (int i = 0; i < headers.Count; i++)
|
||
{
|
||
sb.Append($"\t/// <summary>\n");
|
||
var descLines = headers[i].FieldDesc?.Replace("\r", "").Split('\n');
|
||
if (descLines != null)
|
||
{
|
||
foreach (var line in descLines)
|
||
{
|
||
sb.Append($"\t/// {line}\n");
|
||
}
|
||
}
|
||
sb.Append($"\t/// </summary>\n");
|
||
var type = headers[i].FieldType.ToLower();
|
||
var origType = headers[i].FieldType;
|
||
var fieldName = headers[i].FieldName;
|
||
if (enumMap.ContainsKey(type))
|
||
{
|
||
sb.Append($"\tpublic {enumMap[type]} {fieldName} {{ get; set; }}\n\n");
|
||
}
|
||
else if (type.Equals("vector"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<float> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("vectorlist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<List<float>> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("intlist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<int> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("boollist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<bool> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("floatlist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<float> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("stringlist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<string> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("longlist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<long> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("uintlist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<uint> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("ushortlist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<ushort> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("sbytelist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<sbyte> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Equals("bytelist"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<byte> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (type.Contains("list<"))
|
||
{
|
||
var tempS = type.Substring(5);
|
||
var newType = tempS.Substring(0, tempS.Length - 1);
|
||
if (newType.Equals("int"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<int> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (newType.Equals("bool"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<bool> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (newType.Equals("float"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<float> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (newType.Equals("long"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<long> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else if (newType.Equals("string"))
|
||
{
|
||
sb.Append(string.Format("\tpublic List<string> {0}", headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
else
|
||
{
|
||
ConsoleHelper.WriteErrorLine("数组类型List<T>,T类型不支持");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
sb.Append(string.Format("\tpublic {0} {1}", headers[i].FieldType.ToLower(), headers[i].FieldName));
|
||
sb.Append(" { get; set; }\n\n");
|
||
}
|
||
}
|
||
sb.Append("\n\tpublic void DeSerialize(BinaryReader reader)\n");
|
||
sb.Append("\t{\n");
|
||
foreach (var header in headers)
|
||
{
|
||
var type = header.FieldType.ToLower();
|
||
var name = header.FieldName;
|
||
if (enumMap.ContainsKey(type))
|
||
{
|
||
sb.Append($"\t\t{name} = ({enumMap[type]})reader.ReadByte();\n");
|
||
}
|
||
else if (type.Equals("int"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadInt32();\n");
|
||
}
|
||
else if (type.Equals("uint"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadUInt32();\n");
|
||
}
|
||
else if (type.Equals("short"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadInt16();\n");
|
||
}
|
||
else if (type.Equals("ushort"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadUInt16();\n");
|
||
}
|
||
else if (type.Equals("sbyte"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadSByte();\n");
|
||
}
|
||
else if (type.Equals("byte"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadByte();\n");
|
||
}
|
||
else if (type.Equals("bool"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadBoolean();\n");
|
||
}
|
||
else if (type.Equals("float"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadSingle();\n");
|
||
}
|
||
else if (type.Equals("string"))
|
||
{
|
||
sb.Append($"\t\t{name} = reader.ReadString();\n");
|
||
}
|
||
else if (type.Equals("vector"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<float>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadSingle());\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("vectorlist"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<List<float>>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\tvar tempList = new List<float>();\n");
|
||
sb.Append($"\t\t\t\tvar tempListCount = reader.ReadInt32();\n");
|
||
sb.Append($"\t\t\t\tfor (int j = 0; j < tempListCount; j++)\n");
|
||
sb.Append("\t\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t\ttempList.Add(reader.ReadSingle());\n");
|
||
sb.Append("\t\t\t\t}\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(tempList);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("intlist"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<int>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadInt32());\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}else if (type.Equals("uintlist"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<uint>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadUInt32());\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("boollist"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<bool>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadBoolean());\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("floatlist"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<float>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadSingle());\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("stringlist"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<string>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadString());\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("longlist"))
|
||
{
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = new List<long>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadInt64());\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Contains("list<"))
|
||
{
|
||
var tempS = type.Substring(5);
|
||
var listTType = tempS.Substring(0, tempS.Length - 1);
|
||
sb.Append($"\t\tvar {name.ToCamelCase()}Count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tif ({name.ToCamelCase()}Count > 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
if (listTType.Equals("int"))
|
||
{
|
||
sb.Append($"\t\t\t{name} = new List<int>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadInt32());\n");
|
||
}
|
||
else if (listTType.Equals("bool"))
|
||
{
|
||
sb.Append($"\t\t\t{name} = new List<bool>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadBoolean());\n");
|
||
}
|
||
else if (listTType.Equals("float"))
|
||
{
|
||
sb.Append($"\t\t\t{name} = new List<float>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadSingle());\n");
|
||
}
|
||
else if (listTType.Equals("long"))
|
||
{
|
||
sb.Append($"\t\t\t{name} = new List<long>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadInt64());\n");
|
||
}
|
||
else if (listTType.Equals("string"))
|
||
{
|
||
sb.Append($"\t\t\t{name} = new List<string>();\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name.ToCamelCase()}Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t{name}.Add(reader.ReadString());\n");
|
||
}
|
||
else
|
||
{
|
||
"数组泛型T不是指定类型".WriteErrorLine();
|
||
}
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{name} = null;\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else
|
||
{
|
||
$"类型:{type}没有解析 {fileName}处理异常".WriteErrorLine();
|
||
return false;
|
||
}
|
||
}
|
||
sb.Append("\t}\n\n");
|
||
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
|
||
sb.Append("\t{\n");
|
||
foreach (var header in headers)
|
||
{
|
||
var type = header.FieldType.ToLower();
|
||
var name = header.FieldName;
|
||
if (enumMap.ContainsKey(type))
|
||
{
|
||
sb.Append($"\t\twriter.Write((byte){name});\n");
|
||
}
|
||
else if (type.Equals("int") ||
|
||
type.Equals("bool") ||
|
||
type.Equals("float") ||
|
||
type.Equals("string") ||
|
||
type.Equals("uint") ||
|
||
type.Equals("ushort") ||
|
||
type.Equals("short") ||
|
||
type.Equals("sbyte") ||
|
||
type.Equals("byte"))
|
||
{
|
||
sb.Append($"\t\twriter.Write({name});\n");
|
||
}
|
||
else if (type.Equals("vector"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("vectorlist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i].Count);\n");
|
||
sb.Append($"\t\t\t\tfor (int j = 0; j < {name}[i].Count; j++)\n");
|
||
sb.Append("\t\t\t\t{\n");
|
||
sb.Append($"\t\t\t\t\twriter.Write({name}[i][j]);\n");
|
||
sb.Append("\t\t\t\t}\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("intlist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("boollist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("floatlist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("longlist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("stringlist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("uintlist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("ushortlist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("sbytelist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
else if (type.Equals("bytelist"))
|
||
{
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
}
|
||
|
||
else if (type.Contains("list<"))
|
||
{
|
||
var tempS = type.Substring(5);
|
||
var listTType = tempS.Substring(0, tempS.Length - 1);
|
||
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append("\t\t\twriter.Write(0);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t\telse\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||
sb.Append("\t\t\t{\n");
|
||
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||
sb.Append("\t\t\t}\n");
|
||
sb.Append("\t\t}\n");
|
||
if (listTType.Equals("int"))
|
||
{
|
||
|
||
}
|
||
else if (listTType.Equals("bool"))
|
||
{
|
||
|
||
}
|
||
else if (listTType.Equals("float"))
|
||
{
|
||
|
||
}
|
||
else if (listTType.Equals("long"))
|
||
{
|
||
|
||
}
|
||
else if (listTType.Equals("string"))
|
||
{
|
||
|
||
}
|
||
else
|
||
{
|
||
ConsoleHelper.WriteErrorLine("数组泛型T不是指定类型");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ConsoleHelper.WriteErrorLine($"类型:{type}没有解析 {fileName}处理异常");
|
||
return false;
|
||
}
|
||
}
|
||
sb.Append("\t}\n");
|
||
sb.Append("}\n");
|
||
|
||
sb.Append("\n");
|
||
sb.Append("[Serializable]\n");
|
||
sb.Append($"public partial class {sheetName}Config : IBinarySerializable\n");
|
||
sb.Append("{\n");
|
||
// sb.Append($"\tpublic List<{excelName}> {excelName}Infos = new List<{excelName}>();\n");
|
||
sb.Append($"\tDictionary<uint,{sheetName}> m_{sheetName.ToCamelCase()}Infos = new();\n");
|
||
sb.Append($"\tList<{sheetName}> m_{sheetName.ToCamelCase()}InfoList;\n");
|
||
sb.Append("\n");
|
||
sb.Append($"\tpublic List<{sheetName}> {sheetName}List()\n");
|
||
sb.Append("\t{\n");
|
||
sb.Append($"\t\tthis.m_{sheetName.ToCamelCase()}InfoList ??= new List<{sheetName}>(m_{sheetName.ToCamelCase()}Infos.Values);\n");
|
||
sb.Append($"\t\treturn this.m_{sheetName.ToCamelCase()}InfoList;\n");
|
||
sb.Append("\t}\n");
|
||
sb.Append("\n");
|
||
sb.Append($"\tpublic void DeSerialize(BinaryReader reader)\n");
|
||
sb.Append("\t{\n");
|
||
sb.Append($"\t\tint count = reader.ReadInt32();\n");
|
||
sb.Append($"\t\tfor (int i = 0;i < count; i++)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\t{sheetName} tempData = new();\n");
|
||
sb.Append($"\t\t\ttempData.DeSerialize(reader);\n");
|
||
// sb.Append($"\t\t\t{excelName}Infos.Add(tempData);\n");
|
||
sb.Append($"\t\t\tthis.m_{sheetName.ToCamelCase()}Infos.Add(tempData.Id, tempData);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t}\n");
|
||
sb.Append("\n");
|
||
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
|
||
sb.Append("\t{\n");
|
||
sb.Append($"\t\twriter.Write(this.m_{sheetName.ToCamelCase()}Infos.Count);\n");
|
||
sb.Append($"\t\tfor (uint i = 0; i < this.m_{sheetName.ToCamelCase()}Infos.Count; i++)\n");
|
||
sb.Append("\t\t{\n");
|
||
sb.Append($"\t\t\tthis.m_{sheetName.ToCamelCase()}Infos[i].Serialize(writer);\n");
|
||
sb.Append("\t\t}\n");
|
||
sb.Append("\t}\n\n");
|
||
sb.Append($"\tpublic {sheetName} QueryById(uint id)\n");
|
||
sb.Append("\t{\n");
|
||
// sb.Append($"\t\tvar datas = from d in {excelName}Infos\n");
|
||
// sb.Append($"\t\t\t\t\twhere d.Id == id\n");
|
||
// sb.Append($"\t\t\t\t\tselect d;\n");
|
||
// sb.Append("\t\treturn datas.First();\n");
|
||
sb.Append($"\t\treturn this.m_{sheetName.ToCamelCase()}Infos.GetValueOrDefault(id);\n");
|
||
sb.Append("\t}\n");
|
||
sb.Append("}\n");
|
||
|
||
if (!string.IsNullOrEmpty(nameSpace))
|
||
{
|
||
sb.Append("}\n"); // namespace 结束
|
||
}
|
||
FileManager.WriteToFile(Path.Combine(outputDir, $"{sheetName}.cs"), sb.ToString());
|
||
}
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ex.ToString().WriteErrorLine();
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|