mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
impr: Various web build improvements, API cleanup (#1541)
This commit is contained in:
@@ -17,10 +17,13 @@ add_executable(main ${APPLICATION_TYPE}
|
||||
source/messaging/win.cpp
|
||||
source/messaging/web.cpp
|
||||
|
||||
|
||||
source/init/splash_window.cpp
|
||||
source/init/tasks.cpp
|
||||
|
||||
source/init/run/common.cpp
|
||||
source/init/run/native.cpp
|
||||
source/init/run/web.cpp
|
||||
|
||||
${IMHEX_ICON}
|
||||
)
|
||||
|
||||
|
||||
12
main/gui/include/init/run.hpp
Normal file
12
main/gui/include/init/run.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <init/splash_window.hpp>
|
||||
|
||||
namespace hex::init {
|
||||
|
||||
void handleFileOpenRequest();
|
||||
|
||||
std::unique_ptr<WindowSplash> initializeImHex();
|
||||
void deinitializeImHex();
|
||||
|
||||
}
|
||||
48
main/gui/source/init/run/common.cpp
Normal file
48
main/gui/source/init/run/common.cpp
Normal 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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
46
main/gui/source/init/run/native.cpp
Normal file
46
main/gui/source/init/run/native.cpp
Normal 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
|
||||
81
main/gui/source/init/run/web.cpp
Normal file
81
main/gui/source/init/run/web.cpp
Normal 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
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "messaging.hpp"
|
||||
|
||||
#include "init/splash_window.hpp"
|
||||
#include "init/tasks.hpp"
|
||||
|
||||
#include <hex/api/task_manager.hpp>
|
||||
#include <hex/api/plugin_manager.hpp>
|
||||
@@ -16,20 +15,17 @@
|
||||
#include "hex/subcommands/subcommands.hpp"
|
||||
|
||||
#include <wolv/io/fs.hpp>
|
||||
#include <wolv/utils/guards.hpp>
|
||||
#include <fcntl.h>
|
||||
|
||||
#if defined(OS_WINDOWS)
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <codecvt>
|
||||
#elif defined(OS_WEB)
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
#endif
|
||||
|
||||
using namespace hex;
|
||||
|
||||
namespace hex::init { int runImHex(); }
|
||||
|
||||
namespace {
|
||||
|
||||
/**
|
||||
@@ -95,150 +91,6 @@ namespace {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
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
|
||||
if (!splashWindow.loop())
|
||||
ImHexApi::System::impl::addInitArgument("tasks-failed");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Deinitializes ImHex by running all exit tasks
|
||||
*/
|
||||
void deinitializeImHex() {
|
||||
// Run exit tasks
|
||||
init::runExitTasks();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#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();
|
||||
|
||||
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<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);
|
||||
|
||||
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;
|
||||
RequestRestartImHex::subscribe([&] {
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,5 +114,5 @@ int main(int argc, char **argv) {
|
||||
|
||||
ImHexApi::System::impl::setPortableVersion(isPortableVersion());
|
||||
|
||||
return runImHex();
|
||||
return init::runImHex();
|
||||
}
|
||||
|
||||
@@ -106,6 +106,17 @@ namespace hex {
|
||||
|
||||
m_popupsToOpen.push_back(name);
|
||||
});
|
||||
|
||||
LayoutManager::registerLoadCallback([this](std::string_view line) {
|
||||
int width = 0, height = 0;
|
||||
sscanf(line.data(), "MainWindowSize=%d,%d", &width, &height);
|
||||
|
||||
if (width > 0 && height > 0) {
|
||||
TaskManager::doLater([width, height, this]{
|
||||
glfwSetWindowSize(m_window, width, height);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Window::fullFrame() {
|
||||
@@ -840,39 +851,14 @@ namespace hex {
|
||||
|
||||
handler.ReadOpenFn = [](ImGuiContext *ctx, ImGuiSettingsHandler *, const char *) -> void* { return ctx; };
|
||||
|
||||
handler.ReadLineFn = [](ImGuiContext *, ImGuiSettingsHandler *handler, void *, const char *line) {
|
||||
auto window = static_cast<Window*>(handler->UserData);
|
||||
|
||||
for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) {
|
||||
std::string format = view->getUnlocalizedName().get() + "=%d";
|
||||
sscanf(line, format.c_str(), &view->getWindowOpenState());
|
||||
}
|
||||
for (auto &[name, function, detached] : ContentRegistry::Tools::impl::getEntries()) {
|
||||
std::string format = name + "=%d";
|
||||
sscanf(line, format.c_str(), &detached);
|
||||
}
|
||||
|
||||
int width = 0, height = 0;
|
||||
sscanf(line, "MainWindowSize=%d,%d", &width, &height);
|
||||
|
||||
if (width > 0 && height > 0) {
|
||||
TaskManager::doLater([width, height, window]{
|
||||
glfwSetWindowSize(window->m_window, width, height);
|
||||
});
|
||||
}
|
||||
handler.ReadLineFn = [](ImGuiContext *, ImGuiSettingsHandler *, void *, const char *line) {
|
||||
LayoutManager::onLoad(line);
|
||||
};
|
||||
|
||||
handler.WriteAllFn = [](ImGuiContext *, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buf) {
|
||||
buf->appendf("[%s][General]\n", handler->TypeName);
|
||||
|
||||
for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) {
|
||||
buf->appendf("%s=%d\n", name.c_str(), view->getWindowOpenState());
|
||||
}
|
||||
for (auto &[name, function, detached] : ContentRegistry::Tools::impl::getEntries()) {
|
||||
buf->appendf("%s=%d\n", name.c_str(), detached);
|
||||
}
|
||||
|
||||
buf->append("\n");
|
||||
handler.WriteAllFn = [](ImGuiContext *, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buffer) {
|
||||
buffer->appendf("[%s][General]\n", handler->TypeName);
|
||||
LayoutManager::onStore(buffer);
|
||||
buffer->append("\n");
|
||||
};
|
||||
|
||||
handler.UserData = this;
|
||||
|
||||
Reference in New Issue
Block a user