2024-05-08 22:30:53 +02:00
|
|
|
#include <fonts/codicons_font.h>
|
2023-07-15 14:29:14 +02:00
|
|
|
#include <hex/plugin.hpp>
|
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/task_manager.hpp>
|
2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/localization_manager.hpp>
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
|
|
|
|
|
|
|
#include <loaders/dotnet/dotnet_loader.hpp>
|
|
|
|
|
|
|
|
#include <romfs/romfs.hpp>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
using namespace hex;
|
|
|
|
using namespace hex::script::loader;
|
|
|
|
|
|
|
|
using ScriptLoaders = std::tuple<
|
2024-05-17 21:01:35 +02:00
|
|
|
#if defined(IMHEX_DOTNET_SCRIPT_SUPPORT)
|
2023-07-15 14:29:14 +02:00
|
|
|
DotNetLoader
|
|
|
|
#endif
|
|
|
|
>;
|
|
|
|
|
2024-05-08 22:30:53 +02:00
|
|
|
IMHEX_DEFINE_PLUGIN_FEATURES(){ };
|
|
|
|
|
2023-07-15 14:29:14 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
ScriptLoaders s_loaders;
|
|
|
|
|
|
|
|
void loadScript(std::vector<const Script*> &scripts, auto &loader) {
|
|
|
|
loader.loadAll();
|
|
|
|
|
2024-06-07 21:57:39 +02:00
|
|
|
for (auto &script : std::as_const(loader).getScripts())
|
2023-07-15 14:29:14 +02:00
|
|
|
scripts.emplace_back(&script);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const Script*> loadAllScripts() {
|
2024-05-08 22:30:53 +02:00
|
|
|
std::vector<const Script*> scripts;
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
|
|
try {
|
2024-05-08 22:30:53 +02:00
|
|
|
std::apply([&scripts](auto&&... args) {
|
|
|
|
(loadScript(scripts, args), ...);
|
2023-07-15 14:29:14 +02:00
|
|
|
}, s_loaders);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Error when loading scripts: {}", e.what());
|
|
|
|
}
|
|
|
|
|
2024-05-08 22:30:53 +02:00
|
|
|
{
|
|
|
|
std::vector<hex::Feature> features;
|
|
|
|
for (const auto &script : scripts) {
|
|
|
|
if (!script->background)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
features.emplace_back(script->name, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
IMHEX_PLUGIN_FEATURES = features;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scripts;
|
2023-07-15 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void initializeLoader(u32 &count, auto &loader) {
|
|
|
|
try {
|
|
|
|
if (loader.initialize())
|
|
|
|
count += 1;
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Error when initializing script loader: {}", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initializeAllLoaders() {
|
|
|
|
u32 count = 0;
|
|
|
|
|
|
|
|
std::apply([&count](auto&&... args) {
|
|
|
|
try {
|
|
|
|
(initializeLoader(count, args), ...);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Error when initializing script loaders: {}", e.what());
|
|
|
|
}
|
|
|
|
}, s_loaders);
|
|
|
|
|
|
|
|
return count > 0;
|
|
|
|
}
|
|
|
|
|
2023-07-21 20:25:56 +02:00
|
|
|
void addScriptsMenu() {
|
2024-03-10 22:05:26 +01:00
|
|
|
static std::vector<const Script*> scripts;
|
2023-07-15 14:29:14 +02:00
|
|
|
static TaskHolder runnerTask, updaterTask;
|
|
|
|
hex::ContentRegistry::Interface::addMenuItemSubMenu({ "hex.builtin.menu.extras" }, 5000, [] {
|
|
|
|
static bool menuJustOpened = true;
|
|
|
|
|
2024-05-08 22:30:53 +02:00
|
|
|
if (ImGui::BeginMenuEx("hex.script_loader.menu.run_script"_lang, ICON_VS_LIBRARY)) {
|
2023-07-15 14:29:14 +02:00
|
|
|
if (menuJustOpened) {
|
|
|
|
menuJustOpened = false;
|
|
|
|
if (!updaterTask.isRunning()) {
|
|
|
|
updaterTask = TaskManager::createBackgroundTask("Updating Scripts...", [] (auto&) {
|
|
|
|
scripts = loadAllScripts();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updaterTask.isRunning()) {
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextSpinner("hex.script_loader.menu.loading"_lang);
|
2023-07-15 14:29:14 +02:00
|
|
|
} else if (scripts.empty()) {
|
|
|
|
ImGui::TextUnformatted("hex.script_loader.menu.no_scripts"_lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &script : scripts) {
|
2024-06-07 21:57:39 +02:00
|
|
|
const auto &[name, path, background, entryPoint, loader] = *script;
|
2024-05-08 22:30:53 +02:00
|
|
|
if (background)
|
|
|
|
continue;
|
2023-07-15 14:29:14 +02:00
|
|
|
|
2024-05-17 21:01:35 +02:00
|
|
|
if (ImGui::MenuItem(name.c_str(), loader->getTypeName().c_str())) {
|
2023-07-15 14:29:14 +02:00
|
|
|
runnerTask = TaskManager::createTask("Running script...", TaskManager::NoProgress, [entryPoint](auto&) {
|
|
|
|
entryPoint();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
} else {
|
|
|
|
menuJustOpened = true;
|
|
|
|
}
|
|
|
|
}, [] {
|
|
|
|
return !runnerTask.isRunning();
|
|
|
|
});
|
2024-03-10 22:05:26 +01:00
|
|
|
|
|
|
|
updaterTask = TaskManager::createBackgroundTask("Updating Scripts...", [] (auto&) {
|
|
|
|
scripts = loadAllScripts();
|
|
|
|
});
|
2023-07-15 14:29:14 +02:00
|
|
|
}
|
2023-07-21 20:25:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IMHEX_PLUGIN_SETUP("Script Loader", "WerWolv", "Script Loader plugin") {
|
|
|
|
hex::log::debug("Using romfs: '{}'", romfs::name());
|
|
|
|
for (auto &path : romfs::list("lang"))
|
|
|
|
hex::ContentRegistry::Language::addLocalization(nlohmann::json::parse(romfs::get(path).string()));
|
|
|
|
|
2023-07-24 15:36:57 +02:00
|
|
|
if (initializeAllLoaders()) {
|
|
|
|
addScriptsMenu();
|
|
|
|
}
|
2024-05-17 21:01:35 +02:00
|
|
|
}
|