Implemented ROM JS interface object.

This commit is contained in:
harry 2024-02-05 06:43:27 -05:00
parent 61da515f11
commit a5071f10f0
2 changed files with 140 additions and 0 deletions

View File

@ -46,6 +46,7 @@
#include "../../debug.h" #include "../../debug.h"
#include "common/os_utils.h" #include "common/os_utils.h"
#include "utils/xstring.h"
#include "Qt/QtScriptManager.h" #include "Qt/QtScriptManager.h"
#include "Qt/main.h" #include "Qt/main.h"
@ -66,6 +67,9 @@
#define JS_PIXEL_G(PIX) (((PIX) >> 8) & 0xff) #define JS_PIXEL_G(PIX) (((PIX) >> 8) & 0xff)
#define JS_PIXEL_B(PIX) ((PIX) & 0xff) #define JS_PIXEL_B(PIX) ((PIX) & 0xff)
// File Base Name from Core
extern char FileBase[];
namespace JS namespace JS
{ {
//---------------------------------------------------- //----------------------------------------------------
@ -367,6 +371,103 @@ QJSValue EmuScriptObject::getScreenPixel(int x, int y, bool useBackup)
return jsVal; return jsVal;
} }
//---------------------------------------------------- //----------------------------------------------------
//---- ROM Script Object
//----------------------------------------------------
//----------------------------------------------------
RomScriptObject::RomScriptObject(QObject* parent)
: QObject(parent)
{
script = qobject_cast<QtScriptInstance*>(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<int8_t>(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; i<size; i++)
{
int byte = FCEU_ReadRomByte(start + i);
QJSValue element = byte;
array.setProperty(i, element);
}
}
return array;
}
//----------------------------------------------------
void RomScriptObject::writeByte(int address, int value)
{
if (address < 16)
{
script->print("rom.writebyte() can't edit the ROM header.");
}
else
{
FCEU_WriteRomByte(address, value);
}
}
//----------------------------------------------------
//---- Memory Script Object //---- Memory Script Object
//---------------------------------------------------- //----------------------------------------------------
//---------------------------------------------------- //----------------------------------------------------
@ -822,6 +923,11 @@ void QtScriptInstance::shutdownEngine()
delete emu; delete emu;
emu = nullptr; emu = nullptr;
} }
if (rom != nullptr)
{
delete rom;
rom = nullptr;
}
if (mem != nullptr) if (mem != nullptr)
{ {
mem->reset(); mem->reset();
@ -848,20 +954,27 @@ int QtScriptInstance::initEngine()
engine = new QJSEngine(this); engine = new QJSEngine(this);
emu = new JS::EmuScriptObject(this); emu = new JS::EmuScriptObject(this);
rom = new JS::RomScriptObject(this);
mem = new JS::MemoryScriptObject(this); mem = new JS::MemoryScriptObject(this);
emu->setDialog(dialog); emu->setDialog(dialog);
rom->setDialog(dialog);
mem->setDialog(dialog); mem->setDialog(dialog);
engine->installExtensions(QJSEngine::ConsoleExtension); engine->installExtensions(QJSEngine::ConsoleExtension);
emu->setEngine(engine); emu->setEngine(engine);
rom->setEngine(engine);
mem->setEngine(engine); mem->setEngine(engine);
QJSValue emuObject = engine->newQObject(emu); QJSValue emuObject = engine->newQObject(emu);
engine->globalObject().setProperty("emu", emuObject); engine->globalObject().setProperty("emu", emuObject);
QJSValue romObject = engine->newQObject(rom);
engine->globalObject().setProperty("rom", romObject);
QJSValue memObject = engine->newQObject(mem); QJSValue memObject = engine->newQObject(mem);
engine->globalObject().setProperty("memory", memObject); engine->globalObject().setProperty("memory", memObject);
@ -1878,6 +1991,7 @@ void QScriptDialog_t::openScriptFile(void)
void QScriptDialog_t::startScript(void) void QScriptDialog_t::startScript(void)
{ {
FCEU_WRAPPER_LOCK(); FCEU_WRAPPER_LOCK();
jsOutput->clear();
clearPropertyTree(); clearPropertyTree();
scriptInstance->resetEngine(); scriptInstance->resetEngine();
if (scriptInstance->loadScriptFile(scriptPath->text())) if (scriptInstance->loadScriptFile(scriptPath->text()))

View File

@ -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 class MemoryScriptObject: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -237,6 +262,7 @@ private:
QJSEngine* engine = nullptr; QJSEngine* engine = nullptr;
QScriptDialog_t* dialog = nullptr; QScriptDialog_t* dialog = nullptr;
JS::EmuScriptObject* emu = nullptr; JS::EmuScriptObject* emu = nullptr;
JS::RomScriptObject* rom = nullptr;
JS::MemoryScriptObject* mem = nullptr; JS::MemoryScriptObject* mem = nullptr;
QWidget* ui_rootWidget = nullptr; QWidget* ui_rootWidget = nullptr;
QJSValue *onFrameBeginCallback = nullptr; QJSValue *onFrameBeginCallback = nullptr;