private bool ValidateCell(string data, int expectedLen) { byte[] bytes = Utility.StringToBytes(data); int len = bytes.Length; if (len != expectedLen) { if (expectedLen == 1) { MessageBox.Show("There must be " + expectedLen + " byte.", "Error"); } else { MessageBox.Show("There must be " + expectedLen + " bytes.", "Error"); } return(false); } return(true); }
private bool ValidateCellFrame(string data, int rowIdx, int columnIdx) { bool valid = true; try { if (columnIdx == 2) { if (parser.Format >= 7) { valid = ValidateCell(data, 32); } else { valid = ValidateCell(data, 16); } } else if (columnIdx == 3) { byte[] bytes = Utility.StringToBytes(data); int len = bytes.Length; if (parser.Format >= 7) { valid = ValidateCell(data, 64); } else { valid = ValidateCell(data, 16); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); valid = false; } return(valid); }
private bool ValidateCellSubmesh(string data, int rowIdx, int columnIdx) { bool valid = true; try { if (columnIdx == 2) { if (parser.Format >= 7) { valid = ValidateCell(data, 64); } else { valid = ValidateCell(data, 16); } } else if (columnIdx == 3) { if (parser.Format >= 7) { valid = ValidateCell(data, 20); } } else if (columnIdx == 4) { if (parser.Format >= 7) { valid = ValidateCell(data, 100); } } else if (columnIdx == 5) { if (parser.Format >= 7) { valid = ValidateCell(data, 284); } else if (parser.Format >= 3) { valid = ValidateCell(data, 64); } } else if (columnIdx == 6) { if (parser.Format >= 8) { byte[] bytes = Utility.StringToBytes(data); int nameLength = BitConverter.ToInt32(bytes, 1); valid = ValidateCell(data, 1 + 4 + nameLength + 12 + 4); } else if (parser.Format >= 5) { valid = ValidateCell(data, 20); } } else if (columnIdx == 7) { if (parser.Format == 6) { valid = ValidateCell(data, 28); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); valid = false; } return(valid); }
public object ExecuteExpr(Expr expr) { object result = null; if (expr is Literal) { Literal literal = (Literal)expr; if (literal.Type == ExprType.String) { result = literal.Value; } else if (literal.Type == ExprType.Bytes) { result = Utility.StringToBytes(literal.Value); } else if (literal.Type == ExprType.Name) { try { result = Variables[literal.Value.ToLowerInvariant()]; } catch (KeyNotFoundException) { result = null; } } else if (literal.Type == ExprType.Bool) { result = Boolean.Parse(literal.Value); } else if (literal.Type == ExprType.Number) { result = Double.Parse(literal.Value); } else if (literal.Type == ExprType.HexInt) { if (literal.Value.EndsWith("L", true, null)) { result = long.Parse(literal.Value.Substring(0, literal.Value.Length - 1), System.Globalization.NumberStyles.AllowHexSpecifier); } else { result = Int32.Parse(literal.Value, System.Globalization.NumberStyles.AllowHexSpecifier); } } else if (literal.Type == ExprType.Null) { } else { throw new Exception("Unexpected literal: " + literal.Type); } } else if (expr is Command) { Command cmd = (Command)expr; if (cmd.Type == ExprType.Root) { foreach (var arg in cmd.Args) { if (arg.Type == ExprType.Assign) { result = ExecuteExpr(arg); } else { result = ExecuteExpr(arg); defaultVar = result; } } } else if ((cmd.Type == ExprType.DotInstanceDefault) || (cmd.Type == ExprType.DotInstanceDefaultChain)) { result = ExecuteFunction((Command)cmd.Args[1], ExecuteExpr(cmd.Args[0]), true); } else if ((cmd.Type == ExprType.DotInstance) || (cmd.Type == ExprType.DotInstanceChain)) { result = ExecuteFunction((Command)cmd.Args[1], ExecuteExpr(cmd.Args[0]), false); } else if (cmd.Type == ExprType.DotDefault) { result = ExecuteFunction((Command)cmd.Args[0], null, true); } else if (cmd.Type == ExprType.Function) { result = ExecuteFunction(cmd, null, false); } else if ((cmd.Type == ExprType.DotProperty) || (cmd.Type == ExprType.DotPropertyChain)) { object obj = ExecuteExpr(cmd.Args[0]); string propertyName = ((Literal)cmd.Args[1]).Value; result = obj.GetType().GetProperty(propertyName).GetValue(obj, null); } else if (cmd.Type == ExprType.Indexed) { object obj = ExecuteExpr(cmd.Args[0]); object indexArg = ExecuteExpr(cmd.Args[1]); int index = indexArg is Double ? (int)(Double)indexArg : (int)indexArg; var type = obj.GetType(); if (type.IsArray) { result = ((Array)obj).GetValue(index); } else { var attributes = type.GetCustomAttributes(typeof(DefaultMemberAttribute), true); if (attributes.Length > 0) { var indexerName = ((DefaultMemberAttribute)attributes[0]).MemberName; result = type.GetProperty(indexerName).GetValue(obj, new object[] { index }); } else { throw new Exception(obj.ToString() + " can't be indexed."); } } } else if (cmd.Type == ExprType.Array) { object[] array = new object[cmd.Args.Count]; for (int i = 0; i < array.Length; i++) { array[i] = ExecuteExpr(cmd.Args[i]); } result = array; } else if (cmd.Type == ExprType.Assign) { string name = ((Literal)cmd.Args[0]).Value.ToLowerInvariant(); result = ExecuteExpr(cmd.Args[1]); if (Variables.ContainsKey(name)) { if (result != null) { Variables[name] = result; } else { Variables.Remove(name); } } else if (result != null) { Variables.Add(name, result); } } else if (cmd.Type == ExprType.Negative) { result = ((double)ExecuteExpr(cmd.Args[0])) * -1; } else if (cmd.Type == ExprType.Mod) { result = ((double)ExecuteExpr(cmd.Args[0])) % ((double)ExecuteExpr(cmd.Args[1])); } else if (cmd.Type == ExprType.Div) { result = ((double)ExecuteExpr(cmd.Args[0])) / ((double)ExecuteExpr(cmd.Args[1])); } else if (cmd.Type == ExprType.Mul) { result = ((double)ExecuteExpr(cmd.Args[0])) * ((double)ExecuteExpr(cmd.Args[1])); } else if (cmd.Type == ExprType.Sub) { result = ((double)ExecuteExpr(cmd.Args[0])) - ((double)ExecuteExpr(cmd.Args[1])); } else if (cmd.Type == ExprType.Add) { object left = ExecuteExpr(cmd.Args[0]); object right = ExecuteExpr(cmd.Args[1]); if ((left is String) && (right is String)) { result = ((string)left) + ((string)right); } else { result = ((double)left) + ((double)right); } } else { throw new Exception("Unexpected command: " + cmd.Type); } } return(result); }