/// <summary> /// Initializes a new instance of the <see cref="ButtplugJsonMessageParser"/> class. /// </summary> /// <param name="aLogManager">Log manager, passed from the parser owner.</param> public ButtplugJsonMessageParser([NotNull] IButtplugLogManager aLogManager) { // Set up logging. if (aLogManager == null) { throw new ArgumentNullException(nameof(aLogManager)); } _bpLogger = aLogManager.GetLogger(GetType()); _bpLogger?.Info($"Setting up {GetType().Name}"); _serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Error }; _messageTypes = new Dictionary <string, Type>(); foreach (var aMessageType in ButtplugUtils.GetAllMessageTypes()) { _bpLogger?.Debug($"- {aMessageType.Name}"); _messageTypes.Add(aMessageType.Name, aMessageType); } // If we can't find any message types in our assembly, the system is basically useless. if (!_messageTypes.Any()) { throw new ButtplugMessageException(_bpLogger, "No message types available."); } // Load the schema for validation. Schema file is an embedded resource in the library. var jsonSchemaString = ButtplugUtils.GetStringFromFileResource("B******g.b******g-schema.json"); _schema = JsonSchema.FromJsonAsync(jsonSchemaString)?.GetAwaiter().GetResult() ?? throw new InvalidOperationException(); }
/// <summary> /// Initializes a new instance of the <see cref="ButtplugJsonMessageParser"/> class. /// </summary> /// <param name="aLogManager">Log manager, passed from the parser owner.</param> public ButtplugJsonMessageParser([NotNull] IButtplugLogManager aLogManager) { // Set up logging. if (aLogManager == null) { throw new ArgumentNullException(nameof(aLogManager)); } _bpLogger = aLogManager.GetLogger(GetType()); _bpLogger?.Info($"Setting up {GetType().Name}"); _serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Error }; _messageTypes = new Dictionary <string, Type>(); foreach (var aMessageType in ButtplugUtils.GetAllMessageTypes()) { _bpLogger?.Debug($"- {aMessageType.Name}"); _messageTypes.Add(aMessageType.Name, aMessageType); } // If we can't find any message types in our assembly, the system is basically useless. if (!_messageTypes.Any()) { throw new ButtplugMessageException(_bpLogger, "No message types available."); } // Load the schema for validation. Schema file is an embedded resource in the library. var assembly = Assembly.GetExecutingAssembly(); const string resourceName = "B******g.b******g-schema.json"; var stream = assembly.GetManifestResourceStream(resourceName); try { using (var reader = new StreamReader(stream ?? throw new InvalidOperationException())) { stream = null; var result = reader.ReadToEnd(); _schema = JsonSchema4.FromJsonAsync(result)?.GetAwaiter().GetResult() ?? throw new InvalidOperationException(); } } finally { // Always make sure we dispose of the resource stream, even if we throw. All // exceptions should be rethrown though. stream?.Dispose(); } }