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); }
private static long GetId(ICommand cmd) { var spec = AutoSerializeBase.GetPropertySpecForType(cmd.GetType()); int hashCode = 0; foreach (AutoSerializeBase.PropertySpec prop in spec.Properties.Where(p => p.IsCommandId)) { hashCode = (hashCode * 256) + 1 + Convert.ToInt32(prop.Getter.DynamicInvoke(cmd)); } return(hashCode); }
public static IReadOnlyList <string> Validate(DeviceProfile profile, ICommand cmd, bool failOnFirst = false) { var res = new List <string>(); if (!(cmd is AutoSerializeBase asbCmd)) { return(res); } var props = AutoSerializeBase.GetPropertySpecForType(asbCmd.GetType()); List <string> propNamesToCheck = null; // If there is a mask, only validate the ones specified by the mask AutoSerializeBase.PropertySpec maskProp = props.Properties.Where(p => p.PropInfo.Name == "Mask").FirstOrDefault(); if (maskProp != null) { // Only check fields which are indicated by the mask propNamesToCheck = maskProp.Getter.DynamicInvoke(cmd).ToString() .Split(',').Select(s => s.Trim()).Concat(new[] { "Mask" }).ToList(); } foreach (AutoSerializeBase.PropertySpec prop in props.Properties) { if (prop.Getter == null) { continue; } // If prop is not to check and not commandId then skip if (!prop.IsCommandId && propNamesToCheck != null && !propNamesToCheck.Contains(prop.PropInfo.Name)) { continue; } object val = prop.Getter.DynamicInvoke(cmd); if (!prop.SerAttr.IsValid(prop.PropInfo, val) || !AvailabilityChecker.IsAvailable(profile, val)) { res.Add("Invalid value: " + asbCmd.GetType().FullName + "." + prop.PropInfo.Name + ": " + val); if (failOnFirst) { return(res); } } } return(res); }
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()); }
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); }
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; } }
static AtemConnection() { AutoSerializeBase.CompilePropertySpecForTypes(CommandManager.GetAllTypes().SelectMany(g => g.Value.Select(t => t.Item2))); AutoSerializeBase.CompilePropertySpecForTypes(MacroOpManager.FindAll().Select(m => m.Value.Item2)); }
static AtemConnection() { AutoSerializeBase.CompilePropertySpecForTypes(CommandManager.GetAllTypes().Values); AutoSerializeBase.CompilePropertySpecForTypes(MacroOpManager.FindAll().Values); }