From b06f7394da73aafa68c833baa38ac92cc19f200b Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 12 Feb 2018 11:33:32 +0100 Subject: [PATCH] Further normalize the response --- ArchiSteamFarm/IPC.cs | 46 ++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/ArchiSteamFarm/IPC.cs b/ArchiSteamFarm/IPC.cs index cc355d387..6f71efabd 100644 --- a/ArchiSteamFarm/IPC.cs +++ b/ArchiSteamFarm/IPC.cs @@ -549,10 +549,9 @@ namespace ArchiSteamFarm { return true; } - Dictionary properties = new Dictionary { - { nameof(targetType.BaseType), targetType.BaseType?.GetUnifiedName() }, - { nameof(targetType.CustomAttributes), targetType.CustomAttributes.Select(attribute => attribute.AttributeType.GetUnifiedName()) } - }; + string baseType = targetType.BaseType?.GetUnifiedName(); + HashSet customAttributes = new HashSet(targetType.CustomAttributes.Select(attribute => attribute.AttributeType.GetUnifiedName())); + string underlyingType = null; Dictionary body = new Dictionary(); @@ -566,13 +565,15 @@ namespace ArchiSteamFarm { } } else if (targetType.IsEnum) { Type enumType = Enum.GetUnderlyingType(targetType); - properties["UnderlyingType"] = enumType.GetUnifiedName(); + underlyingType = enumType.GetUnifiedName(); foreach (object value in Enum.GetValues(targetType)) { body[value.ToString()] = Convert.ChangeType(value, enumType).ToString(); } } + TypeResponse.TypeProperties properties = new TypeResponse.TypeProperties(baseType, customAttributes.Count > 0 ? customAttributes : null, underlyingType); + await ResponseJsonObject(request, response, new GenericResponse(true, "OK", new TypeResponse(body, properties))).ConfigureAwait(false); return true; } @@ -867,13 +868,13 @@ namespace ArchiSteamFarm { private sealed class ASFResponse { [JsonProperty] - internal readonly uint MemoryUsage; + private readonly uint MemoryUsage; [JsonProperty] - internal readonly DateTime ProcessStartTime; + private readonly DateTime ProcessStartTime; [JsonProperty] - internal readonly Version Version; + private readonly Version Version; internal ASFResponse(uint memoryUsage, DateTime processStartTime, Version version) { if ((memoryUsage == 0) || (processStartTime == DateTime.MinValue) || (version == null)) { @@ -902,13 +903,13 @@ namespace ArchiSteamFarm { private sealed class GenericResponse { [JsonProperty] - internal readonly string Message; + private readonly string Message; [JsonProperty] - internal readonly object Result; + private readonly object Result; [JsonProperty] - internal readonly bool Success; + private readonly bool Success; internal GenericResponse(bool success, string message = null, object result = null) { Success = success; @@ -925,12 +926,12 @@ namespace ArchiSteamFarm { private sealed class TypeResponse { [JsonProperty] - internal readonly Dictionary Body; + private readonly Dictionary Body; [JsonProperty] - internal readonly Dictionary Properties; + private readonly TypeProperties Properties; - internal TypeResponse(Dictionary body, Dictionary properties) { + internal TypeResponse(Dictionary body, TypeProperties properties) { if ((body == null) || (properties == null)) { throw new ArgumentNullException(nameof(body) + " || " + nameof(properties)); } @@ -938,6 +939,23 @@ namespace ArchiSteamFarm { Body = body; Properties = properties; } + + internal sealed class TypeProperties { + [JsonProperty] + private readonly string BaseType; + + [JsonProperty] + private readonly HashSet CustomAttributes; + + [JsonProperty] + private readonly string UnderlyingType; + + internal TypeProperties(string baseType = null, HashSet customAttributes = null, string underlyingType = null) { + BaseType = baseType; + CustomAttributes = customAttributes; + UnderlyingType = underlyingType; + } + } } } } \ No newline at end of file