From 522f23212d81e17f732a310b4e8d86e25cc1ecb4 Mon Sep 17 00:00:00 2001 From: Esiur Project Date: Tue, 6 Sep 2022 23:29:44 +0300 Subject: [PATCH] TemplateType.Wrapper Removed --- Esiur/Data/RepresentationType.cs | 2 +- Esiur/Proxy/TemplateGenerator.cs | 2 +- Esiur/Resource/Instance.cs | 156 +++++++++--------------- Esiur/Resource/Template/TemplateType.cs | 2 - Esiur/Resource/Template/TypeTemplate.cs | 13 +- Esiur/Resource/Warehouse.cs | 67 +++++----- Test/Program.cs | 2 +- 7 files changed, 107 insertions(+), 137 deletions(-) diff --git a/Esiur/Data/RepresentationType.cs b/Esiur/Data/RepresentationType.cs index 5e6dfef..5e0c6a5 100644 --- a/Esiur/Data/RepresentationType.cs +++ b/Esiur/Data/RepresentationType.cs @@ -172,7 +172,7 @@ namespace Esiur.Data (RepresentationTypeIdentifier.Resource) => typeof(IResource), (RepresentationTypeIdentifier.Record) => typeof(IRecord), (RepresentationTypeIdentifier.TypedRecord) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Record)?.DefinedType, - (RepresentationTypeIdentifier.TypedResource) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Unspecified)?.DefinedType, + (RepresentationTypeIdentifier.TypedResource) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Resource)?.DefinedType, (RepresentationTypeIdentifier.Enum) => Warehouse.GetTemplateByClassId((Guid)GUID, TemplateType.Enum)?.DefinedType, _ => null diff --git a/Esiur/Proxy/TemplateGenerator.cs b/Esiur/Proxy/TemplateGenerator.cs index a175e3d..c7d35aa 100644 --- a/Esiur/Proxy/TemplateGenerator.cs +++ b/Esiur/Proxy/TemplateGenerator.cs @@ -122,7 +122,7 @@ public static class TemplateGenerator 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; + name = templates.First(x => x.ClassId == representationType.GUID && (x.Type == TemplateType.Resource)).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) diff --git a/Esiur/Resource/Instance.cs b/Esiur/Resource/Instance.cs index b450f44..6097624 100644 --- a/Esiur/Resource/Instance.cs +++ b/Esiur/Resource/Instance.cs @@ -35,8 +35,8 @@ public class Instance public event PropertyModifiedEvent PropertyModified; - - + + public event EventOccurredEvent EventOccurred; public event CustomEventOccurredEvent CustomEventOccurred; public event ResourceDestroyedEvent Destroyed; @@ -46,7 +46,7 @@ public class Instance //KeyList attributes; List ages = new(); - List modificationDates = new (); + List modificationDates = new(); private ulong instanceAge; private DateTime instanceModificationDate; @@ -97,10 +97,10 @@ public class Instance */ } - public Map GetAttributes(string[] attributes = null) + public Map GetAttributes(string[] attributes = null) { // @TODO - var rt = new Map(); + var rt = new Map(); if (attributes != null) { @@ -169,7 +169,7 @@ public class Instance */ } - public bool SetAttributes(Map attributes, bool clearAttributes = false) + public bool SetAttributes(Map attributes, bool clearAttributes = false) { // @ TODO @@ -305,7 +305,7 @@ public class Instance { modificationDates[index] = value; if (value > instanceModificationDate) - instanceModificationDate = (DateTime) value; + instanceModificationDate = (DateTime)value; } } @@ -616,8 +616,8 @@ public class Instance var eventTemplate = template.GetEventTemplateByIndex(eventIndex); EventOccurred?.Invoke(new EventOccurredInfo(res, eventTemplate, value)); } - } - + } + internal void EmitCustomResourceEventByIndex(object issuer, Func receivers, byte eventIndex, object value) { IResource res; @@ -866,13 +866,6 @@ public class Instance /// public AutoList Managers => managers; - - public void CallMeTest(Instance ins, int? val) => - ins.EmitResourceEventByIndex(201, val); - - public void CallMeTest2(Instance instance, object issuer, Func receivers, int? val) => - instance.EmitCustomResourceEventByIndex(issuer, receivers, 201, val); - /// /// Create new instance. /// @@ -911,110 +904,77 @@ public class Instance modificationDates.Add(DateTime.MinValue); } + // connect events - Type t = ResourceProxy.GetBaseType(resource); - -#if NETSTANDARD - var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly); - -#else - var events = t.GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly); -#endif - - - var emitEventByIndexMethod = GetType().GetMethod("EmitResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic); - var emitCustomEventByIndexMethod = GetType().GetMethod("EmitCustomResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic); - - foreach (var evt in template.Events) + if (!(resource is DistributedResource)) { - if (evt.EventInfo == null) - continue; + Type t = ResourceProxy.GetBaseType(resource); - var eventGenericType = evt.EventInfo.EventHandlerType.GetGenericTypeDefinition(); + var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance); - if (eventGenericType == typeof(ResourceEventHandler<>)) + var emitEventByIndexMethod = GetType().GetMethod("EmitResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic); + var emitCustomEventByIndexMethod = GetType().GetMethod("EmitCustomResourceEventByIndex", BindingFlags.Instance | BindingFlags.NonPublic); + + foreach (var evt in template.Events) { - var dm = new DynamicMethod("_", null, - new Type[] {typeof(Instance), evt.EventInfo.EventHandlerType.GenericTypeArguments[0] }, - typeof(Instance).Module, true); + if (evt.EventInfo == null) + continue; + + var eventGenericType = evt.EventInfo.EventHandlerType.GetGenericTypeDefinition(); + + if (eventGenericType == typeof(ResourceEventHandler<>)) + { + + var dm = new DynamicMethod("_", null, + new Type[] { typeof(Instance), evt.EventInfo.EventHandlerType.GenericTypeArguments[0] }, + typeof(Instance).Module, true); - var il = dm.GetILGenerator(); - il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Ldc_I4, (int)evt.Index); - il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]); - il.Emit(OpCodes.Callvirt, emitEventByIndexMethod); - il.Emit(OpCodes.Nop); - il.Emit(OpCodes.Ret); + var il = dm.GetILGenerator(); + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldc_I4, (int)evt.Index); + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]); + il.Emit(OpCodes.Callvirt, emitEventByIndexMethod); + il.Emit(OpCodes.Nop); + il.Emit(OpCodes.Ret); - var proxyDelegate= dm.CreateDelegate(evt.EventInfo.EventHandlerType, this); + var proxyDelegate = dm.CreateDelegate(evt.EventInfo.EventHandlerType, this); - //ResourceEventHandler proxyDelegate = new ResourceEventHandler((args) => EmitResourceEvent(evt, args)); - evt.EventInfo.AddEventHandler(resource, proxyDelegate); + //ResourceEventHandler proxyDelegate = new ResourceEventHandler((args) => EmitResourceEvent(evt, args)); + evt.EventInfo.AddEventHandler(resource, proxyDelegate); - } - else if (eventGenericType == typeof(CustomResourceEventHandler<>)) - { - var dm = new DynamicMethod("_", null, - new Type[] { typeof(Instance), typeof(object), typeof(Func), + } + else if (eventGenericType == typeof(CustomResourceEventHandler<>)) + { + var dm = new DynamicMethod("_", null, + new Type[] { typeof(Instance), typeof(object), typeof(Func), evt.EventInfo.EventHandlerType.GenericTypeArguments[0] }, - typeof(Instance).Module, true); + typeof(Instance).Module, true); - var il = dm.GetILGenerator(); - il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Ldarg_2); - il.Emit(OpCodes.Ldc_I4, (int)evt.Index); - il.Emit(OpCodes.Ldarg_3); - il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]); - il.Emit(OpCodes.Callvirt, emitCustomEventByIndexMethod); - il.Emit(OpCodes.Nop); - il.Emit(OpCodes.Ret); + var il = dm.GetILGenerator(); + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Ldarg_2); + il.Emit(OpCodes.Ldc_I4, (int)evt.Index); + il.Emit(OpCodes.Ldarg_3); + il.Emit(OpCodes.Box, evt.EventInfo.EventHandlerType.GenericTypeArguments[0]); + il.Emit(OpCodes.Callvirt, emitCustomEventByIndexMethod); + il.Emit(OpCodes.Nop); + il.Emit(OpCodes.Ret); - var proxyDelegate = dm.CreateDelegate(evt.EventInfo.EventHandlerType, this); + var proxyDelegate = dm.CreateDelegate(evt.EventInfo.EventHandlerType, this); - //CustomResourceEventHandler proxyDelegate = (issuer, receivers, args) => EmitCustomResourceEvent(issuer, receivers, evt, args); - evt.EventInfo.AddEventHandler(resource, proxyDelegate); - } - - - /* - else if (evt.EventHandlerType == typeof(CustomUsersEventHanlder)) - { - var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true); - if (ca.Length == 0) - continue; - - CustomUsersEventHanlder proxyDelegate = (users, args) => EmitResourceEvent(evt.Name, users, null, args); - evt.AddEventHandler(resource, proxyDelegate); - } - else if (evt.EventHandlerType == typeof(CustomConnectionsEventHanlder)) - { - var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true); - if (ca.Length == 0) - continue; - - CustomConnectionsEventHanlder proxyDelegate = (connections, args) => EmitResourceEvent(evt.Name, null, connections, args); - evt.AddEventHandler(resource, proxyDelegate); - } - else if (evt.EventHandlerType == typeof(CustomReceiversEventHanlder)) - { - var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true); - if (ca.Length == 0) - continue; - - CustomReceiversEventHanlder proxyDelegate = (users, connections, args) => EmitResourceEvent(evt.Name, users, connections, args); - evt.AddEventHandler(resource, proxyDelegate); + evt.EventInfo.AddEventHandler(resource, proxyDelegate); + } } - */ } } diff --git a/Esiur/Resource/Template/TemplateType.cs b/Esiur/Resource/Template/TemplateType.cs index a1e19c8..c66c292 100644 --- a/Esiur/Resource/Template/TemplateType.cs +++ b/Esiur/Resource/Template/TemplateType.cs @@ -5,9 +5,7 @@ using System.Text; namespace Esiur.Resource.Template; public enum TemplateType : byte { - Unspecified, Resource, Record, - Wrapper, Enum } diff --git a/Esiur/Resource/Template/TypeTemplate.cs b/Esiur/Resource/Template/TypeTemplate.cs index 827e377..ad71e79 100644 --- a/Esiur/Resource/Template/TypeTemplate.cs +++ b/Esiur/Resource/Template/TypeTemplate.cs @@ -350,7 +350,7 @@ public class TypeTemplate }; getDependenciesFunc(template, list); - return list.ToArray(); + return list.Distinct().ToArray(); } @@ -394,12 +394,12 @@ public class TypeTemplate } + public bool IsWrapper { get; private set; } public TypeTemplate(Type type, bool addToWarehouse = false) { - if (Codec.InheritsClass(type, typeof(DistributedResource))) - templateType = TemplateType.Wrapper; - else if (Codec.ImplementsInterface(type, typeof(IResource))) + + if (Codec.ImplementsInterface(type, typeof(IResource))) templateType = TemplateType.Resource; else if (Codec.ImplementsInterface(type, typeof(IRecord))) templateType = TemplateType.Record; @@ -408,6 +408,8 @@ public class TypeTemplate else throw new Exception("Type must implement IResource, IRecord or inherit from DistributedResource."); + IsWrapper = Codec.InheritsClass(type, typeof(DistributedResource)); + //if (isRecord && isResource) // throw new Exception("Type can't have both IResource and IRecord interfaces"); @@ -457,8 +459,7 @@ public class TypeTemplate } } - if (templateType == TemplateType.Resource - || templateType == TemplateType.Wrapper) + if (templateType == TemplateType.Resource) { if (hierarchy.ContainsKey(MemberTypes.Method)) { diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index 322f12c..9d0b37d 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -53,14 +53,14 @@ public static class Warehouse static uint resourceCounter = 0; - + static KeyList> templates = new KeyList>() { - [TemplateType.Unspecified] = new KeyList(), + //[TemplateType.Unspecified] = new KeyList(), [TemplateType.Resource] = new KeyList(), [TemplateType.Record] = new KeyList(), - [TemplateType.Wrapper] = new KeyList(), + //[TemplateType.Wrapper] = new KeyList(), [TemplateType.Enum] = new KeyList(), }; @@ -595,7 +595,7 @@ public static class Warehouse resource.Instance = new Instance(resourceCounter++, instanceName, resource, store, customTemplate, age); if (attributes != null) - resource.Instance.SetAttributes(Map.FromObject(attributes)); + resource.Instance.SetAttributes(Map.FromObject(attributes)); if (manager != null) resource.Instance.Managers.Add(manager); @@ -691,7 +691,7 @@ public static class Warehouse if (properties != null) { - var ps = Map.FromObject(properties); + var ps = Map.FromObject(properties); foreach (var p in ps) { @@ -753,6 +753,9 @@ public static class Warehouse /// Resource template. public static void PutTemplate(TypeTemplate template) { + if (templates[template.Type].ContainsKey(template.ClassId)) + throw new Exception($"Template with same class Id already exists. {templates[template.Type][template.ClassId].ClassName} -> {template.ClassName}"); + templates[template.Type][template.ClassId] = template; } @@ -765,10 +768,12 @@ public static class Warehouse public static TypeTemplate GetTemplateByType(Type type) { - TemplateType templateType = TemplateType.Unspecified; + //TemplateType templateType = TemplateType.Unspecified; - if (Codec.InheritsClass(type, typeof(DistributedResource))) - templateType = TemplateType.Wrapper; + //if (Codec.InheritsClass(type, typeof(DistributedResource))) + // templateType = TemplateType.Wrapper; + + TemplateType templateType; if (Codec.ImplementsInterface(type, typeof(IResource))) templateType = TemplateType.Resource; else if (Codec.ImplementsInterface(type, typeof(IRecord))) @@ -801,32 +806,33 @@ public static class Warehouse /// /// Class Id. /// Resource template. - public static TypeTemplate GetTemplateByClassId(Guid classId, TemplateType templateType = TemplateType.Unspecified) + public static TypeTemplate GetTemplateByClassId(Guid classId, TemplateType? templateType = null) { - if (templateType == TemplateType.Unspecified) + if (templateType == null) { - // look in resources + // look into resources var template = templates[TemplateType.Resource][classId]; if (template != null) return template; - // look in records + // look into records template = templates[TemplateType.Record][classId]; if (template != null) return template; - // look in enums + // look into enums template = templates[TemplateType.Enum][classId]; - if (template != null) - return template; - - - // look in wrappers - template = templates[TemplateType.Wrapper][classId]; return template; + //if (template != null) + + + //// look in wrappers + //template = templates[TemplateType.Wrapper][classId]; + //return template; } else - return templates[templateType][classId]; + return templates[templateType.Value][classId]; + } /// @@ -834,27 +840,32 @@ public static class Warehouse /// /// Class name. /// Resource template. - public static TypeTemplate GetTemplateByClassName(string className, TemplateType templateType = TemplateType.Unspecified) + public static TypeTemplate GetTemplateByClassName(string className, TemplateType? templateType = null) { - if (templateType == TemplateType.Unspecified) + if (templateType == null) { - // look in resources + // look into resources var template = templates[TemplateType.Resource].Values.FirstOrDefault(x => x.ClassName == className); if (template != null) return template; - // look in records + // look into records template = templates[TemplateType.Record].Values.FirstOrDefault(x => x.ClassName == className); if (template != null) return template; - // look in wrappers - template = templates[TemplateType.Wrapper].Values.FirstOrDefault(x => x.ClassName == className); + // look into enums + template = templates[TemplateType.Enum].Values.FirstOrDefault(x => x.ClassName == className); + //if (template != null) return template; + + //// look in wrappers + //template = templates[TemplateType.Wrapper].Values.FirstOrDefault(x => x.ClassName == className); + //return template; } else { - return templates[templateType].Values.FirstOrDefault(x => x.ClassName == className); + return templates[templateType.Value].Values.FirstOrDefault(x => x.ClassName == className); } } @@ -873,7 +884,7 @@ public static class Warehouse resources.TryRemove(resource.Instance.Id, out resourceReference); else return false; - + if (resource != resource.Instance.Store) { diff --git a/Test/Program.cs b/Test/Program.cs index 5d40579..e715a4d 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -111,7 +111,7 @@ namespace Test var temp = await con.Call("temp"); Console.WriteLine("Temp: " + temp.GetHashCode()); - var template = await con.GetTemplateByClassName("Test.MyResource"); + //var template = await con.GetTemplateByClassName("Test.MyResource"); TestObjectProps(local, remote);