Я сам не знаю как и что, но оно работает как то

This commit is contained in:
2025-03-01 16:40:40 +03:00
parent af65f22aa9
commit 8f891ff59e
8 changed files with 128 additions and 82 deletions
+6 -4
View File
@@ -183,7 +183,7 @@ namespace mROA.Cbor
if (type == typeof(ulong)) if (type == typeof(ulong))
return reader.ReadUInt64(); return reader.ReadUInt64();
return reader.ReadInt32(); return reader.ReadUInt64();
case CborReaderState.ByteString: case CborReaderState.ByteString:
if (type == typeof(Guid)) if (type == typeof(Guid))
return new Guid(reader.ReadByteString()); return new Guid(reader.ReadByteString());
@@ -284,6 +284,7 @@ namespace mROA.Cbor
private object ReadObject(CborReader reader, Type type, IEndPointContext? context) private object ReadObject(CborReader reader, Type type, IEndPointContext? context)
{ {
if (type == typeof(object)) if (type == typeof(object))
{ {
return new PreParsedValue(ReadList(reader, null, context) as List<object>); return new PreParsedValue(ReadList(reader, null, context) as List<object>);
@@ -310,13 +311,14 @@ namespace mROA.Cbor
} }
private void FillObject(object obj, Type type, CborReader reader, IEndPointContext? context) private void FillObject(object obj, Type type, CborReader reader, IEndPointContext? context)
{
try
{ {
var propertyInfos = type.GetProperties(); var propertyInfos = type.GetProperties();
var properties = FilterProperties(propertyInfos); var properties = FilterProperties(propertyInfos);
var length = reader.ReadStartArray(); var length = reader.ReadStartArray();
try
{
#if TRACE #if TRACE
Console.WriteLine($"Reading list of {length} objects, {properties.Count} properties found"); Console.WriteLine($"Reading list of {length} objects, {properties.Count} properties found");
#endif #endif
@@ -342,7 +344,7 @@ namespace mROA.Cbor
public static List<PropertyInfo> FilterProperties(PropertyInfo[] properties) public static List<PropertyInfo> FilterProperties(PropertyInfo[] properties)
{ {
var finalProperties = new List<PropertyInfo>(properties.Length); var finalProperties = new List<PropertyInfo>(properties.Length);
foreach (var property in properties) foreach (var property in properties.Where(i => i.CanWrite && i.CanRead))
{ {
if (property.GetCustomAttribute<SerializationIgnoreAttribute>() == null) if (property.GetCustomAttribute<SerializationIgnoreAttribute>() == null)
finalProperties.Add(property); finalProperties.Add(property);
+26 -4
View File
@@ -5,13 +5,18 @@ using mROA.Implementation;
namespace mROA.Cbor namespace mROA.Cbor
{ {
public class PreParsedValue public interface IPreParsedValue
{ {
public List<object> Properties { get; set; } object? ToObject(Type type, IEndPointContext? context);
}
public class PreParsedValue : IPreParsedValue
{
private List<object> _properties { get; set; }
public PreParsedValue(List<object> properties) public PreParsedValue(List<object> properties)
{ {
Properties = properties; _properties = properties;
} }
public object? ToObject(Type type, IEndPointContext? context) public object? ToObject(Type type, IEndPointContext? context)
@@ -29,9 +34,26 @@ namespace mROA.Cbor
for (var index = 0; index < properties.Count; index++) for (var index = 0; index < properties.Count; index++)
{ {
var property = properties[index]; var property = properties[index];
property.SetValue(instance, Properties[index]); property.SetValue(instance, _properties[index] is IPreParsedValue ppv ? ppv.ToObject(property.PropertyType, context) : _properties[index]);
} }
return instance; return instance;
} }
} }
public class ParsedValue : IPreParsedValue
{
public ParsedValue(object? value)
{
_value = value;
}
private object? _value;
public object? ToObject(Type type, IEndPointContext? context)
{
return _value;
}
}
} }
@@ -60,7 +60,7 @@ namespace mROA.Implementation.Backend
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject) public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject)
{ {
return (T)GetObject(sharedObject.ContextId); return (T)GetObject(sharedObject.Identifier.ContextId);
} }
public object GetObject(int id) public object GetObject(int id)
@@ -41,8 +41,8 @@ namespace mROA.Implementation.Backend
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject) public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject)
{ {
var repository = GetRepository(sharedObject.OwnerId); var repository = GetRepository(sharedObject.Identifier.OwnerId);
return repository.GetObject<T>(sharedObject.ContextId); return repository.GetObject<T>(sharedObject.Identifier.ContextId);
} }
public object GetObject(int id) public object GetObject(int id)
@@ -25,8 +25,8 @@ namespace mROA.Implementation
throw new NullReferenceException("representation producer is not initialized"); throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = _representationProducer.Produce(sharedObject.OwnerId); var representationModule = _representationProducer.Produce(sharedObject.Identifier.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObject.ContextId, var remote = (T)Activator.CreateInstance(remoteType, sharedObject.Identifier.ContextId,
representationModule)!; representationModule)!;
return remote; return remote;
} }
+1 -1
View File
@@ -21,7 +21,7 @@ namespace mROA.Implementation
public int Id => _identifier.ContextId; public int Id => _identifier.ContextId;
public int OwnerId => _identifier.OwnerId; public int OwnerId => _identifier.OwnerId;
public UniversalObjectIdentifier Identifier => _identifier;
protected async Task<T> GetResultAsync<T>(int methodId, object? parameter = default, protected async Task<T> GetResultAsync<T>(int methodId, object? parameter = default,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
+41 -66
View File
@@ -56,44 +56,58 @@ namespace mROA.Implementation
}; };
private IContextRepository GetDefaultContextRepository() => private IContextRepository GetDefaultContextRepository() =>
(OwnerId == EndPointContext.HostId (_identifier.OwnerId == EndPointContext.HostId
? EndPointContext.RealRepository ? EndPointContext.RealRepository
: EndPointContext.RemoteRepository) ?? : EndPointContext.RemoteRepository) ??
throw new NullReferenceException( throw new NullReferenceException(
"DefaultContextRepository was not defined"); "DefaultContextRepository was not defined");
private int _contextId = -2; private UniversalObjectIdentifier _identifier = UniversalObjectIdentifier.Null;
private int _ownerId = -1;
public int OwnerId public UniversalObjectIdentifier Identifier
{ {
get get
{ {
_ownerId = _ownerId == -1 ? EndPointContext.OwnerId : _ownerId; _identifier.OwnerId = _identifier.OwnerId == -1 ? EndPointContext.OwnerId : _identifier.OwnerId;
return _ownerId; return _identifier;
}
set => _ownerId = value;
}
// ReSharper disable once MemberCanBePrivate.Global
public int ContextId
{
// ReSharper disable once UnusedMember.Global
get
{
if (_contextId != -2)
return _contextId;
_contextId = EndPointContext.RealRepository.GetObjectIndex(Value);
return _contextId;
} }
set set
{ {
_contextId = value; _identifier = value;
Value = GetDefaultContextRepository().GetObjectBySharedObject(this); Value = GetDefaultContextRepository().GetObjectBySharedObject(this);
} }
} }
// public int OwnerId
// {
// get
// {
// _ownerId = _ownerId == -1 ? EndPointContext.OwnerId : _ownerId;
// return _ownerId;
// }
// set => _ownerId = value;
// }
//
// // ReSharper disable once MemberCanBePrivate.Global
// public int ContextId
// {
// // ReSharper disable once UnusedMember.Global
// get
// {
// if (_contextId != -2)
// return _contextId;
//
// _contextId = EndPointContext.RealRepository.GetObjectIndex(Value);
// return _contextId;
// }
// set
// {
// _contextId = value;
// Value = GetDefaultContextRepository().GetObjectBySharedObject(this);
// }
// }
[JsonIgnore] [SerializationIgnore] public T Value { get; private set; } [JsonIgnore] [SerializationIgnore] public T Value { get; private set; }
// ReSharper disable once MemberCanBePrivate.Global // ReSharper disable once MemberCanBePrivate.Global
@@ -109,11 +123,13 @@ namespace mROA.Implementation
if (value is RemoteObjectBase ro) if (value is RemoteObjectBase ro)
{ {
_ownerId = ro.OwnerId; _identifier = ro.Identifier;
_contextId = ro.Id;
} }
else else
_ownerId = EndPointContext.HostId; {
_identifier.OwnerId = EndPointContext.HostId;
_identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value);
}
} }
public static implicit operator T(SharedObject<T> value) => value.Value; public static implicit operator T(SharedObject<T> value) => value.Value;
@@ -121,45 +137,4 @@ namespace mROA.Implementation
public static implicit operator SharedObject<T>(T value) => public static implicit operator SharedObject<T>(T value) =>
new(value); new(value);
} }
public struct UniversalObjectIdentifier : IEquatable<UniversalObjectIdentifier>
{
public int ContextId;
public int OwnerId;
public override string ToString()
{
return $"{nameof(ContextId)}: {ContextId}, {nameof(OwnerId)}: {OwnerId}";
}
public bool IsStatic => ContextId == -1;
public ulong Flat
{
get
{
return (ulong)OwnerId << 32 | (uint)ContextId;
}
set
{
OwnerId = (int)(value >> 32);
ContextId = (int)(value & 0xFFFFFFFF);
}
}
public bool Equals(UniversalObjectIdentifier other)
{
return ContextId == other.ContextId && OwnerId == other.OwnerId;
}
public override bool Equals(object? obj)
{
return obj is UniversalObjectIdentifier other && Equals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(ContextId, OwnerId);
}
}
} }
@@ -0,0 +1,47 @@
using System;
namespace mROA.Implementation
{
#pragma warning disable CS8618, CS9264
public struct UniversalObjectIdentifier : IEquatable<UniversalObjectIdentifier>
{
public int ContextId;
public int OwnerId;
public static UniversalObjectIdentifier Null = new UniversalObjectIdentifier { ContextId = -2, OwnerId = -1 };
public override string ToString()
{
return $"{{ {nameof(ContextId)}: {ContextId}, {nameof(OwnerId)}: {OwnerId} }}";
}
public bool IsStatic => ContextId == -1;
public ulong Flat
{
get
{
return (ulong)OwnerId << 32 | (uint)ContextId;
}
set
{
OwnerId = (int)(value >> 32);
ContextId = (int)(value & 0xFFFFFFFF);
}
}
public bool Equals(UniversalObjectIdentifier other)
{
return ContextId == other.ContextId && OwnerId == other.OwnerId;
}
public override bool Equals(object? obj)
{
return obj is UniversalObjectIdentifier other && Equals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(ContextId, OwnerId);
}
}
}