2021-04-20 21:46:48 +02:00
|
|
|
#include "init/tasks.hpp"
|
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
#include <imgui.h>
|
2023-11-24 11:29:05 +01:00
|
|
|
|
2022-12-05 15:29:19 +01:00
|
|
|
#include <romfs/romfs.hpp>
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2023-05-11 23:21:52 +02:00
|
|
|
#include <hex/helpers/http_requests.hpp>
|
|
|
|
#include <hex/helpers/fs.hpp>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
2024-06-22 10:44:55 +02:00
|
|
|
#include <hex/helpers/default_paths.hpp>
|
2023-05-11 23:21:52 +02:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-05-11 23:21:52 +02:00
|
|
|
#include <hex/api/plugin_manager.hpp>
|
2023-08-06 21:33:15 +02:00
|
|
|
#include <hex/api/achievement_manager.hpp>
|
2023-05-11 23:21:52 +02:00
|
|
|
|
2023-04-08 12:08:45 +02:00
|
|
|
#include <hex/ui/view.hpp>
|
|
|
|
#include <hex/ui/popup.hpp>
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/fs.hpp>
|
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
namespace hex::init {
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
2022-12-05 15:29:19 +01:00
|
|
|
bool setupEnvironment() {
|
|
|
|
hex::log::debug("Using romfs: '{}'", romfs::name());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-29 19:15:09 +02:00
|
|
|
static bool isSubPathWritable(std::fs::path path) {
|
2024-06-29 21:13:04 +02:00
|
|
|
for (u32 i = 0; i < 128; i++) {
|
2024-06-29 19:15:09 +02:00
|
|
|
if (hex::fs::isPathWritable(path))
|
|
|
|
return true;
|
|
|
|
|
2024-06-29 21:13:04 +02:00
|
|
|
auto parentPath = path.parent_path();
|
|
|
|
if (parentPath == path)
|
|
|
|
break;
|
|
|
|
|
|
|
|
path = std::move(parentPath);
|
2024-06-29 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
bool createDirectories() {
|
2021-05-21 23:46:36 +02:00
|
|
|
bool result = true;
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Try to create all default directories
|
2024-06-22 10:44:55 +02:00
|
|
|
for (auto path : paths::All) {
|
2024-06-29 13:01:25 +02:00
|
|
|
for (auto &folder : path->all()) {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
2024-06-29 19:15:09 +02:00
|
|
|
if (isSubPathWritable(folder.parent_path()))
|
2024-06-29 13:01:25 +02:00
|
|
|
wolv::io::fs::createDirectories(folder);
|
2021-05-21 23:46:36 +02:00
|
|
|
} catch (...) {
|
2023-03-12 18:43:05 +01:00
|
|
|
log::error("Failed to create folder {}!", wolv::util::toUTF8String(folder));
|
2021-05-21 23:46:36 +02:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
2021-05-21 23:46:36 +02:00
|
|
|
if (!result)
|
2022-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("folder-creation-error");
|
2021-05-21 23:46:36 +02:00
|
|
|
|
|
|
|
return result;
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
bool prepareExit() {
|
2023-12-06 11:04:35 +01:00
|
|
|
// Terminate all asynchronous tasks
|
|
|
|
TaskManager::exit();
|
|
|
|
|
2024-03-26 19:48:38 +01:00
|
|
|
// Unlock font atlas, so it can be deleted in case of a crash
|
|
|
|
if (ImGui::GetCurrentContext() != nullptr) {
|
|
|
|
if (ImGui::GetIO().Fonts != nullptr) {
|
|
|
|
ImGui::GetIO().Fonts->Locked = false;
|
|
|
|
ImGui::GetIO().Fonts = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2023-12-02 11:09:32 +01:00
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
// Print a nice message if a crash happened while cleaning up resources
|
|
|
|
// To the person fixing this:
|
|
|
|
// ALWAYS wrap static heap allocated objects inside libimhex such as std::vector, std::string, std::function, etc. in a AutoReset<T>
|
|
|
|
// e.g `AutoReset<std::vector<MyStruct>> m_structs;`
|
|
|
|
//
|
2024-03-26 19:48:38 +01:00
|
|
|
// The reason this is necessary because each plugin / dynamic library gets its own instance of `std::allocator`
|
2024-01-30 11:21:34 +01:00
|
|
|
// which will try to free the allocated memory when the object is destroyed. However since the storage is static, this
|
|
|
|
// will happen only when libimhex is unloaded after main() returns. At this point all plugins have been unloaded already so
|
|
|
|
// the std::allocator will try to free memory in a heap that does not exist anymore which will cause a crash.
|
|
|
|
// By wrapping the object in a AutoReset<T>, the `EventImHexClosing` event will automatically handle clearing the object
|
|
|
|
// while the heap is still valid.
|
|
|
|
// The heap stays valid right up to the point where `PluginManager::unload()` is called.
|
2024-01-30 16:32:48 +01:00
|
|
|
EventAbnormalTermination::subscribe([](int) {
|
2024-01-30 11:21:34 +01:00
|
|
|
log::fatal("A crash happened while cleaning up resources during exit!");
|
|
|
|
log::fatal("This is most certainly because WerWolv again forgot to mark a heap allocated object as 'AutoReset'.");
|
|
|
|
log::fatal("Please report this issue on the ImHex GitHub page!");
|
|
|
|
log::fatal("To the person fixing this, read the comment above this message for more information.");
|
|
|
|
});
|
|
|
|
|
2024-02-19 22:06:46 +01:00
|
|
|
ImHexApi::System::impl::cleanup();
|
2024-03-26 19:48:38 +01:00
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
EventImHexClosing::post();
|
|
|
|
EventManager::clear();
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadPlugins() {
|
2023-07-22 18:38:14 +02:00
|
|
|
// Load all plugins
|
2024-01-12 23:03:13 +01:00
|
|
|
#if !defined(IMHEX_STATIC_LINK_PLUGINS)
|
2024-06-22 10:44:55 +02:00
|
|
|
for (const auto &dir : paths::Plugins.read()) {
|
2024-01-22 23:35:00 +01:00
|
|
|
PluginManager::addLoadPath(dir);
|
2024-01-12 23:03:13 +01:00
|
|
|
}
|
2024-01-22 23:35:00 +01:00
|
|
|
|
2024-03-13 19:49:04 +01:00
|
|
|
PluginManager::loadLibraries();
|
2024-01-22 23:35:00 +01:00
|
|
|
PluginManager::load();
|
2024-01-12 23:03:13 +01:00
|
|
|
#endif
|
2023-07-22 18:38:14 +02:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Get loaded plugins
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto &plugins = PluginManager::getPlugins();
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// If no plugins were loaded, ImHex wasn't installed properly. This will trigger an error popup later on
|
2022-02-01 18:09:40 +01:00
|
|
|
if (plugins.empty()) {
|
2022-01-13 14:34:27 +01:00
|
|
|
log::error("No plugins found!");
|
|
|
|
|
2022-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("no-plugins");
|
2021-04-20 21:46:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto shouldLoadPlugin = [executablePath = wolv::io::fs::getExecutablePath()](const Plugin &plugin) {
|
2023-02-17 12:03:53 +01:00
|
|
|
// In debug builds, ignore all plugins that are not part of the executable directory
|
2022-11-25 10:28:05 +01:00
|
|
|
#if !defined(DEBUG)
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!executablePath.has_value())
|
|
|
|
return true;
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
if (!PluginManager::getPluginLoadPaths().empty())
|
2024-01-22 23:35:00 +01:00
|
|
|
return true;
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Check if the plugin is somewhere in the same directory tree as the executable
|
2022-11-25 10:28:05 +01:00
|
|
|
return !std::fs::relative(plugin.getPath(), executablePath->parent_path()).string().starts_with("..");
|
|
|
|
};
|
|
|
|
|
2024-01-12 23:03:13 +01:00
|
|
|
u32 loadErrors = 0;
|
|
|
|
std::set<std::string> pluginNames;
|
|
|
|
|
2023-12-23 21:09:41 +01:00
|
|
|
// Load library plugins first since plugins might depend on them
|
|
|
|
for (const auto &plugin : plugins) {
|
|
|
|
if (!plugin.isLibraryPlugin()) continue;
|
|
|
|
|
2022-11-25 10:28:05 +01:00
|
|
|
if (!shouldLoadPlugin(plugin)) {
|
2024-01-12 23:03:13 +01:00
|
|
|
log::debug("Skipping library plugin {}", plugin.getPath().string());
|
2022-11-25 10:28:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-12 23:03:13 +01:00
|
|
|
// Initialize the library
|
2022-02-01 23:57:48 +01:00
|
|
|
if (!plugin.initializePlugin()) {
|
2024-01-12 23:03:13 +01:00
|
|
|
log::error("Failed to initialize library plugin {}", wolv::util::toUTF8String(plugin.getPath().filename()));
|
2022-02-01 23:57:48 +01:00
|
|
|
loadErrors++;
|
|
|
|
}
|
2024-01-12 23:03:13 +01:00
|
|
|
pluginNames.insert(plugin.getPluginName());
|
2022-02-01 23:57:48 +01:00
|
|
|
}
|
|
|
|
|
2024-01-12 23:03:13 +01:00
|
|
|
// Load all plugins
|
2022-02-01 23:57:48 +01:00
|
|
|
for (const auto &plugin : plugins) {
|
2023-12-23 21:09:41 +01:00
|
|
|
if (plugin.isLibraryPlugin()) continue;
|
2022-02-01 23:57:48 +01:00
|
|
|
|
2022-11-25 10:28:05 +01:00
|
|
|
if (!shouldLoadPlugin(plugin)) {
|
|
|
|
log::debug("Skipping plugin {}", plugin.getPath().string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// Initialize the plugin
|
2022-02-01 18:09:40 +01:00
|
|
|
if (!plugin.initializePlugin()) {
|
2023-03-12 18:43:05 +01:00
|
|
|
log::error("Failed to initialize plugin {}", wolv::util::toUTF8String(plugin.getPath().filename()));
|
2022-02-01 18:09:40 +01:00
|
|
|
loadErrors++;
|
|
|
|
}
|
2024-01-12 23:03:13 +01:00
|
|
|
pluginNames.insert(plugin.getPluginName());
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 12:03:53 +01:00
|
|
|
// If no plugins were loaded successfully, ImHex wasn't installed properly. This will trigger an error popup later on
|
2022-02-01 18:09:40 +01:00
|
|
|
if (loadErrors == plugins.size()) {
|
|
|
|
log::error("No plugins loaded successfully!");
|
2022-09-19 16:54:19 +02:00
|
|
|
ImHexApi::System::impl::addInitArgument("no-plugins");
|
2022-02-01 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
2024-01-12 23:03:13 +01:00
|
|
|
if (pluginNames.size() != plugins.size()) {
|
|
|
|
log::error("Duplicate plugins detected!");
|
|
|
|
ImHexApi::System::impl::addInitArgument("duplicate-plugins");
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-11 21:29:30 +01:00
|
|
|
bool deleteOldFiles() {
|
2023-11-30 11:23:12 +01:00
|
|
|
bool result = true;
|
2023-11-10 20:47:08 +01:00
|
|
|
|
2024-06-22 10:44:55 +02:00
|
|
|
auto keepNewest = [&](u32 count, const paths::impl::DefaultPath &pathType) {
|
|
|
|
for (const auto &path : pathType.write()) {
|
2023-12-11 21:29:30 +01:00
|
|
|
try {
|
|
|
|
std::vector<std::filesystem::directory_entry> files;
|
|
|
|
|
|
|
|
for (const auto& file : std::filesystem::directory_iterator(path))
|
|
|
|
files.push_back(file);
|
2023-05-16 14:45:24 +02:00
|
|
|
|
2023-12-11 21:29:30 +01:00
|
|
|
if (files.size() <= count)
|
|
|
|
return;
|
2023-05-16 14:45:24 +02:00
|
|
|
|
2023-12-11 21:29:30 +01:00
|
|
|
std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) {
|
|
|
|
return std::filesystem::last_write_time(a) > std::filesystem::last_write_time(b);
|
|
|
|
});
|
2023-05-16 14:45:24 +02:00
|
|
|
|
2023-12-11 21:29:30 +01:00
|
|
|
for (auto it = files.begin() + count; it != files.end(); it += 1)
|
|
|
|
std::filesystem::remove(it->path());
|
|
|
|
} catch (std::filesystem::filesystem_error &e) {
|
|
|
|
log::error("Failed to clear old file! {}", e.what());
|
|
|
|
result = false;
|
|
|
|
}
|
2023-07-24 15:36:29 +02:00
|
|
|
}
|
2023-12-11 21:29:30 +01:00
|
|
|
};
|
|
|
|
|
2024-06-22 10:44:55 +02:00
|
|
|
keepNewest(10, paths::Logs);
|
|
|
|
keepNewest(25, paths::Backups);
|
2023-05-16 14:45:24 +02:00
|
|
|
|
2023-11-30 11:23:12 +01:00
|
|
|
return result;
|
2023-05-16 14:45:24 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
bool unloadPlugins() {
|
|
|
|
PluginManager::unload();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadSettings() {
|
2021-05-21 23:46:36 +02:00
|
|
|
try {
|
2023-02-17 12:03:53 +01:00
|
|
|
// Try to load settings from file
|
2023-03-21 15:33:43 +01:00
|
|
|
ContentRegistry::Settings::impl::load();
|
2022-01-13 14:34:27 +01:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
log::error("Failed to load configuration! {}", e.what());
|
2022-07-30 11:19:56 +02:00
|
|
|
|
2021-05-21 23:46:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
// Run all exit tasks, and print to console
|
2023-10-11 22:38:54 +02:00
|
|
|
void runExitTasks() {
|
|
|
|
for (const auto &[name, task, async] : init::getExitTasks()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
const bool result = task();
|
2023-11-11 00:54:16 +01:00
|
|
|
log::info("Exit task '{0}' finished {1}", name, result ? "successfully" : "unsuccessfully");
|
2023-10-11 22:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
std::vector<Task> getInitTasks() {
|
|
|
|
return {
|
2022-12-05 15:29:19 +01:00
|
|
|
{ "Setting up environment", setupEnvironment, false },
|
2022-10-20 08:28:29 +02:00
|
|
|
{ "Creating directories", createDirectories, false },
|
|
|
|
{ "Loading settings", loadSettings, false },
|
2024-01-08 09:39:01 +01:00
|
|
|
{ "Loading plugins", loadPlugins, false },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Task> getExitTasks() {
|
|
|
|
return {
|
2024-01-30 11:21:34 +01:00
|
|
|
{ "Prepare exit", prepareExit, false },
|
2023-11-10 23:25:02 +01:00
|
|
|
{ "Unloading plugins", unloadPlugins, false },
|
2023-12-11 21:29:30 +01:00
|
|
|
{ "Deleting old files", deleteOldFiles, false },
|
2021-04-20 21:46:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:03:45 +02:00
|
|
|
}
|