This commit is contained in:
Gamaiel Zavala 2025-04-01 18:59:35 +10:30 committed by GitHub
commit 212ec029be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 108 additions and 1 deletions

View file

@ -853,6 +853,12 @@ namespace Mesen.Debugger.Utilities
[IconFile("HdPack")]
CopyToHdPackFormat,
[IconFile("CheatCode")]
CopyTileMemory,
[IconFile("Paste")]
PasteTileMemory,
[IconFile("Find")]
CheatDatabase,

View file

@ -0,0 +1,77 @@
using Avalonia;
using Mesen.Interop;
using Mesen.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tmds.DBus;
namespace Mesen.Debugger.Utilities
{
public static class MemCopyHelper
{
public static string ToString(AddressInfo tileAddr, int len)
{
StringBuilder sb = new StringBuilder();
if(tileAddr.Type == MemoryType.NesPpuMemory) {
tileAddr = DebugApi.GetAbsoluteAddress(tileAddr);
}
for(int i = 0; i < len; i++) {
if(sb.Length > 0) {
sb.Append(" ");
}
sb.Append(DebugApi.GetMemoryValue(tileAddr.Type, (uint)(tileAddr.Address + i)).ToString("X2"));
}
return sb.ToString();
}
public static int GetBytesPerTile(TileFormat format)
{
int bitsPerPixel = format.GetBitsPerPixel();
PixelSize tileSize = format.GetTileSize();
int bytesPerTile = tileSize.Width * tileSize.Height * bitsPerPixel / 8;
return bytesPerTile;
}
public static void CopyTileMem(int address, MemoryType memoryType, TileFormat format)
{
int bytesPerTile = GetBytesPerTile(format);
AddressInfo addr = new AddressInfo() { Address = address, Type = memoryType };
string tileMem = MemCopyHelper.ToString(addr, bytesPerTile);
if(tileMem.Length > 0) {
ApplicationHelper.GetMainWindow()?.Clipboard?.SetTextAsync(tileMem);
}
}
public static void PasteTileMem(int address, MemoryType memoryType, TileFormat format)
{
var clipboard = ApplicationHelper.GetMainWindow()?.Clipboard;
if(clipboard != null) {
string? text = Task.Run(() => clipboard?.GetTextAsync()).GetAwaiter().GetResult();
if(text != null) {
text = text.Replace("\n", "").Replace("\r", "").Replace(" ", "");
int bytesPerTile = GetBytesPerTile(format);
int charsPerTile = bytesPerTile * 2;
string pattern = $"^[A-F0-9]{{{charsPerTile}}}$";
if(Regex.IsMatch(text, pattern, RegexOptions.IgnoreCase)) {
byte[] pastedData = HexUtilities.HexToArray(text);
for(int i = 0; i < bytesPerTile; i++) {
DebugApi.SetMemoryValue(memoryType, (uint)(address + i), (byte)pastedData[i]);
}
}
}
}
}
}
}

View file

@ -169,7 +169,7 @@ namespace Mesen.Debugger.ViewModels
}
}
},
new ContextMenuSeparator() { IsVisible = () => CpuType == CpuType.Nes },
new ContextMenuSeparator(),
new ContextMenuAction() {
ActionType = ActionType.CopyToHdPackFormat,
IsVisible = () => CpuType == CpuType.Nes,
@ -180,6 +180,28 @@ namespace Mesen.Debugger.ViewModels
HdPackCopyHelper.CopyToHdPackFormat(address, Config.Source, RawPalette, SelectedPalette, false);
}
}
},
new ContextMenuAction() {
ActionType = ActionType.CopyTileMemory,
Shortcut = () => ConfigManager.Config.Debug.Shortcuts.Get(DebuggerShortcut.Copy),
IsEnabled = () => GetSelectedTileAddress() >= 0,
OnClick = () => {
int address = GetSelectedTileAddress();
if(address >= 0) {
MemCopyHelper.CopyTileMem(address, Config.Source, Config.Format);
}
}
},
new ContextMenuAction() {
ActionType = ActionType.PasteTileMemory,
Shortcut = () => ConfigManager.Config.Debug.Shortcuts.Get(DebuggerShortcut.Paste),
IsEnabled = () => GetSelectedTileAddress() >= 0,
OnClick = () => {
int address = GetSelectedTileAddress();
if(address >= 0) {
MemCopyHelper.PasteTileMem(address, Config.Source, Config.Format);
}
}
}
}));

View file

@ -3651,6 +3651,8 @@ E
<Value ID="ResetAccessCounters">Reset access stats</Value>
<Value ID="CopyToHdPackFormat">Copy tile (HD pack format)</Value>
<Value ID="CopyTileMemory">Copy tile (memory)</Value>
<Value ID="PasteTileMemory">Paste tile (memory)</Value>
<Value ID="CheatDatabase">Cheat database</Value>
<Value ID="ToggleBilinearInterpolation">Use bilinear interpolation</Value>