/// <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();
        }
Example #2
0
        /// <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();
            }
        }
Example #3
0
 /// <inheritdoc />
 /// <summary>
 /// Creates a ButtplugException.
 /// </summary>
 /// <param name="aLogger">Logger to log exception error message through (gives type context for the message).</param>
 /// <param name="aMessage">Exception message.</param>
 /// <param name="aClass">Exception class, based on B******g Error Message Classes. (https://b******g-spec.docs.b******g.io/status.html#error)</param>
 /// <param name="aId">Message ID for the resulting B******g Error Message.</param>
 /// <param name="aInner">Optional inner exception.</param>
 public ButtplugException([NotNull] IButtplugLog aLogger, string aMessage, Error.ErrorClass aClass = Error.ErrorClass.ERROR_UNKNOWN, uint aId = ButtplugConsts.SystemMsgId, Exception aInner = null)
     : this(aMessage, aClass, aId, aInner)
 {
     ButtplugUtils.ArgumentNotNull(aLogger, nameof(aLogger));
     aLogger.Error(aMessage);
 }
Example #4
0
 public static ButtplugException FromError(IButtplugLog aLogger, Error aMsg)
 {
     ButtplugUtils.ArgumentNotNull(aLogger, nameof(aLogger));
     aLogger.Error(aMsg.ErrorMessage);
     return(FromError(aMsg));
 }