impr: Various web build improvements, API cleanup (#1541)

This commit is contained in:
Nik
2024-02-10 23:31:05 +01:00
committed by GitHub
parent 4b20e35fd2
commit bcbcb1f23c
43 changed files with 977 additions and 847 deletions

View File

@@ -0,0 +1,48 @@
#include <hex/api/event_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <hex/helpers/utils.hpp>
#include <init/splash_window.hpp>
#include <init/tasks.hpp>
namespace hex::init {
/**
* @brief Handles a file open request by opening the file specified by OS-specific means
*/
void handleFileOpenRequest() {
if (auto path = hex::getInitialFilePath(); path.has_value()) {
RequestOpenFile::post(path.value());
}
}
/**
* @brief Displays ImHex's splash screen and runs all initialization tasks. The splash screen will be displayed until all tasks have finished.
*/
[[maybe_unused]]
std::unique_ptr<init::WindowSplash> initializeImHex() {
auto splashWindow = std::make_unique<init::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();
return splashWindow;
}
/**
* @brief Deinitializes ImHex by running all exit tasks
*/
void deinitializeImHex() {
// Run exit tasks
init::runExitTasks();
}
}

View File

@@ -0,0 +1,46 @@
#if !defined(OS_WEB)
#include <hex/api/event_manager.hpp>
#include <wolv/utils/guards.hpp>
#include <init/run.hpp>
#include <window.hpp>
namespace hex::init {
int runImHex() {
bool shouldRestart = false;
do {
// Register an event handler that will make ImHex restart when requested
shouldRestart = false;
RequestRestartImHex::subscribe([&] {
shouldRestart = true;
});
{
auto splashWindow = initializeImHex();
// Draw the splash window while tasks are running
if (!splashWindow->loop())
ImHexApi::System::impl::addInitArgument("tasks-failed");
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

View File

@@ -0,0 +1,81 @@
#if defined(OS_WEB)
#include <emscripten.h>
#include <emscripten/html5.h>
#include <hex/api/imhex_api.hpp>
#include <hex/api/event_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <window.hpp>
#include <init/run.hpp>
namespace hex::init {
void saveFsData() {
EM_ASM({
FS.syncfs(function (err) {
if (!err)
return;
alert("Failed to save permanent file system: "+err);
});
});
}
int runImHex() {
static std::unique_ptr<init::WindowSplash> splashWindow;
splashWindow = initializeImHex();
RequestRestartImHex::subscribe([&] {
MAIN_THREAD_EM_ASM({
location.reload();
});
});
// Draw the splash window while tasks are running
emscripten_set_main_loop_arg([](void *arg) {
auto &splashWindow = *reinterpret_cast<std::unique_ptr<init::WindowSplash>*>(arg);
FrameResult frameResult = splashWindow->fullFrame();
if (frameResult == 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);
emscripten_cancel_main_loop();
try {
saveFsData();
deinitializeImHex();
return "";
} catch (const std::exception &e) {
static std::string message;
message = hex::format("Failed to deinitialize ImHex!\nThis is just a message warning you of this, the application has already closed, you probably can't do anything about it.\n\nError: {}", e.what());
return message.c_str();
}
});
// Delete splash window (do it before creating the main window so glfw destroys the window)
splashWindow.reset();
emscripten_cancel_main_loop();
// Main window
static std::optional<Window> window;
window.emplace();
emscripten_set_main_loop([]() {
window->fullFrame();
}, 60, 0);
}
}, &splashWindow, 60, 0);
return -1;
}
}
#endif