From d25ea2a7fd862ed528e992d56823411c7460f582 Mon Sep 17 00:00:00 2001 From: Viet Dinh <54ckb0y789@gmail.com> Date: Sun, 3 Nov 2024 17:09:55 -0500 Subject: [PATCH] Allow requesting and listening to requests fron JS --- src/async_handler.cpp | 2 ++ src/platform/emscripten/interface.cpp | 1 + src/platform/emscripten/interface.h | 2 ++ src/web_api.cpp | 6 ++++++ src/web_api.h | 2 ++ 5 files changed, 13 insertions(+) diff --git a/src/async_handler.cpp b/src/async_handler.cpp index b9160a762..b2d781665 100644 --- a/src/async_handler.cpp +++ b/src/async_handler.cpp @@ -15,6 +15,7 @@ * along with EasyRPG Player. If not, see . */ +#include "web_api.h" #include #include #include @@ -247,6 +248,7 @@ FileRequestAsync* AsyncHandler::RequestFile(StringView folder_name, StringView f } //Output::Debug("Waiting for {}", path); + Web_API::OnRequestFile(path); return RegisterRequest(std::move(path), std::string(folder_name), std::string(file_name)); } diff --git a/src/platform/emscripten/interface.cpp b/src/platform/emscripten/interface.cpp index ba5a88918..76b8309f4 100644 --- a/src/platform/emscripten/interface.cpp +++ b/src/platform/emscripten/interface.cpp @@ -177,6 +177,7 @@ EMSCRIPTEN_BINDINGS(player_interface) { .class_function("setNametagMode", &Emscripten_Interface::SetNametagMode) .class_function("setSessionToken", &Emscripten_Interface::SetSessionToken, emscripten::allow_raw_pointers()) .class_function("resetCanvas", &Emscripten_Interface::ResetCanvas) + .class_function("preloadFile", &Emscripten_Interface::PreloadFile, emscripten::allow_raw_pointers()) ; emscripten::class_("api_private") diff --git a/src/platform/emscripten/interface.h b/src/platform/emscripten/interface.h index f2c909e2a..cf1878cf7 100644 --- a/src/platform/emscripten/interface.h +++ b/src/platform/emscripten/interface.h @@ -39,6 +39,8 @@ class Emscripten_Interface { static void SetNametagMode(int mode); static void SetSessionToken(std::string t); static bool ResetCanvas(); + + static void PreloadFile(std::string dir, std::string path, bool graphic); }; class Emscripten_Interface_Private { diff --git a/src/web_api.cpp b/src/web_api.cpp index 9b1c4d5d8..b116f8c1e 100644 --- a/src/web_api.cpp +++ b/src/web_api.cpp @@ -104,3 +104,9 @@ bool Web_API::ShouldConnectPlayer(std::string_view uuid) { }, uuid.data(), uuid.size()); return result == 1; } + +void Web_API::OnRequestFile(std::string_view path) { + EM_ASM({ + onRequestFile(UTF8ToString($0, $1)); + }, path.data(), path.size()); +} diff --git a/src/web_api.h b/src/web_api.h index cc126dc68..1862d8929 100644 --- a/src/web_api.h +++ b/src/web_api.h @@ -25,6 +25,8 @@ namespace Web_API { void ShowToastMessage(std::string_view msg, std::string_view icon); bool ShouldConnectPlayer(std::string_view uuid); + + void OnRequestFile(std::string_view path); } #endif