Skip to content

Commit a589db3

Browse files
authored
qol: Make version label displayed to admins even on public host addresses (AscensionGameDev#2676)
* show engine version label to people with the permission (currently tied to legacy admin permission) * fix developer being removed on LAN * fix developer being removed on LAN * log instead of crash so this is safer to merge * remove log line * cleanup log line * don't access the tuple variable directly when I can use the local copy * TODO for checking why this code exists * enable cancelling pending thread queue actions on dispose
1 parent be6997a commit a589db3

35 files changed

+986
-219
lines changed

Framework/Intersect.Framework.Core/Intersect.Framework.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@
6969
</Compile>
7070
</ItemGroup>
7171

72+
<ItemGroup>
73+
<Folder Include="Network\Handlers\" />
74+
</ItemGroup>
75+
7276
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Intersect.Framework.Core.Network.Packets.Security;
2+
using Intersect.Framework.Core.Security;
3+
using Intersect.Network;
4+
5+
namespace Intersect.Framework.Core.Network.Handlers.Security;
6+
7+
[PacketHandler(typeof(ActivePermissionSetPacket))]
8+
public class ActivePermissionSetPacketHandler : AbstractPacketHandler<ActivePermissionSetPacket>
9+
{
10+
public override bool Handle(IPacketSender packetSender, ActivePermissionSetPacket packet)
11+
{
12+
PermissionSet.ActivePermissionSetName = packet.PermissionSetName;
13+
14+
return true;
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Intersect.Core;
2+
using Intersect.Framework.Core.Network.Packets.Security;
3+
using Intersect.Framework.Core.Security;
4+
using Intersect.Network;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Intersect.Framework.Core.Network.Handlers.Security;
8+
9+
[PacketHandler(typeof(PermissionSetPacket))]
10+
public class PermissionSetPacketHandler : AbstractPacketHandler<PermissionSetPacket>
11+
{
12+
public override bool Handle(IPacketSender packetSender, PermissionSetPacket packet)
13+
{
14+
ApplicationContext.Context.Value?.Logger.LogInformation(
15+
"Updated {PermissionSet} '{PermissionSetName}'",
16+
nameof(PermissionSet),
17+
packet.PermissionSet.Name
18+
);
19+
20+
// The deserializer will update the known permission set lookup, no need to do it here
21+
22+
return true;
23+
}
24+
}

Framework/Intersect.Framework.Core/Network/IntersectPacket.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ namespace Intersect.Network;
1010
public abstract partial class IntersectPacket : IPacket
1111
{
1212
[IgnoreMember]
13-
private byte[] mCachedData = null;
13+
private byte[]? _cachedData;
1414

1515
[IgnoreMember]
16-
private byte[] mCachedCompresedData = null;
16+
private byte[]? _cachedCompressedData = null;
1717

1818
/// <inheritdoc />
1919
public virtual void Dispose()
2020
{
21+
GC.SuppressFinalize(this);
2122
}
2223

2324
/// <inheritdoc />
@@ -26,19 +27,19 @@ public virtual byte[] Data
2627
{
2728
get
2829
{
29-
mCachedData ??= MessagePacker.Instance.Serialize(this) ??
30+
_cachedData ??= MessagePacker.Instance.Serialize(this) ??
3031
throw new Exception($"Failed to serialize {this.GetFullishName()}");
3132

3233
#if DIAGNOSTIC
3334
ApplicationContext.Context.Value?.Logger.LogDebug($"{GetType().FullName}({mCachedData.Length})={Convert.ToHexString(mCachedData)}");
3435
#endif
35-
return mCachedData;
36+
return _cachedData;
3637
}
3738
}
3839

3940
public virtual void ClearCachedData()
4041
{
41-
mCachedData = null;
42+
_cachedData = null;
4243
}
4344

4445
[IgnoreMember]

Framework/Intersect.Framework.Core/Network/PacketHandlerAttribute.cs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,47 +87,56 @@ public static Type GetPacketType(MethodInfo methodInfo)
8787
return expectedPacketType;
8888
}
8989

90-
public static Type GetHandlerInterface(Type implementationType)
90+
public static Type[] GetHandlerInterfaces(Type implementationType)
9191
{
92-
if (implementationType == null)
93-
{
94-
throw new ArgumentNullException(nameof(implementationType));
95-
}
92+
ArgumentNullException.ThrowIfNull(implementationType, nameof(implementationType));
9693

97-
return implementationType.GetInterfaces()
98-
.FirstOrDefault(
94+
var interfaces = implementationType.GetInterfaces()
95+
.Where(
9996
interfaceType => interfaceType.IsGenericType &&
10097
interfaceType.GetGenericTypeDefinition() == TypeIPacketHandlerInterface_1
98+
)
99+
.ToArray();
100+
101+
if (interfaces.Length < 1)
102+
{
103+
throw new ArgumentException(
104+
$"{implementationType.GetName(qualified: true)} does not directly implement {TypeIPacketHandlerInterface_1.GetName(qualified: true)}",
105+
nameof(implementationType)
101106
);
107+
}
108+
109+
return interfaces;
102110
}
103111

104-
public static Type GetPacketType<THandler>() where THandler : IPacketHandler =>
105-
GetPacketType(typeof(THandler));
112+
public static Type[] GetPacketTypes<THandler>() where THandler : IPacketHandler =>
113+
GetPacketTypes(typeof(THandler));
106114

107-
public static Type GetPacketType(Type packetHandlerType)
115+
public static Type[] GetPacketTypes(Type packetHandlerType)
108116
{
109-
if (packetHandlerType == default)
110-
{
111-
throw new ArgumentNullException(nameof(packetHandlerType));
112-
}
117+
ArgumentNullException.ThrowIfNull(packetHandlerType, nameof(packetHandlerType));
113118

114-
var interfaceType = GetHandlerInterface(packetHandlerType);
115-
if (interfaceType == null)
119+
var interfaceTypes = GetHandlerInterfaces(packetHandlerType);
120+
if (interfaceTypes.Length < 1)
116121
{
117122
throw new ArgumentException(
118-
$"{packetHandlerType.FullName} does not implement {TypeIPacketHandlerInterface_1.FullName}.",
123+
$"{packetHandlerType.GetName(qualified: true)} does not implement {TypeIPacketHandlerInterface_1.GetName(qualified: true)}.",
119124
nameof(packetHandlerType)
120125
);
121126
}
122127

123-
var packetTypeFromType = interfaceType.GenericTypeArguments.FirstOrDefault();
124-
if (packetTypeFromType.IsInterface ||
125-
packetTypeFromType.IsAbstract ||
126-
packetTypeFromType.IsGenericType ||
127-
!TypeIPacket.IsAssignableFrom(packetTypeFromType))
128+
var genericTypeArguments = interfaceTypes.SelectMany(interfaceType => interfaceType.GenericTypeArguments).Distinct().ToArray();
129+
var packetTypesFromType = genericTypeArguments.Where(
130+
packetTypeFromType =>
131+
packetTypeFromType is { IsInterface: false, IsAbstract: false, IsGenericType: false } &&
132+
packetTypeFromType.Extends(TypeIPacket)
133+
)
134+
.ToArray();
135+
136+
if (packetTypesFromType.Length < 1)
128137
{
129138
throw new ArgumentException(
130-
$"Invalid packet generic type ({packetTypeFromType.FullName}) in handler declaration '{packetHandlerType.FullName}'.",
139+
$"{packetHandlerType.GetName(qualified: true)} handles no packet types",
131140
nameof(packetHandlerType)
132141
);
133142
}
@@ -136,17 +145,20 @@ public static Type GetPacketType(Type packetHandlerType)
136145
GetCustomAttribute(packetHandlerType, typeof(PacketHandlerAttribute)) as PacketHandlerAttribute;
137146

138147
var packetTypeFromAttribute = packetHandlerAttribute?.PacketType;
139-
var expectedPacketType = packetTypeFromAttribute ?? packetTypeFromType;
148+
var expectedPacketTypes = packetTypeFromAttribute == null ? packetTypesFromType : [packetTypeFromAttribute];
140149

141-
if (packetTypeFromType != expectedPacketType)
150+
if (!packetTypesFromType.SequenceEqual(expectedPacketTypes))
142151
{
152+
var packetHandlerTypeName = packetHandlerType.GetName(qualified: true);
153+
var packetTypeNames = string.Join(", ", packetTypesFromType.Select(type => type.GetName(qualified: true)));
154+
var expectedPacketTypeNames = string.Join(", ", expectedPacketTypes.Select(type => type.GetName(qualified: true)));
143155
throw new ArgumentException(
144-
$"{packetHandlerType.FullName} is a packet handler for {packetTypeFromType.FullName} but is marked with an attribute for {packetTypeFromAttribute.FullName}.",
156+
$"{packetHandlerTypeName} is a packet handler for {packetTypeNames} but is marked with an attribute for {expectedPacketTypeNames}.",
145157
nameof(packetHandlerType)
146158
);
147159
}
148160

149-
return expectedPacketType;
161+
return expectedPacketTypes;
150162
}
151163

152164
public static bool IsValidHandler(MethodInfo methodInfo)
@@ -186,7 +198,7 @@ public static bool IsValidHandler(Type type)
186198

187199
try
188200
{
189-
return GetPacketType(type) != default;
201+
return GetPacketTypes(type).Length > 0;
190202
}
191203
catch (ArgumentNullException)
192204
{

0 commit comments

Comments
 (0)