Skip to content

Commit

Permalink
add a copy method in ram viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
architdate committed Nov 27, 2021
1 parent 2f876f4 commit 0379c6d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 21 deletions.
20 changes: 16 additions & 4 deletions AutoLegalityMod/GUI/SimpleHexEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions AutoLegalityMod/GUI/SimpleHexEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public SimpleHexEditor(byte[] originalBytes, PokeSysBotMini? bot = null, ulong a
address = addr;
method = rwm;
psb = bot;
CB_CopyMethod.DataSource = Enum.GetValues(typeof(CopyMethod)).Cast<CopyMethod>();
CB_CopyMethod.SelectedItem = CopyMethod.Bytes;
RTB_RAM.Text = string.Join(" ", originalBytes.Select(z => $"{z:X2}"));
Bytes = originalBytes;
}
Expand Down Expand Up @@ -65,6 +67,11 @@ private void Update_Click(object sender, EventArgs e)
Close();
}

private void ChangeCopyMethod(object sender, EventArgs e)
{
RTB_RAM.method = (CopyMethod)CB_CopyMethod.SelectedItem;
}

private void CB_AutoRefresh_CheckedChanged(object sender, EventArgs e)
{
if (CB_AutoRefresh.Checked)
Expand All @@ -87,4 +94,47 @@ private void SimpleHexEditor_FormClosing(object sender, FormClosingEventArgs e)
refresh.Stop();
}
}

internal class HexRichTextBox : RichTextBox
{
public CopyMethod method = CopyMethod.Bytes;

protected override bool ProcessCmdKey(ref Message msg, Keys e)
{
bool ctrlV = e == (Keys.Control | Keys.V);
bool shiftIns = e == (Keys.Shift | Keys.Insert);
bool ctrlC = e == (Keys.Control | Keys.C);
bool ctrlX = e == (Keys.Control | Keys.X);

if (method == CopyMethod.Integers && (ctrlV || shiftIns))
{
var text = Clipboard.GetText();
if (text != null)
{
var split = new string[text.Length / 2 + (text.Length % 2 == 0 ? 0 : 1)];
for (int i = 0; i < split.Length; i++)
split[i] = text.Substring(i * 2, i * 2 + 2 > text.Length ? 1 : 2);
Clipboard.SetText(string.Join(" ", split));
}
}
var handled = base.ProcessCmdKey(ref msg, e);
if (method == CopyMethod.Integers)
{
if (ctrlC || ctrlX)
{
if (string.IsNullOrWhiteSpace(SelectedText))
return false;
Clipboard.SetText(string.Join(string.Empty, SelectedText.Split(' ').Reverse()));
return true;
}
}
return handled;
}
}

internal enum CopyMethod
{
Bytes,
Integers,
}
}
52 changes: 35 additions & 17 deletions PKHeX.Core.Injection/Protocols/LPBDSP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static class LPBDSP
private const int DAYCARE_BLOCK_SIZE = 0x2C0;
private const int DAYCARE_BLOCK_SIZE_RAM = 0x8 * 4;

private const int MYSTATUS_BLOCK_SIZE = 0x50;
private const int MYSTATUS_BLOCK_SIZE_RAM = 0x34;

#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
public static Dictionary<string, (Func<PokeSysBotMini, byte[]?>, Action<PokeSysBotMini, byte[]>)> FunctionMap = new ()
{
Expand Down Expand Up @@ -139,22 +142,26 @@ public static void SendBox(PokeSysBotMini psb, byte[] boxData, int box)
var ptr = GetTrainerPointer(lv);
if (ptr == null || psb.com is not ICommunicatorNX sb)
return null;
var size = RamOffsets.GetTrainerBlockSize(lv);
var retval = new byte[size];
var retval = new byte[MYSTATUS_BLOCK_SIZE];
var ram_block = InjectionUtil.GetPointerAddress(sb, ptr);
if (ram_block == InjectionUtil.INVALID_PTR)
throw new Exception("Invalid Pointer string.");

var trainer_name = ptr + "]+14";
var trainer_name = ptr.ExtendPointer(0x14);
var trainer_name_addr = InjectionUtil.GetPointerAddress(sb, trainer_name);
if (trainer_name_addr == InjectionUtil.INVALID_PTR)
throw new Exception("Invalid Pointer string.");
psb.com.ReadBytes(trainer_name_addr, 0x1A).CopyTo(retval);

var trainer_block_ram = InjectionUtil.GetPointerAddress(sb, ptr);
if (trainer_block_ram == InjectionUtil.INVALID_PTR)
throw new Exception("Invalid Pointer string.");
psb.com.ReadBytes(trainer_block_ram + 0x8, size - 0x1A - 0x2).CopyTo(retval, 0x1A + 0x2);

// manually set ROM Code to avoid throwing exceptions (bad ram possibly)
retval[0x2B] = BrilliantDiamond.Contains(lv) ? (byte)0 : (byte)1;
var extra = psb.com.ReadBytes(ram_block, MYSTATUS_BLOCK_SIZE_RAM);
// TID, SID, Money, Male
extra.Slice(0x8, 0x9).CopyTo(retval, 0x1C);
// Region Code, Badge Count, TrainerView, ROMCode, GameClear
extra.Slice(0x11, 0x5).CopyTo(retval, 0x28);
// BodyType, Fashion ID
extra.Slice(0x16, 0x2).CopyTo(retval, 0x30);
// StarterType, DSPlayer, FollowIndex, X, Y, Height, Rotation
extra.SliceEnd(0x18).CopyTo(retval, 0x34);

return retval;
};
Expand Down Expand Up @@ -227,14 +234,25 @@ public static void SetMyStatusBlock(PokeSysBotMini psb, byte[] data)
var ptr = GetTrainerPointer(lv);
if (ptr == null || psb.com is not ICommunicatorNX sb)
return;
var size = RamOffsets.GetTrainerBlockSize(lv);
var size = MYSTATUS_BLOCK_SIZE;
data = data.Slice(0, size);
var trainer_name = ptr + "]+14";
var trainer_name = ptr.ExtendPointer(0x14);
var trainer_name_addr = InjectionUtil.GetPointerAddress(sb, trainer_name);
if (trainer_name_addr == InjectionUtil.INVALID_PTR)
throw new Exception("Invalid Pointer string.");

