Skip to content

Commit cb65446

Browse files
authored
feat: client map cache (AscensionGameDev#2101)
* feat: generic object data request packet * fix: correct checksum, separate entities/items packets from map packet since they are not 'static' * fix MessagePack.Annotations reference * skip Enqueue if packet is null * fix: clear cache when map data is changed
1 parent 259642b commit cb65446

File tree

57 files changed

+2089
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2089
-136
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<IntersectRepoPath Condition="'$(IntersectRepoPath)' == ''">$(MSBuildProjectDirectory)\..</IntersectRepoPath>
55
</PropertyGroup>
66

7-
<Import Project=".\Intersect.props"/>
7+
<Import Project=".\Intersect.props" />
88

99
<PropertyGroup>
1010
<IntersectProjectName>$(MSBuildProjectName.Substring(10))</IntersectProjectName>

Framework/Directory.Build.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<IntersectRepoPath>$(MSBuildProjectDirectory)\..\..</IntersectRepoPath>
4+
</PropertyGroup>
5+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Intersect.Framework.Reflection;
2+
using Newtonsoft.Json;
3+
4+
namespace Intersect.Framework.Converters.Json;
5+
6+
public abstract class GenericFactoryNewtonsoftJsonConverter : JsonConverter
7+
{
8+
private static readonly Dictionary<Type, JsonConverter> _converters = new Dictionary<Type, JsonConverter>();
9+
10+
private readonly Type _converterTypeDefinition;
11+
private readonly Type _serializedGenericTypeDefinition;
12+
13+
protected GenericFactoryNewtonsoftJsonConverter(Type converterTypeDefinition, Type serializedGenericTypeDefinition)
14+
{
15+
_converterTypeDefinition = converterTypeDefinition ??
16+
throw new ArgumentNullException(nameof(converterTypeDefinition));
17+
_serializedGenericTypeDefinition = serializedGenericTypeDefinition ??
18+
throw new ArgumentNullException(nameof(serializedGenericTypeDefinition));
19+
}
20+
21+
public override bool CanConvert(Type objectType) =>
22+
objectType.IsGenericType && objectType.GetGenericTypeDefinition() == _serializedGenericTypeDefinition;
23+
24+
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue,
25+
JsonSerializer serializer)
26+
{
27+
if (reader.Value == default)
28+
{
29+
return reader.Value;
30+
}
31+
32+
var actualConverter = CreateActualConverter(
33+
_converterTypeDefinition,
34+
_serializedGenericTypeDefinition,
35+
objectType
36+
);
37+
return actualConverter.ReadJson(reader, objectType, existingValue, serializer);
38+
}
39+
40+
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
41+
{
42+
if (value == default)
43+
{
44+
writer.WriteNull();
45+
}
46+
else
47+
{
48+
var actualConverter = CreateActualConverter(
49+
_converterTypeDefinition,
50+
_serializedGenericTypeDefinition,
51+
value.GetType()
52+
);
53+
actualConverter.WriteJson(writer, value, serializer);
54+
}
55+
}
56+
57+
private static JsonConverter CreateActualConverter(
58+
Type converterTypeDefinition,
59+
Type serializedGenericTypeDefinition,
60+
Type objectType
61+
)
62+
{
63+
if (_converters.TryGetValue(objectType, out JsonConverter? jsonConverter))
64+
{
65+
return jsonConverter;
66+
}
67+
68+
var targetType = objectType
69+
.FindGenericTypeParameters(typeof(Nullable<>), false)
70+
.FirstOrDefault()
71+
?? objectType;
72+
73+
var identifiedType = targetType.FindGenericTypeParameters(serializedGenericTypeDefinition);
74+
var converterType = converterTypeDefinition.MakeGenericType(identifiedType);
75+
jsonConverter = Activator.CreateInstance(converterType) as JsonConverter;
76+
_converters[objectType] = jsonConverter ?? throw new InvalidOperationException();
77+
return jsonConverter;
78+
}
79+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Intersect.Framework.Converters.Json;
5+
6+
public abstract class GenericSystemJsonConverterFactory : JsonConverterFactory
7+
{
8+
private readonly Type _converterTypeDefinition;
9+
private readonly Type _serializedGenericTypeDefinition;
10+
11+
protected GenericSystemJsonConverterFactory(Type converterTypeDefinition, Type serializedGenericTypeDefinition)
12+
{
13+
_converterTypeDefinition = converterTypeDefinition ??
14+
throw new ArgumentNullException(nameof(converterTypeDefinition));
15+
_serializedGenericTypeDefinition = serializedGenericTypeDefinition ??
16+
throw new ArgumentNullException(nameof(serializedGenericTypeDefinition));
17+
}
18+
19+
/// <inheritdoc/>
20+
public override bool CanConvert(Type typeToConvert) =>
21+
typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == _serializedGenericTypeDefinition;
22+
23+
/// <inheritdoc/>
24+
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
25+
{
26+
var identifiedType = typeToConvert.GetGenericArguments()[0];
27+
var converterType = _converterTypeDefinition.MakeGenericType(identifiedType);
28+
var jsonConverter = Activator.CreateInstance(converterType) as JsonConverter;
29+
return jsonConverter;
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Intersect.Framework.Converters.Json;
4+
5+
public sealed class HandleNewtonsoftJsonConverter : GenericFactoryNewtonsoftJsonConverter
6+
{
7+
public HandleNewtonsoftJsonConverter() : base(typeof(HandleNewtonsoftJsonConverter<>), typeof(Handle<>))
8+
{
9+
}
10+
}
11+
12+
public class HandleNewtonsoftJsonConverter<THandle> : JsonConverter<Handle<THandle>>
13+
{
14+
public override Handle<THandle> ReadJson(JsonReader reader, Type objectType, Handle<THandle> existingValue,
15+
bool hasExistingValue, JsonSerializer serializer) =>
16+
new Handle<THandle>(new IntPtr(serializer.Deserialize<long>(reader)));
17+
18+
public override void WriteJson(JsonWriter writer, Handle<THandle> value, JsonSerializer serializer) =>
19+
serializer.Serialize(writer, value.Pointer.ToInt64());
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Intersect.Framework.Converters.Json;
5+
6+
/// <summary>
7+
/// Creates <see cref="JsonConverter{T}"/> instances for <see cref="Id{T}"/>.
8+
/// </summary>
9+
public sealed class HandleSystemJsonConverterFactory : GenericSystemJsonConverterFactory
10+
{
11+
public HandleSystemJsonConverterFactory() : base(
12+
typeof(HandleJsonConverter<>),
13+
typeof(Handle<>)
14+
)
15+
{
16+
}
17+
18+
private class HandleJsonConverter<T> : JsonConverter<Handle<T>>
19+
{
20+
public override Handle<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new Handle<T>(new IntPtr(reader.GetInt64()));
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, Handle<T> value, JsonSerializerOptions options)
26+
{
27+
writer.WriteNumberValue(value.Pointer.ToInt64());
28+
}
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Intersect.Framework.Converters.Json;
4+
5+
public sealed class IdNewtonsoftJsonConverter : GenericFactoryNewtonsoftJsonConverter
6+
{
7+
public IdNewtonsoftJsonConverter() : base(
8+
typeof(IdNewtonsoftJsonConverter<>),
9+
typeof(Id<>)
10+
)
11+
{
12+
}
13+
}
14+
15+
public class IdNewtonsoftJsonConverter<TId> : JsonConverter<Id<TId>>
16+
{
17+
public override Id<TId> ReadJson(JsonReader reader, Type objectType, Id<TId> existingValue, bool hasExistingValue,
18+
JsonSerializer serializer) =>
19+
new Id<TId>(serializer.Deserialize<Guid>(reader));
20+
21+
public override void WriteJson(JsonWriter writer, Id<TId> value, JsonSerializer serializer) =>
22+
serializer.Serialize(writer, value.Guid);
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Intersect.Framework.Converters.Json;
5+
6+
/// <summary>
7+
/// Creates <see cref="JsonConverter{T}"/> instances for <see cref="Id{T}"/>.
8+
/// </summary>
9+
public sealed class IdSystemJsonConverterFactory : GenericSystemJsonConverterFactory
10+
{
11+
public IdSystemJsonConverterFactory() : base(
12+
typeof(IdJsonConverter<>),
13+
typeof(Id<>)
14+
)
15+
{
16+
}
17+
18+
private class IdJsonConverter<T> : JsonConverter<Id<T>>
19+
{
20+
public override Id<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
21+
new Id<T>(reader.GetGuid());
22+
23+
public override void Write(Utf8JsonWriter writer, Id<T> value, JsonSerializerOptions options) =>
24+
writer.WriteStringValue(value.Guid);
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Intersect.Framework.Eventing;
2+
3+
public sealed record AggregateSender : IAggregateSender
4+
{
5+
// ReSharper disable once ConvertToPrimaryConstructor
6+
public AggregateSender(object currentSender, object? originalSender)
7+
{
8+
CurrentSender = currentSender;
9+
OriginalSender = originalSender;
10+
}
11+
12+
/// <inheritdoc />
13+
public object CurrentSender { get; }
14+
15+
/// <inheritdoc />
16+
public object? OriginalSender { get; }
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Intersect.Framework.Eventing;
2+
3+
public sealed class AggregateSender<T> : IAggregateSender<T> where T : notnull
4+
{
5+
public AggregateSender(T currentSender, object? originalSender)
6+
{
7+
CurrentSender = currentSender;
8+
OriginalSender = originalSender;
9+
}
10+
11+
/// <inheritdoc />
12+
public T CurrentSender { get; }
13+
14+
/// <inheritdoc />
15+
public object? OriginalSender { get; }
16+
}

0 commit comments

Comments
 (0)