feat: Support for building ImHex for the web (#1328)

Co-authored-by: WerWolv <werwolv98@gmail.com>
Co-authored-by: AnnsAnn <git@annsann.eu>
This commit is contained in:
iTrooz
2023-10-04 12:00:32 +02:00
committed by GitHub
parent a62ede7840
commit d15bd4771d
84 changed files with 1825 additions and 676 deletions

View File

@@ -19,6 +19,11 @@
#include <wolv/io/fs.hpp>
#include <wolv/utils/guards.hpp>
#if defined(OS_WEB)
#include <emscripten.h>
#include <emscripten/html5.h>
#endif
using namespace hex;
namespace {
@@ -66,6 +71,7 @@ namespace {
/**
* @brief Displays ImHex's splash screen and runs all initialization tasks. The splash screen will be displayed until all tasks have finished.
*/
[[maybe_unused]]
void initializeImHex() {
init::WindowSplash splashWindow;
@@ -105,6 +111,103 @@ namespace {
}
}
#if defined(OS_WEB)
using namespace hex::init;
void saveFsData() {
EM_ASM({
FS.syncfs(function (err) {
if (!err)
return;
alert("Failed to save permanent file system: "+err);
});
});
}
int runImHex() {
auto splashWindow = new WindowSplash();
log::info("Using '{}' GPU", ImHexApi::System::getGPUVendor());
// Add initialization tasks to run
TaskManager::init();
for (const auto &[name, task, async] : init::getInitTasks())
splashWindow->addStartupTask(name, task, async);
splashWindow->startStartupTasks();
// Draw the splash window while tasks are running
emscripten_set_main_loop_arg([](void *arg) {
auto splashWindow = reinterpret_cast<WindowSplash*>(arg);
FrameResult res = splashWindow->fullFrame();
if (res == FrameResult::success) {
handleFileOpenRequest();
// Clean up everything after the main window is closed
emscripten_set_beforeunload_callback(nullptr, [](int eventType, const void *reserved, void *userData) {
hex::unused(eventType, reserved, userData);
try {
saveFsData();
deinitializeImHex();
return "";
} catch (const std::exception &ex) {
std::string *msg = new std::string("Failed to deinitialize ImHex. This is just a message warning you of this, the application has already closed, you probably can't do anything about it. Message: ");
msg->append(std::string(ex.what()));
log::fatal("{}", *msg);
return msg->c_str();
}
});
// Delete splash window (do it before creating the main window so glfw destroys the window)
delete splashWindow;
emscripten_cancel_main_loop();
// Main window
static Window window;
emscripten_set_main_loop([]() {
window.fullFrame();
}, 60, 0);
}
}, splashWindow, 60, 0);
return -1;
}
#else
int runImHex() {
bool shouldRestart = false;
do {
// Register an event handler that will make ImHex restart when requested
shouldRestart = false;
EventManager::subscribe<RequestRestartImHex>([&] {
shouldRestart = true;
});
initializeImHex();
handleFileOpenRequest();
// Clean up everything after the main window is closed
ON_SCOPE_EXIT {
deinitializeImHex();
};
// Main window
Window window;
window.loop();
} while (shouldRestart);
return EXIT_SUCCESS;
}
#endif
}
/**
@@ -127,27 +230,5 @@ int main(int argc, char **argv) {
ImHexApi::System::impl::setPortableVersion(isPortableVersion());
bool shouldRestart = false;
do {
// Register an event handler that will make ImHex restart when requested
shouldRestart = false;
EventManager::subscribe<RequestRestartImHex>([&] {
shouldRestart = true;
});
initializeImHex();
handleFileOpenRequest();
// Clean up everything after the main window is closed
ON_SCOPE_EXIT {
deinitializeImHex();
};
// Main window
Window window;
window.loop();
} while (shouldRestart);
return EXIT_SUCCESS;
}
return runImHex();
};