Exemple #1
0
        private static bool Equals(MacroOpBase x, MacroOpBase y)
        {
            if (x == null || y == null)
            {
                return(false);
            }

            if (x.GetType() != y.GetType())
            {
                return(false);
            }

            AutoSerializeBase.CommandPropertySpec info = AutoSerializeBase.GetPropertySpecForType(x.GetType());

            foreach (var prop in info.Properties)
            {
                object xVal = prop.Getter.DynamicInvoke(x);
                object yVal = prop.Getter.DynamicInvoke(y);
                if (Equals(xVal, yVal))
                {
                    continue;
                }

                if (prop.PropInfo.PropertyType == typeof(double) && Math.Abs((double)xVal - (double)yVal) <= 0.001)
                {
                    continue;
                }

                return(false);
            }

            return(true);
        }
Exemple #2
0
        private static string ToString(MacroOpBase op)
        {
            AutoSerializeBase.CommandPropertySpec info = AutoSerializeBase.GetPropertySpecForType(op.GetType());

            var sb = new StringBuilder();

            sb.Append(op.GetType().Name);
            sb.Append(":\n");

            foreach (var prop in info.Properties)
            {
                sb.AppendFormat("    {0}={1}\n", prop.PropInfo.Name, prop.Getter.DynamicInvoke(op));
            }

            return(sb.ToString());
        }
Exemple #3
0
        public static bool ValidateCommandMatches <T>(ICommand cmd, T expectedCmd, bool followMask, params string[] ignoreProps) where T : AutoSerializeBase
        {
            if (cmd is T cmd2)
            {
                AutoSerializeBase.CommandPropertySpec spec = AutoSerializeBase.GetPropertySpecForType(typeof(T));

                AutoSerializeBase.PropertySpec maskProp = spec.Properties.FirstOrDefault(p => p.PropInfo.Name == "Mask");
                var maskedNames = new HashSet <string> {
                    "Mask"
                };
                if (maskProp != null)
                {
                    Enum maskVal = maskProp.Getter.DynamicInvoke(cmd) as Enum;
                    IEnumerable <string> components = Enum.GetValues(maskProp.PropInfo.PropertyType).OfType <Enum>()
                                                      .Where(v => maskVal.HasFlag(v)).Select(c => c.ToString());
                    maskedNames.AddRange(components);
                }

                foreach (var prop in spec.Properties)
                {
                    if (followMask && !maskedNames.Contains(prop.PropInfo.Name))
                    {
                        continue;
                    }
                    if (ignoreProps.Contains(prop.PropInfo.Name))
                    {
                        continue;
                    }

                    object expectedValue = prop.Getter.DynamicInvoke(expectedCmd);
                    object actualValue   = prop.Getter.DynamicInvoke(cmd2);
                    Assert.Equal(expectedValue, actualValue);
                }

                // Accept it
                return(true);
            }

            return(false);
        }
Exemple #4
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;
            }
        }