Skip to content
Merged

Dev #56

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions DeadCellsMultiplayerMod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<SupportedOSPlatform>Windows</SupportedOSPlatform>
<NoWarn>$(NoWarn);CA1416;CS0414;CS9107</NoWarn>
<!-- don't change this version -->
<Version>0.5.0</Version>
<AssemblyVersion>0.5.0</AssemblyVersion>
<FileVersion>0.5.0</FileVersion>
<InformationalVersion>0.5.0-vanilla-host-authoritative-mobsync</InformationalVersion>
<!-- Release version -->
<Version>0.5.1</Version>
<AssemblyVersion>0.5.1</AssemblyVersion>
<FileVersion>0.5.1</FileVersion>
<InformationalVersion>0.5.1-vanilla-host-authoritative-mobsync</InformationalVersion>
<ModType>mod</ModType>
<ModMain>DeadCellsMultiplayerMod.ModEntry</ModMain>

Expand Down
111 changes: 72 additions & 39 deletions Mobs/Bosses/BossReflection.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Reflection;

namespace DeadCellsMultiplayerMod.Mobs.Bosses;
Expand All @@ -14,32 +16,41 @@ internal static class BossReflection
private const BindingFlags Flags =
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;

private sealed class MemberAccessorCache : ConcurrentDictionary<string, MemberAccessor>
{
}

private sealed class MemberAccessor
{
internal static readonly MemberAccessor Missing = new(null, null);

internal readonly PropertyInfo? Property;
internal readonly FieldInfo? Field;

internal MemberAccessor(PropertyInfo? property, FieldInfo? field)
{
Property = property;
Field = field;
}
}

private static readonly ConditionalWeakTable<Type, MemberAccessorCache> MemberCaches = new();

internal static object? TryReadMember(object? target, string name)
{
if (target == null || string.IsNullOrEmpty(name))
return null;

for (var t = target.GetType(); t != null; t = t.BaseType)
var accessor = ResolveAccessor(target.GetType(), name);
try
{
if (accessor.Property is { CanRead: true } property)
return property.GetValue(target);
if (accessor.Field != null)
return accessor.Field.GetValue(target);
}
catch
{
try
{
var p = t.GetProperty(name, Flags);
if (p != null && p.CanRead)
return p.GetValue(target);
}
catch
{
}

try
{
var f = t.GetField(name, Flags);
if (f != null)
return f.GetValue(target);
}
catch
{
}
}

return null;
Expand All @@ -50,36 +61,58 @@ internal static bool TryWriteMember(object? target, string name, object? value)
if (target == null || string.IsNullOrEmpty(name))
return false;

for (var t = target.GetType(); t != null; t = t.BaseType)
var accessor = ResolveAccessor(target.GetType(), name);
try
{
try
if (accessor.Property is { CanWrite: true } property)
{
var p = t.GetProperty(name, Flags);
if (p != null && p.CanWrite)
{
p.SetValue(target, CoerceValue(value, p.PropertyType));
return true;
}
property.SetValue(target, CoerceValue(value, property.PropertyType));
return true;
}
catch

if (accessor.Field != null)
{
accessor.Field.SetValue(target, CoerceValue(value, accessor.Field.FieldType));
return true;
}
}
catch
{
}

return false;
}

try
private static MemberAccessor ResolveAccessor(Type type, string name)
{
var cache = MemberCaches.GetOrCreateValue(type);
return cache.GetOrAdd(name, static (memberName, declaringType) =>
{
for (var current = declaringType; current != null; current = current.BaseType)
{
var f = t.GetField(name, Flags);
if (f != null)
try
{
var property = current.GetProperty(memberName, Flags);
if (property != null)
return new MemberAccessor(property, null);
}
catch
{
}

try
{
var field = current.GetField(memberName, Flags);
if (field != null)
return new MemberAccessor(null, field);
}
catch
{
f.SetValue(target, CoerceValue(value, f.FieldType));
return true;
}
}
catch
{
}
}

return false;
return MemberAccessor.Missing;
}, type);
}

internal static int? TryReadInt(object? target, string name)
Expand Down
Loading
Loading