2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00

Static Arrays

This commit is contained in:
2021-07-14 05:14:26 +03:00
parent d6b2a27221
commit 7940c152f0
24 changed files with 384 additions and 491 deletions

View File

@ -17,7 +17,7 @@ namespace Esiur.Resource.Template
}
public AttributeTemplate(ResourceTemplate template, byte index, string name)
public AttributeTemplate(TypeTemplate template, byte index, string name)
: base(template, MemberType.Attribute, index, name)
{

View File

@ -48,7 +48,7 @@ namespace Esiur.Resource.Template
}
public EventTemplate(ResourceTemplate template, byte index, string name, TemplateDataType argumentType, string expansion = null, bool listenable=false)
public EventTemplate(TypeTemplate template, byte index, string name, TemplateDataType argumentType, string expansion = null, bool listenable=false)
:base(template, MemberType.Property, index, name)
{
this.Expansion = expansion;

View File

@ -64,7 +64,7 @@ namespace Esiur.Resource.Template
}
public FunctionTemplate(ResourceTemplate template, byte index, string name, ArgumentTemplate[] arguments, TemplateDataType returnType, string expansion = null)
public FunctionTemplate(TypeTemplate template, byte index, string name, ArgumentTemplate[] arguments, TemplateDataType returnType, string expansion = null)
: base(template, MemberType.Property, index, name)
{
//this.IsVoid = isVoid;

View File

@ -21,14 +21,14 @@ namespace Esiur.Resource.Template
public string Name => name;
public MemberType Type => type;
ResourceTemplate template;
TypeTemplate template;
string name;
MemberType type;
byte index;
public ResourceTemplate Template => template;
public TypeTemplate Template => template;
public MemberTemplate(ResourceTemplate template, MemberType type, byte index, string name)
public MemberTemplate(TypeTemplate template, MemberType type, byte index, string name)
{
this.template = template;
this.type = type;

View File

@ -130,7 +130,7 @@ namespace Esiur.Resource.Template
}
}
public PropertyTemplate(ResourceTemplate template, byte index, string name, TemplateDataType valueType, string read = null, string write = null, bool recordable = false)
public PropertyTemplate(TypeTemplate template, byte index, string name, TemplateDataType valueType, string read = null, string write = null, bool recordable = false)
: base(template, MemberType.Property, index, name)
{
this.Recordable = recordable;

View File

@ -1,277 +0,0 @@
using Esiur.Data;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Esiur.Resource.Template
{
public class RecordTemplate : ResourceTemplate
{
//Guid classId;
//public Guid ClassId => classId;
//string className;
//public string ClassName => className;
public RecordTemplate()
{
}
public new static RecordTemplate Parse(byte[] data, uint offset, uint contentLength)
{
uint ends = offset + contentLength;
uint oOffset = offset;
// start parsing...
var od = new RecordTemplate();
od.content = data.Clip(offset, contentLength);
od.classId = data.GetGuid(offset);
offset += 16;
od.className = data.GetString(offset + 1, data[offset]);
offset += (uint)data[offset] + 1;
od.version = data.GetInt32(offset);
offset += 4;
ushort methodsCount = data.GetUInt16(offset);
offset += 2;
for (int i = 0; i < methodsCount; i++)
{
var type = data[offset] >> 5;
if (type == 0) // function
{
string expansion = null;
var hasExpansion = ((data[offset++] & 0x10) == 0x10);
var name = data.GetString(offset + 1, data[offset]);
offset += (uint)data[offset] + 1;
// return type
var (rts, returnType) = TemplateDataType.Parse(data, offset);
offset += rts;
// arguments count
var argsCount = data[offset++];
List<ArgumentTemplate> arguments = new();
for (var a = 0; a < argsCount; a++)
{
var (cs, argType) = ArgumentTemplate.Parse(data, offset);
arguments.Add(argType);
offset += cs;
}
// arguments
if (hasExpansion) // expansion ?
{
var cs = data.GetUInt32(offset);
offset += 4;
expansion = data.GetString(offset, cs);
offset += cs;
}
var ft = new FunctionTemplate(od, functionIndex++, name, arguments.ToArray(), returnType, expansion);
od.functions.Add(ft);
}
else if (type == 1) // property
{
string readExpansion = null, writeExpansion = null;
var hasReadExpansion = ((data[offset] & 0x8) == 0x8);
var hasWriteExpansion = ((data[offset] & 0x10) == 0x10);
var recordable = ((data[offset] & 1) == 1);
var permission = (PropertyTemplate.PropertyPermission)((data[offset++] >> 1) & 0x3);
var name = data.GetString(offset + 1, data[offset]);// Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]);
offset += (uint)data[offset] + 1;
var (dts, valueType) = TemplateDataType.Parse(data, offset);
offset += dts;
if (hasReadExpansion) // expansion ?
{
var cs = data.GetUInt32(offset);
offset += 4;
readExpansion = data.GetString(offset, cs);
offset += cs;
}
if (hasWriteExpansion) // expansion ?
{
var cs = data.GetUInt32(offset);
offset += 4;
writeExpansion = data.GetString(offset, cs);
offset += cs;
}
var pt = new PropertyTemplate(od, propertyIndex++, name, valueType, readExpansion, writeExpansion, recordable);
od.properties.Add(pt);
}
else if (type == 2) // Event
{
string expansion = null;
var hasExpansion = ((data[offset] & 0x10) == 0x10);
var listenable = ((data[offset++] & 0x8) == 0x8);
var name = data.GetString(offset + 1, data[offset]);// Encoding.ASCII.GetString(data, (int)offset + 1, (int)data[offset]);
offset += (uint)data[offset] + 1;
var (dts, argType) = TemplateDataType.Parse(data, offset);
offset += dts;
if (hasExpansion) // expansion ?
{
var cs = data.GetUInt32(offset);
offset += 4;
expansion = data.GetString(offset, cs);
offset += cs;
}
var et = new EventTemplate(od, eventIndex++, name, argType, expansion, listenable);
od.events.Add(et);
}
}
// append signals
for (int i = 0; i < od.events.Count; i++)
od.members.Add(od.events[i]);
// append slots
for (int i = 0; i < od.functions.Count; i++)
od.members.Add(od.functions[i]);
// append properties
for (int i = 0; i < od.properties.Count; i++)
od.members.Add(od.properties[i]);
//od.isReady = true;
/*
var oo = owner.Socket.Engine.GetObjectDescription(od.GUID);
if (oo != null)
{
Console.WriteLine("Already there ! description");
return oo;
}
else
{
owner.Socket.Engine.AddObjectDescription(od);
return od;
}
*/
return od;
}
public RecordTemplate(Type type)
{
if (!Codec.ImplementsInterface(type, typeof(IRecord)))
throw new Exception("Type is not a record.");
className = type.FullName;
classId = ResourceTemplate.GetTypeGuid(className);
#if NETSTANDARD
PropertyInfo[] propsInfo = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance);
#else
PropertyInfo[] propsInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
#endif
bool classIsPublic = type.GetCustomAttribute<PublicAttribute>() != null;
byte i = 0;
if (classIsPublic)
{
foreach (var pi in propsInfo)
{
var privateAttr = pi.GetCustomAttribute<PrivateAttribute>(true);
if (privateAttr == null)
continue;
var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true);
var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true);
var pt = new PropertyTemplate(this, i++, pi.Name, TemplateDataType.FromType(pi.PropertyType));
if (storageAttr != null)
pt.Recordable = storageAttr.Mode == StorageMode.Recordable;
if (annotationAttr != null)
pt.ReadExpansion = annotationAttr.Annotation;
else
pt.ReadExpansion = pi.PropertyType.Name;
pt.PropertyInfo = pi;
//pt.Serilize = publicAttr.Serialize;
properties.Add(pt);
members.Add(pt);
}
}
else
{
foreach (var pi in propsInfo)
{
var publicAttr = pi.GetCustomAttribute<PublicAttribute>(true);
if (publicAttr == null)
continue;
var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true);
var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true);
var valueType = TemplateDataType.FromType(pi.PropertyType);
var pt = new PropertyTemplate(this, i++, pi.Name, valueType);//, rp.ReadExpansion, rp.WriteExpansion, rp.Storage);
if (storageAttr != null)
pt.Recordable = storageAttr.Mode == StorageMode.Recordable;
if (annotationAttr != null)
pt.ReadExpansion = annotationAttr.Annotation;
else
pt.ReadExpansion = pi.PropertyType.Name;
pt.PropertyInfo = pi;
//pt.Serilize = publicAttr.Serialize;
properties.Add(pt);
members.Add(pt);
}
}
// bake it binarily
var b = new BinaryList();
b.AddGuid(classId)
.AddUInt8((byte)className.Length)
.AddString(className)
.AddInt32(version)
.AddUInt16((ushort)members.Count);
foreach (var pt in properties)
b.AddUInt8Array(pt.Compose());
content = b.ToArray();
}
}
}

