mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-06-09 20:41:38 +12:00
Add load_rom http server command
This commit is contained in:
parent
da3ec42326
commit
63f9cfe0c2
3 changed files with 60 additions and 4 deletions
|
@ -4,6 +4,7 @@
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
#include <filesystem>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
@ -12,7 +13,7 @@
|
||||||
|
|
||||||
#include "helpers.hpp"
|
#include "helpers.hpp"
|
||||||
|
|
||||||
enum class HttpActionType { None, Screenshot, Key };
|
enum class HttpActionType { None, Screenshot, Key, LoadRom };
|
||||||
|
|
||||||
class Emulator;
|
class Emulator;
|
||||||
namespace httplib {
|
namespace httplib {
|
||||||
|
@ -42,6 +43,7 @@ class HttpAction {
|
||||||
|
|
||||||
static std::unique_ptr<HttpAction> createScreenshotAction(DeferredResponseWrapper& response);
|
static std::unique_ptr<HttpAction> createScreenshotAction(DeferredResponseWrapper& response);
|
||||||
static std::unique_ptr<HttpAction> createKeyAction(uint32_t key, bool state);
|
static std::unique_ptr<HttpAction> createKeyAction(uint32_t key, bool state);
|
||||||
|
static std::unique_ptr<HttpAction> createLoadRomAction(std::filesystem::path path, bool paused);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct HttpServer {
|
struct HttpServer {
|
||||||
|
|
|
@ -114,6 +114,9 @@ void Emulator::render() {}
|
||||||
|
|
||||||
void Emulator::run() {
|
void Emulator::run() {
|
||||||
while (running) {
|
while (running) {
|
||||||
|
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
||||||
|
httpServer.processActions();
|
||||||
|
#endif
|
||||||
runFrame();
|
runFrame();
|
||||||
HIDService& hid = kernel.getServiceManager().getHID();
|
HIDService& hid = kernel.getServiceManager().getHID();
|
||||||
|
|
||||||
|
@ -345,9 +348,6 @@ void Emulator::run() {
|
||||||
|
|
||||||
void Emulator::runFrame() {
|
void Emulator::runFrame() {
|
||||||
if (romType != ROMType::None) {
|
if (romType != ROMType::None) {
|
||||||
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
|
||||||
httpServer.processActions();
|
|
||||||
#endif
|
|
||||||
cpu.runFrame(); // Run 1 frame of instructions
|
cpu.runFrame(); // Run 1 frame of instructions
|
||||||
gpu.display(); // Display graphics
|
gpu.display(); // Display graphics
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <system_error>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -30,12 +31,27 @@ class HttpActionKey : public HttpAction {
|
||||||
bool getState() const { return state; }
|
bool getState() const { return state; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class HttpActionLoadRom : public HttpAction {
|
||||||
|
std::filesystem::path path;
|
||||||
|
bool paused;
|
||||||
|
|
||||||
|
public:
|
||||||
|
HttpActionLoadRom(std::filesystem::path path, bool paused) : HttpAction(HttpActionType::LoadRom), path(path), paused(paused) {}
|
||||||
|
|
||||||
|
std::filesystem::path getPath() const { return path; }
|
||||||
|
bool getPaused() const { return paused; }
|
||||||
|
};
|
||||||
|
|
||||||
std::unique_ptr<HttpAction> HttpAction::createScreenshotAction(DeferredResponseWrapper& response) {
|
std::unique_ptr<HttpAction> HttpAction::createScreenshotAction(DeferredResponseWrapper& response) {
|
||||||
return std::make_unique<HttpActionScreenshot>(response);
|
return std::make_unique<HttpActionScreenshot>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<HttpAction> HttpAction::createKeyAction(u32 key, bool state) { return std::make_unique<HttpActionKey>(key, state); }
|
std::unique_ptr<HttpAction> HttpAction::createKeyAction(u32 key, bool state) { return std::make_unique<HttpActionKey>(key, state); }
|
||||||
|
|
||||||
|
std::unique_ptr<HttpAction> HttpAction::createLoadRomAction(std::filesystem::path path, bool paused) {
|
||||||
|
return std::make_unique<HttpActionLoadRom>(path, paused);
|
||||||
|
}
|
||||||
|
|
||||||
HttpServer::HttpServer(Emulator* emulator)
|
HttpServer::HttpServer(Emulator* emulator)
|
||||||
: emulator(emulator), server(std::make_unique<httplib::Server>()), keyMap({
|
: emulator(emulator), server(std::make_unique<httplib::Server>()), keyMap({
|
||||||
{"A", {HID::Keys::A}},
|
{"A", {HID::Keys::A}},
|
||||||
|
@ -110,6 +126,37 @@ void HttpServer::startHttpServer() {
|
||||||
|
|
||||||
server->Get("/status", [this](const httplib::Request&, httplib::Response& response) { response.set_content(status(), "text/plain"); });
|
server->Get("/status", [this](const httplib::Request&, httplib::Response& response) { response.set_content(status(), "text/plain"); });
|
||||||
|
|
||||||
|
server->Get("/load_rom", [this](const httplib::Request& request, httplib::Response& response) {
|
||||||
|
printf("load_rom\n");
|
||||||
|
auto it = request.params.find("path");
|
||||||
|
if (it == request.params.end()) {
|
||||||
|
response.set_content("error", "text/plain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::path romPath = it->second;
|
||||||
|
if (romPath.empty()) {
|
||||||
|
response.set_content("error", "text/plain");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
std::error_code error;
|
||||||
|
if (!std::filesystem::is_regular_file(romPath, error)) {
|
||||||
|
std::string message = "error: " + error.message();
|
||||||
|
response.set_content(message, "text/plain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool paused = false;
|
||||||
|
it = request.params.find("paused");
|
||||||
|
if (it != request.params.end()) {
|
||||||
|
paused = (it->second == "1");
|
||||||
|
}
|
||||||
|
|
||||||
|
pushAction(HttpAction::createLoadRomAction(romPath, paused));
|
||||||
|
response.set_content("ok", "text/plain");
|
||||||
|
});
|
||||||
|
|
||||||
// TODO: ability to specify host and port
|
// TODO: ability to specify host and port
|
||||||
printf("Starting HTTP server on port 1234\n");
|
printf("Starting HTTP server on port 1234\n");
|
||||||
server->listen("localhost", 1234);
|
server->listen("localhost", 1234);
|
||||||
|
@ -165,6 +212,13 @@ void HttpServer::processActions() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case HttpActionType::LoadRom: {
|
||||||
|
HttpActionLoadRom* loadRomAction = static_cast<HttpActionLoadRom*>(action.get());
|
||||||
|
emulator->loadROM(loadRomAction->getPath());
|
||||||
|
paused = loadRomAction->getPaused();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue