2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00
This commit is contained in:
2022-03-09 21:55:30 +03:00
parent 530df018ec
commit 9a174f406f
106 changed files with 5166 additions and 4398 deletions

View File

@ -26,7 +26,7 @@ public static class TemplateGenerator
rt.AppendLine("using System;\r\nusing Esiur.Resource;\r\nusing Esiur.Core;\r\nusing Esiur.Data;\r\nusing Esiur.Net.IIP;");
rt.AppendLine($"namespace { nameSpace} {{");
rt.AppendLine($"public class {className} : IRecord {{");
rt.AppendLine($"[Public] public class {className} : IRecord {{");
foreach (var p in template.Properties)
@ -41,58 +41,81 @@ public static class TemplateGenerator
return rt.ToString();
}
static string GetTypeName(TemplateDataType templateDataType, TypeTemplate[] templates)
internal static string GenerateEnum(TypeTemplate template, TypeTemplate[] templates)
{
var cls = template.ClassName.Split('.');
if (templateDataType.Type == DataType.Resource)
return templates.First(x => x.ClassId == templateDataType.TypeGuid && (x.Type == TemplateType.Resource || x.Type == TemplateType.Wrapper)).ClassName;
else if (templateDataType.Type == DataType.ResourceArray)
return templates.First(x => x.ClassId == templateDataType.TypeGuid && (x.Type == TemplateType.Resource || x.Type == TemplateType.Wrapper)).ClassName + "[]";
else if (templateDataType.Type == DataType.Record)
return templates.First(x => x.ClassId == templateDataType.TypeGuid && x.Type == TemplateType.Record).ClassName;
else if (templateDataType.Type == DataType.RecordArray)
return templates.First(x => x.ClassId == templateDataType.TypeGuid && x.Type == TemplateType.Record).ClassName + "[]";
var nameSpace = string.Join(".", cls.Take(cls.Length - 1));
var className = cls.Last();
var name = templateDataType.Type switch
var rt = new StringBuilder();
rt.AppendLine("using System;\r\nusing Esiur.Resource;\r\nusing Esiur.Core;\r\nusing Esiur.Data;\r\nusing Esiur.Net.IIP;");
rt.AppendLine($"namespace { nameSpace} {{");
rt.AppendLine($"[Public] public enum {className} {{");
rt.AppendLine(String.Join(",\r\n", template.Constants.Select(x => $"{x.Name}={x.Value}")));
rt.AppendLine("\r\n}\r\n}");
return rt.ToString();
}
static string GetTypeName(RepresentationType representationType, TypeTemplate[] templates)
{
string name;
if (representationType.Identifier == RepresentationTypeIdentifier.TypedResource)// == DataType.Resource)
name = templates.First(x => x.ClassId == representationType.GUID && (x.Type == TemplateType.Resource || x.Type == TemplateType.Wrapper)).ClassName;
else if (representationType.Identifier == RepresentationTypeIdentifier.TypedRecord)
name = templates.First(x => x.ClassId == representationType.GUID && x.Type == TemplateType.Record).ClassName;
else if (representationType.Identifier == RepresentationTypeIdentifier.Enum)
name = templates.First(x => x.ClassId == representationType.GUID && x.Type == TemplateType.Enum).ClassName;
else if (representationType.Identifier == RepresentationTypeIdentifier.TypedList)
name = GetTypeName(representationType.SubTypes[0], templates) + "[]";
else if (representationType.Identifier == RepresentationTypeIdentifier.TypedMap)
name = "Map<" + GetTypeName(representationType.SubTypes[0], templates)
+ "," + GetTypeName(representationType.SubTypes[1], templates)
+ ">";
else if (representationType.Identifier == RepresentationTypeIdentifier.Tuple2 ||
representationType.Identifier == RepresentationTypeIdentifier.Tuple3 ||
representationType.Identifier == RepresentationTypeIdentifier.Tuple4 ||
representationType.Identifier == RepresentationTypeIdentifier.Tuple5 ||
representationType.Identifier == RepresentationTypeIdentifier.Tuple6 ||
representationType.Identifier == RepresentationTypeIdentifier.Tuple7)
name = "(" + String.Join(",", representationType.SubTypes.Select(x=> GetTypeName(x, templates)))
+ ")";
else
{
DataType.Bool => "bool",
DataType.BoolArray => "bool[]",
DataType.Char => "char",
DataType.CharArray => "char[]",
DataType.DateTime => "DateTime",
DataType.DateTimeArray => "DateTime[]",
DataType.Decimal => "decimal",
DataType.DecimalArray => "decimal[]",
DataType.Float32 => "float",
DataType.Float32Array => "float[]",
DataType.Float64 => "double",
DataType.Float64Array => "double[]",
DataType.Int16 => "short",
DataType.Int16Array => "short[]",
DataType.Int32 => "int",
DataType.Int32Array => "int[]",
DataType.Int64 => "long",
DataType.Int64Array => "long[]",
DataType.Int8 => "sbyte",
DataType.Int8Array => "sbyte[]",
DataType.String => "string",
DataType.StringArray => "string[]",
DataType.Structure => "Structure",
DataType.StructureArray => "Structure[]",
DataType.UInt16 => "ushort",
DataType.UInt16Array => "ushort[]",
DataType.UInt32 => "uint",
DataType.UInt32Array => "uint[]",
DataType.UInt64 => "ulong",
DataType.UInt64Array => "ulong[]",
DataType.UInt8 => "byte",
DataType.UInt8Array => "byte[]",
DataType.VarArray => "object[]",
DataType.Void => "object",
_ => "object"
};
return name;
name = representationType.Identifier switch
{
RepresentationTypeIdentifier.Dynamic => "object",
RepresentationTypeIdentifier.Bool => "bool",
RepresentationTypeIdentifier.Char => "char",
RepresentationTypeIdentifier.DateTime => "DateTime",
RepresentationTypeIdentifier.Decimal => "decimal",
RepresentationTypeIdentifier.Float32 => "float",
RepresentationTypeIdentifier.Float64 => "double",
RepresentationTypeIdentifier.Int16 => "short",
RepresentationTypeIdentifier.Int32 => "int",
RepresentationTypeIdentifier.Int64 => "long",
RepresentationTypeIdentifier.Int8 => "sbyte",
RepresentationTypeIdentifier.String => "string",
RepresentationTypeIdentifier.Map => "Map<object, object>",
RepresentationTypeIdentifier.UInt16 => "ushort",
RepresentationTypeIdentifier.UInt32 => "uint",
RepresentationTypeIdentifier.UInt64 => "ulong",
RepresentationTypeIdentifier.UInt8 => "byte",
RepresentationTypeIdentifier.List => "object[]",
RepresentationTypeIdentifier.Resource => "IResource",
RepresentationTypeIdentifier.Record => "IRecord",
_ => "object"
};
}
return (representationType.Nullable) ? name + "?" : name;
}
public static string GetTemplate(string url, string dir = null, string username = null, string password = null)
@ -142,16 +165,27 @@ public static class TemplateGenerator
var source = GenerateRecord(tmp, templates);
File.WriteAllText(tempDir.FullName + Path.DirectorySeparatorChar + tmp.ClassName + ".Generated.cs", source);
}
else if (tmp.Type == TemplateType.Enum)
{
var source = GenerateEnum(tmp, templates);
File.WriteAllText(tempDir.FullName + Path.DirectorySeparatorChar + tmp.ClassName + ".Generated.cs", source);
}
}
// generate info class
var typesFile = "using System; \r\n namespace Esiur { public static class Generated { public static Type[] Resources {get;} = new Type[] { " +
var typesFile = @"using System;
namespace Esiur {
public static class Generated {
public static Type[] Resources {get;} = new Type[] { " +
string.Join(",", templates.Where(x => x.Type == TemplateType.Resource).Select(x => $"typeof({x.ClassName})"))
+ " }; \r\n public static Type[] Records { get; } = new Type[] { " +
+ @" };
public static Type[] Records { get; } = new Type[] { " +
string.Join(",", templates.Where(x => x.Type == TemplateType.Record).Select(x => $"typeof({x.ClassName})"))
+ " }; " +
+ @" };
public static Type[] Enums { get; } = new Type[] { " +
string.Join(",", templates.Where(x => x.Type == TemplateType.Enum).Select(x => $"typeof({x.ClassName})"))
+ @" };" +
"\r\n } \r\n}";
@ -163,7 +197,6 @@ public static class TemplateGenerator
}
catch (Exception ex)
{
//File.WriteAllText("C:\\gen\\gettemplate.err", ex.ToString());
throw ex;
}
}
@ -179,13 +212,22 @@ public static class TemplateGenerator
rt.AppendLine("using System;\r\nusing Esiur.Resource;\r\nusing Esiur.Core;\r\nusing Esiur.Data;\r\nusing Esiur.Net.IIP;");
rt.AppendLine($"namespace { nameSpace} {{");
rt.AppendLine($"public class {className} : DistributedResource {{");
// extends
if (template.ParentId == null)
rt.AppendLine($"public class {className} : DistributedResource {{");
else
rt.AppendLine($"public class {className} : {templates.First(x => x.ClassId == template.ParentId && x.Type == TemplateType.Resource).ClassName} {{");
rt.AppendLine($"public {className}(DistributedConnection connection, uint instanceId, ulong age, string link) : base(connection, instanceId, age, link) {{}}");
rt.AppendLine($"public {className}() {{}}");
foreach (var f in template.Functions)
{
if (f.Inherited)
continue;
var rtTypeName = GetTypeName(f.ReturnType, templates);
rt.Append($"public AsyncReply<{rtTypeName}> {f.Name}(");
rt.Append(string.Join(",", f.Arguments.Select(x => GetTypeName(x.Type, templates) + " " + x.Name)));
@ -201,6 +243,9 @@ public static class TemplateGenerator
foreach (var p in template.Properties)
{
if (p.Inherited)
continue;
var ptTypeName = GetTypeName(p.ValueType, templates);
rt.AppendLine($"public {ptTypeName} {p.Name} {{");
rt.AppendLine($"get => ({ptTypeName})properties[{p.Index}];");
@ -208,8 +253,19 @@ public static class TemplateGenerator
rt.AppendLine("}");
}
foreach (var c in template.Constants)
{
if (c.Inherited)
continue;
var ctTypeName = GetTypeName(c.ValueType, templates);
rt.AppendLine($"public const {ctTypeName} {c.Name} = {c.Value};");
}
if (template.Events.Length > 0)
{
rt.AppendLine("protected override void _EmitEventByIndex(byte index, object args) {");
rt.AppendLine("switch (index) {");