From 6bd02248ccfbcb46960a6f39eaad23888d190eb5 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 15 Sep 2023 09:50:45 +0200 Subject: Some refactoring --- LibMatrix/StateEvent.cs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'LibMatrix/StateEvent.cs') diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index 175d706..9ca9141 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -6,28 +6,41 @@ using ArcaneLibs; using ArcaneLibs.Extensions; using LibMatrix.Helpers; using LibMatrix.Interfaces; +using LibMatrix.StateEventTypes; namespace LibMatrix; public class StateEvent { - public static List KnownStateEventTypes = - new ClassCollector().ResolveFromAllAccessibleAssemblies(); + public static readonly List KnownStateEventTypes = + new ClassCollector().ResolveFromAllAccessibleAssemblies(); + + public static readonly Dictionary KnownStateEventTypesByName = KnownStateEventTypes.Aggregate( + new Dictionary(), + (dict, type) => { + var attrs = type.GetCustomAttributes(); + foreach (var attr in attrs) { + dict[attr.EventName] = type; + } + + return dict; + }); public static Type GetStateEventType(string type) { if (type == "m.receipt") { return typeof(Dictionary); } - var eventType = KnownStateEventTypes.FirstOrDefault(x => - x.GetCustomAttributes()?.Any(y => y.EventName == type) ?? false); + // var eventType = KnownStateEventTypes.FirstOrDefault(x => + // x.GetCustomAttributes()?.Any(y => y.EventName == type) ?? false); + var eventType = KnownStateEventTypesByName.GetValueOrDefault(type); - return eventType ?? typeof(object); + return eventType ?? typeof(UnknownEventContent); } - public object TypedContent { + public EventContent TypedContent { get { try { - return RawContent.Deserialize(GetType)!; + return (EventContent) RawContent.Deserialize(GetType)!; } catch (JsonException e) { Console.WriteLine(e); @@ -42,19 +55,8 @@ public class StateEvent { [JsonPropertyName("state_key")] public string StateKey { get; set; } = ""; - private string _type; - [JsonPropertyName("type")] - public string Type { - get => _type; - set { - _type = value; - // if (RawContent is not null && this is StateEventResponse stateEventResponse) { - // if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return; - // var x = GetType.Name; - // } - } - } + public string Type { get; set; } [JsonPropertyName("replaces_state")] public string? ReplacesState { get; set; } @@ -79,7 +81,7 @@ public class StateEvent { var type = GetStateEventType(Type); //special handling for some types - // if (type == typeof(RoomEmotesEventData)) { + // if (type == typeof(RoomEmotesEventContent)) { // RawContent["emote"] = RawContent["emote"]?.AsObject() ?? new JsonObject(); // } // -- cgit 1.4.1