View File

@ -10,7 +10,7 @@ namespace Esiur.Resource.Template
{
public DataType Type { get; set; }
//public string TypeName { get; set; }
public ResourceTemplate TypeTemplate => TypeGuid == null ? null : Warehouse.GetTemplateByClassId((Guid)TypeGuid);
public TypeTemplate TypeTemplate => TypeGuid == null ? null : Warehouse.GetTemplateByClassId((Guid)TypeGuid);
public Guid? TypeGuid { get; set; }
//public TemplateDataType(DataType type, string typeName)
@ -49,6 +49,7 @@ namespace Esiur.Resource.Template
_ when t == typeof(string) => DataType.String,
_ when t == typeof(DateTime) => DataType.DateTime,
_ when t == typeof(IResource) => DataType.Void, // Dynamic resource (unspecified type)
_ when t == typeof(IRecord) => DataType.Void, // Dynamic record (unspecified type)
_ when typeof(Structure).IsAssignableFrom(t) || t == typeof(ExpandoObject) => DataType.Structure,
_ when Codec.ImplementsInterface(t, typeof(IResource)) => DataType.Resource,
_ when Codec.ImplementsInterface(t, typeof(IRecord)) => DataType.Record,
@ -59,7 +60,7 @@ namespace Esiur.Resource.Template
Guid? typeGuid = null;
if (dt == DataType.Resource || dt == DataType.Record)
typeGuid = ResourceTemplate.GetTypeGuid(t);
typeGuid = TypeTemplate.GetTypeGuid(t);
if (type.IsArray)
dt = (DataType)((byte)dt | 0x80);

View File

@ -6,7 +6,9 @@ namespace Esiur.Resource.Template
{
public enum TemplateType:byte
{
Unspecified,
Resource,
Record,
Wrapper,
}
}

View File

@ -18,7 +18,7 @@ namespace Esiur.Resource.Template
// Record
//}
public class ResourceTemplate
public class TypeTemplate
{
protected Guid classId;
@ -44,7 +44,7 @@ namespace Esiur.Resource.Template
public TemplateType Type => templateType;
public Type ResourceType { get; set; }
public Type DefinedType { get; set; }
@ -146,7 +146,7 @@ namespace Esiur.Resource.Template
public ResourceTemplate()
public TypeTemplate()
{
}
@ -171,18 +171,18 @@ namespace Esiur.Resource.Template
public static ResourceTemplate[] GetDependencies(ResourceTemplate template)
public static TypeTemplate[] GetDependencies(TypeTemplate template)
{
var list = new List<ResourceTemplate>();
var list = new List<TypeTemplate>();
list.Add(template);
Action<ResourceTemplate, List<ResourceTemplate>> getDependenciesFunc = null;
Action<TypeTemplate, List<TypeTemplate>> getDependenciesFunc = null;
getDependenciesFunc = (ResourceTemplate tmp, List<ResourceTemplate> bag) =>
getDependenciesFunc = (TypeTemplate tmp, List<TypeTemplate> bag) =>
{
if (template.ResourceType == null)
if (template.DefinedType == null)
return;
// functions
@ -267,14 +267,16 @@ namespace Esiur.Resource.Template
return list.ToArray();
}
public ResourceTemplate(Type type)
public TypeTemplate(Type type, bool addToWarehouse = false)
{
if (Codec.ImplementsInterface(type, typeof(IRecord)))
templateType = TemplateType.Record;
if (Codec.InheritsClass(type, typeof(DistributedResource)))
templateType = TemplateType.Wrapper;
else if (Codec.ImplementsInterface(type, typeof(IResource)))
templateType = TemplateType.Resource;
else if (Codec.ImplementsInterface(type, typeof(IRecord)))
templateType = TemplateType.Record;
else
throw new Exception("Type is neither a resource nor a record.");
throw new Exception("Type must implement IResource, IRecord or inherit from DistributedResource.");
//if (isRecord && isResource)
// throw new Exception("Type can't have both IResource and IRecord interfaces");
@ -284,7 +286,7 @@ namespace Esiur.Resource.Template
type = ResourceProxy.GetBaseType(type);
ResourceType = type;
DefinedType = type;
className = type.FullName;
@ -292,7 +294,10 @@ namespace Esiur.Resource.Template
// set guid
classId = GetTypeGuid(className);
if (addToWarehouse)
Warehouse.PutTemplate(this);
#if NETSTANDARD
PropertyInfo[] propsInfo = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
EventInfo[] eventsInfo = type.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
@ -395,8 +400,7 @@ namespace Esiur.Resource.Template
Name = x.Name,
Type = TemplateDataType.FromType(x.ParameterType),
ParameterInfo = x
})
.ToArray();
}).ToArray();
var ft = new FunctionTemplate(this, i++, mi.Name, arguments, returnType);// mi.ReturnType == typeof(void));
@ -546,13 +550,13 @@ namespace Esiur.Resource.Template
}
public static ResourceTemplate Parse(byte[] data)
public static TypeTemplate Parse(byte[] data)
{
return Parse(data, 0, (uint)data.Length);
}
public static ResourceTemplate Parse(byte[] data, uint offset, uint contentLength)
public static TypeTemplate Parse(byte[] data, uint offset, uint contentLength)
{
uint ends = offset + contentLength;
@ -561,7 +565,7 @@ namespace Esiur.Resource.Template
// start parsing...
var od = new ResourceTemplate();
var od = new TypeTemplate();
od.content = data.Clip(offset, contentLength);
od.templateType = (TemplateType)data[offset++];