Skip to content

Commit

Permalink
Working display and editing of memory, if ugly and with hardcoded fon…
Browse files Browse the repository at this point in the history
…t. Squashed following commits:

First try: cocoahexedit is view, source, and delegate. IDK if it compiles. No updates.

I...think it compiles?

I...think it compiles?

trying to separate tableview out like in table_view.cpp

Attempt to set document view to table view so table shows (still not working)

IT DISPLAYS A BLANK TABLE NOW

IT DISPLAYS THINGS! YAY

changed to objectValueForTableColumn to show text.

Separate columns (with all spacing removed so they fit) in prep for editing

Somehow getting "CLIENT ERROR: TUINSRemoteViewController does not override -viewServiceDidTerminateWithError: and thus cannot react to catastrophic errors beyond logging them" on any kind of editing...

Tried emptying function and removing delegate/target stuff so data source only. No dice.

try simplest thing (always replace with 0) - not working. :(

Corrected the typo (thanks yam). Editing works now.

Reformat code

Add some documentation tehe

Make sure we don't read bytes past the length! oops.
  • Loading branch information
Cathrach committed Dec 15, 2024
1 parent a752b85 commit 64a8a37
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 28 deletions.
4 changes: 2 additions & 2 deletions desktop-ui/tools/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ ToolsWindow::ToolsWindow() {

panelList.append(ListViewItem().setText("Manifest").setIcon(Icon::Emblem::Binary));
panelList.append(ListViewItem().setText("Cheats").setIcon(Icon::Emblem::Text));
#if !defined(PLATFORM_MACOS)

// Cocoa hiro is missing the hex editor widget
panelList.append(ListViewItem().setText("Memory").setIcon(Icon::Device::Storage));
#endif

panelList.append(ListViewItem().setText("Graphics").setIcon(Icon::Emblem::Image));
panelList.append(ListViewItem().setText("Streams").setIcon(Icon::Emblem::Audio));
panelList.append(ListViewItem().setText("Properties").setIcon(Icon::Emblem::Text));
Expand Down
162 changes: 138 additions & 24 deletions hiro/cocoa/widget/hex-edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,157 @@
-(id) initWith:(hiro::mHexEdit&)hexEditReference {
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
hexEdit = &hexEditReference;
if (tableView = [[NSTableView alloc] initWithFrame:[self bounds]]) {
[tableView setDataSource:self];
[tableView setHeaderView:nil];

// remove padding and stuff to fit all 16 columns
[tableView setIntercellSpacing:NSMakeSize(0, 0)];
[tableView setStyle:NSTableViewStylePlain];
NSTableColumn* addressCol = [[NSTableColumn alloc] initWithIdentifier:@"Address"];
addressCol.editable = NO;
[tableView addTableColumn:addressCol];

for (int i = 0; i < hexEdit->columns(); i++) {
NSTableColumn* col = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%d",
i]];
col.width = 20;
col.title = @"";
[tableView addTableColumn:col];
}
/*
NSTableColumn* hexCol = [[NSTableColumn alloc] initWithIdentifier:@"Hex"];
[hexCol setTitle:@"Hex"];
[tableView addTableColumn:hexCol];
*/

NSTableColumn* ansiCol = [[NSTableColumn alloc] initWithIdentifier:@"ANSI"];
ansiCol.editable = NO;
[tableView addTableColumn:ansiCol];

// must programmatically do this so the table shows up.
[self setDocumentView:tableView];
[self setHasVerticalScroller:YES];
}
}
return self;
}

@end

