Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugger: Tile Viewer - Add copy/paste tile memory #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions UI/Debugger/Utilities/ContextMenuAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,12 @@ public enum ActionType

[IconFile("HdPack")]
CopyToHdPackFormat,

[IconFile("CheatCode")]
CopyTileMemory,

[IconFile("Paste")]
PasteTileMemory,

[IconFile("Find")]
CheatDatabase,
Expand Down
77 changes: 77 additions & 0 deletions UI/Debugger/Utilities/MemCopyHelper.cs
Original file line number Diff line number Diff line change
@@ -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]);
}
}
}
}
}
}
}
24 changes: 23 additions & 1 deletion UI/Debugger/ViewModels/TileViewerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public TileViewerViewModel(CpuType cpuType, PictureViewer picViewer, Window? wnd
}
}
},
new ContextMenuSeparator() { IsVisible = () => CpuType == CpuType.Nes },
new ContextMenuSeparator(),
new ContextMenuAction() {
ActionType = ActionType.CopyToHdPackFormat,
IsVisible = () => CpuType == CpuType.Nes,
Expand All @@ -197,6 +197,28 @@ public TileViewerViewModel(CpuType cpuType, PictureViewer picViewer, Window? wnd
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);
}
}
}
});

Expand Down
2 changes: 2 additions & 0 deletions UI/Localization/resources.en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,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>

Expand Down