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

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
@@ -60,7 +60,7 @@ namespace mROA.Implementation.Backend
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject)
{
return (T)GetObject(sharedObject.ContextId);
return (T)GetObject(sharedObject.Identifier.ContextId);
}
public object GetObject(int id)
@@ -41,8 +41,8 @@ namespace mROA.Implementation.Backend
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject)
{
var repository = GetRepository(sharedObject.OwnerId);
return repository.GetObject<T>(sharedObject.ContextId);
var repository = GetRepository(sharedObject.Identifier.OwnerId);
return repository.GetObject<T>(sharedObject.Identifier.ContextId);
}
public object GetObject(int id)
@@ -25,8 +25,8 @@ namespace mROA.Implementation
throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = _representationProducer.Produce(sharedObject.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObject.ContextId,
var representationModule = _representationProducer.Produce(sharedObject.Identifier.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObject.Identifier.ContextId,
representationModule)!;
return remote;
}
+1 -1
View File
@@ -21,7 +21,7 @@ namespace mROA.Implementation
public int Id => _identifier.ContextId;
public int OwnerId => _identifier.OwnerId;
public UniversalObjectIdentifier Identifier => _identifier;
protected async Task<T> GetResultAsync<T>(int methodId, object? parameter = default,
CancellationToken cancellationToken = default)
{
+41 -66
View File
@@ -56,44 +56,58 @@ namespace mROA.Implementation
};
private IContextRepository GetDefaultContextRepository() =>
(OwnerId == EndPointContext.HostId
(_identifier.OwnerId == EndPointContext.HostId
? EndPointContext.RealRepository
: EndPointContext.RemoteRepository) ??
throw new NullReferenceException(
"DefaultContextRepository was not defined");
private int _contextId = -2;
private int _ownerId = -1;
private UniversalObjectIdentifier _identifier = UniversalObjectIdentifier.Null;
public int OwnerId
public UniversalObjectIdentifier Identifier
{
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;
_identifier.OwnerId = _identifier.OwnerId == -1 ? EndPointContext.OwnerId : _identifier.OwnerId;
return _identifier;
}
set
{
_contextId = value;
_identifier = value;
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; }
// ReSharper disable once MemberCanBePrivate.Global
@@ -109,11 +123,13 @@ namespace mROA.Implementation
if (value is RemoteObjectBase ro)
{
_ownerId = ro.OwnerId;
_contextId = ro.Id;
_identifier = ro.Identifier;
}
else
_ownerId = EndPointContext.HostId;
{
_identifier.OwnerId = EndPointContext.HostId;
_identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(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) =>
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);
}
}
}