/// <summary> /// Initializes a new instance of the <see cref="AddressItem" /> class. /// </summary> /// <param name="baseAddress">The base address. This will be added as an offset from the resolved base identifier.</param> /// <param name="elementType">The data type of the value at this address.</param> /// <param name="description">The description of this address.</param> /// <param name="resolveType">The identifier type for this address item.</param> /// <param name="baseIdentifier">The identifier for the base address of this object.</param> /// <param name="offsets">The pointer offsets of this address item.</param> /// <param name="isValueHex">A value indicating whether the value at this address should be displayed as hex.</param> /// <param name="value">The value at this address. If none provided, it will be figured out later. Used here to allow immediate view updates upon creation.</param> public AddressItem( IntPtr baseAddress, Type elementType, String description = "New Address", AddressResolver.ResolveTypeEnum resolveType = AddressResolver.ResolveTypeEnum.Module, String baseIdentifier = null, IEnumerable <Int32> offsets = null, Boolean isValueHex = false, String value = null) : base(description) { // Bypass setters to avoid running setter code this.baseAddress = baseAddress; this.ElementType = elementType; this.resolveType = resolveType; this.baseIdentifier = baseIdentifier; this.offsets = offsets; this.isValueHex = isValueHex; if (!this.isValueHex && CheckSyntax.CanParseValue(elementType, value)) { this.addressValue = Conversions.ParsePrimitiveStringAsDynamic(elementType, value); } else if (this.isValueHex && CheckSyntax.CanParseHex(elementType, value)) { this.addressValue = Conversions.ParseHexStringAsPrimitiveString(elementType, value); } }
/// <summary> /// Converts an address string to the corresponding value. /// </summary> /// <param name="context">Type descriptor context.</param> /// <param name="culture">Globalization info.</param> /// <param name="value">The value being converted.</param> /// <returns>The converted value.</returns> public override Object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) { Type valueType = null; Boolean isHex = false; if (context.Instance.GetType().IsAssignableFrom(typeof(AddressItem))) { valueType = (context.Instance as AddressItem)?.ElementType; } if (context.Instance.GetType().IsAssignableFrom(typeof(AddressItem))) { isHex = (context.Instance as AddressItem).IsValueHex; } if (valueType == null || !value.GetType().IsAssignableFrom(typeof(String))) { return(base.ConvertFrom(context, culture, value)); } if (!(isHex ? CheckSyntax.CanParseHex(valueType, value as String) : CheckSyntax.CanParseValue(valueType, value as String))) { return(base.ConvertFrom(context, culture, value)); } return(isHex ? Conversions.ParseHexStringAsDynamic(valueType, value as String) : Conversions.ParsePrimitiveStringAsDynamic(valueType, value as String)); }
/// <summary> /// Event for double clicking a project node. /// </summary> /// <param name="sender">Sending object.</param> /// <param name="e">Tree node mouse event args.</param> private void ProjectExplorerTreeViewNodeMouseDoubleClick(Object sender, TreeNodeAdvMouseEventArgs e) { if (e == null || e.Node == null) { return; } ProjectItem projectItem = this.GetProjectItemFromNode(e.Node); if (projectItem is AddressItem) { ValueEditorModel valueEditor = new ValueEditorModel(); AddressItem addressItem = projectItem as AddressItem; dynamic result = valueEditor.EditValue(null, null, addressItem); if (CheckSyntax.CanParseValue(addressItem.ElementType, result?.ToString())) { addressItem.Value = result; } } else if (projectItem is ScriptItem) { ScriptEditorModel scriptEditor = new ScriptEditorModel(); ScriptItem scriptItem = projectItem as ScriptItem; scriptItem.Script = scriptEditor.EditValue(null, null, scriptItem.Script) as String; } }
/// <summary> /// Converts a value to an address. /// </summary> /// <param name="context">Type descriptor context.</param> /// <param name="culture">Globalization info.</param> /// <param name="value">The value being converted.</param> /// <param name="destinationType">The target type to convert to.</param> /// <returns>The converted value.</returns> public override Object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType) { if (CheckSyntax.CanParseValue(typeof(UInt64), value.ToString())) { return(Conversions.ToHex(Conversions.ParsePrimitiveStringAsDynamic(typeof(UInt64), value.ToString()), formatAsAddress: true, includePrefix: false)); } return(base.ConvertTo(context, culture, value, destinationType)); }
/// <summary> /// Invoked when the convert to hexedecimal menu item is clicked. /// </summary> /// <param name="sender">Sending object.</param> /// <param name="e">Event args.</param> private void ConvertToHexMenuItemClick(Object sender, EventArgs e) { if (CheckSyntax.CanParseValue(this.ElementType, this.Text)) { this.Text = Conversions.ParsePrimitiveStringAsHexString(this.ElementType, this.Text); } this.IsHex = true; }
/// <summary> /// Converts a value to the proper dynamic type. /// </summary> /// <param name="context">Type descriptor context.</param> /// <param name="culture">Globalization info.</param> /// <param name="value">The value being converted.</param> /// <param name="destinationType">The target type to convert to.</param> /// <returns>The converted value.</returns> public override Object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType) { String valueString = (value == null) ? String.Empty : value.ToString(); Type valueType = (value == null) ? null : value.GetType(); Boolean isHex = false; if (context.Instance.GetType().IsAssignableFrom(typeof(AddressItem))) { isHex = (context.Instance as AddressItem).IsValueHex; } if (value == null || !CheckSyntax.CanParseValue(valueType, valueString)) { return(base.ConvertTo(context, culture, value, destinationType)); } return(isHex ? Conversions.ParseDynamicAsHexString(valueType, valueString) : valueString); }
/// <summary> /// Sets the raw value being represented. /// </summary> /// <param name="value">The raw value.</param> public void SetValue(Object value) { String valueString = value?.ToString(); if (!CheckSyntax.CanParseValue(this.ElementType, valueString)) { return; } if (this.IsHex) { this.Text = Conversions.ParsePrimitiveStringAsHexString(this.ElementType, valueString); } else { this.Text = valueString; } }
/// <summary> /// Initializes a new instance of the <see cref="AddressItem" /> class. /// </summary> /// <param name="baseAddress">The base address. This will be added as an offset from the resolved base identifier.</param> /// <param name="dataType">The data type of the value at this address.</param> /// <param name="description">The description of this address.</param> /// <param name="baseIdentifier">The identifier for the base address of this object.</param> /// <param name="offsets">The pointer offsets of this address item.</param> /// <param name="isValueHex">A value indicating whether the value at this address should be displayed as hex.</param> /// <param name="value">The value at this address. If none provided, it will be figured out later. Used here to allow immediate view updates upon creation.</param> public AddressItem( Type dataType, String description = "New Address", Boolean isValueHex = false, String value = null) : base(description) { // Bypass setters to avoid running setter code this.dataType = dataType; this.isValueHex = isValueHex; if (!this.isValueHex && CheckSyntax.CanParseValue(dataType, value)) { this.addressValue = Conversions.ParsePrimitiveStringAsPrimitive(dataType, value); } else if (this.isValueHex && CheckSyntax.CanParseHex(dataType, value)) { this.addressValue = Conversions.ParseHexStringAsPrimitiveString(dataType, value); } }
/// <summary> /// Edits a project item based on the project item type. /// </summary> /// <param name="projectItem">The project item to edit.</param> private void EditProjectItem(ProjectItem projectItem) { if (projectItem is AddressItem) { ValueEditorModel valueEditor = new ValueEditorModel(); AddressItem addressItem = projectItem as AddressItem; dynamic result = valueEditor.EditValue(null, null, addressItem); if (CheckSyntax.CanParseValue(addressItem.DataType, result?.ToString())) { addressItem.AddressValue = result; } } else if (projectItem is ScriptItem) { ScriptEditorModel scriptEditor = new ScriptEditorModel(); ScriptItem scriptItem = projectItem as ScriptItem; scriptItem.Script = scriptEditor.EditValue(null, null, scriptItem.Script) as String; } }
/// <summary> /// Determines if the current text is valid for the current data type. /// </summary> private void UpdateValidity() { if (this.IsHex) { if (CheckSyntax.CanParseHex(this.ElementType, this.Text)) { this.IsTextValid = true; return; } } else { if (CheckSyntax.CanParseValue(this.ElementType, this.Text)) { this.IsTextValid = true; return; } } this.IsTextValid = false; return; }