From a5071f10f0505c1268132930d8fb941dee913a17 Mon Sep 17 00:00:00 2001 From: harry Date: Mon, 5 Feb 2024 06:43:27 -0500 Subject: [PATCH] Implemented ROM JS interface object. --- src/drivers/Qt/QtScriptManager.cpp | 114 +++++++++++++++++++++++++++++ src/drivers/Qt/QtScriptManager.h | 26 +++++++ 2 files changed, 140 insertions(+) diff --git a/src/drivers/Qt/QtScriptManager.cpp b/src/drivers/Qt/QtScriptManager.cpp index 30d1a144..ee364b4c 100644 --- a/src/drivers/Qt/QtScriptManager.cpp +++ b/src/drivers/Qt/QtScriptManager.cpp @@ -46,6 +46,7 @@ #include "../../debug.h" #include "common/os_utils.h" +#include "utils/xstring.h" #include "Qt/QtScriptManager.h" #include "Qt/main.h" @@ -66,6 +67,9 @@ #define JS_PIXEL_G(PIX) (((PIX) >> 8) & 0xff) #define JS_PIXEL_B(PIX) ((PIX) & 0xff) +// File Base Name from Core +extern char FileBase[]; + namespace JS { //---------------------------------------------------- @@ -367,6 +371,103 @@ QJSValue EmuScriptObject::getScreenPixel(int x, int y, bool useBackup) return jsVal; } //---------------------------------------------------- +//---- ROM Script Object +//---------------------------------------------------- +//---------------------------------------------------- +RomScriptObject::RomScriptObject(QObject* parent) + : QObject(parent) +{ + script = qobject_cast(parent); +} +//---------------------------------------------------- +RomScriptObject::~RomScriptObject() +{ +} +//---------------------------------------------------- +bool RomScriptObject::isLoaded() +{ + return (GameInfo != nullptr); +} +//---------------------------------------------------- +QString RomScriptObject::getFileName() +{ + QString baseName; + + if (GameInfo != nullptr) + { + baseName = FileBase; + } + return baseName; +} +//---------------------------------------------------- +QString RomScriptObject::getHash(const QString& type) +{ + QString hash; + + if (GameInfo != nullptr) + { + MD5DATA md5hash = GameInfo->MD5; + + if (type.compare("md5", Qt::CaseInsensitive) == 0) + { + hash = md5_asciistr(md5hash); + } + else if (type.compare("base64", Qt::CaseInsensitive) == 0) + { + hash = BytesToString(md5hash.data, MD5DATA::size).c_str(); + } + } + return hash; +} +//---------------------------------------------------- +uint8_t RomScriptObject::readByte(int address) +{ + return FCEU_ReadRomByte(address); +} +//---------------------------------------------------- +uint8_t RomScriptObject::readByteUnsigned(int address) +{ + return FCEU_ReadRomByte(address); +} +//---------------------------------------------------- +int8_t RomScriptObject::readByteSigned(int address) +{ + return static_cast(FCEU_ReadRomByte(address)); +} +//---------------------------------------------------- +QJSValue RomScriptObject::readByteRange(int start, int end) +{ + QJSValue array; + int size = end - start + 1; + + if (size > 0) + { + array = engine->newArray(size); + + for (int i=0; iprint("rom.writebyte() can't edit the ROM header."); + } + else + { + FCEU_WriteRomByte(address, value); + } +} +//---------------------------------------------------- //---- Memory Script Object //---------------------------------------------------- //---------------------------------------------------- @@ -822,6 +923,11 @@ void QtScriptInstance::shutdownEngine() delete emu; emu = nullptr; } + if (rom != nullptr) + { + delete rom; + rom = nullptr; + } if (mem != nullptr) { mem->reset(); @@ -848,20 +954,27 @@ int QtScriptInstance::initEngine() engine = new QJSEngine(this); emu = new JS::EmuScriptObject(this); + rom = new JS::RomScriptObject(this); mem = new JS::MemoryScriptObject(this); emu->setDialog(dialog); + rom->setDialog(dialog); mem->setDialog(dialog); engine->installExtensions(QJSEngine::ConsoleExtension); emu->setEngine(engine); + rom->setEngine(engine); mem->setEngine(engine); QJSValue emuObject = engine->newQObject(emu); engine->globalObject().setProperty("emu", emuObject); + QJSValue romObject = engine->newQObject(rom); + + engine->globalObject().setProperty("rom", romObject); + QJSValue memObject = engine->newQObject(mem); engine->globalObject().setProperty("memory", memObject); @@ -1878,6 +1991,7 @@ void QScriptDialog_t::openScriptFile(void) void QScriptDialog_t::startScript(void) { FCEU_WRAPPER_LOCK(); + jsOutput->clear(); clearPropertyTree(); scriptInstance->resetEngine(); if (scriptInstance->loadScriptFile(scriptPath->text())) diff --git a/src/drivers/Qt/QtScriptManager.h b/src/drivers/Qt/QtScriptManager.h index c5e5f10f..0e6123d6 100644 --- a/src/drivers/Qt/QtScriptManager.h +++ b/src/drivers/Qt/QtScriptManager.h @@ -111,6 +111,31 @@ public slots: }; +class RomScriptObject: public QObject +{ + Q_OBJECT +public: + RomScriptObject(QObject* parent = nullptr); + ~RomScriptObject(); + + void setEngine(QJSEngine* _engine){ engine = _engine; } + void setDialog(QScriptDialog_t* _dialog){ dialog = _dialog; } +private: + QJSEngine* engine = nullptr; + QScriptDialog_t* dialog = nullptr; + QtScriptInstance* script = nullptr; + +public slots: + Q_INVOKABLE bool isLoaded(); + Q_INVOKABLE QString getFileName(); + Q_INVOKABLE QString getHash(const QString& type); + Q_INVOKABLE uint8_t readByte(int address); + Q_INVOKABLE int8_t readByteSigned(int address); + Q_INVOKABLE uint8_t readByteUnsigned(int address); + Q_INVOKABLE QJSValue readByteRange(int start, int end); + Q_INVOKABLE void writeByte(int address, int value); +}; + class MemoryScriptObject: public QObject { Q_OBJECT @@ -237,6 +262,7 @@ private: QJSEngine* engine = nullptr; QScriptDialog_t* dialog = nullptr; JS::EmuScriptObject* emu = nullptr; + JS::RomScriptObject* rom = nullptr; JS::MemoryScriptObject* mem = nullptr; QWidget* ui_rootWidget = nullptr; QJSValue *onFrameBeginCallback = nullptr;