From 8dadf547033d71480fd7756809992c0f32549f59 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Thu, 11 Jan 2024 07:31:09 +0100 Subject: Cleanup, more message formatters, messagebuilder start --- LibMatrix/StateEvent.cs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'LibMatrix/StateEvent.cs') diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index 4a0adbd..1a8df11 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -6,7 +6,9 @@ using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; using ArcaneLibs; +using ArcaneLibs.Attributes; using ArcaneLibs.Extensions; +using Castle.DynamicProxy; using LibMatrix.EventTypes; using LibMatrix.Extensions; @@ -14,7 +16,7 @@ namespace LibMatrix; public class StateEvent { public static FrozenSet KnownStateEventTypes { get; } = new ClassCollector().ResolveFromAllAccessibleAssemblies().ToFrozenSet(); - + public static FrozenDictionary KnownStateEventTypesByName { get; } = KnownStateEventTypes.Aggregate( new Dictionary(), (dict, type) => { @@ -22,14 +24,24 @@ public class StateEvent { foreach (var attr in attrs) { dict[attr.EventName] = type; } + return dict; }).ToFrozenDictionary(); public static Type GetStateEventType(string type) => KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent); - + [JsonIgnore] public Type MappedType => GetStateEventType(Type); + [JsonIgnore] + public bool IsLegacyType => MappedType.GetCustomAttributes().FirstOrDefault(x => x.EventName == Type)?.Legacy ?? false; + + [JsonIgnore] + public string FriendlyTypeName => MappedType.GetFriendlyNameOrNull() ?? Type; + + [JsonIgnore] + public string FriendlyTypeNamePlural => MappedType.GetFriendlyNamePluralOrNull() ?? Type; + private static readonly JsonSerializerOptions TypedContentSerializerOptions = new() { Converters = { new JsonFloatStringConverter(), @@ -38,15 +50,30 @@ public class StateEvent { } }; + private class EventContentInterceptor : IInterceptor { + public void Intercept(IInvocation invocation) { + Console.WriteLine($"Intercepting {invocation.Method.Name}"); + // if (invocation.Method.Name == "ToString") { + // invocation.ReturnValue = "EventContent"; + // return; + // } + + invocation.Proceed(); + } + } + [JsonIgnore] [SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")] public EventContent? TypedContent { get { // if (Type == "m.receipt") { - // return null; + // return null; // } try { - return (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!; + var c= (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!; + // c = (EventContent)new ProxyGenerator().CreateClassProxyWithTarget(GetStateEventType(Type), c, new EventContentInterceptor()); + // Console.WriteLine(c.GetType().Name + ": " + string.Join(", ", c.GetType().GetRuntimeProperties().Select(x=>x.Name))); + return c; } catch (JsonException e) { Console.WriteLine(e); @@ -127,7 +154,6 @@ public class StateEvent { public string InternalContentTypeName => TypedContent?.GetType().Name ?? "null"; } - public class StateEventResponse : StateEvent { [JsonPropertyName("origin_server_ts")] public ulong? OriginServerTs { get; set; } @@ -213,4 +239,4 @@ public class StateEventContentPolymorphicTypeInfoResolver : DefaultJsonTypeInfoR } */ -#endregion +#endregion \ No newline at end of file -- cgit 1.4.1