Пример #1
0
        public static byte[] ToByteArray(this MacroOpBase cmd)
        {
            var builder = new ByteArrayBuilder(false);

            cmd.Serialize(builder);
            return(builder.ToByteArray());
        }
Пример #2
0
        public static MacroOpBase CreateFromData(byte[] arr, bool safe)
        {
            int opId = (arr[3] << 8) | arr[2];
            MacroOperationType macroOp = (MacroOperationType)opId;

            try
            {
                if (!macroOp.IsValid())
                {
                    throw new SerializationException("FTDa", "Invalid MacroOperationType: {0}", opId);
                }

                var parsed = new ParsedByteArray(arr, false);

                Type type = FindForType(macroOp);
                if (type == null)
                {
                    throw new SerializationException("FTDa", "Failed to find MacroOperationType: {0}", macroOp);
                }

                MacroOpBase cmd = (MacroOpBase)Activator.CreateInstance(type);

                if (!safe)
                {
                    cmd.Deserialize(parsed);
                }
                else
                {
                    AutoSerializeBase.CommandPropertySpec info = AutoSerializeBase.GetPropertySpecForType(cmd.GetType());

                    int attrLength = info.Length;
                    if (attrLength != -1 && attrLength != parsed.BodyLength)
                    {
                        Log.WarnFormat("{0}: Auto deserialize length mismatch", cmd.GetType().Name);
                    }

                    foreach (AutoSerializeBase.PropertySpec prop in info.Properties)
                    {
                        try
                        {
                            prop.Setter?.DynamicInvoke(cmd, prop.SerAttr.Deserialize(parsed.ReverseBytes, parsed.Body, prop.Attr.StartByte, prop.PropInfo));
                        }
                        catch (Exception e)
                        {
                            Log.WarnFormat("{0}: Failed to deserialize property {1}: {2}", cmd.GetType().Name, prop.PropInfo.Name, e.ToString());
                        }
                    }
                }

                return(cmd);
            }
            catch (Exception)
            {
                if (safe)
                {
                    return(null);
                }

                throw;
            }
        }