2021-08-29 14:18:45 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2024-01-21 21:29:03 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/localization_manager.hpp>
|
2023-11-30 14:40:07 +01:00
|
|
|
#include <hex/api/shortcut_manager.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/helpers/fmt.hpp>
|
2024-01-21 21:29:03 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2023-12-01 14:07:10 +01:00
|
|
|
#include <wolv/math_eval/math_evaluator.hpp>
|
2024-01-21 21:29:03 +01:00
|
|
|
#include <wolv/utils/guards.hpp>
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
2023-12-07 16:15:00 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Value {
|
|
|
|
public:
|
|
|
|
enum class Unit {
|
|
|
|
Unitless,
|
|
|
|
Decimal,
|
|
|
|
Hexadecimal,
|
|
|
|
Binary,
|
|
|
|
Octal,
|
|
|
|
Bits,
|
|
|
|
Bytes,
|
|
|
|
Invalid
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit Value(std::string value) {
|
|
|
|
if (!value.starts_with("0x") && !value.starts_with("0b")) {
|
|
|
|
auto index = value.find_first_not_of("0123456789.,");
|
|
|
|
if (index == std::string::npos) {
|
|
|
|
m_unit = Unit::Unitless;
|
|
|
|
} else {
|
|
|
|
std::tie(m_unit, m_multiplier) = parseUnit(value.substr(index));
|
|
|
|
value = value.substr(0, index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_unit = Unit::Unitless;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!value.contains('.')) {
|
|
|
|
m_value = i128(std::stoull(value, nullptr, 0) * static_cast<long double>(m_multiplier));
|
|
|
|
} else {
|
|
|
|
m_value = std::stod(value) * m_multiplier;
|
|
|
|
}
|
2023-12-27 16:33:49 +01:00
|
|
|
} catch (const std::exception &) {
|
2024-01-21 21:39:50 +01:00
|
|
|
m_value = i128(0);
|
2023-12-07 16:15:00 +01:00
|
|
|
m_unit = Unit::Invalid;
|
|
|
|
m_unitString.clear();
|
|
|
|
m_multiplier = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string formatAs(Value other) {
|
|
|
|
return std::visit([&, this]<typename T>(T value) -> std::string {
|
|
|
|
|
|
|
|
auto unit = other.getUnit();
|
|
|
|
auto multipler = other.getMultiplier();
|
|
|
|
|
|
|
|
bool isInteger = std::integral<T> && multipler == 1;
|
|
|
|
switch (m_unit) {
|
|
|
|
case Unit::Invalid:
|
|
|
|
if (this->getUnitString() != other.getUnitString())
|
|
|
|
return "hex.builtin.command.convert.invalid_conversion"_lang;
|
|
|
|
unit = Unit::Decimal;
|
|
|
|
[[fallthrough]];
|
|
|
|
case Unit::Unitless: {
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case Unit::Unitless:
|
|
|
|
case Unit::Decimal:
|
|
|
|
if (isInteger)
|
|
|
|
return hex::format("{0}", value / multipler);
|
|
|
|
else
|
|
|
|
return hex::format("{0:.3f}", value / multipler);
|
|
|
|
case Unit::Hexadecimal:
|
|
|
|
return hex::format("0x{0:x}", u128(value / multipler));
|
|
|
|
case Unit::Binary:
|
|
|
|
return hex::format("0b{0:b}", u128(value / multipler));
|
|
|
|
case Unit::Octal:
|
|
|
|
return hex::format("0o{0:o}", u128(value / multipler));
|
|
|
|
case Unit::Bytes:
|
|
|
|
return hex::format("{0}", u128(value / multipler));
|
|
|
|
default:
|
|
|
|
return "hex.builtin.command.convert.invalid_conversion"_lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Unit::Bits: {
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case Unit::Bits:
|
|
|
|
case Unit::Decimal:
|
|
|
|
if (isInteger)
|
|
|
|
return hex::format("{0}", value / multipler);
|
|
|
|
else
|
|
|
|
return hex::format("{0:.3f}", value / multipler);
|
|
|
|
case Unit::Hexadecimal:
|
|
|
|
return hex::format("0x{0:x}", u128(value / multipler));
|
|
|
|
case Unit::Binary:
|
|
|
|
return hex::format("0b{0:b}", u128(value / multipler));
|
|
|
|
case Unit::Octal:
|
|
|
|
return hex::format("0o{0:o}", u128(value / multipler));
|
|
|
|
case Unit::Bytes:
|
|
|
|
return hex::format("{0}", u128((value / multipler) / 8));
|
|
|
|
default:
|
|
|
|
return "hex.builtin.command.convert.invalid_conversion"_lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Unit::Bytes: {
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case Unit::Bytes:
|
|
|
|
case Unit::Decimal:
|
|
|
|
if (isInteger)
|
|
|
|
return hex::format("{0}", value / multipler);
|
|
|
|
else
|
|
|
|
return hex::format("{0:.3f}", value / multipler);
|
|
|
|
case Unit::Hexadecimal:
|
|
|
|
return hex::format("0x{0:x}", u128(value / multipler));
|
|
|
|
case Unit::Binary:
|
|
|
|
return hex::format("0b{0:b}", u128(value / multipler));
|
|
|
|
case Unit::Octal:
|
|
|
|
return hex::format("0o{0:o}", u128(value / multipler));
|
|
|
|
case Unit::Bits:
|
|
|
|
return hex::format("{0}", u128((value / multipler) * 8));
|
|
|
|
default:
|
|
|
|
return "hex.builtin.command.convert.invalid_conversion"_lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "hex.builtin.command.convert.invalid_input"_lang;
|
|
|
|
}
|
|
|
|
}, m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Unit getUnit() const { return m_unit; }
|
|
|
|
[[nodiscard]] double getMultiplier() const { return m_multiplier; }
|
|
|
|
[[nodiscard]] const std::string& getUnitString() const { return m_unitString; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::pair<Unit, double> parseUnit(std::string unitString, bool parseMultiplier = true) {
|
|
|
|
auto unitStringCopy = unitString;
|
|
|
|
unitString = wolv::util::trim(unitString);
|
|
|
|
|
|
|
|
double multiplier = 1;
|
|
|
|
if (parseMultiplier && !unitString.starts_with("dec") && !unitString.starts_with("hex") && !unitString.starts_with("bin") && !unitString.starts_with("oct")) {
|
|
|
|
if (unitString.starts_with("Ki")) { multiplier = 1024ULL; unitString = unitString.substr(2); }
|
|
|
|
else if (unitString.starts_with("Mi")) { multiplier = 1024ULL * 1024ULL; unitString = unitString.substr(2); }
|
|
|
|
else if (unitString.starts_with("Gi")) { multiplier = 1024ULL * 1024ULL * 1024ULL; unitString = unitString.substr(2); }
|
|
|
|
else if (unitString.starts_with("Ti")) { multiplier = 1024ULL * 1024ULL * 1024ULL * 1024ULL; unitString = unitString.substr(2); }
|
|
|
|
else if (unitString.starts_with("Pi")) { multiplier = 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL; unitString = unitString.substr(2); }
|
|
|
|
else if (unitString.starts_with("k")) { multiplier = 1E3; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("M")) { multiplier = 1E6; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("G")) { multiplier = 1E9; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("T")) { multiplier = 1E12; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("P")) { multiplier = 1E15; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("E")) { multiplier = 1E18; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("Z")) { multiplier = 1E21; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("Y")) { multiplier = 1E24; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("d")) { multiplier = 1E-1; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("c")) { multiplier = 1E-2; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("m")) { multiplier = 1E-3; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("u")) { multiplier = 1E-6; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("n")) { multiplier = 1E-9; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("p")) { multiplier = 1E-12; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("f")) { multiplier = 1E-15; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("a")) { multiplier = 1E-18; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("z")) { multiplier = 1E-21; unitString = unitString.substr(1); }
|
|
|
|
else if (unitString.starts_with("y")) { multiplier = 1E-24; unitString = unitString.substr(1); }
|
2023-12-27 16:33:49 +01:00
|
|
|
else { return parseUnit(unitString, false); }
|
2023-12-07 16:15:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unitString = wolv::util::trim(unitString);
|
2023-12-19 13:10:25 +01:00
|
|
|
m_unitString = unitString;
|
2023-12-07 16:15:00 +01:00
|
|
|
if (unitString.empty()) {
|
2023-12-27 16:33:49 +01:00
|
|
|
if (multiplier == 1) {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Unitless, 1 };
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_unitString = unitStringCopy;
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Unitless, 1 };
|
|
|
|
}
|
|
|
|
|
2023-12-27 16:33:49 +01:00
|
|
|
} else if (unitString == "bit" || unitString == "bits" || unitString == "b") {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Bits, multiplier };
|
2023-12-27 16:33:49 +01:00
|
|
|
} else if (unitString == "byte" || unitString == "bytes" || unitString == "B") {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Bytes, multiplier };
|
2023-12-27 16:33:49 +01:00
|
|
|
} else if (unitString == "hex" || unitString == "hex.builtin.command.convert.hexadecimal"_lang.get()) {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Hexadecimal, multiplier };
|
2023-12-27 16:33:49 +01:00
|
|
|
} else if (unitString == "bin" || unitString == "hex.builtin.command.convert.binary"_lang.get()) {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Binary, multiplier };
|
2023-12-27 16:33:49 +01:00
|
|
|
} else if (unitString == "oct" || unitString == "hex.builtin.command.convert.octal"_lang.get()) {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Octal, multiplier };
|
2023-12-27 16:33:49 +01:00
|
|
|
} else if (unitString == "dec" || unitString == "hex.builtin.command.convert.decimal"_lang.get()) {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Decimal, multiplier };
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-12-07 16:15:00 +01:00
|
|
|
return { Unit::Invalid, multiplier };
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2023-12-07 16:15:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Unit m_unit;
|
|
|
|
std::string m_unitString;
|
|
|
|
double m_multiplier = 1;
|
|
|
|
std::variant<i128, double> m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> splitConversionCommandInput(const std::string &input) {
|
2023-12-15 08:11:51 +01:00
|
|
|
std::vector<std::string> parts = wolv::util::splitString(input, " ", true);
|
2023-12-07 16:15:00 +01:00
|
|
|
std::erase_if(parts, [](auto &part) { return part.empty(); });
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool verifyConversionInput(const std::vector<std::string> &parts) {
|
|
|
|
if (parts.size() == 3) {
|
|
|
|
return parts[1] == "hex.builtin.command.convert.to"_lang.get() || parts[1] == "hex.builtin.command.convert.in"_lang.get() || parts[1] == "hex.builtin.command.convert.as"_lang.get();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string handleConversionCommand(const std::string &input) {
|
|
|
|
auto parts = splitConversionCommandInput(input);
|
|
|
|
|
|
|
|
if (!verifyConversionInput(parts))
|
|
|
|
return "hex.builtin.command.convert.invalid_input"_lang;
|
|
|
|
|
|
|
|
auto from = Value(parts[0]);
|
|
|
|
auto to = Value("1" + parts[2]);
|
|
|
|
|
|
|
|
return fmt::format("% {}", from.formatAs(to));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
void registerCommandPaletteCommands() {
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
ContentRegistry::CommandPaletteCommands::add(
|
2022-01-24 20:53:17 +01:00
|
|
|
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
|
2023-12-06 16:20:06 +01:00
|
|
|
"=",
|
2022-01-24 20:53:17 +01:00
|
|
|
"hex.builtin.command.calc.desc",
|
|
|
|
[](auto input) {
|
2023-12-01 14:07:10 +01:00
|
|
|
wolv::math_eval::MathEvaluator<long double> evaluator;
|
2022-01-24 20:53:17 +01:00
|
|
|
evaluator.registerStandardVariables();
|
|
|
|
evaluator.registerStandardFunctions();
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::optional<long double> result = evaluator.evaluate(input);
|
2022-01-24 20:53:17 +01:00
|
|
|
if (result.has_value())
|
2023-12-06 16:20:06 +01:00
|
|
|
return hex::format("{0} = {1}", input.data(), result.value());
|
2022-05-27 20:42:07 +02:00
|
|
|
else if (evaluator.hasError())
|
|
|
|
return hex::format("Error: {}", *evaluator.getLastError());
|
2022-01-24 20:53:17 +01:00
|
|
|
else
|
|
|
|
return std::string("???");
|
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
ContentRegistry::CommandPaletteCommands::add(
|
2022-01-24 20:53:17 +01:00
|
|
|
ContentRegistry::CommandPaletteCommands::Type::KeywordCommand,
|
|
|
|
"/web",
|
|
|
|
"hex.builtin.command.web.desc",
|
|
|
|
[](auto input) {
|
|
|
|
return hex::format("hex.builtin.command.web.result"_lang, input.data());
|
|
|
|
},
|
|
|
|
[](auto input) {
|
|
|
|
hex::openWebpage(input);
|
|
|
|
});
|
2021-02-08 19:56:04 +01:00
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
ContentRegistry::CommandPaletteCommands::add(
|
2022-01-24 20:53:17 +01:00
|
|
|
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
|
|
|
|
"$",
|
|
|
|
"hex.builtin.command.cmd.desc",
|
|
|
|
[](auto input) {
|
|
|
|
return hex::format("hex.builtin.command.cmd.result"_lang, input.data());
|
|
|
|
},
|
|
|
|
[](auto input) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::executeCommand(input);
|
2022-01-24 20:53:17 +01:00
|
|
|
});
|
2023-03-20 14:11:43 +01:00
|
|
|
|
|
|
|
ContentRegistry::CommandPaletteCommands::addHandler(
|
|
|
|
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
|
|
|
|
">",
|
|
|
|
[](const auto &input) {
|
2023-03-21 15:33:43 +01:00
|
|
|
std::vector<ContentRegistry::CommandPaletteCommands::impl::QueryResult> result;
|
2023-03-20 14:11:43 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
for (const auto &[priority, entry] : ContentRegistry::Interface::impl::getMenuItems()) {
|
2023-03-20 14:11:43 +01:00
|
|
|
if (!entry.enabledCallback())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<std::string> names;
|
2023-11-21 14:38:01 +01:00
|
|
|
std::transform(entry.unlocalizedNames.begin(), entry.unlocalizedNames.end(), std::back_inserter(names), [](auto &name) { return Lang(name); });
|
2023-03-20 14:11:43 +01:00
|
|
|
|
|
|
|
if (auto combined = wolv::util::combineStrings(names, " -> "); hex::containsIgnoreCase(combined, input) && !combined.contains(ContentRegistry::Interface::impl::SeparatorValue) && !combined.contains(ContentRegistry::Interface::impl::SubMenuValue)) {
|
2023-03-21 15:33:43 +01:00
|
|
|
result.emplace_back(ContentRegistry::CommandPaletteCommands::impl::QueryResult {
|
2023-03-20 14:11:43 +01:00
|
|
|
std::move(combined),
|
2023-11-18 14:34:33 +01:00
|
|
|
[&entry](const auto&) { entry.callback(); }
|
2023-03-20 14:11:43 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[](auto input) {
|
|
|
|
return hex::format("Menu Item: {}", input.data());
|
|
|
|
});
|
|
|
|
|
2024-01-21 21:29:03 +01:00
|
|
|
ContentRegistry::CommandPaletteCommands::addHandler(
|
|
|
|
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
|
|
|
|
".",
|
|
|
|
[](const auto &input) {
|
|
|
|
std::vector<ContentRegistry::CommandPaletteCommands::impl::QueryResult> result;
|
|
|
|
|
|
|
|
u32 index = 0;
|
|
|
|
for (const auto &provider : ImHexApi::Provider::getProviders()) {
|
|
|
|
ON_SCOPE_EXIT { index += 1; };
|
|
|
|
|
|
|
|
auto name = provider->getName();
|
|
|
|
if (!hex::containsIgnoreCase(name, input))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.emplace_back(ContentRegistry::CommandPaletteCommands::impl::QueryResult {
|
|
|
|
provider->getName(),
|
2024-01-21 21:39:50 +01:00
|
|
|
[index](const auto&) { ImHexApi::Provider::setCurrentProvider(index); }
|
2024-01-21 21:29:03 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[](auto input) {
|
|
|
|
return hex::format("Provider: {}", input.data());
|
|
|
|
});
|
|
|
|
|
2023-12-07 16:15:00 +01:00
|
|
|
ContentRegistry::CommandPaletteCommands::add(
|
|
|
|
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
|
|
|
|
"%",
|
|
|
|
"hex.builtin.command.convert.desc",
|
|
|
|
handleConversionCommand);
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|