From 2adbca628be76bf6b8afbc5f02f20cdb66e5ef59 Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 23 Jan 2020 21:30:39 -0800 Subject: [PATCH] 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 --- .../Saves/Substructures/Gen7/SaveBlock.cs | 4 + .../Gen8/SAV_BlockDump8.Designer.cs | 3 +- .../Save Editors/Gen8/SAV_BlockDump8.cs | 96 ++++++++++++++++--- 3 files changed, 88 insertions(+), 15 deletions(-) diff --git a/PKHeX.Core/Saves/Substructures/Gen7/SaveBlock.cs b/PKHeX.Core/Saves/Substructures/Gen7/SaveBlock.cs index 061fe673c..1b3bf85b5 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/SaveBlock.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/SaveBlock.cs @@ -1,3 +1,5 @@ +using System.ComponentModel; + namespace PKHeX.Core { /// @@ -5,7 +7,9 @@ namespace PKHeX.Core /// 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; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.Designer.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.Designer.cs index 0cb9913bd..6a8eefbc8 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.Designer.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.Designer.cs @@ -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 // diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs index aaca54af5..36fdf58c8 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs @@ -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 + { + 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(block, block.GetValue()), + SCTypeCode.UInt16 => new WrappedValueView(block, block.GetValue()), + SCTypeCode.UInt32 => new WrappedValueView(block, block.GetValue()), + SCTypeCode.UInt64 => new WrappedValueView(block, block.GetValue()), + + SCTypeCode.SByte => new WrappedValueView(block, block.GetValue()), + SCTypeCode.Int16 => new WrappedValueView(block, block.GetValue()), + SCTypeCode.Int32 => new WrappedValueView(block, block.GetValue()), + SCTypeCode.Int64 => new WrappedValueView(block, block.GetValue()), + + SCTypeCode.Single => new WrappedValueView(block, block.GetValue()), + SCTypeCode.Double => new WrappedValueView(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); + } } }