mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-06-03 04:17:18 +12:00
We can now load lua scripts properly
This commit is contained in:
parent
c57f2db6c0
commit
6386605b97
6 changed files with 73 additions and 2 deletions
|
@ -119,6 +119,8 @@ class Emulator {
|
||||||
EmulatorConfig& getConfig() { return config; }
|
EmulatorConfig& getConfig() { return config; }
|
||||||
Cheats& getCheats() { return cheats; }
|
Cheats& getCheats() { return cheats; }
|
||||||
ServiceManager& getServiceManager() { return kernel.getServiceManager(); }
|
ServiceManager& getServiceManager() { return kernel.getServiceManager(); }
|
||||||
|
LuaManager& getLua() { return lua; }
|
||||||
|
|
||||||
RendererType getRendererType() const { return config.rendererType; }
|
RendererType getRendererType() const { return config.rendererType; }
|
||||||
Renderer* getRenderer() { return gpu.getRenderer(); }
|
Renderer* getRenderer() { return gpu.getRenderer(); }
|
||||||
u64 getTicks() { return cpu.getTicks(); }
|
u64 getTicks() { return cpu.getTicks(); }
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "helpers.hpp"
|
#include "helpers.hpp"
|
||||||
#include "memory.hpp"
|
#include "memory.hpp"
|
||||||
|
|
||||||
|
@ -36,6 +38,8 @@ class LuaManager {
|
||||||
void initialize();
|
void initialize();
|
||||||
void initializeThunks();
|
void initializeThunks();
|
||||||
void loadFile(const char* path);
|
void loadFile(const char* path);
|
||||||
|
void loadString(const std::string& code);
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
void signalEvent(LuaEvent e) {
|
void signalEvent(LuaEvent e) {
|
||||||
if (haveScript) [[unlikely]] {
|
if (haveScript) [[unlikely]] {
|
||||||
|
@ -52,6 +56,7 @@ class LuaManager {
|
||||||
void close() {}
|
void close() {}
|
||||||
void initialize() {}
|
void initialize() {}
|
||||||
void loadFile(const char* path) {}
|
void loadFile(const char* path) {}
|
||||||
|
void loadString(const std::string& code) {}
|
||||||
void reset() {}
|
void reset() {}
|
||||||
void signalEvent(LuaEvent e) {}
|
void signalEvent(LuaEvent e) {}
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,7 +21,7 @@ class MainWindow : public QMainWindow {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Types of messages we might send from the GUI thread to the emulator thread
|
// Types of messages we might send from the GUI thread to the emulator thread
|
||||||
enum class MessageType { LoadROM, Reset, Pause, Resume, TogglePause, DumpRomFS, PressKey, ReleaseKey };
|
enum class MessageType { LoadROM, Reset, Pause, Resume, TogglePause, DumpRomFS, PressKey, ReleaseKey, LoadLuaScript };
|
||||||
|
|
||||||
// Tagged union representing our message queue messages
|
// Tagged union representing our message queue messages
|
||||||
struct EmulatorMessage {
|
struct EmulatorMessage {
|
||||||
|
@ -35,6 +35,10 @@ class MainWindow : public QMainWindow {
|
||||||
struct {
|
struct {
|
||||||
u32 key;
|
u32 key;
|
||||||
} key;
|
} key;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
std::string* str;
|
||||||
|
} string;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -72,4 +76,5 @@ class MainWindow : public QMainWindow {
|
||||||
|
|
||||||
void keyPressEvent(QKeyEvent* event) override;
|
void keyPressEvent(QKeyEvent* event) override;
|
||||||
void keyReleaseEvent(QKeyEvent* event) override;
|
void keyReleaseEvent(QKeyEvent* event) override;
|
||||||
|
void loadLuaScript(const std::string& code);
|
||||||
};
|
};
|
25
src/lua.cpp
25
src/lua.cpp
|
@ -51,6 +51,31 @@ void LuaManager::loadFile(const char* path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LuaManager::loadString(const std::string& code) {
|
||||||
|
// Initialize Lua if it has not been initialized
|
||||||
|
if (!initialized) {
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
// If init failed, don't execute
|
||||||
|
if (!initialized) {
|
||||||
|
printf("Lua initialization failed, file won't run\n");
|
||||||
|
haveScript = false;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = luaL_loadstring(L, code.c_str()); // load Lua script
|
||||||
|
int ret = lua_pcall(L, 0, 0, 0); // tell Lua to run the script
|
||||||
|
|
||||||
|
if (ret != 0) {
|
||||||
|
haveScript = false;
|
||||||
|
fprintf(stderr, "%s\n", lua_tostring(L, -1)); // tell us what mistake we made
|
||||||
|
} else {
|
||||||
|
haveScript = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void LuaManager::signalEventInternal(LuaEvent e) {
|
void LuaManager::signalEventInternal(LuaEvent e) {
|
||||||
lua_getglobal(L, "eventHandler"); // We want to call the event handler
|
lua_getglobal(L, "eventHandler"); // We want to call the event handler
|
||||||
lua_pushnumber(L, static_cast<int>(e)); // Push event type
|
lua_pushnumber(L, static_cast<int>(e)); // Push event type
|
||||||
|
|
|
@ -195,6 +195,11 @@ void MainWindow::dispatchMessage(const EmulatorMessage& message) {
|
||||||
delete message.path.p;
|
delete message.path.p;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case MessageType::LoadLuaScript:
|
||||||
|
emu->getLua().loadString(*message.string.str);
|
||||||
|
delete message.string.str;
|
||||||
|
break;
|
||||||
|
|
||||||
case MessageType::Pause: emu->pause(); break;
|
case MessageType::Pause: emu->pause(); break;
|
||||||
case MessageType::Resume: emu->resume(); break;
|
case MessageType::Resume: emu->resume(); break;
|
||||||
case MessageType::TogglePause: emu->togglePause(); break;
|
case MessageType::TogglePause: emu->togglePause(); break;
|
||||||
|
@ -259,3 +264,11 @@ void MainWindow::keyReleaseEvent(QKeyEvent* event) {
|
||||||
case Qt::Key_Backspace: releaseKey(HID::Keys::Select); break;
|
case Qt::Key_Backspace: releaseKey(HID::Keys::Select); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::loadLuaScript(const std::string& code) {
|
||||||
|
EmulatorMessage message{.type = MessageType::LoadLuaScript};
|
||||||
|
|
||||||
|
// Make a copy of the code on the heap to send via the message queue
|
||||||
|
message.string.str = new std::string(code);
|
||||||
|
sendMessage(message);
|
||||||
|
}
|
|
@ -1,6 +1,10 @@
|
||||||
#include "panda_qt/text_editor.hpp"
|
#include "panda_qt/text_editor.hpp"
|
||||||
|
|
||||||
|
#include <QPushButton>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
|
|
||||||
|
#include "panda_qt/main_window.hpp"
|
||||||
|
|
||||||
using namespace Zep;
|
using namespace Zep;
|
||||||
|
|
||||||
TextEditorWindow::TextEditorWindow(QWidget* parent, const std::string& filename, const std::string& initialText)
|
TextEditorWindow::TextEditorWindow(QWidget* parent, const std::string& filename, const std::string& initialText)
|
||||||
|
@ -13,8 +17,25 @@ TextEditorWindow::TextEditorWindow(QWidget* parent, const std::string& filename,
|
||||||
|
|
||||||
zepWidget.GetEditor().SetGlobalMode(Zep::ZepMode_Standard::StaticName());
|
zepWidget.GetEditor().SetGlobalMode(Zep::ZepMode_Standard::StaticName());
|
||||||
zepWidget.GetEditor().InitWithText(filename, initialText);
|
zepWidget.GetEditor().InitWithText(filename, initialText);
|
||||||
|
|
||||||
QVBoxLayout* mainLayout = new QVBoxLayout();
|
QVBoxLayout* mainLayout = new QVBoxLayout();
|
||||||
setLayout(mainLayout);
|
setLayout(mainLayout);
|
||||||
|
|
||||||
|
QPushButton* button = new QPushButton(tr("Load script"), this);
|
||||||
|
button->setFixedSize(100, 20);
|
||||||
|
|
||||||
|
connect(button, &QPushButton::pressed, this, [this]() {
|
||||||
|
if (parentWidget()) {
|
||||||
|
auto buffer = zepWidget.GetEditor().GetMRUBuffer();
|
||||||
|
const std::string text = buffer->GetBufferText(buffer->Begin(), buffer->End());
|
||||||
|
|
||||||
|
static_cast<MainWindow*>(parentWidget())->loadLuaScript(text);
|
||||||
|
} else {
|
||||||
|
// This should be unreachable, only here for safety purposes
|
||||||
|
printf("Text editor does not have any parent widget, click doesn't work :(\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mainLayout->addWidget(button);
|
||||||
mainLayout->addWidget(&zepWidget);
|
mainLayout->addWidget(&zepWidget);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue