namespace etoy { class CSharpKeyValueTableGenerater : IGenerater { //private const string CLASS_TAIL = "Table"; // C# Table作为类型后缀 public void Generate(Context context) { var tables = context.Blackboard.KeyValueTables; foreach (var table in tables) GenSingle(context, table); } void GenSingle(Context context, KeyValueTable table) { const string FIELD_REPEATED = "[]"; string fileName = "KeyValue.cs"; string directory = context.Option.ClientCodeOutput; string path = Path.Combine(directory, fileName); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); using CodeWriter writer = new CodeWriter(path); writer.WriteLine("// Generate By EToy"); writer.WriteLine("// Don't Edit It!!"); writer.WriteLine(); // namespace writer.Bracket("namespace XGame.Database"); { // class writer.Bracket($"public static class KeyValue"); { for (int i = 0, length = table.Rows.Length; i < length; i++) { var row = table.Rows[i]; if (!row.IsClientField) continue; string summary = row.Description; string fieldType = row.Type; string fieldName = row.Key; string content = row.Value; // summary writer.Summary(summary); writer.Summary(content); // field var fieldValue = string.Empty; var repeadIdx = fieldType.IndexOf(FIELD_REPEATED); if (repeadIdx > 0) { // 是数组类型 fieldValue = content.Replace("[", "{").Replace("]", "}"); } else { switch (fieldType) { case FieldTypeDefine.Boolean: fieldValue = (content == "1" || content == "true") ? "true" : "false"; break; case FieldTypeDefine.Int: fieldValue = content; break; case FieldTypeDefine.Long: fieldValue = content; break; case FieldTypeDefine.String: fieldValue = content.Replace("\"", "\\\""); // "前面多加个反斜杠 fieldValue = $"\"{fieldValue}\""; break; default: throw new NotSupportedException($"KeyValue表C#代码生成失败, 不支持string/int外的类型{row.Type}字段。{table.Path}"); } } writer.WriteLine($"public static readonly {fieldType} {fieldName} = {fieldValue};"); // enter if (i < length - 1) writer.WriteLine(); } writer.EndBracket(); } writer.EndBracket(); } } } }