Skip to content

Commit 7cb022e

Browse files
authored
Struct Type & Editor/Window by Jahorta (#181)
* Implement Struct Type and create Editor/Window for Struct editing/creation
1 parent 44341b4 commit 7cb022e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4829
-127
lines changed

Source/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ set(SRCS ${DolphinProcessSrc}
6666
GUI/MemViewer/MemViewerWidget.cpp
6767
GUI/MainWindow.cpp
6868
GUI/Widgets/AddressInputWidget.cpp
69+
GUI/StructEditor/StructEditorWidget.cpp
70+
GUI/StructEditor/StructDetailModel.cpp
71+
GUI/StructEditor/StructSelectModel.cpp
72+
Structs/StructTreeNode.cpp
73+
Structs/StructDef.cpp
74+
Structs/FieldDef.cpp
6975
Resources/resource.qrc
7076
${ExeIconSrc}
7177
main.cpp)

Source/Common/MemoryCommon.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ size_t getSizeForType(const MemType type, const size_t length)
7373
return length;
7474
case MemType::type_byteArray:
7575
return length;
76+
case MemType::type_struct:
77+
return length;
7678
default:
7779
return 0;
7880
}
@@ -96,6 +98,8 @@ bool shouldBeBSwappedForType(const MemType type)
9698
return false;
9799
case MemType::type_byteArray:
98100
return false;
101+
case MemType::type_struct:
102+
return false;
99103
default:
100104
return false;
101105
}
@@ -119,6 +123,8 @@ int getNbrBytesAlignmentForType(const MemType type)
119123
return 1;
120124
case MemType::type_byteArray:
121125
return 1;
126+
case MemType::type_struct:
127+
return 1;
122128
default:
123129
return 1;
124130
}
@@ -455,6 +461,12 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen
455461
index++;
456462
}
457463
actualLength = bytes.size();
464+
break;
465+
}
466+
467+
default:
468+
{
469+
break;
458470
}
459471
}
460472
return buffer;

Source/Common/MemoryCommon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ enum class MemType
3434
type_double,
3535
type_string,
3636
type_byteArray,
37+
type_struct,
38+
type_none // Placeholder for the entry of a child node of a collapsed container
3739
};
3840

3941
enum class MemBase

Source/GUI/GUICommon.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ QStringList g_memTypeNames =
1515
QCoreApplication::translate("Common", "Float"),
1616
QCoreApplication::translate("Common", "Double"),
1717
QCoreApplication::translate("Common", "String"),
18-
QCoreApplication::translate("Common", "Array of bytes")});
18+
QCoreApplication::translate("Common", "Array of bytes"),
19+
QCoreApplication::translate("Common", "Struct")});
1920

2021
QStringList g_memBaseNames = QStringList({QCoreApplication::translate("Common", "Decimal"),
2122
QCoreApplication::translate("Common", "Hexadecimal"),
@@ -46,6 +47,7 @@ QString getStringFromType(const Common::MemType type, const size_t length)
4647
case Common::MemType::type_word:
4748
case Common::MemType::type_float:
4849
case Common::MemType::type_double:
50+
case Common::MemType::type_struct:
4951
return GUICommon::g_memTypeNames.at(static_cast<int>(type));
5052
case Common::MemType::type_string:
5153
return QString::fromStdString("string[" + std::to_string(length) + "]");
@@ -73,6 +75,17 @@ QString getNameFromBase(const Common::MemBase base)
7375
}
7476
}
7577

