private List <PackageField> GetFields(TableReader reader) { List <PackageField> fields = new List <PackageField>(); int number = reader.ReadInt32(); //字段数量 for (int i = 0; i < number; ++i) { if (reader.ReadInt8() == 0) //基础类型 { int type = reader.ReadInt8(); //基础类型索引 bool array = reader.ReadBool(); //是否是数组 fields.Add(new PackageField() { Index = i, Name = "Value" + i, Type = BasicUtil.GetType((BasicEnum)type).ScorpioName, Array = array, }); } else //自定义类 { string type = reader.ReadString(); //自定义类名称 bool array = reader.ReadBool(); //是否是数组 fields.Add(new PackageField() { Index = i, Name = "Value" + i, Type = type, Array = array, }); } } return(fields); }
private void WriteField(TableWriter writer, object value, PackageField field) { var basic = BasicUtil.GetType(field.Type); if (basic != null) { if (field.Array) { var list = value as ScriptArray; if (Util.IsEmptyValue(list)) { writer.WriteInt32(0); } else { int count = list.Count(); writer.WriteInt32(count); for (int i = 0; i < count; ++i) { basic.WriteValue(writer, list.GetValue(i).ToString()); } } } else { basic.WriteValue(writer, value.ToString()); } } else if (field.Enum) { if (field.Array) { var list = value as ScriptArray; if (Util.IsEmptyValue(list)) { writer.WriteInt32(0); } else { int count = list.Count(); writer.WriteInt32(count); for (int i = 0; i < count; ++i) { writer.WriteInt32(GetEnumValue(field.Type, value.ToString())); } } } else { writer.WriteInt32(GetEnumValue(field.Type, value.ToString())); } } else { WriteCustom(writer, value as ScriptArray, mCustoms[field.Type], field.Array); } }
/// <summary> 生成Table类的代码 </summary> private void CreateTable(ProgramInfo info) { var str = info.GenerateTable.Generate(mPackage); str = str.Replace("__TableName", TableClassName); str = str.Replace("__DataName", DataClassName); str = str.Replace("__KeyName", mKeyName); str = str.Replace("__KeyType", BasicUtil.GetType(BasicEnum.INT32).GetCode(info.Code)); str = str.Replace("__MD5", GetClassMD5Code()); info.CreateFile(TableClassName, str); }
protected override string Generate_impl() { var builder = new StringBuilder(); builder.Append($@"//本文件为自动生成,请不要手动修改 {ClassName} = ["); foreach (var field in Fields) { var languageType = field.GetLanguageType(Language); languageType = field.IsEnum ? BasicUtil.GetType(BasicEnum.INT32).Name : languageType; builder.Append($@" /* {field.Comment} 默认值({field.Default}) */ {{ Index : {field.Index}, Name : ""{field.Name}"", Type : ""{languageType}"", Array : {field.IsArray.ToString().ToLower()}, Attribute : {field.AttributeString} }}, "); } builder.Append(@" ]"); return(builder.ToString()); }
private void WriteFields(TableWriter writer, List <PackageField> fields) { writer.WriteInt32(fields.Count); for (int i = 0; i < fields.Count; ++i) { var field = fields[i]; var basic = BasicUtil.GetType(field.Type); if (basic != null) { writer.WriteByte(0); writer.WriteByte((sbyte)basic.BasicIndex); writer.WriteBool(field.Array); } else { writer.WriteByte(1); writer.WriteString(field.Type); writer.WriteBool(field.Array); } } }
protected override string Generate_impl() { StringBuilder builder = new StringBuilder(); builder.Append(@"//本文件为自动生成,请不要手动修改 __ClassName = ["); foreach (var field in m_Fields) { string str = @" { Index = __Index, Name = ""__Name"", Type = ""__Type"", Array = __Array, Attribute = __Attribute }, /* __Note 默认值(__Default) */"; str = str.Replace("__Index", field.Index.ToString()); str = str.Replace("__Name", field.Name); str = str.Replace("__Type", field.Enum ? BasicUtil.GetType(BasicEnum.INT32).ScorpioName : field.Type); str = str.Replace("__Note", field.Comment); str = str.Replace("__Attribute", field.Attribute != null ? field.Attribute.ToJson() : "{}"); str = str.Replace("__Default", field.Default); str = str.Replace("__Array", field.Array ? "true" : "false"); builder.Append(str); } builder.Append(@" ]"); builder = builder.Replace("__ClassName", m_ClassName); return(builder.ToString()); }
//生成data文件数据 private void Transform_impl() { int iColums = mMaxColumn; //数据列数 int iRows = mMaxRow; //数据行数 TableWriter writer = new TableWriter(); writer.WriteInt32(0); //写入行数(最后计算出有效数据然后写入) writer.WriteString(GetClassMD5Code()); //写入文件MD5码 WriteFields(writer, mFields); //写入表结构 writer.WriteInt32(mUsedCustoms.Count); //写入用到的自定义类数量 foreach (var key in mUsedCustoms) { writer.WriteString(key); WriteFields(writer, mCustoms[key]); } List <string> keys = new List <string>(); int count = 0; for (int i = START_ROW; i < iRows; ++i) { string ID = ""; for (int j = 0; j < iColums; ++j) { string value = mDataTable[i][j]; var field = mFields[j]; try { if (string.IsNullOrEmpty(value)) { value = string.IsNullOrEmpty(field.Default) ? Util.EmptyString : field.Default; //数据为空设置为默认值 } if (j == 0) { if (keys.Contains(value)) { throw new Exception(string.Format("ID有重复项[{0}]", value)); } else if (Util.IsEmptyString(value)) { throw new Exception("ID字段不能为空"); } ID = value; keys.Add(value); } if (ExecuteField != null) { ExecuteField(field, ID, ref value); } var basic = BasicUtil.GetType(field.Type); if (basic != null || field.Enum) { if (field.Array) { WriteField(writer, Util.ReadValue(mEnums, mDataTable[i].RowNumber, Util.GetLineName(j), value, false, true), field); } else { WriteField(writer, value, field); } } else { WriteCustom(writer, Util.ReadValue(mEnums, mDataTable[i].RowNumber, Util.GetLineName(j), value, true, field.Array), mCustoms[field.Type], field.Array); } } catch (System.Exception ex) { throw new SystemException(string.Format("[{0}]行[{1}]列出错 数据内容为[{2}] : {3}", mDataTable[i].RowNumber, Util.GetLineName(j), value, ex.ToString())); } } count++; } writer.Seek(0); writer.WriteInt32(count); Create_impl(writer.ToArray()); }
public string GetCodeType(BasicEnum type) { var b = BasicUtil.GetType(type); return(b != null?b.GetCode(m_Code) : type.ToString()); }
public string GetCodeType(string type) { var b = BasicUtil.GetType(type); return(b != null?b.GetCode(m_Code) : type); }