2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/shortcut_manager.hpp>
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2022-01-13 14:33:30 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2024-01-30 11:21:34 +01:00
|
|
|
#include <hex/helpers/auto_reset.hpp>
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/ui/view.hpp>
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <hex/data_processor/node.hpp>
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2021-01-11 20:31:40 +01:00
|
|
|
#include <filesystem>
|
2023-11-02 20:21:38 +01:00
|
|
|
#include <jthread.hpp>
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
#if defined(OS_WEB)
|
|
|
|
#include <emscripten.h>
|
|
|
|
#endif
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2021-01-11 20:31:40 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::Settings {
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
[[maybe_unused]] constexpr auto SettingsFile = "settings.json";
|
2022-07-30 11:19:56 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
nlohmann::json& getSetting(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &defaultValue) {
|
2023-03-21 15:33:43 +01:00
|
|
|
auto &settings = getSettingsData();
|
2021-03-01 08:56:49 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
if (!settings.contains(unlocalizedCategory))
|
|
|
|
settings[unlocalizedCategory] = {};
|
|
|
|
|
|
|
|
if (!settings[unlocalizedCategory].contains(unlocalizedName))
|
|
|
|
settings[unlocalizedCategory][unlocalizedName] = defaultValue;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return settings[unlocalizedCategory][unlocalizedName];
|
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json &getSettingsData() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<nlohmann::json> settings;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
#if defined(OS_WEB)
|
|
|
|
void load() {
|
|
|
|
char *data = (char *) MAIN_THREAD_EM_ASM_INT({
|
|
|
|
let data = localStorage.getItem("config");
|
|
|
|
return data ? stringToNewUTF8(data) : null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (data == nullptr) {
|
|
|
|
store();
|
|
|
|
} else {
|
|
|
|
getSettingsData() = nlohmann::json::parse(data);
|
2023-03-21 15:33:43 +01:00
|
|
|
}
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
void store() {
|
|
|
|
auto data = getSettingsData().dump();
|
|
|
|
MAIN_THREAD_EM_ASM({
|
|
|
|
localStorage.setItem("config", UTF8ToString($0));
|
|
|
|
}, data.c_str());
|
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
void clear() {
|
|
|
|
MAIN_THREAD_EM_ASM({
|
|
|
|
localStorage.removeItem("config");
|
|
|
|
});
|
feat: Added hex::group attribute and various fixes (#1302)
As discussed (many times) on Discord, does the same as the new favorite
tag, but instead allows you to add multiple groups.
Initially, this would cause some insane issues with draw/reset
(apparantly) fighting eachother in the pattern drawer. After a lot of
trial and error, I decided to rewrite the flow that is responsible for
calling reset. Now evaluating patterns is the one to decide when the
reset happens, not the core "game"-loop.
To make sure that draw and reset can never happen at the same time, the
mutex originally used for the favorites has been repurposed. Due to the
restructuring, the mutex in the favorite-task is no longer needed, as
that will only ever kick-off after reset is called and if there are
actually patterns, which can never line up to be accessed on different
threads at the same time.
Last but not least, I noticed that hard crashes could result in your
config file getting overridden. I added a check to prevent that.
Last I issue I can see is that if you use an excessive amount of
favorites/groups, a crash can still happen, but it only happens when you
close the program (occasionally, but unpredictable). Before, this would
happen if you ran the evaluation a second time. I boiled the cause of
the crash down to these lines of code in evaluator.cpp >
patternDestroyed:
```cpp
if (pattern->isPatternLocal()) {
if (auto it = this->m_patternLocalStorage.find(pattern->getHeapAddress()); it != this->m_patternLocalStorage.end()) {
auto &[key, data] = *it;
data.referenceCount--;
if (data.referenceCount == 0)
this->m_patternLocalStorage.erase(it);
} else if (!this->m_evaluated) {
err::E0001.throwError(fmt::format("Double free of variable named '{}'.", pattern->getVariableName()));
}
}
```
Specifically, trying to access the `*it` is the reason for the crash
(this was also the cause of the crashes before my fixes, but then during
evaluation).
I'm suspecting the root cause is somewhere in the `.clone` methods of
the patterns. I'd say that for now a crash when closing the program is
more acceptable than during evaluation (which can even happen if you use
favorites).
2023-09-16 13:09:59 +02:00
|
|
|
}
|
2023-10-04 12:00:32 +02:00
|
|
|
#else
|
|
|
|
void load() {
|
|
|
|
bool loaded = false;
|
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) {
|
|
|
|
wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Read);
|
|
|
|
|
|
|
|
if (file.isValid()) {
|
|
|
|
getSettingsData() = nlohmann::json::parse(file.readString());
|
|
|
|
loaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
feat: Added hex::group attribute and various fixes (#1302)
As discussed (many times) on Discord, does the same as the new favorite
tag, but instead allows you to add multiple groups.
Initially, this would cause some insane issues with draw/reset
(apparantly) fighting eachother in the pattern drawer. After a lot of
trial and error, I decided to rewrite the flow that is responsible for
calling reset. Now evaluating patterns is the one to decide when the
reset happens, not the core "game"-loop.
To make sure that draw and reset can never happen at the same time, the
mutex originally used for the favorites has been repurposed. Due to the
restructuring, the mutex in the favorite-task is no longer needed, as
that will only ever kick-off after reset is called and if there are
actually patterns, which can never line up to be accessed on different
threads at the same time.
Last but not least, I noticed that hard crashes could result in your
config file getting overridden. I added a check to prevent that.
Last I issue I can see is that if you use an excessive amount of
favorites/groups, a crash can still happen, but it only happens when you
close the program (occasionally, but unpredictable). Before, this would
happen if you ran the evaluation a second time. I boiled the cause of
the crash down to these lines of code in evaluator.cpp >
patternDestroyed:
```cpp
if (pattern->isPatternLocal()) {
if (auto it = this->m_patternLocalStorage.find(pattern->getHeapAddress()); it != this->m_patternLocalStorage.end()) {
auto &[key, data] = *it;
data.referenceCount--;
if (data.referenceCount == 0)
this->m_patternLocalStorage.erase(it);
} else if (!this->m_evaluated) {
err::E0001.throwError(fmt::format("Double free of variable named '{}'.", pattern->getVariableName()));
}
}
```
Specifically, trying to access the `*it` is the reason for the crash
(this was also the cause of the crashes before my fixes, but then during
evaluation).
I'm suspecting the root cause is somewhere in the `.clone` methods of
the patterns. I'd say that for now a crash when closing the program is
more acceptable than during evaluation (which can even happen if you use
favorites).
2023-09-16 13:09:59 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
if (!loaded)
|
|
|
|
store();
|
|
|
|
}
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
void store() {
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto &settingsData = getSettingsData();
|
2023-12-27 16:53:03 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// During a crash settings can be empty, causing them to be overwritten.
|
2023-12-27 16:53:03 +01:00
|
|
|
if (settingsData.empty()) {
|
2023-10-04 12:00:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto result = settingsData.dump(4);
|
|
|
|
if (result.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-04 12:00:32 +02:00
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) {
|
2023-12-27 16:53:03 +01:00
|
|
|
wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Write);
|
2023-10-04 12:00:32 +02:00
|
|
|
|
|
|
|
if (file.isValid()) {
|
2023-12-27 16:53:03 +01:00
|
|
|
file.setSize(0);
|
|
|
|
file.writeString(result);
|
2023-10-04 12:00:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-03-21 15:33:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-30 11:19:56 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
void clear() {
|
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) {
|
|
|
|
wolv::io::fs::remove(dir / SettingsFile);
|
|
|
|
}
|
2023-03-21 15:33:43 +01:00
|
|
|
}
|
2023-10-04 12:00:32 +02:00
|
|
|
#endif
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
template<typename T>
|
2023-12-19 12:22:28 +01:00
|
|
|
static T* insertOrGetEntry(std::vector<T> &vector, const UnlocalizedString &unlocalizedName) {
|
2023-10-21 23:07:33 +02:00
|
|
|
T *foundEntry = nullptr;
|
|
|
|
for (auto &entry : vector) {
|
|
|
|
if (entry.unlocalizedName == unlocalizedName) {
|
|
|
|
foundEntry = &entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
if (foundEntry == nullptr) {
|
|
|
|
if (unlocalizedName.empty())
|
|
|
|
foundEntry = &*vector.emplace(vector.begin(), unlocalizedName);
|
|
|
|
else
|
|
|
|
foundEntry = &vector.emplace_back(unlocalizedName);
|
2023-03-21 15:33:43 +01:00
|
|
|
}
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
return foundEntry;
|
2022-02-18 22:34:54 +01:00
|
|
|
}
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
std::vector<Category> &getSettings() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Category>> categories;
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
return categories;
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
Widgets::Widget* add(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedSubCategory, const UnlocalizedString &unlocalizedName, std::unique_ptr<Widgets::Widget> &&widget) {
|
2023-11-10 14:48:26 +01:00
|
|
|
const auto category = insertOrGetEntry(getSettings(), unlocalizedCategory);
|
|
|
|
const auto subCategory = insertOrGetEntry(category->subCategories, unlocalizedSubCategory);
|
|
|
|
const auto entry = insertOrGetEntry(subCategory->entries, unlocalizedName);
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
entry->widget = std::move(widget);
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
return entry->widget.get();
|
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void setCategoryDescription(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedDescription) {
|
2023-11-10 14:48:26 +01:00
|
|
|
const auto category = insertOrGetEntry(impl::getSettings(), unlocalizedCategory);
|
2023-10-21 23:07:33 +02:00
|
|
|
|
|
|
|
category->unlocalizedDescription = unlocalizedDescription;
|
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
nlohmann::json read(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &defaultValue) {
|
2023-10-21 23:07:33 +02:00
|
|
|
auto setting = impl::getSetting(unlocalizedCategory, unlocalizedName, defaultValue);
|
2021-02-13 15:15:32 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
if (setting.is_number() && defaultValue.is_boolean())
|
|
|
|
setting = setting.get<int>() != 0;
|
2023-11-18 17:23:50 +01:00
|
|
|
if (setting.is_null())
|
|
|
|
setting = defaultValue;
|
2021-02-13 15:15:32 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
return setting;
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void write(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &value) {
|
2023-10-21 23:07:33 +02:00
|
|
|
impl::getSetting(unlocalizedCategory, unlocalizedName, value) = value;
|
|
|
|
}
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
namespace Widgets {
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
bool Checkbox::draw(const std::string &name) {
|
2023-12-19 13:10:25 +01:00
|
|
|
return ImGui::Checkbox(name.c_str(), &m_value);
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
void Checkbox::load(const nlohmann::json &data) {
|
|
|
|
if (data.is_number()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = data.get<int>() != 0;
|
2023-10-21 23:07:33 +02:00
|
|
|
} else if (data.is_boolean()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = data.get<bool>();
|
2023-10-21 23:07:33 +02:00
|
|
|
} else {
|
|
|
|
log::warn("Invalid data type loaded from settings for checkbox!");
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
nlohmann::json Checkbox::store() {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_value;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
bool SliderInteger::draw(const std::string &name) {
|
2023-12-19 13:10:25 +01:00
|
|
|
return ImGui::SliderInt(name.c_str(), &m_value, m_min, m_max);
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
void SliderInteger::load(const nlohmann::json &data) {
|
|
|
|
if (data.is_number_integer()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = data.get<int>();
|
2023-10-21 23:07:33 +02:00
|
|
|
} else {
|
|
|
|
log::warn("Invalid data type loaded from settings for slider!");
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
nlohmann::json SliderInteger::store() {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_value;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
bool SliderFloat::draw(const std::string &name) {
|
2023-12-19 13:10:25 +01:00
|
|
|
return ImGui::SliderFloat(name.c_str(), &m_value, m_min, m_max);
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SliderFloat::load(const nlohmann::json &data) {
|
|
|
|
if (data.is_number()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = data.get<float>();
|
2023-10-21 23:07:33 +02:00
|
|
|
} else {
|
|
|
|
log::warn("Invalid data type loaded from settings for slider!");
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
nlohmann::json SliderFloat::store() {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_value;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
ColorPicker::ColorPicker(ImColor defaultColor) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = {
|
2023-10-21 23:07:33 +02:00
|
|
|
defaultColor.Value.x,
|
|
|
|
defaultColor.Value.y,
|
|
|
|
defaultColor.Value.z,
|
|
|
|
defaultColor.Value.w
|
|
|
|
};
|
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
bool ColorPicker::draw(const std::string &name) {
|
2023-12-19 13:10:25 +01:00
|
|
|
return ImGui::ColorEdit4(name.c_str(), m_value.data(), ImGuiColorEditFlags_NoInputs);
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
void ColorPicker::load(const nlohmann::json &data) {
|
|
|
|
if (data.is_number()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
const ImColor color(data.get<u32>());
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = { color.Value.x, color.Value.y, color.Value.z, color.Value.w };
|
2023-10-21 23:07:33 +02:00
|
|
|
} else {
|
|
|
|
log::warn("Invalid data type loaded from settings for color picker!");
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
nlohmann::json ColorPicker::store() {
|
2023-12-19 13:10:25 +01:00
|
|
|
const ImColor color(m_value[0], m_value[1], m_value[2], m_value[3]);
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
return static_cast<ImU32>(color);
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-09-12 13:59:23 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
ImColor ColorPicker::getColor() const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return { m_value[0], m_value[1], m_value[2], m_value[3] };
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
bool DropDown::draw(const std::string &name) {
|
2024-01-30 11:21:34 +01:00
|
|
|
auto preview = "";
|
2023-12-19 13:10:25 +01:00
|
|
|
if (static_cast<size_t>(m_value) < m_items.size())
|
|
|
|
preview = m_items[m_value].c_str();
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
bool changed = false;
|
2023-11-21 14:38:01 +01:00
|
|
|
if (ImGui::BeginCombo(name.c_str(), Lang(preview))) {
|
2021-09-12 13:59:23 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
int index = 0;
|
2023-12-19 13:10:25 +01:00
|
|
|
for (const auto &item : m_items) {
|
|
|
|
const bool selected = index == m_value;
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
if (ImGui::Selectable(Lang(item), selected)) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = index;
|
2023-10-21 23:07:33 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
if (selected)
|
|
|
|
ImGui::SetItemDefaultFocus();
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
index += 1;
|
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DropDown::load(const nlohmann::json &data) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = 0;
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-10-22 17:31:53 +02:00
|
|
|
int defaultItemIndex = 0;
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
int index = 0;
|
2023-12-19 13:10:25 +01:00
|
|
|
for (const auto &item : m_settingsValues) {
|
|
|
|
if (item == m_defaultItem)
|
2023-10-22 17:31:53 +02:00
|
|
|
defaultItemIndex = index;
|
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
if (item == data) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = index;
|
2023-10-22 17:31:53 +02:00
|
|
|
return;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-09-12 20:27:56 +02:00
|
|
|
|
2023-10-21 23:07:33 +02:00
|
|
|
index += 1;
|
|
|
|
}
|
2023-10-22 17:31:53 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = defaultItemIndex;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json DropDown::store() {
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_value == -1)
|
|
|
|
return m_defaultItem;
|
|
|
|
if (static_cast<size_t>(m_value) >= m_items.size())
|
|
|
|
return m_defaultItem;
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_settingsValues[m_value];
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const nlohmann::json& DropDown::getValue() const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_settingsValues[m_value];
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TextBox::draw(const std::string &name) {
|
2023-12-19 13:10:25 +01:00
|
|
|
return ImGui::InputText(name.c_str(), m_value);
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::load(const nlohmann::json &data) {
|
|
|
|
if (data.is_string()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = data.get<std::string>();
|
2023-10-21 23:07:33 +02:00
|
|
|
} else {
|
|
|
|
log::warn("Invalid data type loaded from settings for text box!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json TextBox::store() {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_value;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FilePicker::draw(const std::string &name) {
|
|
|
|
bool changed = false;
|
2023-12-19 13:10:25 +01:00
|
|
|
if (ImGui::InputText("##font_path", m_value)) {
|
2023-10-21 23:07:33 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
2024-01-28 22:14:59 +01:00
|
|
|
if (ImGuiExt::IconButton("...", ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
|
2023-10-21 23:07:33 +02:00
|
|
|
return fs::openFileBrowser(fs::DialogMode::Open, { { "TTF Font", "ttf" }, { "OTF Font", "otf" } },
|
|
|
|
[&](const std::fs::path &path) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = wolv::util::toUTF8String(path);
|
2023-10-21 23:07:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{}", name);
|
2023-10-21 23:07:33 +02:00
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilePicker::load(const nlohmann::json &data) {
|
|
|
|
if (data.is_string()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_value = data.get<std::string>();
|
2023-10-21 23:07:33 +02:00
|
|
|
} else {
|
|
|
|
log::warn("Invalid data type loaded from settings for file picker!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json FilePicker::store() {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_value;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
2021-09-12 13:59:23 +02:00
|
|
|
|
2023-12-09 12:14:45 +01:00
|
|
|
bool Label::draw(const std::string& name) {
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted(name.c_str());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-02-01 19:03:28 +01:00
|
|
|
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 21:11:03 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::CommandPaletteCommands {
|
2021-01-11 23:02:55 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void add(Type type, const std::string &command, const UnlocalizedString &unlocalizedDescription, const impl::DisplayCallback &displayCallback, const impl::ExecuteCallback &executeCallback) {
|
2022-07-30 11:26:51 +02:00
|
|
|
log::debug("Registered new command palette command: {}", command);
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
impl::getEntries().push_back(impl::Entry { type, command, unlocalizedDescription, displayCallback, executeCallback });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
void addHandler(Type type, const std::string &command, const impl::QueryCallback &queryCallback, const impl::DisplayCallback &displayCallback) {
|
2023-03-20 14:11:43 +01:00
|
|
|
log::debug("Registered new command palette command handler: {}", command);
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
impl::getHandlers().push_back(impl::Handler { type, command, queryCallback, displayCallback });
|
2023-03-20 14:11:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
2021-01-11 23:02:55 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
std::vector<Entry> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Entry>> commands;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return commands;
|
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
std::vector<Handler> &getHandlers() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Handler>> commands;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return commands;
|
|
|
|
}
|
2023-03-20 14:11:43 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-11 23:02:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 23:54:12 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::PatternLanguage {
|
2021-01-11 23:54:12 +01:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
static std::string getFunctionName(const pl::api::Namespace &ns, const std::string &name) {
|
2022-02-02 00:36:09 +01:00
|
|
|
std::string functionName;
|
|
|
|
for (auto &scope : ns)
|
|
|
|
functionName += scope + "::";
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
functionName += name;
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
return functionName;
|
|
|
|
}
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
pl::PatternLanguage& getRuntime() {
|
|
|
|
static PerProvider<pl::PatternLanguage> runtime;
|
|
|
|
|
|
|
|
return *runtime;
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:46:13 +02:00
|
|
|
std::mutex& getRuntimeLock() {
|
2023-04-17 16:18:48 +02:00
|
|
|
static std::mutex runtimeLock;
|
|
|
|
|
2023-05-12 15:46:13 +02:00
|
|
|
return runtimeLock;
|
2023-04-17 16:18:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 09:34:40 +02:00
|
|
|
void configureRuntime(pl::PatternLanguage &runtime, prv::Provider *provider) {
|
|
|
|
runtime.reset();
|
2021-01-11 23:54:12 +01:00
|
|
|
|
2022-08-10 09:26:48 +02:00
|
|
|
if (provider != nullptr) {
|
2022-12-16 11:20:39 +01:00
|
|
|
runtime.setDataSource(provider->getBaseAddress(), provider->getActualSize(),
|
|
|
|
[provider](u64 offset, u8 *buffer, size_t size) {
|
|
|
|
provider->read(offset, buffer, size);
|
|
|
|
},
|
|
|
|
[provider](u64 offset, const u8 *buffer, size_t size) {
|
|
|
|
if (provider->isWritable())
|
|
|
|
provider->write(offset, buffer, size);
|
|
|
|
}
|
|
|
|
);
|
2022-08-10 09:26:48 +02:00
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2022-09-15 09:34:40 +02:00
|
|
|
runtime.setIncludePaths(fs::getDefaultPaths(fs::ImHexPath::PatternsInclude) | fs::getDefaultPaths(fs::ImHexPath::Patterns));
|
2021-12-20 20:40:28 +01:00
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
for (const auto &[ns, name, paramCount, callback, dangerous] : impl::getFunctions()) {
|
|
|
|
if (dangerous)
|
|
|
|
runtime.addDangerousFunction(ns, name, paramCount, callback);
|
2022-04-17 16:57:30 +02:00
|
|
|
else
|
2023-11-10 14:48:26 +01:00
|
|
|
runtime.addFunction(ns, name, paramCount, callback);
|
2022-04-17 16:57:30 +02:00
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
for (const auto &[name, callback] : impl::getPragmas()) {
|
2022-09-15 09:34:40 +02:00
|
|
|
runtime.addPragma(name, callback);
|
2022-04-17 16:57:30 +02:00
|
|
|
}
|
2021-12-20 20:40:28 +01:00
|
|
|
|
2022-09-15 09:34:40 +02:00
|
|
|
runtime.addDefine("__IMHEX__");
|
2023-06-26 14:01:45 +02:00
|
|
|
runtime.addDefine("__IMHEX_VERSION__", ImHexApi::System::getImHexVersion());
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
void addPragma(const std::string &name, const pl::api::PragmaHandler &handler) {
|
2022-07-30 11:26:51 +02:00
|
|
|
log::debug("Registered new pattern language pragma: {}", name);
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getPragmas()[name] = handler;
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
void addFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func) {
|
2022-07-30 11:26:51 +02:00
|
|
|
log::debug("Registered new pattern language function: {}", getFunctionName(ns, name));
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getFunctions().push_back({
|
2022-04-17 16:57:30 +02:00
|
|
|
ns, name,
|
|
|
|
parameterCount, func,
|
|
|
|
false
|
|
|
|
});
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
void addDangerousFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func) {
|
2022-07-30 11:26:51 +02:00
|
|
|
log::debug("Registered new dangerous pattern language function: {}", getFunctionName(ns, name));
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getFunctions().push_back({
|
2022-04-17 16:57:30 +02:00
|
|
|
ns, name,
|
|
|
|
parameterCount, func,
|
|
|
|
true
|
|
|
|
});
|
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-01-20 21:16:28 +01:00
|
|
|
|
2023-10-31 10:56:20 +01:00
|
|
|
void addVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, pl::api::FunctionParameterCount parameterCount) {
|
2023-01-20 21:16:28 +01:00
|
|
|
log::debug("Registered new pattern visualizer function: {}", name);
|
|
|
|
impl::getVisualizers()[name] = impl::Visualizer { parameterCount, function };
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:56:20 +01:00
|
|
|
void addInlineVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, pl::api::FunctionParameterCount parameterCount) {
|
2023-07-04 22:18:06 +02:00
|
|
|
log::debug("Registered new inline pattern visualizer function: {}", name);
|
|
|
|
impl::getInlineVisualizers()[name] = impl::Visualizer { parameterCount, function };
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::map<std::string, Visualizer> &getVisualizers() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, Visualizer>> visualizers;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return visualizers;
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::map<std::string, Visualizer> &getInlineVisualizers() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, Visualizer>> visualizers;
|
2023-07-04 22:18:06 +02:00
|
|
|
|
|
|
|
return visualizers;
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
std::map<std::string, pl::api::PragmaHandler> &getPragmas() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, pl::api::PragmaHandler>> pragmas;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return pragmas;
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<FunctionDefinition> &getFunctions() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<FunctionDefinition>> functions;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return functions;
|
|
|
|
}
|
2022-04-17 16:57:30 +02:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2021-01-11 23:54:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::Views {
|
2021-01-12 16:50:15 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
2023-04-08 12:08:45 +02:00
|
|
|
std::map<std::string, std::unique_ptr<View>> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, std::unique_ptr<View>>> views;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return views;
|
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-01-12 16:50:15 +01:00
|
|
|
|
2023-04-08 12:08:45 +02:00
|
|
|
void impl::add(std::unique_ptr<View> &&view) {
|
2023-12-19 12:22:28 +01:00
|
|
|
log::debug("Registered new view: {}", view->getUnlocalizedName().get());
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
getEntries().insert({ view->getUnlocalizedName(), std::move(view) });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-01-12 16:50:15 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
View* getViewByName(const UnlocalizedString &unlocalizedName) {
|
2023-03-21 15:33:43 +01:00
|
|
|
auto &views = impl::getEntries();
|
2022-02-02 00:36:09 +01:00
|
|
|
|
|
|
|
if (views.contains(unlocalizedName))
|
2023-04-08 12:08:45 +02:00
|
|
|
return views[unlocalizedName].get();
|
2022-02-02 00:36:09 +01:00
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-01-18 00:10:10 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::Tools {
|
2021-01-12 16:50:15 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void add(const UnlocalizedString &unlocalizedName, const impl::Callback &function) {
|
|
|
|
log::debug("Registered new tool: {}", unlocalizedName.get());
|
2021-01-12 16:50:15 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getEntries().emplace_back(impl::Entry { unlocalizedName, function, false });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
std::vector<Entry> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Entry>> tools;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return tools;
|
|
|
|
}
|
2021-01-12 16:50:15 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::DataInspector {
|
2021-01-13 01:24:27 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void add(const UnlocalizedString &unlocalizedName, size_t requiredSize, impl::GeneratorFunction displayGeneratorFunction, std::optional<impl::EditingFunction> editingFunction) {
|
|
|
|
log::debug("Registered new data inspector format: {}", unlocalizedName.get());
|
2021-01-13 01:24:27 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getEntries().push_back({ unlocalizedName, requiredSize, requiredSize, std::move(displayGeneratorFunction), std::move(editingFunction) });
|
2022-08-01 13:20:20 +02:00
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void add(const UnlocalizedString &unlocalizedName, size_t requiredSize, size_t maxSize, impl::GeneratorFunction displayGeneratorFunction, std::optional<impl::EditingFunction> editingFunction) {
|
|
|
|
log::debug("Registered new data inspector format: {}", unlocalizedName.get());
|
2022-08-01 13:20:20 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getEntries().push_back({ unlocalizedName, requiredSize, maxSize, std::move(displayGeneratorFunction), std::move(editingFunction) });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<Entry> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Entry>> entries;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
2021-01-13 01:24:27 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2021-01-13 01:24:27 +01:00
|
|
|
}
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::DataProcessorNode {
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
void impl::add(const Entry &entry) {
|
2023-12-19 12:22:28 +01:00
|
|
|
log::debug("Registered new data processor node type: [{}]: {}", entry.unlocalizedCategory.get(), entry.unlocalizedName.get());
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
getEntries().push_back(entry);
|
|
|
|
}
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
void addSeparator() {
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getEntries().push_back({ "", "", [] { return nullptr; } });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<Entry> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Entry>> nodes;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}
|
2022-02-02 00:36:09 +01:00
|
|
|
|
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
2021-02-11 00:35:30 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::Language {
|
2021-02-11 00:35:30 +01:00
|
|
|
|
2022-12-02 12:00:04 +01:00
|
|
|
void addLocalization(const nlohmann::json &data) {
|
2023-02-02 10:08:47 +01:00
|
|
|
if (!data.is_object())
|
|
|
|
return;
|
|
|
|
|
2022-12-02 12:00:04 +01:00
|
|
|
if (!data.contains("code") || !data.contains("country") || !data.contains("language") || !data.contains("translations")) {
|
|
|
|
log::error("Localization data is missing required fields!");
|
|
|
|
return;
|
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2022-12-02 12:00:04 +01:00
|
|
|
const auto &code = data["code"];
|
|
|
|
const auto &country = data["country"];
|
|
|
|
const auto &language = data["language"];
|
|
|
|
const auto &translations = data["translations"];
|
|
|
|
|
|
|
|
if (!code.is_string() || !country.is_string() || !language.is_string() || !translations.is_object()) {
|
|
|
|
log::error("Localization data has invalid fields!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.contains("fallback")) {
|
|
|
|
const auto &fallback = data["fallback"];
|
2021-02-11 00:35:30 +01:00
|
|
|
|
2022-12-02 12:00:04 +01:00
|
|
|
if (fallback.is_boolean() && fallback.get<bool>())
|
2023-11-21 14:38:01 +01:00
|
|
|
LocalizationManager::impl::setFallbackLanguage(code.get<std::string>());
|
2022-12-02 12:00:04 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getLanguages().insert({ code.get<std::string>(), hex::format("{} ({})", language.get<std::string>(), country.get<std::string>()) });
|
2022-12-02 12:00:04 +01:00
|
|
|
|
|
|
|
std::map<std::string, std::string> translationDefinitions;
|
|
|
|
for (auto &[key, value] : translations.items()) {
|
|
|
|
if (!value.is_string()) {
|
|
|
|
log::error("Localization data has invalid fields!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
translationDefinitions[key] = value.get<std::string>();
|
|
|
|
}
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getLanguageDefinitions()[code.get<std::string>()].emplace_back(std::move(translationDefinitions));
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-02-11 00:35:30 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
std::map<std::string, std::string> &getLanguages() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, std::string>> languages;
|
2021-02-11 00:35:30 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return languages;
|
|
|
|
}
|
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>> &getLanguageDefinitions() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>>> definitions;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return definitions;
|
|
|
|
}
|
2022-02-02 00:36:09 +01:00
|
|
|
|
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
}
|
2021-02-18 12:09:19 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::Interface {
|
2021-02-18 12:09:19 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void registerMainMenuItem(const UnlocalizedString &unlocalizedName, u32 priority) {
|
|
|
|
log::debug("Registered new main menu item: {}", unlocalizedName.get());
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getMainMenuItems().insert({ priority, { unlocalizedName } });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-01-18 00:10:10 +01:00
|
|
|
|
2024-01-15 20:52:08 +01:00
|
|
|
void addMenuItem(const std::vector<UnlocalizedString> &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, const impl::SelectedCallback &selectedCallback, View *view) {
|
2024-01-21 18:39:32 +01:00
|
|
|
addMenuItem(unlocalizedMainMenuNames, "", priority, shortcut, function, enabledCallback, selectedCallback, view);
|
2024-01-08 21:51:48 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 18:39:32 +01:00
|
|
|
void addMenuItem(const std::vector<UnlocalizedString> &unlocalizedMainMenuNames, const Icon &icon, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view) {
|
2024-01-15 20:52:08 +01:00
|
|
|
addMenuItem(unlocalizedMainMenuNames, icon, priority, shortcut, function, enabledCallback, []{ return false; }, view);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addMenuItem(const std::vector<UnlocalizedString> &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view) {
|
2024-01-21 18:39:32 +01:00
|
|
|
addMenuItem(unlocalizedMainMenuNames, "", priority, shortcut, function, enabledCallback, []{ return false; }, view);
|
2024-01-15 20:52:08 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 18:39:32 +01:00
|
|
|
void addMenuItem(const std::vector<UnlocalizedString> &unlocalizedMainMenuNames, const Icon &icon, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, const impl::SelectedCallback &selectedCallback, View *view) {
|
2023-12-19 12:22:28 +01:00
|
|
|
log::debug("Added new menu item to menu {} with priority {}", unlocalizedMainMenuNames[0].get(), priority);
|
2022-01-18 00:10:10 +01:00
|
|
|
|
2024-01-21 18:39:32 +01:00
|
|
|
Icon coloredIcon = icon;
|
|
|
|
if (coloredIcon.color == 0x00)
|
|
|
|
coloredIcon.color = ImGuiCustomCol_ToolbarGray;
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getMenuItems().insert({
|
2024-01-21 18:39:32 +01:00
|
|
|
priority, impl::MenuItem { unlocalizedMainMenuNames, coloredIcon, std::make_unique<Shortcut>(shortcut), view, function, enabledCallback, selectedCallback, -1 }
|
2023-03-20 13:11:43 +01:00
|
|
|
});
|
|
|
|
|
2023-11-17 14:46:21 +01:00
|
|
|
if (shortcut != Shortcut::None) {
|
|
|
|
if (shortcut.isLocal() && view != nullptr)
|
|
|
|
ShortcutManager::addShortcut(view, shortcut, unlocalizedMainMenuNames.back(), function);
|
|
|
|
else
|
|
|
|
ShortcutManager::addGlobalShortcut(shortcut, unlocalizedMainMenuNames.back(), function);
|
|
|
|
}
|
2023-03-20 13:11:43 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void addMenuItemSubMenu(std::vector<UnlocalizedString> unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback) {
|
2024-01-21 18:39:32 +01:00
|
|
|
addMenuItemSubMenu(std::move(unlocalizedMainMenuNames), "", priority, function, enabledCallback);
|
2024-01-08 21:51:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void addMenuItemSubMenu(std::vector<UnlocalizedString> unlocalizedMainMenuNames, const char *icon, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback) {
|
2023-12-19 12:22:28 +01:00
|
|
|
log::debug("Added new menu item sub menu to menu {} with priority {}", unlocalizedMainMenuNames[0].get(), priority);
|
2023-03-20 13:11:43 +01:00
|
|
|
|
|
|
|
unlocalizedMainMenuNames.emplace_back(impl::SubMenuValue);
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getMenuItems().insert({
|
2024-01-21 18:39:32 +01:00
|
|
|
priority, impl::MenuItem { unlocalizedMainMenuNames, icon, std::make_unique<Shortcut>(), nullptr, function, enabledCallback, []{ return false; }, -1 }
|
2023-03-20 13:11:43 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void addMenuItemSeparator(std::vector<UnlocalizedString> unlocalizedMainMenuNames, u32 priority) {
|
2023-03-20 13:11:43 +01:00
|
|
|
unlocalizedMainMenuNames.emplace_back(impl::SeparatorValue);
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getMenuItems().insert({
|
2024-01-21 18:39:32 +01:00
|
|
|
priority, impl::MenuItem { unlocalizedMainMenuNames, "", std::make_unique<Shortcut>(), nullptr, []{}, []{ return true; }, []{ return false; }, -1 }
|
2022-02-02 00:36:09 +01:00
|
|
|
});
|
|
|
|
}
|
2022-01-23 02:28:38 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
void addWelcomeScreenEntry(const impl::DrawCallback &function) {
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getWelcomeScreenEntries().push_back(function);
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-01-23 02:28:38 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
void addFooterItem(const impl::DrawCallback &function) {
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getFooterItems().push_back(function);
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-02-18 12:09:19 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
void addToolbarItem(const impl::DrawCallback &function) {
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getToolbarItems().push_back(function);
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-02-18 12:09:19 +01:00
|
|
|
|
2024-01-21 18:39:32 +01:00
|
|
|
void addMenuItemToToolbar(const UnlocalizedString& unlocalizedName, ImGuiCustomCol color) {
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto maxIndex = std::ranges::max_element(impl::getMenuItems(), [](const auto &a, const auto &b) {
|
2024-01-21 18:39:32 +01:00
|
|
|
return a.second.toolbarIndex < b.second.toolbarIndex;
|
|
|
|
})->second.toolbarIndex;
|
|
|
|
|
|
|
|
for (auto &[priority, menuItem] : impl::getMenuItems()) {
|
|
|
|
if (menuItem.unlocalizedNames.back() == unlocalizedName) {
|
|
|
|
menuItem.toolbarIndex = maxIndex + 1;
|
|
|
|
menuItem.icon.color = color;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-14 15:55:25 +01:00
|
|
|
void addSidebarItem(const std::string &icon, const impl::DrawCallback &function, const impl::EnabledCallback &enabledCallback) {
|
|
|
|
impl::getSidebarItems().push_back({ icon, function, enabledCallback });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2021-08-21 00:52:11 +02:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void addTitleBarButton(const std::string &icon, const UnlocalizedString &unlocalizedTooltip, const impl::ClickCallback &function) {
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getTitleBarButtons().push_back({ icon, unlocalizedTooltip, function });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-01-22 22:03:19 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
2022-01-18 00:10:10 +01:00
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::multimap<u32, MainMenuItem> &getMainMenuItems() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::multimap<u32, MainMenuItem>> items;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return items;
|
|
|
|
}
|
2023-11-10 14:48:26 +01:00
|
|
|
std::multimap<u32, MenuItem> &getMenuItems() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::multimap<u32, MenuItem>> items;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return items;
|
|
|
|
}
|
2022-01-18 00:10:10 +01:00
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<DrawCallback> &getWelcomeScreenEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<DrawCallback>> entries;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return entries;
|
|
|
|
}
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<DrawCallback> &getFooterItems() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<DrawCallback>> items;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return items;
|
|
|
|
}
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<DrawCallback> &getToolbarItems() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<DrawCallback>> items;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return items;
|
|
|
|
}
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<SidebarItem> &getSidebarItems() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<SidebarItem>> items;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return items;
|
|
|
|
}
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<TitleBarButton> &getTitleBarButtons() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<TitleBarButton>> buttons;
|
2021-12-07 22:47:41 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return buttons;
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-01-18 00:10:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::Provider {
|
2021-12-07 22:47:41 +01:00
|
|
|
|
2023-11-18 14:34:33 +01:00
|
|
|
namespace impl {
|
2021-12-07 22:47:41 +01:00
|
|
|
|
2023-11-18 14:34:33 +01:00
|
|
|
void add(const std::string &typeName, ProviderCreationFunction creationFunction) {
|
2023-12-08 10:29:44 +01:00
|
|
|
(void)RequestCreateProvider::subscribe([expectedName = typeName, creationFunction](const std::string &name, bool skipLoadInterface, bool selectProvider, prv::Provider **provider) {
|
2023-11-18 14:34:33 +01:00
|
|
|
if (name != expectedName) return;
|
2022-01-13 14:34:27 +01:00
|
|
|
|
2023-11-18 14:34:33 +01:00
|
|
|
prv::Provider *newProvider = creationFunction();
|
2021-12-07 22:47:41 +01:00
|
|
|
|
2023-11-18 14:34:33 +01:00
|
|
|
ImHexApi::Provider::add(newProvider, skipLoadInterface, selectProvider);
|
|
|
|
|
|
|
|
if (provider != nullptr)
|
|
|
|
*provider = newProvider;
|
|
|
|
});
|
|
|
|
}
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
std::vector<std::string> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<std::string>> providerNames;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return providerNames;
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void addProviderName(const UnlocalizedString &unlocalizedName) {
|
|
|
|
log::debug("Registered new provider: {}", unlocalizedName.get());
|
2023-11-18 14:34:33 +01:00
|
|
|
|
|
|
|
getEntries().push_back(unlocalizedName);
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
}
|
2022-01-13 00:27:31 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::DataFormatter {
|
2022-01-13 00:27:31 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void add(const UnlocalizedString &unlocalizedName, const impl::Callback &callback) {
|
|
|
|
log::debug("Registered new data formatter: {}", unlocalizedName.get());
|
2022-01-13 00:27:31 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getEntries().push_back({ unlocalizedName, callback });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-01-13 14:34:19 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<Entry> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Entry>> entries;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
2022-01-13 00:27:31 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-01-13 00:27:31 +01:00
|
|
|
}
|
2022-01-13 14:34:19 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
namespace ContentRegistry::FileHandler {
|
2022-01-13 14:34:19 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
void add(const std::vector<std::string> &extensions, const impl::Callback &callback) {
|
|
|
|
for (const auto &extension : extensions)
|
2022-07-30 11:26:51 +02:00
|
|
|
log::debug("Registered new data handler for extensions: {}", extension);
|
2022-01-13 14:34:19 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
impl::getEntries().push_back({ extensions, callback });
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-01-13 14:34:19 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
std::vector<Entry> &getEntries() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Entry>> entries;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
2022-01-13 14:34:19 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-01-13 14:34:19 +01:00
|
|
|
}
|
2022-02-02 00:36:09 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
namespace ContentRegistry::HexEditor {
|
|
|
|
|
2022-06-03 10:53:03 +02:00
|
|
|
const int DataVisualizer::TextInputFlags = ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoHorizontalScroll;
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-02-18 22:20:02 +01:00
|
|
|
bool DataVisualizer::drawDefaultScalarEditingTextBox(u64 address, const char *format, ImGuiDataType dataType, u8 *data, ImGuiInputTextFlags flags) const {
|
2022-05-27 20:42:07 +02:00
|
|
|
struct UserData {
|
|
|
|
u8 *data;
|
|
|
|
i32 maxChars;
|
|
|
|
|
|
|
|
bool editingDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
UserData userData = {
|
|
|
|
.data = data,
|
|
|
|
.maxChars = this->getMaxCharsPerCell(),
|
|
|
|
|
|
|
|
.editingDone = false
|
|
|
|
};
|
|
|
|
|
|
|
|
ImGui::PushID(reinterpret_cast<void*>(address));
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InputScalarCallback("##editing_input", dataType, data, format, flags | TextInputFlags | ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) -> int {
|
2023-11-10 14:48:26 +01:00
|
|
|
auto &userData = *static_cast<UserData*>(data->UserData);
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
if (data->BufTextLen >= userData.maxChars)
|
|
|
|
userData.editingDone = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}, &userData);
|
|
|
|
ImGui::PopID();
|
|
|
|
|
2022-10-12 10:56:03 +02:00
|
|
|
return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Escape);
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-18 22:20:02 +01:00
|
|
|
bool DataVisualizer::drawDefaultTextEditingTextBox(u64 address, std::string &data, ImGuiInputTextFlags flags) const {
|
|
|
|
struct UserData {
|
|
|
|
std::string *data;
|
|
|
|
i32 maxChars;
|
|
|
|
|
|
|
|
bool editingDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
UserData userData = {
|
|
|
|
.data = &data,
|
|
|
|
.maxChars = this->getMaxCharsPerCell(),
|
|
|
|
|
|
|
|
.editingDone = false
|
|
|
|
};
|
|
|
|
|
|
|
|
ImGui::PushID(reinterpret_cast<void*>(address));
|
|
|
|
ImGui::InputText("##editing_input", data.data(), data.size() + 1, flags | TextInputFlags | ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) -> int {
|
2023-11-10 14:48:26 +01:00
|
|
|
auto &userData = *static_cast<UserData*>(data->UserData);
|
2023-02-18 22:20:02 +01:00
|
|
|
|
|
|
|
userData.data->resize(data->BufSize);
|
|
|
|
|
|
|
|
if (data->BufTextLen >= userData.maxChars)
|
|
|
|
userData.editingDone = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}, &userData);
|
|
|
|
ImGui::PopID();
|
|
|
|
|
|
|
|
return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Escape);
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-06-21 17:48:51 +02:00
|
|
|
void addDataVisualizer(std::shared_ptr<DataVisualizer> &&visualizer) {
|
|
|
|
getVisualizers().emplace_back(std::move(visualizer));
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:48:51 +02:00
|
|
|
std::vector<std::shared_ptr<DataVisualizer>> &getVisualizers() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<std::shared_ptr<DataVisualizer>>> visualizers;
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
return visualizers;
|
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2024-01-28 15:28:55 +01:00
|
|
|
std::vector<std::shared_ptr<MiniMapVisualizer>> &getMiniMapVisualizers() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<std::shared_ptr<MiniMapVisualizer>>> visualizers;
|
2024-01-28 15:28:55 +01:00
|
|
|
|
|
|
|
return visualizers;
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
std::shared_ptr<DataVisualizer> getVisualizerByName(const UnlocalizedString &unlocalizedName) {
|
2023-06-21 17:48:51 +02:00
|
|
|
for (const auto &visualizer : impl::getVisualizers()) {
|
|
|
|
if (visualizer->getUnlocalizedName() == unlocalizedName)
|
|
|
|
return visualizer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-01-28 15:28:55 +01:00
|
|
|
void addMiniMapVisualizer(UnlocalizedString unlocalizedName, MiniMapVisualizer::Callback callback) {
|
|
|
|
impl::getMiniMapVisualizers().emplace_back(std::make_shared<MiniMapVisualizer>(std::move(unlocalizedName), std::move(callback)));
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ContentRegistry::Diffing {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<Algorithm>>& getAlgorithms() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<std::unique_ptr<Algorithm>>> algorithms;
|
2024-01-21 18:39:13 +01:00
|
|
|
|
|
|
|
return algorithms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addAlgorithm(std::unique_ptr<Algorithm> &&hash) {
|
|
|
|
getAlgorithms().emplace_back(std::move(hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-21 15:33:43 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 16:36:46 +02:00
|
|
|
namespace ContentRegistry::Hashes {
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
2022-05-30 16:36:46 +02:00
|
|
|
|
2023-06-11 10:47:17 +02:00
|
|
|
std::vector<std::unique_ptr<Hash>> &getHashes() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<std::unique_ptr<Hash>>> hashes;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
return hashes;
|
|
|
|
}
|
|
|
|
|
2023-06-11 10:47:17 +02:00
|
|
|
void add(std::unique_ptr<Hash> &&hash) {
|
|
|
|
getHashes().emplace_back(std::move(hash));
|
2023-03-21 15:33:43 +01:00
|
|
|
}
|
2022-05-30 16:36:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:30:24 +02:00
|
|
|
namespace ContentRegistry::BackgroundServices {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
class Service {
|
|
|
|
public:
|
|
|
|
Service(std::string name, std::jthread thread) : m_name(std::move(name)), m_thread(std::move(thread)) { }
|
|
|
|
Service(const Service&) = delete;
|
|
|
|
Service(Service &&) = default;
|
|
|
|
~Service() {
|
|
|
|
m_thread.request_stop();
|
|
|
|
if (m_thread.joinable())
|
|
|
|
m_thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
Service& operator=(const Service&) = delete;
|
|
|
|
Service& operator=(Service &&) = default;
|
|
|
|
|
|
|
|
[[nodiscard]] const std::string &getName() const {
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const std::jthread &getThread() const {
|
|
|
|
return m_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_name;
|
|
|
|
std::jthread m_thread;
|
2023-12-22 23:39:38 +01:00
|
|
|
};
|
|
|
|
|
2023-05-15 11:30:24 +02:00
|
|
|
std::vector<Service> &getServices() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<Service>> services;
|
2023-05-15 11:30:24 +02:00
|
|
|
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopServices() {
|
2023-12-22 23:39:38 +01:00
|
|
|
auto &services = getServices();
|
|
|
|
services.clear();
|
2023-05-15 11:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void registerService(const UnlocalizedString &unlocalizedName, const impl::Callback &callback) {
|
|
|
|
log::debug("Registered new background service: {}", unlocalizedName.get());
|
2023-05-15 11:30:24 +02:00
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
impl::getServices().emplace_back(
|
2023-05-15 11:30:24 +02:00
|
|
|
unlocalizedName,
|
2023-10-30 16:39:57 +01:00
|
|
|
std::jthread([callback = auto(callback)](const std::stop_token &stopToken){
|
2023-05-15 11:30:24 +02:00
|
|
|
while (!stopToken.stop_requested()) {
|
|
|
|
callback();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
}
|
|
|
|
})
|
2024-01-30 11:21:34 +01:00
|
|
|
);
|
2023-05-15 11:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ContentRegistry::CommunicationInterface {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
std::map<std::string, NetworkCallback> &getNetworkEndpoints() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, NetworkCallback>> endpoints;
|
2023-05-15 11:30:24 +02:00
|
|
|
|
|
|
|
return endpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerNetworkEndpoint(const std::string &endpoint, const impl::NetworkCallback &callback) {
|
|
|
|
log::debug("Registered new network endpoint: {}", endpoint);
|
|
|
|
|
|
|
|
impl::getNetworkEndpoints().insert({ endpoint, callback });
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:48:26 +01:00
|
|
|
namespace ContentRegistry::Experiments {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
std::map<std::string, Experiment> &getExperiments() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::map<std::string, Experiment>> experiments;
|
2023-11-10 14:48:26 +01:00
|
|
|
|
|
|
|
return experiments;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void addExperiment(const std::string &experimentName, const UnlocalizedString &unlocalizedName, const UnlocalizedString &unlocalizedDescription) {
|
2023-11-10 14:48:26 +01:00
|
|
|
auto &experiments = impl::getExperiments();
|
|
|
|
|
|
|
|
if (experiments.contains(experimentName)) {
|
|
|
|
log::error("Experiment with name '{}' already exists!", experimentName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
experiments[experimentName] = impl::Experiment {
|
|
|
|
.unlocalizedName = unlocalizedName,
|
|
|
|
.unlocalizedDescription = unlocalizedDescription,
|
|
|
|
.enabled = false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void enableExperiement(const std::string &experimentName, bool enabled) {
|
|
|
|
auto &experiments = impl::getExperiments();
|
|
|
|
|
|
|
|
if (!experiments.contains(experimentName)) {
|
|
|
|
log::error("Experiment with name '{}' does not exist!", experimentName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
experiments[experimentName].enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool isExperimentEnabled(const std::string &experimentName) {
|
|
|
|
auto &experiments = impl::getExperiments();
|
|
|
|
|
|
|
|
if (!experiments.contains(experimentName)) {
|
|
|
|
log::error("Experiment with name '{}' does not exist!", experimentName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return experiments[experimentName].enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-22 08:26:31 +01:00
|
|
|
namespace ContentRegistry::Reports {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
std::vector<ReportGenerator> &getGenerators() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::vector<ReportGenerator>> generators;
|
2023-11-22 08:26:31 +01:00
|
|
|
|
|
|
|
return generators;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void addReportProvider(impl::Callback callback) {
|
|
|
|
impl::getGenerators().push_back(impl::ReportGenerator { std::move(callback ) });
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:34:54 +01:00
|
|
|
}
|