Add raw value edits

Not done for Array types or Object types
muh reflection, such spooky

mark Offset as non-browsable so it doesn't show up in propertygrid if the block is being edited by a grid :)

I imagine a struct-type-sensitive array property grid edit could be done via Buffer.BlockCopy to a dest array, but there's so few Array blocks... so meh
This commit is contained in:
Kurt 2020-01-23 21:30:39 -08:00
parent 52309fd0d2
commit 2adbca628b
3 changed files with 88 additions and 15 deletions

View File

@ -1,3 +1,5 @@
using System.ComponentModel;
namespace PKHeX.Core
{
/// <summary>
@ -5,7 +7,9 @@ namespace PKHeX.Core
/// </summary>
public abstract class SaveBlock
{
[Browsable(false)]
public int Offset { get; protected set; }
public readonly byte[] Data;
protected readonly SaveFile SAV;
protected SaveBlock(SaveFile sav) => Data = (SAV = sav).Data;

View File

@ -64,7 +64,7 @@ private void InitializeComponent()
//
this.CB_Key.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.CB_Key.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.CB_Key.DropDownWidth = 234;
this.CB_Key.DropDownWidth = 270;
this.CB_Key.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CB_Key.FormattingEnabled = true;
this.CB_Key.Location = new System.Drawing.Point(83, 9);
@ -240,6 +240,7 @@ private void InitializeComponent()
this.PG_BlockView.Name = "PG_BlockView";
this.PG_BlockView.Size = new System.Drawing.Size(114, 130);
this.PG_BlockView.TabIndex = 14;
this.PG_BlockView.PropertyValueChanged += new System.Windows.Forms.PropertyValueChangedEventHandler(this.PG_BlockView_PropertyValueChanged);
//
// RTB_Hex
//

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
@ -79,26 +81,39 @@ private void UpdateBlockSummaryControls()
RTB_Hex.Text = string.Join(" ", block.Data.Select(z => $"{z:X2}"));
string blockName = GetBlockName(block, out SaveBlock obj);
if (blockName == null)
if (blockName != null)
{
L_BlockName.Visible = false;
return;
}
if (obj != null && ModifierKeys != Keys.Control)
{
// property grid instead of hex view?
PG_BlockView.SelectedObject = obj;
var props = ReflectUtil.GetPropertiesCanWritePublicDeclared(obj.GetType());
PG_BlockView.Visible = props.Count() > 1;
L_BlockName.Visible = true;
L_BlockName.Text = blockName;
}
else
{
PG_BlockView.Visible = false;
L_BlockName.Visible = false;
}
L_BlockName.Visible = true;
L_BlockName.Text = blockName;
if (ModifierKeys != Keys.Control)
{
// Show a PropertyGrid to edit
if (obj != null)
{
var props = ReflectUtil.GetPropertiesCanWritePublicDeclared(obj.GetType());
if (props.Count() > 1)
{
PG_BlockView.Visible = true;
PG_BlockView.SelectedObject = obj;
return;
}
}
var o = WrapUtil.GetWrapped(block);
if (o != null)
{
PG_BlockView.Visible = true;
PG_BlockView.SelectedObject = o;
return;
}
}
PG_BlockView.Visible = false;
}
private string GetBlockName(SCBlock block, out SaveBlock saveBlock)
@ -244,5 +259,58 @@ private static void ImportSelectBlock(SCBlock blockTarget)
var bytes = File.ReadAllBytes(path);
bytes.CopyTo(data, 0);
}
private class WrappedValueView<T>
{
private readonly SCBlock Parent;
private T _value;
[Description("Stored Value for this Block")]
public T Value
{
get => _value;
set => Parent.SetValue(_value = value);
}
[Description("Type of Value this Block stores")]
public string ValueType => typeof(T).Name;
public WrappedValueView(SCBlock block, object currentValue)
{
Parent = block;
_value = (T)Convert.ChangeType(currentValue, typeof(T));
}
}
private static class WrapUtil
{
public static object GetWrapped(SCBlock block)
{
return block.Type switch
{
SCTypeCode.Byte => new WrappedValueView<byte>(block, block.GetValue()),
SCTypeCode.UInt16 => new WrappedValueView<ushort>(block, block.GetValue()),
SCTypeCode.UInt32 => new WrappedValueView<uint>(block, block.GetValue()),
SCTypeCode.UInt64 => new WrappedValueView<ulong>(block, block.GetValue()),
SCTypeCode.SByte => new WrappedValueView<sbyte>(block, block.GetValue()),
SCTypeCode.Int16 => new WrappedValueView<short>(block, block.GetValue()),
SCTypeCode.Int32 => new WrappedValueView<int>(block, block.GetValue()),
SCTypeCode.Int64 => new WrappedValueView<long>(block, block.GetValue()),
SCTypeCode.Single => new WrappedValueView<float>(block, block.GetValue()),
SCTypeCode.Double => new WrappedValueView<double>(block, block.GetValue()),
_ => null,
};
}
}
private void PG_BlockView_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
Debug.WriteLine($"ChangedItem = {e.ChangedItem.Label}, OldValue = {e.OldValue}, NewValue = {e.ChangedItem.Value}");
if (CurrentBlock.Type != SCTypeCode.Object && CurrentBlock.Type != SCTypeCode.Array)
L_Detail_R.Text = GetBlockSummary(CurrentBlock);
}
}
}