File: memory.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (92 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
auto MemoryEditor::construct() -> void {
  setCollapsible();
  setVisible(false);

  memoryLabel.setText("Memory Editor").setFont(Font().setBold());
  memoryList.onChange([&] { eventChange(); });
  memoryEditor.setFont(Font().setFamily(Font::Mono));
  memoryEditor.setRows(24);

  exportButton.setText("Export").onActivate([&] {
    eventExport();
  });

  gotoLabel.setText("Goto:");
  gotoAddress.setFont(Font().setFamily(Font::Mono)).onActivate([&] {
    auto address = gotoAddress.text().hex();
    memoryEditor.setAddress(address);
    gotoAddress.setText();
  });

  liveOption.setText("Live");

  refreshButton.setText("Refresh").onActivate([&] {
    memoryEditor.update();
  });
}

auto MemoryEditor::reload() -> void {
  memoryList.reset();
  for(auto memory : ares::Node::enumerate<ares::Node::Debugger::Memory>(emulator->root)) {
    ComboButtonItem item{&memoryList};
    item.setAttribute<ares::Node::Debugger::Memory>("node", memory);
    item.setText(memory->name());
  }
  gotoAddress.setText();
  eventChange();
}

auto MemoryEditor::unload() -> void {
  memoryList.reset();
  gotoAddress.setText();
  eventChange();
}

auto MemoryEditor::refresh() -> void {
  memoryEditor.update();
}

auto MemoryEditor::liveRefresh() -> void {
  if(visible() && liveOption.checked()) refresh();
}

auto MemoryEditor::eventChange() -> void {
  if(auto item = memoryList.selected()) {
    if(auto memory = item.attribute<ares::Node::Debugger::Memory>("node")) {
      memoryEditor.setLength(memory->size());
      memoryEditor.onRead([=](u32 address) -> u8 {
        return memory->read(address);
      });
      memoryEditor.onWrite([=](u32 address, u8 data) -> void {
        return memory->write(address, data);
      });
    }
  } else {
    memoryEditor.setLength(0);
    memoryEditor.onRead();
    memoryEditor.onWrite();
  }
  memoryEditor.setAddress(0);
  if(visible()) memoryEditor.update();
}

auto MemoryEditor::eventExport() -> void {
  if(auto item = memoryList.selected()) {
    if(auto memory = item.attribute<ares::Node::Debugger::Memory>("node")) {
      auto identifier = memory->name().downcase().replace(" ", "-");
      auto datetime = chrono::local::datetime().replace("-", "").replace(":", "").replace(" ", "-");
      auto location = emulator->locate({Location::notsuffix(emulator->game->location), "-", identifier, "-", datetime, ".bin"}, ".bin", settings.paths.debugging);
      if(auto fp = file::open(location, file::mode::write)) {
        for(u32 address : range(memory->size())) {
          fp.write(memory->read(address));
        }
      }
    }
  }
}

auto MemoryEditor::setVisible(bool visible) -> MemoryEditor& {
  if(visible) refresh();
  VerticalLayout::setVisible(visible);
  return *this;
}