public starInfo(string infoString) { var lines = infoString.Split(','); hygID = ParseUtility.SafeParseInt(lines[0]); hipID = ParseUtility.SafeParseInt(lines[1]); ProperName = lines[6]; colourIndex = ParseUtility.SafeParseFloat(lines[16]); color = colourUtility.colourIndexToRGB(colourIndex); AppMagnitude = ParseUtility.SafeParseFloat(lines[13]); AbsoluteMagnitude = ParseUtility.SafeParseFloat(lines[14]); position = new Vector3( ParseUtility.SafeParseFloat(lines[17]), ParseUtility.SafeParseFloat(lines[18]), ParseUtility.SafeParseFloat(lines[19])); position = Vector3.ClampMagnitude(position, 1000); velocity = new Vector3( ParseUtility.SafeParseFloat(lines[20]), ParseUtility.SafeParseFloat(lines[21]), ParseUtility.SafeParseFloat(lines[22])); }
public static bool SupportsType(Type type) { if (!type.IsValueType || string.IsNullOrEmpty(type.AssemblyQualifiedName) || type.FullName == SYSTEM_VOID) { return(false); } if (typeSupportCache.TryGetValue(type.AssemblyQualifiedName, out var info)) { return(info.IsSupported); } var supported = false; var fields = type.GetFields(INSTANCE_FLAGS); if (fields.Length > 0) { if (fields.Any(it => !ParseUtility.CanParse(it.FieldType))) { supported = false; info = new StructInfo(supported, null); } else { supported = true; info = new StructInfo(supported, fields); } } typeSupportCache.Add(type.AssemblyQualifiedName, info); return(supported); }
public void SetValue(object instance, string input, int fieldIndex) { var field = Fields[fieldIndex]; object val; if (field.FieldType == typeof(string)) { val = input; } else { if (!ParseUtility.TryParse(input, field.FieldType, out val, out Exception ex)) { ExplorerCore.LogWarning("Unable to parse input!"); if (ex != null) { ExplorerCore.Log(ex.ReflectionExToString()); } return; } } field.SetValue(instance, val); }
public void EmptyContainers() { JsonValue value = ParseUtility.ParseAndValidate(@"{ ""dict"": { ""array"": [{}] }, ""array"": [ [], {}, [], {} ] }"); Assert.Equal(1, value["dict"].Object.Count); Assert.Equal(4, value["array"].Array.Count); }
public static object GetValue(FieldInfo field, string stringValue) { string type = field.FieldType.Name; if (ParseUtility.IsInt64(type)) { return(long.Parse(stringValue)); } if (ParseUtility.IsInt32(type)) { return(int.Parse(stringValue)); } if (ParseUtility.IsSingle(type)) { return(float.Parse(stringValue)); } if (ParseUtility.IsDouble(type)) { return(double.Parse(stringValue)); } if (ParseUtility.IsBoolean(type)) { return(bool.Parse(stringValue)); } return(stringValue); }
private void OnRequestLine(string part1, string part2, string part3) { if (part1.StartsWith("http/")) { int code; if (!ParseUtility.TryParseInt(part2, out code)) { throw new BadRequestException( string.Concat("Second word in the status line should be a HTTP code, you specified ", part2, ".")); } if (_messageSerializer != null) { _message = new HttpResponse(code, part3, part1); } else { _message = new HttpResponseBase(code, part3, part1); } } else { if (!part3.StartsWith("http/")) { throw new BadRequestException( string.Concat("Status line for requests should end with the HTTP version. Your line ended with ", part3, ".")); } _message = _messageSerializer != null ? new HttpRequest(part1, part2, part3) : new HttpRequestBase(part1, part2, part3); } }
public string GetValue(object instance, int fieldIndex) { var field = Fields[fieldIndex]; var value = field.GetValue(instance); return(ParseUtility.ToStringForInput(value, field.FieldType)); }
public override bool InRectange(FreeUIUtil.Rectangle rec, IEventArgs args) { if (pos == null || change) { pos = selector.Select(args); } int px = (int)(pos.GetX()); int py = (int)(pos.GetY()); float r = 0f; try { r = /*float.Parse(radius)*/ ParseUtility.QuickFloatParse(radius); } catch (Exception) { r = FreeUtil.ReplaceFloat(radius, args); } float x1 = rec.x - r; float x2 = rec.x + rec.width + r; float y1 = rec.y - r; float y2 = rec.y + rec.height + r; // 圆心在矩形的加上圆半径的范围内 return(px >= x1 && px <= x2 && py >= y1 && py <= y2); }
public StarInfo(string csvLine) { var lines = csvLine.Split(','); hygId = ParseUtility.SafeIntParse(lines[0]); hipId = ParseUtility.SafeIntParse(lines[1]); properName = lines[6]; apparentMagnitude = ParseUtility.SafeFloatParse(lines[13]); absoluteMagnitude = ParseUtility.SafeFloatParse(lines[14]); colorIndex = ParseUtility.SafeFloatParse(lines[16]); color = ColorUtility.ColorIndexToRGB(colorIndex); position = new Vector3( ParseUtility.SafeFloatParse(lines[17]), ParseUtility.SafeFloatParse(lines[18]), ParseUtility.SafeFloatParse(lines[19]) ); velocity = new Vector3( ParseUtility.SafeFloatParse(lines[20]), ParseUtility.SafeFloatParse(lines[21]), ParseUtility.SafeFloatParse(lines[22]) ); position = Vector3.ClampMagnitude(position, 1000); }
public void ArrayEnum() { dynamic value = ParseUtility.ParseAndValidate(@"{ ""array"": [ 0, 1, 2, 3, 4 ] }"); dynamic darray = value.array; dynamic darray2 = value["array"]; IEnumerable <int> iarray = darray; IEnumerable <int> iarray2 = darray2; Assert.Equal(iarray, iarray2); object[] array = darray; List <KeyValuePair <string, int> > array1 = value.array; List <KeyValuePair <int, int> > array2 = value.array; int i = 0; foreach (decimal child in array) { int h = (int)child; Assert.Equal(h, int.Parse(array1[i].Key)); Assert.Equal(h, array1[i].Value); Assert.Equal(h, array2[i].Key); Assert.Equal(h, array2[i].Value); Assert.Equal(h, (int)darray[i]); Assert.Equal(h, i++); } for (i = 0; i < array.Length; i++) { int h = (int)(decimal)array[i]; Assert.Equal(h, i); } }
public void SimpleLookupTypes() { dynamic value = ParseUtility.ParseAndValidate( @"{ ""string"": ""bar"", ""int"": 32, ""double"": 32.5, ""bool"": true, ""null"": null, ""array"": [ 0, 1, 2 ], ""dict"": { ""array"": [ 0, 1, 2 ] } }"); string stringValue = value.@string; int intValue = value.@int; double doubleValue = value.@double; bool boolValue = value.@bool; object nullValue = value.@null; object[] arrayValue = value.array; IDictionary <string, object> dictValue = value.dict; Assert.Equal("bar", stringValue); Assert.Equal(32, intValue); Assert.Equal(32.5, doubleValue); Assert.True(boolValue); Assert.Null(nullValue); Assert.Equal(3, arrayValue.Length); Assert.Equal(1, dictValue.Count); }
public void ToDateSuccess() { DateTime date = new DateTime(1970, 7, 4, 12, 0, 30, 500, DateTimeKind.Local); dynamic value = ParseUtility.ParseAndValidate($@"{{ ""date"": ""{date.ToString("O", CultureInfo.InvariantCulture)}"" }}"); DateTime parsedDate = value.date; Assert.Equal(date, parsedDate); }
public void ToDateFailed() { DateTime date = new DateTime(1970, 7, 4, 12, 0, 30, 500, DateTimeKind.Local); dynamic value = ParseUtility.ParseAndValidate($@"{{ ""date"": ""Foo{date.ToString("O", CultureInfo.InvariantCulture)}"" }}"); JsonException ex = Assert.Throws <JsonException>(() => (DateTime)value.date); Assert.IsType <FormatException>(ex.InnerException); }
public void ParseException() { JsonException ex = Assert.Throws <JsonException>(() => ParseUtility.ParseAndValidate(@"{ ""foo"":: bar }")); Assert.True(ex.HasToken); Assert.Equal(0, ex.TokenLine); Assert.Equal(8, ex.TokenColumn); Assert.Equal(":", ex.TokenText); }
public void ConvertArrayIntermediate2() { JsonValue value = ParseUtility.ParseAndValidate(@"[ [ 2, 4, 6, 8 ], [ 1, 3, 5 ] ]"); int[][] ints = value.ToObject <int[][]>(); Assert.Equal(2, ints.Length); Assert.Equal(new int[][] { new int[] { 2, 4, 6, 8 }, new int[] { 1, 3, 5 } }, ints); }
public void ExceptionArray() { JsonException ex = Assert.Throws <JsonException>(() => ParseUtility.ParseAndValidate(@"{ ""foo"": [ 1, 2, { ""3"": { ""array"": [[,,]] } ] } }")); Assert.True(ex.HasToken); Assert.Equal(0, ex.TokenLine); Assert.Equal(37, ex.TokenColumn); Assert.Equal(",", ex.TokenText); }
public void TokenException() { JsonException ex = Assert.Throws <JsonException>(() => ParseUtility.ParseAndValidate(@"{ foo: bar }")); Assert.True(ex.HasToken); Assert.Equal(0, ex.TokenLine); Assert.Equal(2, ex.TokenColumn); Assert.Equal("foo", ex.TokenText.Substring(0, 3)); }
public void WritePrimitives() { Assert.Equal("null", ParseUtility.SerializeAndValidate(null, formatted: false)); Assert.Equal("true", ParseUtility.SerializeAndValidate(true, formatted: false)); Assert.Equal("false", ParseUtility.SerializeAndValidate(false, formatted: false)); Assert.Equal("\"hello world\"", ParseUtility.SerializeAndValidate("hello world", formatted: false)); Assert.Equal("12.34", ParseUtility.SerializeAndValidate(decimal.Parse("12.34"), formatted: false)); Assert.Equal("[1,2,3,4]", ParseUtility.SerializeAndValidate(new uint[] { 1, 2, 3, 4 }, formatted: false)); Assert.Equal("[1,\"2\",3,null,\"01/02/2003 00:00:00\"]", ParseUtility.SerializeAndValidate(new object[] { 1, "2", 3.0, null, DateTime.Parse("1/2/2003") }, formatted: false)); }
public void DictionaryEnum() { dynamic value = ParseUtility.ParseAndValidate(@"{ ""0"": 0, ""1"": 1, ""2"": 2, ""3"": 3, ""4"": 4 }"); foreach (KeyValuePair <string, object> pair in value) { int i = (int)(decimal)pair.Value; Assert.Equal(i, int.Parse(pair.Key)); } }
public void ConvertArrayIntermediate1() { JsonValue value = ParseUtility.ParseAndValidate(@"[ 1, 2, 3, 4 ]"); int[] ints = value.ToObject <int[]>(); object[] objects = value.ToObject <object[]>(); Assert.Equal(4, objects.Length); Assert.Equal(4, ints.Length); Assert.Equal(new int[] { 1, 2, 3, 4 }, ints); }
// CacheObjectCell Apply public virtual void OnCellApplyClicked() { if (State == ValueState.Boolean) { SetUserValue(this.CellView.Toggle.isOn); } else { if (ParseUtility.TryParse(CellView.InputField.Text, LastValueType, out object value, out Exception ex)) { SetUserValue(value); }
public override bool IsIn(IEventArgs args, UnitPosition entity) { if (pos == null || change) { pos = selector.Select(args); } float r = 0f; try { r = /*float.Parse(radius)*/ ParseUtility.QuickFloatParse(radius); } catch (Exception) { r = FreeUtil.ReplaceFloat(radius, args); } double dx = MyMath.Abs(entity.GetX() - pos.GetX()); if (dx > r) { if (useOut) { return(true); } return(false); } double dz = MyMath.Abs(entity.GetZ() - pos.GetZ()); if (dz > r) { if (useOut) { return(true); } return(false); } double dy = MyMath.Abs(entity.GetY() - pos.GetY()); float zrange = FreeUtil.ReplaceFloat(zRange, args); if (zrange <= 0) { zrange = 170; } bool isIn = (dx * dx + dz * dz) <= r * r && (dy < zrange); if (useOut) { return(!isIn); } return(isIn); }
public static IPara GetPara(object obj, string field) { if (obj == null) { return(null); } FieldInfo f = GetField(obj, field); AbstractPara para = null; if (f != null) { string type = f.FieldType.Name; if (ParseUtility.IsInt64(type)) { para = new LongPara(field); } if (ParseUtility.IsInt32(type)) { para = new IntPara(field); } if (ParseUtility.IsSingle(type)) { para = new FloatPara(field); } if (ParseUtility.IsDouble(type)) { para = new DoublePara(field); } if (ParseUtility.IsString(type)) { para = new StringPara(field); } if (ParseUtility.IsBoolean(type)) { para = new BoolPara(field); } try { if (para != null) { para.SetValue(f.GetValue(obj)); } } catch (Exception e) { throw new GameConfigExpception(field + " is not a valid field.\n" + ExceptionUtil.GetExceptionContent(e)); } } return(para); }
protected virtual void SetValueState(CacheObjectCell cell, ValueStateArgs args) { // main value label if (args.valueActive) { cell.ValueLabel.text = ValueLabelText; cell.ValueLabel.supportRichText = args.valueRichText; cell.ValueLabel.color = args.valueColor; } else { cell.ValueLabel.text = ""; } // Type label (for primitives) cell.TypeLabel.gameObject.SetActive(args.typeLabelActive); if (args.typeLabelActive) { cell.TypeLabel.text = SignatureHighlighter.Parse(LastValueType, false); } // toggle for bools cell.Toggle.gameObject.SetActive(args.toggleActive); if (args.toggleActive) { cell.Toggle.interactable = CanWrite; cell.Toggle.isOn = (bool)Value; cell.ToggleText.text = Value.ToString(); } // inputfield for numbers cell.InputField.UIRoot.SetActive(args.inputActive); if (args.inputActive) { cell.InputField.Text = ParseUtility.ToStringForInput(Value, LastValueType); cell.InputField.Component.readOnly = !CanWrite; } // apply for bool and numbers cell.ApplyButton.Component.gameObject.SetActive(args.applyActive); // Inspect button only if last value not null. if (cell.InspectButton != null) { cell.InspectButton.Component.gameObject.SetActive(args.inspectActive && !LastValueWasNull); } // allow IValue for null strings though cell.SubContentButton.Component.gameObject.SetActive(args.subContentButtonActive && (!LastValueWasNull || State == ValueState.String)); }
public void ParseStringWithHeaders(string actual, string expected) { var actualResult = ParseUtility.ParseString( new HeaderStruct(() => new[] { actual }), string.Empty); actualResult.Should().Be(expected); actualResult = ParseUtility.ParseString( new HeaderStruct(() => new List<string> { actual }), string.Empty); actualResult.Should().Be(expected); }
public override IPara Initial(string con, string v) { com.wd.free.para.BoolPara p = (com.wd.free.para.BoolPara) this.Copy(); p.name = EMPTY_NAME; try { p.value = ParseUtility.QuickBoolParse(v); } catch (Exception) { p.value = false; } return(p); }
public void DictionaryEnum() { JsonValue value = ParseUtility.ParseAndValidate(@"{ ""0"": 0, ""1"": 1, ""2"": 2, ""3"": 3, ""4"": 4 }"); foreach (KeyValuePair <string, JsonValue> pair in value.Object) { Assert.Equal(int.Parse(pair.Key), pair.Value.Number); } foreach (string key in value.Object.Keys) { Assert.True(value.Object.ContainsKey(key)); Assert.Equal(int.Parse(key), value[key].Number); } }
public T TryGetHeaderValue <T>(string headerName, T defaultValue) { if (Headers.ContainsKey(headerName)) { try { return(ParseUtility.ChangeType <T>(Headers[headerName])); } catch (Exception) { return(defaultValue); } } return(defaultValue); }
public void Comment() { JsonValue value = ParseUtility.ParseAndValidate( @"{ /* Comment Comment Comment */ ""foo"": 1, // Comment // Comment ""bar"": 2 }"); Assert.Equal(1, value["foo"].Number); Assert.Equal(2, value["bar"].Number); }
public void ParseString(string actual, string expected) { var actualResult = ParseUtility.ParseString( (object)null, new FuncGetter<object>((carrier, name) => new[] { actual }), string.Empty); actualResult.Should().Be(expected); actualResult = ParseUtility.ParseString( (object)null, new FuncGetter<object>((carrier, name) => new List<string> { actual }), string.Empty); actualResult.Should().Be(expected); }