2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 19:42:58 +00:00
This commit is contained in:
Ahmed Zamil 2022-09-19 00:00:32 +03:00
parent c1b9af9e9e
commit 8e17a5b677
5 changed files with 29 additions and 22 deletions

View File

@ -1013,7 +1013,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
if (this.Instance == null) if (this.Instance == null)
{ {
Warehouse.Put(this.RemoteUsername, this, null, Server).Then(x => Warehouse.Put(this.RemoteUsername.Replace("/", "_"), this, null, Server).Then(x =>
{ {
ready = true; ready = true;
Status = ConnectionStatus.Connected; Status = ConnectionStatus.Connected;
@ -1136,7 +1136,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
if (this.Instance == null) if (this.Instance == null)
{ {
Warehouse.Put(this.LocalUsername, this, null, Server).Then(x => Warehouse.Put(this.LocalUsername.Replace("/", "_"), this, null, Server).Then(x =>
{ {
openReply?.Trigger(true); openReply?.Trigger(true);
OnReady?.Invoke(this); OnReady?.Invoke(this);

View File

@ -28,6 +28,9 @@ public static class ResourceProxy
public static Type GetBaseType(Type type) public static Type GetBaseType(Type type)
{ {
if (type == null)
throw new NullReferenceException("Type can't be null.");
if (type.Assembly.IsDynamic) if (type.Assembly.IsDynamic)
return type.GetTypeInfo().BaseType; return type.GetTypeInfo().BaseType;
else else

View File

@ -22,6 +22,8 @@ public static class TemplateGenerator
static string ToLiteral(string input) static string ToLiteral(string input)
{ {
if (input == null) return "null";
var literal = new StringBuilder(); var literal = new StringBuilder();
literal.Append("\""); literal.Append("\"");
foreach (var c in input) foreach (var c in input)
@ -82,6 +84,7 @@ public static class TemplateGenerator
foreach (var p in template.Properties) foreach (var p in template.Properties)
{ {
var ptTypeName = GetTypeName(p.ValueType, templates); var ptTypeName = GetTypeName(p.ValueType, templates);
rt.AppendLine($"[Annotation[{ToLiteral(p.ReadAnnotation)}]");
rt.AppendLine($"public {ptTypeName} {p.Name} {{ get; set; }}"); rt.AppendLine($"public {ptTypeName} {p.Name} {{ get; set; }}");
rt.AppendLine(); rt.AppendLine();
} }
@ -294,9 +297,11 @@ public static class TemplateGenerator
var positionalArgs = f.Arguments.Where((x) => !x.Optional).ToArray(); var positionalArgs = f.Arguments.Where((x) => !x.Optional).ToArray();
var optionalArgs = f.Arguments.Where((x) => x.Optional).ToArray(); var optionalArgs = f.Arguments.Where((x) => x.Optional).ToArray();
rt.AppendLine($"[Annotation[{ToLiteral(f.Annotation)}]");
if (f.IsStatic) if (f.IsStatic)
{ {
rt.Append($"[Public] public static AsyncReply<{rtTypeName}> {f.Name}(DistributedConnection connection"); rt.Append($"[Public] public static AsyncReply<{rtTypeName}> {f.Name}(DistributedConnection connection");
if (positionalArgs.Length > 0) if (positionalArgs.Length > 0)
@ -356,6 +361,8 @@ public static class TemplateGenerator
if (p.Inherited) if (p.Inherited)
continue; continue;
rt.AppendLine($"[Annotation[{ToLiteral(p.ReadAnnotation)}]");
var ptTypeName = GetTypeName(p.ValueType, templates); var ptTypeName = GetTypeName(p.ValueType, templates);
rt.AppendLine($"[Public] public {ptTypeName} {p.Name} {{"); rt.AppendLine($"[Public] public {ptTypeName} {p.Name} {{");
rt.AppendLine($"get => ({ptTypeName})properties[{p.Index}];"); rt.AppendLine($"get => ({ptTypeName})properties[{p.Index}];");
@ -371,6 +378,8 @@ public static class TemplateGenerator
if (c.Inherited) if (c.Inherited)
continue; continue;
rt.AppendLine($"[Annotation[{ToLiteral(c.Annotation)}]");
var ctTypeName = GetTypeName(c.ValueType, templates); var ctTypeName = GetTypeName(c.ValueType, templates);
rt.AppendLine($"[Public] public const {ctTypeName} {c.Name} = {c.Value};"); rt.AppendLine($"[Public] public const {ctTypeName} {c.Name} = {c.Value};");
} }
@ -388,6 +397,8 @@ public static class TemplateGenerator
{ {
var etTypeName = GetTypeName(e.ArgumentType, templates); var etTypeName = GetTypeName(e.ArgumentType, templates);
rt.AppendLine($"case {e.Index}: {e.Name}?.Invoke(({etTypeName})args); break;"); rt.AppendLine($"case {e.Index}: {e.Name}?.Invoke(({etTypeName})args); break;");
eventsList.AppendLine($"[Annotation[{ToLiteral(e.Annotation)}]");
eventsList.AppendLine($"[Public] public event ResourceEventHandler<{etTypeName}> {e.Name};"); eventsList.AppendLine($"[Public] public event ResourceEventHandler<{etTypeName}> {e.Name};");
} }

View File

@ -354,7 +354,7 @@ public class TypeTemplate
} }
public static string GetTypeClassName(Type type, string separator = ".") public static string GetTypeClassName(Type type, char separator = '.')
{ {
if (type.IsGenericType) if (type.IsGenericType)
@ -362,12 +362,12 @@ public class TypeTemplate
var index = type.Name.IndexOf("`"); var index = type.Name.IndexOf("`");
var name = $"{type.Namespace}{separator}{((index > -1) ? type.Name.Substring(0, index) : type.Name)}Of"; var name = $"{type.Namespace}{separator}{((index > -1) ? type.Name.Substring(0, index) : type.Name)}Of";
foreach (var t in type.GenericTypeArguments) foreach (var t in type.GenericTypeArguments)
name += GetTypeClassName(t, "_"); name += GetTypeClassName(t, '_');
return name; return name;
} }
else else
return $"{type.Namespace}{separator}{type.Name}"; return $"{type.Namespace.Replace('.', separator)}{separator}{type.Name}";
} }

View File

@ -767,11 +767,11 @@ public static class Warehouse
/// <returns>Resource template.</returns> /// <returns>Resource template.</returns>
public static TypeTemplate GetTemplateByType(Type type) public static TypeTemplate GetTemplateByType(Type type)
{ {
var baseType = ResourceProxy.GetBaseType(type);
//TemplateType templateType = TemplateType.Unspecified; if (baseType == typeof(IResource)
|| baseType == typeof(IRecord))
//if (Codec.InheritsClass(type, typeof(DistributedResource))) return null;
// templateType = TemplateType.Wrapper;
TemplateType templateType; TemplateType templateType;
if (Codec.ImplementsInterface(type, typeof(IResource))) if (Codec.ImplementsInterface(type, typeof(IResource)))
@ -783,20 +783,13 @@ public static class Warehouse
else else
return null; return null;
var baseType = ResourceProxy.GetBaseType(type); var template = templates[templateType].Values.FirstOrDefault(x => x.DefinedType == baseType);
if (template != null)
return template;
if (baseType == typeof(IResource) // create new template for type
|| baseType == typeof(IRecord)) template = new TypeTemplate(baseType, true);
return null; TypeTemplate.GetDependencies(template);
var template = templates[templateType].Values.FirstOrDefault(x => x.DefinedType == type);
// loaded ?
if (template == null)
{
template = new TypeTemplate(baseType, true);
TypeTemplate.GetDependencies(template);
}
return template; return template;
} }