mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
.Net 6 Upgrade
This commit is contained in:
@ -14,177 +14,176 @@ using Esiur.Data;
|
||||
using System.IO;
|
||||
using Esiur.Core;
|
||||
|
||||
namespace Esiur.Proxy
|
||||
namespace Esiur.Proxy;
|
||||
|
||||
[Generator]
|
||||
public class ResourceGenerator : ISourceGenerator
|
||||
{
|
||||
[Generator]
|
||||
public class ResourceGenerator : ISourceGenerator
|
||||
|
||||
private KeyList<string, TypeTemplate[]> cache = new();
|
||||
// private List<string> inProgress = new();
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
// Register receiver
|
||||
|
||||
context.RegisterForSyntaxNotifications(() => new ResourceGeneratorReceiver());
|
||||
}
|
||||
|
||||
|
||||
void ReportError(GeneratorExecutionContext context, string title, string msg, string category)
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(new DiagnosticDescriptor("MySG001", title, msg, category, DiagnosticSeverity.Error, true), Location.None));
|
||||
}
|
||||
|
||||
|
||||
void GenerateModel(GeneratorExecutionContext context, TypeTemplate[] templates)
|
||||
{
|
||||
foreach (var tmp in templates)
|
||||
{
|
||||
if (tmp.Type == TemplateType.Resource)
|
||||
{
|
||||
var source = TemplateGenerator.GenerateClass(tmp, templates);
|
||||
// File.WriteAllText($@"C:\gen\{tmp.ClassName}.cs", source);
|
||||
context.AddSource(tmp.ClassName + ".Generated.cs", source);
|
||||
}
|
||||
else if (tmp.Type == TemplateType.Record)
|
||||
{
|
||||
var source = TemplateGenerator.GenerateRecord(tmp, templates);
|
||||
// File.WriteAllText($@"C:\gen\{tmp.ClassName}.cs", source);
|
||||
context.AddSource(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[] { " +
|
||||
string.Join(",", templates.Where(x => x.Type == TemplateType.Resource).Select(x => $"typeof({x.ClassName})"))
|
||||
+ " }; \r\n public static Type[] Records { get; } = new Type[] { " +
|
||||
string.Join(",", templates.Where(x => x.Type == TemplateType.Record).Select(x => $"typeof({x.ClassName})"))
|
||||
+ " }; " +
|
||||
|
||||
"\r\n } \r\n}";
|
||||
|
||||
//File.WriteAllText($@"C:\gen\Esiur.Generated.cs", gen);
|
||||
|
||||
context.AddSource("Esiur.Generated.cs", typesFile);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
|
||||
private KeyList<string, TypeTemplate[]> cache = new();
|
||||
// private List<string> inProgress = new();
|
||||
if (!(context.SyntaxContextReceiver is ResourceGeneratorReceiver receiver))
|
||||
return;
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
//if (receiver.Imports.Count > 0 && !Debugger.IsAttached)
|
||||
//{
|
||||
// Debugger.Launch();
|
||||
//}
|
||||
|
||||
foreach (var path in receiver.Imports)
|
||||
{
|
||||
// Register receiver
|
||||
if (!TemplateGenerator.urlRegex.IsMatch(path))
|
||||
continue;
|
||||
|
||||
context.RegisterForSyntaxNotifications(() => new ResourceGeneratorReceiver());
|
||||
}
|
||||
|
||||
|
||||
void ReportError(GeneratorExecutionContext context, string title, string msg, string category)
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(new DiagnosticDescriptor("MySG001", title, msg, category, DiagnosticSeverity.Error, true), Location.None));
|
||||
}
|
||||
//File.WriteAllLines("C:\\gen\\ref.log", context.Compilation.ReferencedAssemblyNames.Select(x => x.ToString()));
|
||||
|
||||
|
||||
void GenerateModel(GeneratorExecutionContext context, TypeTemplate[] templates)
|
||||
{
|
||||
foreach (var tmp in templates)
|
||||
if (cache.Contains(path))
|
||||
{
|
||||
if (tmp.Type == TemplateType.Resource)
|
||||
{
|
||||
var source = TemplateGenerator.GenerateClass(tmp, templates);
|
||||
// File.WriteAllText($@"C:\gen\{tmp.ClassName}.cs", source);
|
||||
context.AddSource(tmp.ClassName + ".Generated.cs", source);
|
||||
}
|
||||
else if (tmp.Type == TemplateType.Record)
|
||||
{
|
||||
var source = TemplateGenerator.GenerateRecord(tmp, templates);
|
||||
// File.WriteAllText($@"C:\gen\{tmp.ClassName}.cs", source);
|
||||
context.AddSource(tmp.ClassName + ".Generated.cs", source);
|
||||
}
|
||||
GenerateModel(context, cache[path]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// generate info class
|
||||
// Syncronization
|
||||
//if (inProgress.Contains(path))
|
||||
// continue;
|
||||
|
||||
//inProgress.Add(path);
|
||||
|
||||
var url = TemplateGenerator.urlRegex.Split(path);
|
||||
|
||||
|
||||
var typesFile = "using System; \r\n 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[] { " +
|
||||
string.Join(",", templates.Where(x => x.Type == TemplateType.Record).Select(x => $"typeof({x.ClassName})"))
|
||||
+ " }; " +
|
||||
try
|
||||
{
|
||||
var con = Warehouse.Get<DistributedConnection>(url[1] + "://" + url[2]).Wait(20000);
|
||||
var templates = con.GetLinkTemplates(url[3]).Wait(60000);
|
||||
|
||||
"\r\n } \r\n}";
|
||||
cache[path] = templates;
|
||||
|
||||
//File.WriteAllText($@"C:\gen\Esiur.Generated.cs", gen);
|
||||
// make sources
|
||||
GenerateModel(context, templates);
|
||||
|
||||
context.AddSource("Esiur.Generated.cs", typesFile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ReportError(context, ex.Source, ex.Message, "Esiur");
|
||||
//System.IO.File.AppendAllText("c:\\gen\\error.log", ex.ToString() + "\r\n");
|
||||
}
|
||||
|
||||
//inProgress.Remove(path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
//#if DEBUG
|
||||
|
||||
//#endif
|
||||
|
||||
//var toImplement = receiver.Classes.Where(x => x.Fields.Length > 0);
|
||||
|
||||
foreach (var ci in receiver.Classes.Values)
|
||||
{
|
||||
|
||||
if (!(context.SyntaxContextReceiver is ResourceGeneratorReceiver receiver))
|
||||
return;
|
||||
|
||||
//if (receiver.Imports.Count > 0 && !Debugger.IsAttached)
|
||||
//{
|
||||
// Debugger.Launch();
|
||||
//}
|
||||
|
||||
foreach (var path in receiver.Imports)
|
||||
try
|
||||
{
|
||||
if (!TemplateGenerator.urlRegex.IsMatch(path))
|
||||
continue;
|
||||
|
||||
|
||||
//File.WriteAllLines("C:\\gen\\ref.log", context.Compilation.ReferencedAssemblyNames.Select(x => x.ToString()));
|
||||
|
||||
if (cache.Contains(path))
|
||||
{
|
||||
GenerateModel(context, cache[path]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Syncronization
|
||||
//if (inProgress.Contains(path))
|
||||
// continue;
|
||||
|
||||
//inProgress.Add(path);
|
||||
|
||||
var url = TemplateGenerator.urlRegex.Split(path);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var con = Warehouse.Get<DistributedConnection>(url[1] + "://" + url[2]).Wait(20000);
|
||||
var templates = con.GetLinkTemplates(url[3]).Wait(60000);
|
||||
|
||||
cache[path] = templates;
|
||||
|
||||
// make sources
|
||||
GenerateModel(context, templates);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ReportError(context, ex.Source, ex.Message, "Esiur");
|
||||
//System.IO.File.AppendAllText("c:\\gen\\error.log", ex.ToString() + "\r\n");
|
||||
}
|
||||
|
||||
//inProgress.Remove(path);
|
||||
}
|
||||
|
||||
|
||||
//#if DEBUG
|
||||
|
||||
//#endif
|
||||
|
||||
//var toImplement = receiver.Classes.Where(x => x.Fields.Length > 0);
|
||||
|
||||
foreach (var ci in receiver.Classes.Values)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var code = @$"using Esiur.Resource;
|
||||
var code = @$"using Esiur.Resource;
|
||||
using Esiur.Core;
|
||||
namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{
|
||||
";
|
||||
|
||||
if (ci.HasInterface)
|
||||
code += $"public partial class {ci.Name} {{";
|
||||
else
|
||||
{
|
||||
code += @$"public partial class {ci.Name} : IResource {{
|
||||
if (ci.HasInterface)
|
||||
code += $"public partial class {ci.Name} {{";
|
||||
else
|
||||
{
|
||||
code += @$"public partial class {ci.Name} : IResource {{
|
||||
public Instance Instance {{ get; set; }}
|
||||
public event DestroyedEvent OnDestroy;
|
||||
public virtual void Destroy() {{ OnDestroy?.Invoke(this); }}
|
||||
";
|
||||
|
||||
if (!ci.HasTrigger)
|
||||
code += "public AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n";
|
||||
}
|
||||
|
||||
//Debugger.Launch();
|
||||
|
||||
foreach (var f in ci.Fields)
|
||||
{
|
||||
var givenName = f.GetAttributes().Where(x=>x.AttributeClass.Name == "PublicAttribute").FirstOrDefault()?.ConstructorArguments.FirstOrDefault().Value;
|
||||
|
||||
var fn = f.Name;
|
||||
var pn = givenName ?? fn.Substring(0, 1).ToUpper() + fn.Substring(1);
|
||||
|
||||
//System.IO.File.AppendAllText("c:\\gen\\fields.txt", fn + " -> " + pn + "\r\n");
|
||||
|
||||
// copy attributes
|
||||
var attrs = string.Join(" ", f.GetAttributes().Select(x => $"[{x.ToString()}]"));
|
||||
code += $"{attrs} public {f.Type} {pn} {{ get => {fn}; set {{ {fn} = value; Instance?.Modified(); }} }}\r\n";
|
||||
}
|
||||
|
||||
code += "}}\r\n";
|
||||
|
||||
//System.IO.File.WriteAllText("c:\\gen\\" + ci.Name + "_esiur.cs", code);
|
||||
context.AddSource(ci.Name + ".Generated.cs", code);
|
||||
|
||||
if (!ci.HasTrigger)
|
||||
code += "public AsyncReply<bool> Trigger(ResourceTrigger trigger) => new AsyncReply<bool>(true);\r\n";
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
//Debugger.Launch();
|
||||
|
||||
foreach (var f in ci.Fields)
|
||||
{
|
||||
//System.IO.File.AppendAllText("c:\\gen\\error.log", ci.Name + " " + ex.ToString() + "\r\n");
|
||||
var givenName = f.GetAttributes().Where(x => x.AttributeClass.Name == "PublicAttribute").FirstOrDefault()?.ConstructorArguments.FirstOrDefault().Value;
|
||||
|
||||
var fn = f.Name;
|
||||
var pn = givenName ?? fn.Substring(0, 1).ToUpper() + fn.Substring(1);
|
||||
|
||||
//System.IO.File.AppendAllText("c:\\gen\\fields.txt", fn + " -> " + pn + "\r\n");
|
||||
|
||||
// copy attributes
|
||||
var attrs = string.Join(" ", f.GetAttributes().Select(x => $"[{x.ToString()}]"));
|
||||
code += $"{attrs} public {f.Type} {pn} {{ get => {fn}; set {{ {fn} = value; Instance?.Modified(); }} }}\r\n";
|
||||
}
|
||||
|
||||
code += "}}\r\n";
|
||||
|
||||
//System.IO.File.WriteAllText("c:\\gen\\" + ci.Name + "_esiur.cs", code);
|
||||
context.AddSource(ci.Name + ".Generated.cs", code);
|
||||
|
||||
}
|
||||
catch //(Exception ex)
|
||||
{
|
||||
//System.IO.File.AppendAllText("c:\\gen\\error.log", ci.Name + " " + ex.ToString() + "\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,18 +4,16 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Proxy
|
||||
namespace Esiur.Proxy;
|
||||
public struct ResourceGeneratorClassInfo
|
||||
{
|
||||
public struct ResourceGeneratorClassInfo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public bool HasInterface { get; set; }
|
||||
public string Name { get; set; }
|
||||
public bool HasInterface { get; set; }
|
||||
|
||||
public bool HasTrigger { get; set; }
|
||||
public List<IFieldSymbol> Fields { get; set; }
|
||||
public ITypeSymbol ClassSymbol { get; set; }
|
||||
public bool HasTrigger { get; set; }
|
||||
public List<IFieldSymbol> Fields { get; set; }
|
||||
public ITypeSymbol ClassSymbol { get; set; }
|
||||
|
||||
public ClassDeclarationSyntax ClassDeclaration { get; set; }
|
||||
public ClassDeclarationSyntax ClassDeclaration { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Proxy
|
||||
namespace Esiur.Proxy;
|
||||
public struct ResourceGeneratorFieldInfo
|
||||
{
|
||||
public struct ResourceGeneratorFieldInfo
|
||||
{
|
||||
public IFieldSymbol FieldSymbol { get; set; }
|
||||
public string[] Attributes { get; set; }
|
||||
}
|
||||
public IFieldSymbol FieldSymbol { get; set; }
|
||||
public string[] Attributes { get; set; }
|
||||
}
|
||||
|
@ -6,92 +6,91 @@ using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Proxy
|
||||
namespace Esiur.Proxy;
|
||||
public class ResourceGeneratorReceiver : ISyntaxContextReceiver
|
||||
{
|
||||
public class ResourceGeneratorReceiver : ISyntaxContextReceiver
|
||||
|
||||
public Dictionary<string, ResourceGeneratorClassInfo> Classes { get; } = new();
|
||||
|
||||
public List<string> Imports { get; } = new();
|
||||
|
||||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
|
||||
{
|
||||
|
||||
public Dictionary<string, ResourceGeneratorClassInfo> Classes { get; } = new();
|
||||
|
||||
public List<string> Imports { get; } = new ();
|
||||
|
||||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
|
||||
if (context.Node is ClassDeclarationSyntax)
|
||||
{
|
||||
var cds = context.Node as ClassDeclarationSyntax;
|
||||
var cls = context.SemanticModel.GetDeclaredSymbol(cds) as ITypeSymbol;
|
||||
var attrs = cls.GetAttributes();
|
||||
|
||||
if (context.Node is ClassDeclarationSyntax)
|
||||
var imports = attrs.Where(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.ImportAttribute");
|
||||
|
||||
foreach (var import in imports)
|
||||
{
|
||||
var cds = context.Node as ClassDeclarationSyntax;
|
||||
var cls = context.SemanticModel.GetDeclaredSymbol(cds) as ITypeSymbol;
|
||||
var attrs = cls.GetAttributes();
|
||||
// Debugger.Launch();
|
||||
|
||||
var imports = attrs.Where(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.ImportAttribute");
|
||||
var url = import.ConstructorArguments.First().Value.ToString();
|
||||
|
||||
foreach (var import in imports)
|
||||
if (!Imports.Contains(url))
|
||||
Imports.Add(url);
|
||||
}
|
||||
|
||||
if (attrs.Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.ResourceAttribute"))
|
||||
{
|
||||
|
||||
|
||||
var hasTrigger = cds.Members
|
||||
.Where(x => x is MethodDeclarationSyntax)
|
||||
.Select(x => context.SemanticModel.GetDeclaredSymbol(x) as IMethodSymbol)
|
||||
.Any(x => x.Name == "Trigger"
|
||||
&& x.Parameters.Length == 1
|
||||
&& x.Parameters[0].Type.ToDisplayString() == "Esiur.Resource.ResourceTrigger");
|
||||
|
||||
var fields = cds.Members.Where(x => x is FieldDeclarationSyntax)
|
||||
.Select(x => context.SemanticModel.GetDeclaredSymbol((x as FieldDeclarationSyntax).Declaration.Variables.First()) as IFieldSymbol)
|
||||
.Where(x => x.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.PublicAttribute"))
|
||||
.ToArray();
|
||||
|
||||
//if (!Debugger.IsAttached)
|
||||
//{
|
||||
// if (cls.Name == "User")
|
||||
// Debugger.Launch();
|
||||
//}
|
||||
|
||||
|
||||
|
||||
// get fields
|
||||
|
||||
var fullName = cls.ContainingAssembly + "." + cls.Name;
|
||||
|
||||
// Partial class check
|
||||
if (Classes.ContainsKey(fullName))
|
||||
{
|
||||
// Debugger.Launch();
|
||||
|
||||
var url = import.ConstructorArguments.First().Value.ToString();
|
||||
|
||||
if (!Imports.Contains(url))
|
||||
Imports.Add(url);
|
||||
// append fields
|
||||
var c = Classes[fullName];
|
||||
c.Fields.AddRange(fields);
|
||||
if (!c.HasInterface)
|
||||
c.HasInterface = cls.Interfaces.Any(x => x.ToDisplayString() == "Esiur.Resource.IResource");
|
||||
if (!c.HasTrigger)
|
||||
c.HasTrigger = hasTrigger;
|
||||
}
|
||||
else
|
||||
{
|
||||
Classes.Add(fullName, new ResourceGeneratorClassInfo()
|
||||
{
|
||||
Name = cls.Name,
|
||||
ClassDeclaration = cds,
|
||||
ClassSymbol = cls,
|
||||
Fields = fields.ToList(),
|
||||
HasInterface = cls.Interfaces.Any(x => x.ToDisplayString() == "Esiur.Resource.IResource"),
|
||||
HasTrigger = hasTrigger
|
||||
});
|
||||
}
|
||||
|
||||
if (attrs.Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.ResourceAttribute"))
|
||||
{
|
||||
|
||||
|
||||
var hasTrigger = cds.Members
|
||||
.Where(x => x is MethodDeclarationSyntax)
|
||||
.Select(x => context.SemanticModel.GetDeclaredSymbol(x) as IMethodSymbol)
|
||||
.Any(x => x.Name == "Trigger"
|
||||
&& x.Parameters.Length == 1
|
||||
&& x.Parameters[0].Type.ToDisplayString() == "Esiur.Resource.ResourceTrigger");
|
||||
|
||||
var fields = cds.Members.Where(x => x is FieldDeclarationSyntax)
|
||||
.Select(x => context.SemanticModel.GetDeclaredSymbol((x as FieldDeclarationSyntax).Declaration.Variables.First()) as IFieldSymbol)
|
||||
.Where(x => x.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.PublicAttribute"))
|
||||
.ToArray();
|
||||
|
||||
//if (!Debugger.IsAttached)
|
||||
//{
|
||||
// if (cls.Name == "User")
|
||||
// Debugger.Launch();
|
||||
//}
|
||||
|
||||
|
||||
|
||||
// get fields
|
||||
|
||||
var fullName = cls.ContainingAssembly + "." + cls.Name;
|
||||
|
||||
// Partial class check
|
||||
if (Classes.ContainsKey(fullName))
|
||||
{
|
||||
// append fields
|
||||
var c = Classes[fullName];
|
||||
c.Fields.AddRange(fields);
|
||||
if (!c.HasInterface)
|
||||
c.HasInterface = cls.Interfaces.Any(x => x.ToDisplayString() == "Esiur.Resource.IResource");
|
||||
if (!c.HasTrigger)
|
||||
c.HasTrigger = hasTrigger;
|
||||
} else
|
||||
{
|
||||
Classes.Add(fullName, new ResourceGeneratorClassInfo()
|
||||
{
|
||||
Name = cls.Name,
|
||||
ClassDeclaration = cds,
|
||||
ClassSymbol = cls,
|
||||
Fields = fields.ToList(),
|
||||
HasInterface = cls.Interfaces.Any(x => x.ToDisplayString() == "Esiur.Resource.IResource"),
|
||||
HasTrigger = hasTrigger
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -7,72 +7,71 @@ using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Proxy
|
||||
namespace Esiur.Proxy;
|
||||
public static class ResourceProxy
|
||||
{
|
||||
public static class ResourceProxy
|
||||
{
|
||||
static Dictionary<Type, Type> cache = new Dictionary<Type, Type>();
|
||||
|
||||
static Dictionary<Type, Type> cache = new Dictionary<Type, Type>();
|
||||
|
||||
#if NETSTANDARD
|
||||
static MethodInfo modifyMethod = typeof(Instance).GetTypeInfo().GetMethod("Modified");
|
||||
static MethodInfo instanceGet = typeof(IResource).GetTypeInfo().GetProperty("Instance").GetGetMethod();
|
||||
static MethodInfo modifyMethod = typeof(Instance).GetTypeInfo().GetMethod("Modified");
|
||||
static MethodInfo instanceGet = typeof(IResource).GetTypeInfo().GetProperty("Instance").GetGetMethod();
|
||||
#else
|
||||
static MethodInfo modifyMethod = typeof(Instance).GetMethod("Modified");
|
||||
static MethodInfo instanceGet = typeof(IResource).GetProperty("Instance").GetGetMethod();
|
||||
#endif
|
||||
|
||||
|
||||
public static Type GetBaseType(object resource)
|
||||
public static Type GetBaseType(object resource)
|
||||
{
|
||||
return GetBaseType(resource.GetType());
|
||||
}
|
||||
|
||||
public static Type GetBaseType(Type type)
|
||||
{
|
||||
if (type.Assembly.IsDynamic)
|
||||
return type.GetTypeInfo().BaseType;
|
||||
else
|
||||
return type;
|
||||
|
||||
// if (type.FullName.Contains("Esiur.Proxy.T"))
|
||||
//#if NETSTANDARD
|
||||
// return type.GetTypeInfo().BaseType;
|
||||
//#else
|
||||
// return type.BaseType;
|
||||
//#endif
|
||||
// else
|
||||
// return type;
|
||||
}
|
||||
|
||||
public static Type GetProxy(Type type)
|
||||
{
|
||||
|
||||
if (cache.ContainsKey(type))
|
||||
return cache[type];
|
||||
|
||||
// check if the type was made with code generation
|
||||
if (type.GetCustomAttribute<ResourceAttribute>(false) != null)
|
||||
{
|
||||
return GetBaseType(resource.GetType());
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
public static Type GetBaseType(Type type)
|
||||
if (!Codec.ImplementsInterface(type, typeof(IResource)))
|
||||
{
|
||||
if (type.Assembly.IsDynamic)
|
||||
return type.GetTypeInfo().BaseType;
|
||||
else
|
||||
return type;
|
||||
|
||||
// if (type.FullName.Contains("Esiur.Proxy.T"))
|
||||
//#if NETSTANDARD
|
||||
// return type.GetTypeInfo().BaseType;
|
||||
//#else
|
||||
// return type.BaseType;
|
||||
//#endif
|
||||
// else
|
||||
// return type;
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
public static Type GetProxy(Type type)
|
||||
{
|
||||
|
||||
if (cache.ContainsKey(type))
|
||||
return cache[type];
|
||||
|
||||
// check if the type was made with code generation
|
||||
if (type.GetCustomAttribute<ResourceAttribute>(false) != null)
|
||||
{
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
if (!Codec.ImplementsInterface(type, typeof(IResource)))
|
||||
{
|
||||
cache.Add(type, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
|
||||
if (typeInfo.IsSealed || typeInfo.IsAbstract)
|
||||
throw new Exception("Sealed/Abastract classes can't be proxied.");
|
||||
if (typeInfo.IsSealed || typeInfo.IsAbstract)
|
||||
throw new Exception("Sealed/Abastract classes can't be proxied.");
|
||||
|
||||
var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
where p.CanWrite && p.SetMethod.IsVirtual && !p.SetMethod.IsFinal &&
|
||||
p.GetCustomAttribute<PublicAttribute>(false) != null
|
||||
select p;
|
||||
var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
where p.CanWrite && p.SetMethod.IsVirtual && !p.SetMethod.IsFinal &&
|
||||
p.GetCustomAttribute<PublicAttribute>(false) != null
|
||||
select p;
|
||||
|
||||
#else
|
||||
if (type.IsSealed)
|
||||
@ -84,172 +83,171 @@ namespace Esiur.Proxy
|
||||
select p;
|
||||
|
||||
#endif
|
||||
var assemblyName = new AssemblyName("Esiur.Proxy.T." + type.Assembly.GetName().Name);// type.Namespace);
|
||||
assemblyName.Version = type.Assembly.GetName().Version;
|
||||
assemblyName.CultureInfo = type.Assembly.GetName().CultureInfo;
|
||||
//assemblyName.SetPublicKeyToken(null);
|
||||
var assemblyName = new AssemblyName("Esiur.Proxy.T." + type.Assembly.GetName().Name);// type.Namespace);
|
||||
assemblyName.Version = type.Assembly.GetName().Version;
|
||||
assemblyName.CultureInfo = type.Assembly.GetName().CultureInfo;
|
||||
//assemblyName.SetPublicKeyToken(null);
|
||||
|
||||
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
|
||||
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
|
||||
var typeName = "Esiur.Proxy.T." + type.FullName;// Assembly.CreateQualifiedName(assemblyName.FullName, "Esiur.Proxy.T." + type.FullName);
|
||||
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
|
||||
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
|
||||
var typeName = "Esiur.Proxy.T." + type.FullName;// Assembly.CreateQualifiedName(assemblyName.FullName, "Esiur.Proxy.T." + type.FullName);
|
||||
|
||||
var typeBuilder = moduleBuilder.DefineType(typeName,
|
||||
TypeAttributes.Public | TypeAttributes.Class, type);
|
||||
var typeBuilder = moduleBuilder.DefineType(typeName,
|
||||
TypeAttributes.Public | TypeAttributes.Class, type);
|
||||
|
||||
foreach (PropertyInfo propertyInfo in props)
|
||||
CreateProperty(propertyInfo, typeBuilder, type);
|
||||
foreach (PropertyInfo propertyInfo in props)
|
||||
CreateProperty(propertyInfo, typeBuilder, type);
|
||||
|
||||
|
||||
|
||||
#if NETSTANDARD
|
||||
var t = typeBuilder.CreateTypeInfo().AsType();
|
||||
cache.Add(type, t);
|
||||
return t;
|
||||
var t = typeBuilder.CreateTypeInfo().AsType();
|
||||
cache.Add(type, t);
|
||||
return t;
|
||||
#else
|
||||
|
||||
var t = typeBuilder.CreateType();
|
||||
cache.Add(type, t);
|
||||
return t;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static Type GetProxy<T>()
|
||||
where T : IResource
|
||||
{
|
||||
return GetProxy(typeof(T));
|
||||
}
|
||||
|
||||
|
||||
|
||||
//private static void C
|
||||
private static void CreateProperty(PropertyInfo pi, TypeBuilder typeBuilder, Type resourceType)
|
||||
{
|
||||
var propertyBuilder = typeBuilder.DefineProperty(pi.Name, PropertyAttributes.None, pi.PropertyType, null);
|
||||
|
||||
// Create set method
|
||||
MethodBuilder builder = typeBuilder.DefineMethod("set_" + pi.Name,
|
||||
MethodAttributes.Public | MethodAttributes.Virtual, null, new Type[] { pi.PropertyType });
|
||||
builder.DefineParameter(1, ParameterAttributes.None, "value");
|
||||
ILGenerator g = builder.GetILGenerator();
|
||||
|
||||
var getInstance = resourceType.GetTypeInfo().GetProperty("Instance").GetGetMethod();
|
||||
|
||||
|
||||
//g.Emit(OpCodes.Ldarg_0);
|
||||
//g.Emit(OpCodes.Ldarg_1);
|
||||
//g.Emit(OpCodes.Call, pi.GetSetMethod());
|
||||
//g.Emit(OpCodes.Nop);
|
||||
|
||||
//g.Emit(OpCodes.Ldarg_0);
|
||||
//g.Emit(OpCodes.Call, getInstance);
|
||||
//g.Emit(OpCodes.Ldstr, pi.Name);
|
||||
//g.Emit(OpCodes.Call, modifyMethod);
|
||||
//g.Emit(OpCodes.Nop);
|
||||
|
||||
//g.Emit(OpCodes.Ret);
|
||||
|
||||
Label exitMethod = g.DefineLabel();
|
||||
Label callModified = g.DefineLabel();
|
||||
|
||||
g.Emit(OpCodes.Nop);
|
||||
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Ldarg_1);
|
||||
g.Emit(OpCodes.Call, pi.GetSetMethod());
|
||||
g.Emit(OpCodes.Nop);
|
||||
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Call, getInstance);
|
||||
g.Emit(OpCodes.Dup);
|
||||
|
||||
g.Emit(OpCodes.Brtrue_S, callModified);
|
||||
|
||||
g.Emit(OpCodes.Pop);
|
||||
g.Emit(OpCodes.Br_S, exitMethod);
|
||||
|
||||
g.MarkLabel(callModified);
|
||||
|
||||
g.Emit(OpCodes.Ldstr, pi.Name);
|
||||
g.Emit(OpCodes.Call, modifyMethod);
|
||||
g.Emit(OpCodes.Nop);
|
||||
|
||||
g.MarkLabel(exitMethod);
|
||||
g.Emit(OpCodes.Ret);
|
||||
propertyBuilder.SetSetMethod(builder);
|
||||
|
||||
|
||||
builder = typeBuilder.DefineMethod("get_" + pi.Name, MethodAttributes.Public | MethodAttributes.Virtual, pi.PropertyType, null);
|
||||
g = builder.GetILGenerator();
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Call, pi.GetGetMethod());
|
||||
g.Emit(OpCodes.Ret);
|
||||
|
||||
propertyBuilder.SetGetMethod(builder);
|
||||
|
||||
// g.Emit(OpCodes.Ldarg_0);
|
||||
// g.Emit(OpCodes.Call, pi.GetGetMethod());
|
||||
// g.Emit(OpCodes.Ret);
|
||||
|
||||
// propertyBuilder.SetGetMethod(builder);
|
||||
|
||||
|
||||
/*
|
||||
Label callModified = g.DefineLabel();
|
||||
Label exitMethod = g.DefineLabel();
|
||||
|
||||
|
||||
// IL_0000: ldarg.0
|
||||
//IL_0001: call instance class [Esiur]Esiur.Resource.Instance [Esiur]Esiur.Resource.Resource::get_Instance()
|
||||
//// (no C# code)
|
||||
//IL_0006: dup
|
||||
//IL_0007: brtrue.s IL_000c
|
||||
//IL_0009: pop
|
||||
//// }
|
||||
//IL_000a: br.s IL_0017
|
||||
//// (no C# code)
|
||||
//IL_000c: ldstr "Level3"
|
||||
//IL_0011: call instance void [Esiur]Esiur.Resource.Instance::Modified(string)
|
||||
//IL_0016: nop
|
||||
//IL_0017: ret
|
||||
|
||||
|
||||
// Add IL code for set method
|
||||
g.Emit(OpCodes.Nop);
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Ldarg_1);
|
||||
g.Emit(OpCodes.Call, pi.GetSetMethod());
|
||||
|
||||
|
||||
// IL_0000: ldarg.0
|
||||
// IL_0001: call instance class [Esiur]Esiur.Resource.Instance [Esiur]Esiur.Resource.Resource::get_Instance()
|
||||
// IL_0006: ldstr "Level3"
|
||||
//IL_000b: callvirt instance void [Esiur]Esiur.Resource.Instance::Modified(string)
|
||||
//IL_0010: ret
|
||||
|
||||
|
||||
// Call property changed for object
|
||||
g.Emit(OpCodes.Nop);
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Call, instanceGet);
|
||||
|
||||
g.Emit(OpCodes.Dup);
|
||||
g.Emit(OpCodes.Brtrue_S, callModified);
|
||||
g.Emit(OpCodes.Pop);
|
||||
g.Emit(OpCodes.Br_S, exitMethod);
|
||||
|
||||
g.MarkLabel(callModified);
|
||||
g.Emit(OpCodes.Ldstr, pi.Name);
|
||||
g.Emit(OpCodes.Callvirt, modifyMethod);
|
||||
g.Emit(OpCodes.Nop);
|
||||
g.MarkLabel(exitMethod);
|
||||
g.Emit(OpCodes.Ret);
|
||||
propertyBuilder.SetSetMethod(builder);
|
||||
|
||||
|
||||
// create get method
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Type GetProxy<T>()
|
||||
where T : IResource
|
||||
{
|
||||
return GetProxy(typeof(T));
|
||||
}
|
||||
|
||||
|
||||
|
||||
//private static void C
|
||||
private static void CreateProperty(PropertyInfo pi, TypeBuilder typeBuilder, Type resourceType)
|
||||
{
|
||||
var propertyBuilder = typeBuilder.DefineProperty(pi.Name, PropertyAttributes.None, pi.PropertyType, null);
|
||||
|
||||
// Create set method
|
||||
MethodBuilder builder = typeBuilder.DefineMethod("set_" + pi.Name,
|
||||
MethodAttributes.Public | MethodAttributes.Virtual, null, new Type[] { pi.PropertyType });
|
||||
builder.DefineParameter(1, ParameterAttributes.None, "value");
|
||||
ILGenerator g = builder.GetILGenerator();
|
||||
|
||||
var getInstance = resourceType.GetTypeInfo().GetProperty("Instance").GetGetMethod();
|
||||
|
||||
|
||||
//g.Emit(OpCodes.Ldarg_0);
|
||||
//g.Emit(OpCodes.Ldarg_1);
|
||||
//g.Emit(OpCodes.Call, pi.GetSetMethod());
|
||||
//g.Emit(OpCodes.Nop);
|
||||
|
||||
//g.Emit(OpCodes.Ldarg_0);
|
||||
//g.Emit(OpCodes.Call, getInstance);
|
||||
//g.Emit(OpCodes.Ldstr, pi.Name);
|
||||
//g.Emit(OpCodes.Call, modifyMethod);
|
||||
//g.Emit(OpCodes.Nop);
|
||||
|
||||
//g.Emit(OpCodes.Ret);
|
||||
|
||||
Label exitMethod = g.DefineLabel();
|
||||
Label callModified = g.DefineLabel();
|
||||
|
||||
g.Emit(OpCodes.Nop);
|
||||
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Ldarg_1);
|
||||
g.Emit(OpCodes.Call, pi.GetSetMethod());
|
||||
g.Emit(OpCodes.Nop);
|
||||
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Call, getInstance);
|
||||
g.Emit(OpCodes.Dup);
|
||||
|
||||
g.Emit(OpCodes.Brtrue_S, callModified);
|
||||
|
||||
g.Emit(OpCodes.Pop);
|
||||
g.Emit(OpCodes.Br_S, exitMethod);
|
||||
|
||||
g.MarkLabel(callModified);
|
||||
|
||||
g.Emit(OpCodes.Ldstr, pi.Name);
|
||||
g.Emit(OpCodes.Call, modifyMethod);
|
||||
g.Emit(OpCodes.Nop);
|
||||
|
||||
g.MarkLabel(exitMethod);
|
||||
g.Emit(OpCodes.Ret);
|
||||
propertyBuilder.SetSetMethod(builder);
|
||||
|
||||
|
||||
builder = typeBuilder.DefineMethod("get_" + pi.Name, MethodAttributes.Public | MethodAttributes.Virtual, pi.PropertyType, null);
|
||||
g = builder.GetILGenerator();
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Call, pi.GetGetMethod());
|
||||
g.Emit(OpCodes.Ret);
|
||||
|
||||
propertyBuilder.SetGetMethod(builder);
|
||||
|
||||
// g.Emit(OpCodes.Ldarg_0);
|
||||
// g.Emit(OpCodes.Call, pi.GetGetMethod());
|
||||
// g.Emit(OpCodes.Ret);
|
||||
|
||||
// propertyBuilder.SetGetMethod(builder);
|
||||
|
||||
|
||||
/*
|
||||
Label callModified = g.DefineLabel();
|
||||
Label exitMethod = g.DefineLabel();
|
||||
|
||||
|
||||
// IL_0000: ldarg.0
|
||||
//IL_0001: call instance class [Esiur]Esiur.Resource.Instance [Esiur]Esiur.Resource.Resource::get_Instance()
|
||||
//// (no C# code)
|
||||
//IL_0006: dup
|
||||
//IL_0007: brtrue.s IL_000c
|
||||
//IL_0009: pop
|
||||
//// }
|
||||
//IL_000a: br.s IL_0017
|
||||
//// (no C# code)
|
||||
//IL_000c: ldstr "Level3"
|
||||
//IL_0011: call instance void [Esiur]Esiur.Resource.Instance::Modified(string)
|
||||
//IL_0016: nop
|
||||
//IL_0017: ret
|
||||
|
||||
|
||||
// Add IL code for set method
|
||||
g.Emit(OpCodes.Nop);
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Ldarg_1);
|
||||
g.Emit(OpCodes.Call, pi.GetSetMethod());
|
||||
|
||||
|
||||
// IL_0000: ldarg.0
|
||||
// IL_0001: call instance class [Esiur]Esiur.Resource.Instance [Esiur]Esiur.Resource.Resource::get_Instance()
|
||||
// IL_0006: ldstr "Level3"
|
||||
//IL_000b: callvirt instance void [Esiur]Esiur.Resource.Instance::Modified(string)
|
||||
//IL_0010: ret
|
||||
|
||||
|
||||
// Call property changed for object
|
||||
g.Emit(OpCodes.Nop);
|
||||
g.Emit(OpCodes.Ldarg_0);
|
||||
g.Emit(OpCodes.Call, instanceGet);
|
||||
|
||||
g.Emit(OpCodes.Dup);
|
||||
g.Emit(OpCodes.Brtrue_S, callModified);
|
||||
g.Emit(OpCodes.Pop);
|
||||
g.Emit(OpCodes.Br_S, exitMethod);
|
||||
|
||||
g.MarkLabel(callModified);
|
||||
g.Emit(OpCodes.Ldstr, pi.Name);
|
||||
g.Emit(OpCodes.Callvirt, modifyMethod);
|
||||
g.Emit(OpCodes.Nop);
|
||||
g.MarkLabel(exitMethod);
|
||||
g.Emit(OpCodes.Ret);
|
||||
propertyBuilder.SetSetMethod(builder);
|
||||
|
||||
|
||||
// create get method
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,229 +10,227 @@ using Esiur.Resource;
|
||||
using Esiur.Net.IIP;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Esiur.Proxy
|
||||
namespace Esiur.Proxy;
|
||||
public static class TemplateGenerator
|
||||
{
|
||||
public static class TemplateGenerator
|
||||
internal static Regex urlRegex = new Regex(@"^(?:([\S]*)://([^/]*)/?)");
|
||||
|
||||
internal static string GenerateRecord(TypeTemplate template, TypeTemplate[] templates)
|
||||
{
|
||||
internal static Regex urlRegex = new Regex(@"^(?:([\S]*)://([^/]*)/?)");
|
||||
var cls = template.ClassName.Split('.');
|
||||
|
||||
internal static string GenerateRecord(TypeTemplate template, TypeTemplate[] templates)
|
||||
var nameSpace = string.Join(".", cls.Take(cls.Length - 1));
|
||||
var className = cls.Last();
|
||||
|
||||
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 class {className} : IRecord {{");
|
||||
|
||||
|
||||
foreach (var p in template.Properties)
|
||||
{
|
||||
var cls = template.ClassName.Split('.');
|
||||
|
||||
var nameSpace = string.Join(".", cls.Take(cls.Length - 1));
|
||||
var className = cls.Last();
|
||||
|
||||
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 class {className} : IRecord {{");
|
||||
|
||||
|
||||
foreach (var p in template.Properties)
|
||||
{
|
||||
var ptTypeName = GetTypeName(p.ValueType, templates);
|
||||
rt.AppendLine($"public {ptTypeName} {p.Name} {{ get; set; }}");
|
||||
rt.AppendLine();
|
||||
}
|
||||
|
||||
rt.AppendLine("\r\n}\r\n}");
|
||||
|
||||
return rt.ToString();
|
||||
var ptTypeName = GetTypeName(p.ValueType, templates);
|
||||
rt.AppendLine($"public {ptTypeName} {p.Name} {{ get; set; }}");
|
||||
rt.AppendLine();
|
||||
}
|
||||
|
||||
static string GetTypeName(TemplateDataType templateDataType, TypeTemplate[] templates)
|
||||
{
|
||||
|
||||
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 name = templateDataType.Type switch
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public static string GetTemplate(string url, string dir = null, string username= null, string password = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
if (!urlRegex.IsMatch(url))
|
||||
throw new Exception("Invalid IIP URL");
|
||||
|
||||
var path = urlRegex.Split(url);
|
||||
var con = Warehouse.Get<DistributedConnection>(path[1] + "://" + path[2],
|
||||
!string.IsNullOrEmpty( username) && !string.IsNullOrEmpty( password) ? new { Username = username, Password = password } : null
|
||||
).Wait(20000);
|
||||
|
||||
if (con == null)
|
||||
throw new Exception("Can't connect to server");
|
||||
|
||||
if (string.IsNullOrEmpty(dir))
|
||||
dir = path[2].Replace(":", "_");
|
||||
|
||||
var templates = con.GetLinkTemplates(path[3]).Wait(60000);
|
||||
// no longer needed
|
||||
Warehouse.Remove(con);
|
||||
|
||||
var tempDir = new DirectoryInfo(Path.GetTempPath() + Path.DirectorySeparatorChar
|
||||
+ Misc.Global.GenerateCode(20) + Path.DirectorySeparatorChar + dir);
|
||||
|
||||
if (!tempDir.Exists)
|
||||
tempDir.Create();
|
||||
else
|
||||
{
|
||||
foreach (FileInfo file in tempDir.GetFiles())
|
||||
file.Delete();
|
||||
}
|
||||
|
||||
// make sources
|
||||
foreach (var tmp in templates)
|
||||
{
|
||||
if (tmp.Type == TemplateType.Resource)
|
||||
{
|
||||
var source = GenerateClass(tmp, templates);
|
||||
File.WriteAllText(tempDir.FullName + Path.DirectorySeparatorChar + tmp.ClassName + ".Generated.cs", source);
|
||||
}
|
||||
else if (tmp.Type == TemplateType.Record)
|
||||
{
|
||||
var source = GenerateRecord(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[] { " +
|
||||
string.Join(",", templates.Where(x => x.Type == TemplateType.Resource).Select(x => $"typeof({x.ClassName})"))
|
||||
+ " }; \r\n public static Type[] Records { get; } = new Type[] { " +
|
||||
string.Join(",", templates.Where(x => x.Type == TemplateType.Record).Select(x => $"typeof({x.ClassName})"))
|
||||
+ " }; " +
|
||||
|
||||
"\r\n } \r\n}";
|
||||
|
||||
|
||||
File.WriteAllText(tempDir.FullName + Path.DirectorySeparatorChar + "Esiur.Generated.cs", typesFile);
|
||||
|
||||
|
||||
return tempDir.FullName;
|
||||
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
//File.WriteAllText("C:\\gen\\gettemplate.err", ex.ToString());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GenerateClass(TypeTemplate template, TypeTemplate[] templates)
|
||||
{
|
||||
var cls = template.ClassName.Split('.');
|
||||
|
||||
var nameSpace = string.Join(".", cls.Take(cls.Length - 1));
|
||||
var className = cls.Last();
|
||||
|
||||
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 class {className} : DistributedResource {{");
|
||||
|
||||
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)
|
||||
{
|
||||
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)));
|
||||
|
||||
rt.AppendLine(") {");
|
||||
rt.AppendLine($"var rt = new AsyncReply<{rtTypeName}>();");
|
||||
rt.AppendLine($"_InvokeByArrayArguments({f.Index}, new object[] {{ { string.Join(", ", f.Arguments.Select(x => x.Name)) } }})");
|
||||
rt.AppendLine($".Then(x => rt.Trigger(({rtTypeName})x))");
|
||||
rt.AppendLine($".Error(x => rt.TriggerError(x))");
|
||||
rt.AppendLine($".Chunk(x => rt.TriggerChunk(x));");
|
||||
rt.AppendLine("return rt; }");
|
||||
}
|
||||
|
||||
foreach (var p in template.Properties)
|
||||
{
|
||||
var ptTypeName = GetTypeName(p.ValueType, templates);
|
||||
rt.AppendLine($"public {ptTypeName} {p.Name} {{");
|
||||
rt.AppendLine($"get => ({ptTypeName})properties[{p.Index}];");
|
||||
rt.AppendLine($"set => _Set({p.Index}, value);");
|
||||
rt.AppendLine("}");
|
||||
}
|
||||
|
||||
if (template.Events.Length > 0)
|
||||
{
|
||||
rt.AppendLine("protected override void _EmitEventByIndex(byte index, object args) {");
|
||||
rt.AppendLine("switch (index) {");
|
||||
|
||||
var eventsList = new StringBuilder();
|
||||
|
||||
foreach (var e in template.Events)
|
||||
{
|
||||
var etTypeName = GetTypeName(e.ArgumentType, templates);
|
||||
rt.AppendLine($"case {e.Index}: {e.Name}?.Invoke(({etTypeName})args); break;");
|
||||
eventsList.AppendLine($"public event ResourceEventHandler<{etTypeName}> {e.Name};");
|
||||
}
|
||||
|
||||
rt.AppendLine("}}");
|
||||
|
||||
rt.AppendLine(eventsList.ToString());
|
||||
|
||||
}
|
||||
|
||||
rt.AppendLine("\r\n}\r\n}");
|
||||
|
||||
return rt.ToString();
|
||||
}
|
||||
rt.AppendLine("\r\n}\r\n}");
|
||||
|
||||
return rt.ToString();
|
||||
}
|
||||
|
||||
static string GetTypeName(TemplateDataType templateDataType, TypeTemplate[] templates)
|
||||
{
|
||||
|
||||
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 name = templateDataType.Type switch
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public static string GetTemplate(string url, string dir = null, string username = null, string password = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
if (!urlRegex.IsMatch(url))
|
||||
throw new Exception("Invalid IIP URL");
|
||||
|
||||
var path = urlRegex.Split(url);
|
||||
var con = Warehouse.Get<DistributedConnection>(path[1] + "://" + path[2],
|
||||
!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password) ? new { Username = username, Password = password } : null
|
||||
).Wait(20000);
|
||||
|
||||
if (con == null)
|
||||
throw new Exception("Can't connect to server");
|
||||
|
||||
if (string.IsNullOrEmpty(dir))
|
||||
dir = path[2].Replace(":", "_");
|
||||
|
||||
var templates = con.GetLinkTemplates(path[3]).Wait(60000);
|
||||
// no longer needed
|
||||
Warehouse.Remove(con);
|
||||
|
||||
var tempDir = new DirectoryInfo(Path.GetTempPath() + Path.DirectorySeparatorChar
|
||||
+ Misc.Global.GenerateCode(20) + Path.DirectorySeparatorChar + dir);
|
||||
|
||||
if (!tempDir.Exists)
|
||||
tempDir.Create();
|
||||
else
|
||||
{
|
||||
foreach (FileInfo file in tempDir.GetFiles())
|
||||
file.Delete();
|
||||
}
|
||||
|
||||
// make sources
|
||||
foreach (var tmp in templates)
|
||||
{
|
||||
if (tmp.Type == TemplateType.Resource)
|
||||
{
|
||||
var source = GenerateClass(tmp, templates);
|
||||
File.WriteAllText(tempDir.FullName + Path.DirectorySeparatorChar + tmp.ClassName + ".Generated.cs", source);
|
||||
}
|
||||
else if (tmp.Type == TemplateType.Record)
|
||||
{
|
||||
var source = GenerateRecord(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[] { " +
|
||||
string.Join(",", templates.Where(x => x.Type == TemplateType.Resource).Select(x => $"typeof({x.ClassName})"))
|
||||
+ " }; \r\n public static Type[] Records { get; } = new Type[] { " +
|
||||
string.Join(",", templates.Where(x => x.Type == TemplateType.Record).Select(x => $"typeof({x.ClassName})"))
|
||||
+ " }; " +
|
||||
|
||||
"\r\n } \r\n}";
|
||||
|
||||
|
||||
File.WriteAllText(tempDir.FullName + Path.DirectorySeparatorChar + "Esiur.Generated.cs", typesFile);
|
||||
|
||||
|
||||
return tempDir.FullName;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//File.WriteAllText("C:\\gen\\gettemplate.err", ex.ToString());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GenerateClass(TypeTemplate template, TypeTemplate[] templates)
|
||||
{
|
||||
var cls = template.ClassName.Split('.');
|
||||
|
||||
var nameSpace = string.Join(".", cls.Take(cls.Length - 1));
|
||||
var className = cls.Last();
|
||||
|
||||
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 class {className} : DistributedResource {{");
|
||||
|
||||
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)
|
||||
{
|
||||
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)));
|
||||
|
||||
rt.AppendLine(") {");
|
||||
rt.AppendLine($"var rt = new AsyncReply<{rtTypeName}>();");
|
||||
rt.AppendLine($"_InvokeByArrayArguments({f.Index}, new object[] {{ { string.Join(", ", f.Arguments.Select(x => x.Name)) } }})");
|
||||
rt.AppendLine($".Then(x => rt.Trigger(({rtTypeName})x))");
|
||||
rt.AppendLine($".Error(x => rt.TriggerError(x))");
|
||||
rt.AppendLine($".Chunk(x => rt.TriggerChunk(x));");
|
||||
rt.AppendLine("return rt; }");
|
||||
}
|
||||
|
||||
foreach (var p in template.Properties)
|
||||
{
|
||||
var ptTypeName = GetTypeName(p.ValueType, templates);
|
||||
rt.AppendLine($"public {ptTypeName} {p.Name} {{");
|
||||
rt.AppendLine($"get => ({ptTypeName})properties[{p.Index}];");
|
||||
rt.AppendLine($"set => _Set({p.Index}, value);");
|
||||
rt.AppendLine("}");
|
||||
}
|
||||
|
||||
if (template.Events.Length > 0)
|
||||
{
|
||||
rt.AppendLine("protected override void _EmitEventByIndex(byte index, object args) {");
|
||||
rt.AppendLine("switch (index) {");
|
||||
|
||||
var eventsList = new StringBuilder();
|
||||
|
||||
foreach (var e in template.Events)
|
||||
{
|
||||
var etTypeName = GetTypeName(e.ArgumentType, templates);
|
||||
rt.AppendLine($"case {e.Index}: {e.Name}?.Invoke(({etTypeName})args); break;");
|
||||
eventsList.AppendLine($"public event ResourceEventHandler<{etTypeName}> {e.Name};");
|
||||
}
|
||||
|
||||
rt.AppendLine("}}");
|
||||
|
||||
rt.AppendLine(eventsList.ToString());
|
||||
|
||||
}
|
||||
|
||||
rt.AppendLine("\r\n}\r\n}");
|
||||
|
||||
return rt.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user