impr: Cleanup main
This commit is contained in:
parent
4b0d980d54
commit
f5fda76414
@ -18,10 +18,8 @@ namespace hex {
|
|||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
inline void println(std::string_view format, Args... args) {
|
inline void println(std::string_view format, Args... args) {
|
||||||
if constexpr (requires { fmt::println(fmt::runtime(format), args...); })
|
|
||||||
fmt::println(fmt::runtime(format), args...);
|
|
||||||
else
|
|
||||||
fmt::print(fmt::runtime(format), args...);
|
fmt::print(fmt::runtime(format), args...);
|
||||||
|
fmt::print("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -12,7 +12,7 @@
|
|||||||
#include <hex/api/task.hpp>
|
#include <hex/api/task.hpp>
|
||||||
#include <hex/api/project_file_manager.hpp>
|
#include <hex/api/project_file_manager.hpp>
|
||||||
#include <hex/api/plugin_manager.hpp>
|
#include <hex/api/plugin_manager.hpp>
|
||||||
#include <hex/api/content_registry.hpp>
|
|
||||||
#include <hex/helpers/fs.hpp>
|
#include <hex/helpers/fs.hpp>
|
||||||
#include "hex/subcommands/subcommands.hpp"
|
#include "hex/subcommands/subcommands.hpp"
|
||||||
|
|
||||||
@ -21,49 +21,56 @@
|
|||||||
|
|
||||||
using namespace hex;
|
using namespace hex;
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
namespace {
|
||||||
Window::initNative();
|
|
||||||
|
|
||||||
hex::crash::setupCrashHandlers();
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles commands passed to ImHex via the command line
|
||||||
|
* @param argc Argument count
|
||||||
|
* @param argv Argument values
|
||||||
|
*/
|
||||||
|
void handleCommandLineInterface(int argc, char **argv) {
|
||||||
|
// Skip the first argument (the executable path) and convert the rest to a vector of strings
|
||||||
std::vector<std::string> args(argv + 1, argv + argc);
|
std::vector<std::string> args(argv + 1, argv + argc);
|
||||||
|
|
||||||
if (argc > 1) {
|
// Load all plugins but don't initialize them
|
||||||
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) {
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) {
|
||||||
PluginManager::load(dir);
|
PluginManager::load(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup messaging system to allow sending commands to the main ImHex instance
|
||||||
hex::messaging::setupMessaging();
|
hex::messaging::setupMessaging();
|
||||||
|
|
||||||
|
// Process the arguments
|
||||||
hex::subcommands::processArguments(args);
|
hex::subcommands::processArguments(args);
|
||||||
|
|
||||||
|
// Unload plugins again
|
||||||
PluginManager::unload();
|
PluginManager::unload();
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info("Welcome to ImHex {}!", ImHexApi::System::getImHexVersion());
|
/**
|
||||||
log::info("Compiled using commit {}@{}", ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash());
|
* @brief Checks if the portable version of ImHex is installed
|
||||||
|
* @note The portable version is detected by the presence of a file named "PORTABLE" in the same directory as the executable
|
||||||
|
* @return True if ImHex is running in portable mode, false otherwise
|
||||||
// Check if ImHex is installed in portable mode
|
*/
|
||||||
{
|
bool isPortableVersion() {
|
||||||
if (const auto executablePath = wolv::io::fs::getExecutablePath(); executablePath.has_value()) {
|
if (const auto executablePath = wolv::io::fs::getExecutablePath(); executablePath.has_value()) {
|
||||||
const auto flagFile = executablePath->parent_path() / "PORTABLE";
|
const auto flagFile = executablePath->parent_path() / "PORTABLE";
|
||||||
|
|
||||||
if (wolv::io::fs::exists(flagFile) && wolv::io::fs::isRegularFile(flagFile))
|
if (wolv::io::fs::exists(flagFile) && wolv::io::fs::isRegularFile(flagFile))
|
||||||
ImHexApi::System::impl::setPortableVersion(true);
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool shouldRestart = false;
|
return false;
|
||||||
// Register an event to handle restarting of ImHex
|
}
|
||||||
EventManager::subscribe<RequestRestartImHex>([&]{ shouldRestart = true; });
|
|
||||||
|
|
||||||
do {
|
/**
|
||||||
shouldRestart = false;
|
* @brief Displays ImHex's splash screen and runs all initialization tasks. The splash screen will be displayed until all tasks have finished.
|
||||||
|
*/
|
||||||
// Initialization
|
void initializeImHex() {
|
||||||
{
|
|
||||||
init::WindowSplash splashWindow;
|
init::WindowSplash splashWindow;
|
||||||
|
|
||||||
|
log::info("Using '{}' GPU", ImHexApi::System::getGPUVendor());
|
||||||
|
|
||||||
// Add initialization tasks to run
|
// Add initialization tasks to run
|
||||||
TaskManager::init();
|
TaskManager::init();
|
||||||
for (const auto &[name, task, async] : init::getInitTasks())
|
for (const auto &[name, task, async] : init::getInitTasks())
|
||||||
@ -74,32 +81,70 @@ int main(int argc, char **argv) {
|
|||||||
ImHexApi::System::getInitArguments().insert({ "tasks-failed", {} });
|
ImHexApi::System::getInitArguments().insert({ "tasks-failed", {} });
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info("Running on {} {} ({})", ImHexApi::System::getOSName(), ImHexApi::System::getOSVersion(), ImHexApi::System::getArchitecture());
|
|
||||||
log::info("Using '{}' GPU", ImHexApi::System::getGPUVendor());
|
|
||||||
|
|
||||||
// Clean up everything after the main window is closed
|
/**
|
||||||
ON_SCOPE_EXIT {
|
* @brief Deinitializes ImHex by running all exit tasks and terminating all asynchronous tasks
|
||||||
|
*/
|
||||||
|
void deinitializeImHex() {
|
||||||
|
// Run exit tasks
|
||||||
for (const auto &[name, task, async] : init::getExitTasks())
|
for (const auto &[name, task, async] : init::getExitTasks())
|
||||||
task();
|
task();
|
||||||
|
|
||||||
|
// Terminate all asynchronous tasks
|
||||||
TaskManager::exit();
|
TaskManager::exit();
|
||||||
};
|
}
|
||||||
|
|
||||||
// Main window
|
/**
|
||||||
{
|
* @brief Handles a file open request by opening the file specified by OS-specific means
|
||||||
Window window;
|
*/
|
||||||
|
void handleFileOpenRequest() {
|
||||||
// Open file that has been requested to be opened through other, OS-specific means
|
|
||||||
if (auto path = hex::getInitialFilePath(); path.has_value()) {
|
if (auto path = hex::getInitialFilePath(); path.has_value()) {
|
||||||
EventManager::post<RequestOpenFile>(path.value());
|
EventManager::post<RequestOpenFile>(path.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the main window
|
|
||||||
|
|
||||||
EventManager::post<EventImHexStartupFinished>();
|
|
||||||
window.loop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main entry point of ImHex
|
||||||
|
* @param argc Argument count
|
||||||
|
* @param argv Argument values
|
||||||
|
* @return Exit code
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
Window::initNative();
|
||||||
|
hex::crash::setupCrashHandlers();
|
||||||
|
|
||||||
|
if (argc > 1) {
|
||||||
|
handleCommandLineInterface(argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
log::info("Welcome to ImHex {}!", ImHexApi::System::getImHexVersion());
|
||||||
|
log::info("Compiled using commit {}@{}", ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash());
|
||||||
|
log::info("Running on {} {} ({})", ImHexApi::System::getOSName(), ImHexApi::System::getOSVersion(), ImHexApi::System::getArchitecture());
|
||||||
|
|
||||||
|
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);
|
} while (shouldRestart);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -91,6 +91,7 @@ namespace hex {
|
|||||||
ContentRegistry::Settings::impl::store();
|
ContentRegistry::Settings::impl::store();
|
||||||
EventManager::post<EventSettingsChanged>();
|
EventManager::post<EventSettingsChanged>();
|
||||||
EventManager::post<EventWindowInitialized>();
|
EventManager::post<EventWindowInitialized>();
|
||||||
|
EventManager::post<EventImHexStartupFinished>();
|
||||||
}
|
}
|
||||||
|
|
||||||
Window::~Window() {
|
Window::~Window() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user