diff options
Diffstat (limited to 'MatrixRoomUtils.Core/Extensions')
4 files changed, 84 insertions, 1 deletions
diff --git a/MatrixRoomUtils.Core/Extensions/ClassCollector.cs b/MatrixRoomUtils.Core/Extensions/ClassCollector.cs new file mode 100644 index 0000000..9d3d3c0 --- /dev/null +++ b/MatrixRoomUtils.Core/Extensions/ClassCollector.cs @@ -0,0 +1,30 @@ +using System.Reflection; + +namespace MatrixRoomUtils.Core.Extensions; + +public class ClassCollector<T> where T : class { + static ClassCollector() { + if (!typeof(T).IsInterface) + throw new ArgumentException( + $"ClassCollector<T> must be used with an interface type. Passed type: {typeof(T).Name}"); + } + + public List<Type> ResolveFromAllAccessibleAssemblies() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(ResolveFromAssembly).ToList(); + + public List<Type> ResolveFromObjectReference(object obj) => ResolveFromTypeReference(obj.GetType()); + + public List<Type> ResolveFromTypeReference(Type t) => Assembly.GetAssembly(t)?.GetReferencedAssemblies().SelectMany(ResolveFromAssemblyName).ToList() ?? new List<Type>(); + + public List<Type> ResolveFromAssemblyName(AssemblyName assemblyName) => ResolveFromAssembly(Assembly.Load(assemblyName)); + + public List<Type> ResolveFromAssembly(Assembly assembly) => assembly.GetTypes() + .Where(x => x is { IsClass: true, IsAbstract: false } && x.GetInterfaces().Contains(typeof(T))).ToList(); + // { + // List<Type> ret = new(); + // foreach (var x in assembly.GetTypes().Where(x => x is { IsClass: true, IsAbstract: false } && x.GetInterfaces().Contains(typeof(T))).ToList()) { + // // Console.WriteLine($"[!!] Found class {x.FullName}"); + // ret.Add(x); + // } + // return ret; + // } +} \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs b/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs new file mode 100644 index 0000000..98b0aab --- /dev/null +++ b/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Text.Json; +using MatrixRoomUtils.Core.Interfaces; +using MatrixRoomUtils.Core.Responses; + +namespace MatrixRoomUtils.Core.Extensions; + +public static class IEnumerableExtensions { + public static List<StateEventResponse> DeserializeMatrixTypes(this List<JsonElement> stateEvents) { + return stateEvents.Select(DeserializeMatrixType).ToList(); + } + + public static StateEventResponse DeserializeMatrixType(this JsonElement stateEvent) { + var type = stateEvent.GetProperty("type").GetString(); + var knownType = StateEvent.KnownStateEventTypes.FirstOrDefault(x => x.GetCustomAttribute<MatrixEventAttribute>()?.EventName == type); + if (knownType == null) { + Console.WriteLine($"Warning: unknown event type '{type}'!"); + return new StateEventResponse(); + } + + var eventInstance = Activator.CreateInstance(typeof(StateEventResponse).MakeGenericType(knownType))!; + stateEvent.Deserialize(eventInstance.GetType()); + + return (StateEventResponse) eventInstance; + } + + public static void Replace(this List<StateEvent> stateEvents, StateEvent old, StateEvent @new) { + var index = stateEvents.IndexOf(old); + if (index == -1) return; + stateEvents[index] = @new; + } +} + +public class MatrixEventAttribute : Attribute { + public string EventName { get; set; } +} \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs b/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs index b007136..78f4456 100644 --- a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs @@ -1,12 +1,13 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace MatrixRoomUtils.Core.Extensions; public static class JsonElementExtensions { - public static void FindExtraJsonFields([DisallowNull] this JsonElement? res, Type t) { + public static void FindExtraJsonElementFields([DisallowNull] this JsonElement? res, Type t) { var props = t.GetProperties(); var unknownPropertyFound = false; foreach (var field in res.Value.EnumerateObject()) { @@ -17,4 +18,18 @@ public static class JsonElementExtensions { if (unknownPropertyFound) Console.WriteLine(res.Value.ToJson()); } + public static void FindExtraJsonObjectFields([DisallowNull] this JsonObject? res, Type t) { + var props = t.GetProperties(); + var unknownPropertyFound = false; + foreach (var field in res) { + if (props.Any(x => x.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name == field.Key)) continue; + Console.WriteLine($"[!!] Unknown property {field.Key} in {t.Name}!"); + unknownPropertyFound = true; + // foreach (var propertyInfo in props) { + // Console.WriteLine($"[!!] Known property {propertyInfo.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name} in {t.Name}!"); + // } + } + + if (unknownPropertyFound) Console.WriteLine(res.ToJson()); + } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs b/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs index 812c81c..a4b0791 100644 --- a/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs @@ -1,5 +1,7 @@ using System.Text.Encodings.Web; using System.Text.Json; +using MatrixRoomUtils.Core.Interfaces; +using MatrixRoomUtils.Core.Responses; namespace MatrixRoomUtils.Core.Extensions; |