123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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();
- }
- }
- }
- }
|