var retval = new byte[MYSTATUS_BLOCK_SIZE_RAM];
// TID, SID, Money, Male
data.Slice(0x1C, 0x9).CopyTo(retval, 0x8);
// Region Code, Badge Count, TrainerView, ROMCode, GameClear
data.Slice(0x28, 0x5).CopyTo(retval, 0x11);
// BodyType, Fashion ID
data.Slice(0x30, 0x2).CopyTo(retval, 0x16);
// StarterType, DSPlayer, FollowIndex, X, Y, Height, Rotation
data.SliceEnd(0x34).CopyTo(retval, 0x18);

psb.com.WriteBytes(data.Slice(0, 0x1A), trainer_name_addr);
psb.com.WriteBytes(data.SliceEnd(0x1A + 0x2), InjectionUtil.GetPointerAddress(sb, ptr) + 0x8);
psb.com.WriteBytes(retval.SliceEnd(0x8), InjectionUtil.GetPointerAddress(sb, ptr) + 0x8);
}

public static byte[]? GetDaycareBlock(PokeSysBotMini psb)
Expand All @@ -244,8 +262,8 @@ public static void SetMyStatusBlock(PokeSysBotMini psb, byte[] data)
return null;
var nx = (ICommunicatorNX)psb.com;
var addr = InjectionUtil.GetPointerAddress(nx, ptr);
var parent_one = psb.com.ReadBytes(InjectionUtil.GetPointerAddress(nx, ptr + "]+20]+20"), 0x158);
var parent_two = psb.com.ReadBytes(InjectionUtil.GetPointerAddress(nx, ptr + "]+28]+20"), 0x158);
var parent_one = psb.com.ReadBytes(InjectionUtil.GetPointerAddress(nx, ptr.ExtendPointer(0x20, 0x20)), 0x158);
var parent_two = psb.com.ReadBytes(InjectionUtil.GetPointerAddress(nx, ptr.ExtendPointer(0x28, 0x20)), 0x158);
var extra = psb.com.ReadBytes(addr + 0x8, 0x18);
var extra_arr = Core.ArrayUtil.EnumerateSplit(extra, 0x8).ToArray();
var block = new byte[DAYCARE_BLOCK_SIZE];
Expand All @@ -264,8 +282,8 @@ public static void SetDaycareBlock(PokeSysBotMini psb, byte[] data)
return;
var nx = (ICommunicatorNX)psb.com;
var addr = InjectionUtil.GetPointerAddress(nx, ptr);
var parent_one_addr = InjectionUtil.GetPointerAddress(nx, ptr + "]+20]+20");
var parent_two_addr = InjectionUtil.GetPointerAddress(nx, ptr + "]+28]+20");
var parent_one_addr = InjectionUtil.GetPointerAddress(nx, ptr.ExtendPointer(0x20, 0x20));
var parent_two_addr = InjectionUtil.GetPointerAddress(nx, ptr.ExtendPointer(0x28, 0x20));
data = data.Slice(0, DAYCARE_BLOCK_SIZE);
psb.com.WriteBytes(data.Slice(0, 0x158), parent_one_addr);
psb.com.WriteBytes(data.Slice(0x158, 0x158), parent_two_addr);
Expand Down
7 changes: 7 additions & 0 deletions PKHeX.Core.Injection/Util/InjectionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,12 @@ public static ulong GetPointerAddress(this ICommunicatorNX sb, string ptr, bool
}
return address;
}

public static string ExtendPointer(this string pointer, params uint[] jumps)
{
foreach (var jump in jumps)
pointer = $"[{pointer}]+{jump:X}";
return pointer;
}
}
}

0 comments on commit 0379c6d

Please sign in to comment.