namespace hiro {

auto pHexEdit::construct() -> void {
cocoaView = cocoaHexEdit = [[CocoaHexEdit alloc] initWith:self()];
- (NSTableView *) tableView {
return tableView;
}

auto pHexEdit::destruct() -> void {
[cocoaView removeFromSuperview];
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView {
return hexEdit->rows();
}

auto pHexEdit::setAddress(u32 offset) -> void {
}

auto pHexEdit::setBackgroundColor(Color color) -> void {
}

auto pHexEdit::setColumns(u32 columns) -> void {
}

auto pHexEdit::setForegroundColor(Color color) -> void {
}

auto pHexEdit::setLength(u32 length) -> void {
-(id) tableView:(NSTableView *) tableView
objectValueForTableColumn:(NSTableColumn *) tableColumn
row:(NSInteger) row {
// return a string with the content of (row, column)
u32 address = hexEdit->address() + row * hexEdit->columns();
// address only
if ([[tableColumn identifier] isEqualToString:@"Address"]) {
return [NSString stringWithUTF8String:hex(address, 8L).data()];
} else if ([[tableColumn identifier] isEqualToString:@"ANSI"]){
// create the ansi string by concatenating
NSMutableString *output = [NSMutableString stringWithCapacity:hexEdit->columns()];
for (auto column : range(hexEdit->columns())) {
if (address < hexEdit->length()) {
u8 data = hexEdit->doRead(address++);
[output appendString:[NSString stringWithFormat:@"%c",
(data >= 0x20 && data <= 0x7e ? (char)data : '.')]];
}
}
return output;
} else {
// return only the single byte at the correct position
NSInteger columnNumber = [tableColumn.identifier integerValue];
address += columnNumber;
if (address < hexEdit->length()) {
u8 data = hexEdit->doRead(address);
return [NSString stringWithUTF8String:hex(data, 2L).data()];
} else {
return @" ";
}
}
}

auto pHexEdit::setRows(u32 rows) -> void {
-(void) tableView:(NSTableView *) tableView
setObjectValue:(id) object
forTableColumn:(NSTableColumn *) tableColumn
row:(NSInteger) row {
// when table is edited, modify underlying data source
NSInteger colNumber = [tableColumn.identifier integerValue];
u32 address = hexEdit->address() + row * hexEdit->columns() + colNumber;

// unclear if this is needed, but just to make sure the cast is safe
if ([object isKindOfClass:[NSString class]]) {
if (address < hexEdit->length()) {
// only get the first 2 characters
NSString* newVal = [(NSString *)object substringToIndex:2];
NSScanner* hexScanner = [NSScanner scannerWithString:newVal];
unsigned int data = 0;
if ([hexScanner scanHexInt:&data]) {
// this.......is probably ok....???
hexEdit->doWrite(address, (u8)data);
}
}

}
[tableView reloadData];
}

auto pHexEdit::update() -> void {
}
@end

namespace hiro {

auto pHexEdit::construct() -> void {
cocoaView = cocoaHexEdit = [[CocoaHexEdit alloc] initWith:self()];
pWidget::construct();
for (NSTableColumn *column in cocoaHexEdit.tableView.tableColumns) {
NSTextFieldCell *cell = column.dataCell;

// Set the font for the data cell.
cell.font = [NSFont monospacedSystemFontOfSize:10 weight:NSFontWeightRegular];
}
update();
}

auto pHexEdit::destruct() -> void {
[cocoaView removeFromSuperview];
}

// these helper functions are unneeded, we can just reload the table data and
// it will automatically reflect the contents of hexEdit!
auto pHexEdit::setAddress(u32 offset) -> void {
update();
}

auto pHexEdit::setBackgroundColor(Color color) -> void {
update();
}

auto pHexEdit::setColumns(u32 columns) -> void {
update();
}

auto pHexEdit::setForegroundColor(Color color) -> void {
update();
}

auto pHexEdit::setLength(u32 length) -> void {
update();
}

auto pHexEdit::setRows(u32 rows) -> void {
update();
}

auto pHexEdit::update() -> void {
[[cocoaHexEdit tableView] reloadData];
}

}

#endif
7 changes: 5 additions & 2 deletions hiro/cocoa/widget/hex-edit.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#if defined(Hiro_HexEdit)

@interface CocoaHexEdit : NSScrollView {
@interface CocoaHexEdit : NSScrollView <NSTableViewDataSource> {
// Not an NSTableViewDelegate because the table is cell-based, not view-based
@public
hiro::mHexEdit* hexEdit;
hiro::mHexEdit* hexEdit; // this will be the data source for tableView.
NSTableView* tableView;
}
-(id) initWith:(hiro::mHexEdit&)hexEdit;
-(NSTableView*) tableView; // helper function used in update()
@end

namespace hiro {
Expand Down

0 comments on commit 64a8a37

Please sign in to comment.