78+
bool isContainerType(const Common::MemType type)
79+
{
80+
switch (type)
81+
{
82+
case Common::MemType::type_struct:
83+
return true;
84+
default:
85+
return false;
86+
}
87+
}
88+
7689
void changeApplicationStyle(const ApplicationStyle style)
7790
{
7891
QApplication::setStyle(QStringLiteral("fusion"));

Source/GUI/GUICommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern QStringList g_memBaseNames;
1313

1414
QString getStringFromType(Common::MemType type, size_t length = 0);
1515
QString getNameFromBase(Common::MemBase base);
16+
bool isContainerType(Common::MemType type);
1617

1718
enum class ApplicationStyle
1819
{

Source/GUI/MainWindow.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ MainWindow::MainWindow()
2929
makeMenus();
3030
DolphinComm::DolphinAccessor::init();
3131
makeMemViewer();
32+
makeStructEditor();
3233

3334
m_autoHookTimer.setInterval(1000);
3435
connect(&m_autoHookTimer, &QTimer::timeout, this, &MainWindow::onHookIfNotHooked);
@@ -40,20 +41,36 @@ MainWindow::MainWindow()
4041

4142
GUICommon::changeApplicationStyle(
4243
static_cast<GUICommon::ApplicationStyle>(SConfig::getInstance().getTheme()));
43-
4444
m_actAutoloadLastFile->setChecked(SConfig::getInstance().getAutoloadLastFile());
4545

46+
// Connect struct updates to mem watch widget
47+
connect(m_structEditor, &StructEditorWidget::updateStructName, m_watcher,
48+
&MemWatchWidget::onUpdateStructName);
49+
connect(m_structEditor, &StructEditorWidget::updateStructDetails, m_watcher,
50+
&MemWatchWidget::onUpdateStructDetails);
51+
connect(m_structEditor, &StructEditorWidget::structAddedRemoved, m_watcher,
52+
&MemWatchWidget::onStructDefAddRemove);
53+
54+
// Connect load/save structs on load/save watch file
55+
connect(m_watcher, &MemWatchWidget::loadStructDefsFromJson, m_structEditor,
56+
&StructEditorWidget::readStructTreeFromFile);
57+
connect(m_watcher, &MemWatchWidget::writeStructDefTreeToJson, m_structEditor,
58+
&StructEditorWidget::writeStructDefTreeToJson);
59+
4660
if (m_actAutoloadLastFile->isChecked() && !SConfig::getInstance().getLastLoadedFile().isEmpty())
4761
{
4862
m_watcher->openWatchFile(SConfig::getInstance().getLastLoadedFile());
63+
m_watcher->setStructDefs(m_structEditor->getStructDefs(), m_structEditor->getStructMap());
4964
}
5065
else
5166
{
67+
m_structEditor->restoreStructTreeFromSettings(SConfig::getInstance().getStructDefs());
68+
m_watcher->setStructDefs(m_structEditor->getStructDefs(), m_structEditor->getStructMap());
5269
m_watcher->restoreWatchModel(SConfig::getInstance().getWatchModel());
5370
}
5471

5572
m_actCollapseGroupsOnSave->setChecked(SConfig::getInstance().getCollapseGroupsOnSave());
56-
73+
m_viewer->setStructDefs(m_structEditor->getStructDefs());
5774
m_actAutoHook->setChecked(SConfig::getInstance().getAutoHook());
5875

5976
if (m_actAutoHook->isChecked())
@@ -67,6 +84,7 @@ MainWindow::~MainWindow()
6784
delete m_copier;
6885
delete m_viewer;
6986
delete m_watcher;
87+
delete m_structEditor;
7088
DolphinComm::DolphinAccessor::free();
7189
}
7290

@@ -105,6 +123,7 @@ void MainWindow::makeMenus()
105123
QSignalBlocker signalBlocker(m_actScanner);
106124
m_actScanner->setChecked(m_splitter->sizes()[0] > 0);
107125
});
126+
m_actStructEditor = new QAction(tr("Struct &Editor"), this);
108127

109128
m_actQuit = new QAction(tr("&Quit"), this);
110129
m_actAbout = new QAction(tr("&About"), this);
@@ -136,6 +155,7 @@ void MainWindow::makeMenus()
136155
connect(m_actMemoryViewer, &QAction::triggered, this, &MainWindow::onOpenMenViewer);
137156
connect(m_actCopyMemory, &QAction::triggered, this, &MainWindow::onCopyMemory);
138157
connect(m_actScanner, &QAction::toggled, this, &MainWindow::onScannerActionToggled);
158+
connect(m_actStructEditor, &QAction::triggered, this, &MainWindow::onOpenStructEditor);
139159

140160
connect(m_actQuit, &QAction::triggered, this, &MainWindow::onQuit);
141161
connect(m_actAbout, &QAction::triggered, this, &MainWindow::onAbout);
@@ -169,6 +189,7 @@ void MainWindow::makeMenus()
169189
m_menuView->addAction(m_actMemoryViewer);
170190
m_menuView->addAction(m_actCopyMemory);
171191
m_menuView->addAction(m_actScanner);
192+
m_menuView->addAction(m_actStructEditor);
172193

173194
m_menuHelp = menuBar()->addMenu(tr("&Help"));
174195
m_menuHelp->addAction(m_actAbout);
@@ -587,6 +608,12 @@ void MainWindow::onQuit()
587608
close();
588609
}
589610

611+
void MainWindow::onOpenStructEditor()
612+
{
613+
m_structEditor->show();
614+
m_structEditor->raise();
615+
}
616+
590617
void MainWindow::closeEvent(QCloseEvent* event)
591618
{
592619
SConfig::getInstance().setAutoHook(m_actAutoHook->isChecked());
@@ -596,6 +623,7 @@ void MainWindow::closeEvent(QCloseEvent* event)
596623
if (!m_actAutoloadLastFile->isChecked() || m_watcher->m_watchListFile.isEmpty())
597624
{
598625
SConfig::getInstance().setWatchModel(m_watcher->saveWatchModel());
626+
SConfig::getInstance().setStructDefs(m_structEditor->saveStructTreeToSettings());
599627
}
600628
SConfig::getInstance().setMainWindowGeometry(saveGeometry());
601629
SConfig::getInstance().setMainWindowState(saveState());
@@ -612,6 +640,7 @@ void MainWindow::closeEvent(QCloseEvent* event)
612640
}
613641

614642
m_viewer->close();
643+
m_structEditor->close();
615644
event->accept();
616645
}
617646

@@ -719,3 +748,9 @@ void MainWindow::updateStatusBar()
719748
const QString toolTip{toolTipLines.join("\n\n")};
720749
m_statusLabel->parentWidget()->setToolTip(toolTip);
721750
}
751+
752+
void MainWindow::makeStructEditor()
753+
{
754+
m_structEditor = new StructEditorWidget(nullptr);
755+
m_structEditor->setWindowIcon(windowIcon());
756+
}

Source/GUI/MainWindow.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "MemScanner/MemScanWidget.h"
1414
#include "MemViewer/MemViewerWidget.h"
1515
#include "MemWatcher/MemWatchWidget.h"
16+
#include "StructEditor/StructEditorWidget.h"
1617

1718
class MainWindow : public QMainWindow
1819
{
@@ -58,20 +59,23 @@ class MainWindow : public QMainWindow
5859
void onQuit();
5960

6061
void onAutoLoadLastFileTriggered(bool checked);
62+
void onOpenStructEditor();
6163

6264
private:
6365
void makeMenus();
6466
void initialiseWidgets();
6567
void makeLayouts();
6668
void makeMemViewer();
6769
void updateStatusBar();
70+
void makeStructEditor();
6871

6972
QSplitter* m_splitter{};
7073

7174
MemWatchWidget* m_watcher{};
7275
MemScanWidget* m_scanner{};
7376
MemViewerWidget* m_viewer{};
7477
DlgCopy* m_copier{};
78+
StructEditorWidget* m_structEditor{};
7579

7680
QTimer m_autoHookTimer;
7781

@@ -95,6 +99,7 @@ class MainWindow : public QMainWindow
9599
QAction* m_actMemoryViewer{};
96100
QAction* m_actCopyMemory{};
97101
QAction* m_actScanner{};
102+
QAction* m_actStructEditor{};
98103
QAction* m_actQuit{};
99104
QAction* m_actAbout{};
100105
QLabel* m_statusIcon{};

Source/GUI/MemViewer/MemViewer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ void MemViewer::memoryValidityChanged(const bool valid)
9595
viewport()->update();
9696
}
9797

98+
void MemViewer::setStructDefs(StructTreeNode* baseNode)
99+
{
100+
m_structDefs = baseNode;
101+
}
102+
98103
void MemViewer::updateMemoryData()
99104
{
100105
std::swap(m_updatedRawMemoryData, m_lastRawMemoryData);
@@ -499,7 +504,8 @@ void MemViewer::addByteIndexAsWatch(int index)
499504
{
500505
MemWatchEntry* entry = new MemWatchEntry();
501506
entry->setConsoleAddress(m_currentFirstAddress + index);
502-
DlgAddWatchEntry dlg(true, entry, this);
507+
DlgAddWatchEntry dlg(true, entry, m_structDefs->getStructNames(), this);
508+
503509
if (dlg.exec() == QDialog::Accepted)
504510
{
505511
emit addWatch(dlg.stealEntry());

Source/GUI/MemViewer/MemViewer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../../Common/CommonTypes.h"
1313
#include "../../Common/MemoryCommon.h"
1414
#include "../../MemoryWatch/MemWatchEntry.h"
15+
#include "../../Structs/StructTreeNode.h"
1516

1617
class MemViewer : public QAbstractScrollArea
1718
{
@@ -38,6 +39,7 @@ class MemViewer : public QAbstractScrollArea
3839
void jumpToAddress(u32 address);
3940
void updateViewer();
4041
void memoryValidityChanged(bool valid);
42+
void setStructDefs(StructTreeNode* baseNode);
4143

4244
signals:
4345
void memErrorOccured();
@@ -119,4 +121,6 @@ class MemViewer : public QAbstractScrollArea
119121
QRect* m_curosrRect{};
120122
QShortcut* m_copyShortcut{};
121123
QElapsedTimer m_elapsedTimer;
124+
125+
StructTreeNode* m_structDefs;
122126
};

Source/GUI/MemViewer/MemViewerWidget.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,8 @@ void MemViewerWidget::goToAddress(u32 address)
105105
{
106106
m_memViewer->jumpToAddress(address);
107107
}
108+
109+
void MemViewerWidget::setStructDefs(StructTreeNode* baseNode)
110+
{
111+
m_memViewer->setStructDefs(baseNode);
112+
}

0 commit comments

Comments
 (0)