From 7bd19f862a8c2e3ce582ac3a3e096c190b872be6 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sun, 26 May 2024 04:52:32 +0100 Subject: [PATCH] refactor: members in CommandRegistry --- include/bedrock/command/command_registry.h | 149 +++++++++++---------- src/endstone_core/command/command_map.cpp | 12 +- 2 files changed, 82 insertions(+), 79 deletions(-) diff --git a/include/bedrock/command/command_registry.h b/include/bedrock/command/command_registry.h index 3ed30fc09..e3d082c65 100644 --- a/include/bedrock/command/command_registry.h +++ b/include/bedrock/command/command_registry.h @@ -129,22 +129,6 @@ class CommandRegistry { SlashCommand = 0x100057, }; - struct Overload { - Overload(const CommandVersion &version, std::unique_ptr (*factory)()) - : version(version), factory(factory) - { - } - - CommandVersion version; // +0 - std::unique_ptr (*factory)(); // +8 - std::vector params; // +16 - std::int32_t unknown1{-1}; // +40 - std::int8_t unknown2{0}; // +44 - std::uint64_t unknown3{0}; // +48 - std::uint64_t unknown4{0}; // +56 - std::uint64_t unknown5{0}; // +64 - }; - class Symbol { public: explicit Symbol(std::uint32_t value = 0) : value_(value){}; @@ -159,19 +143,32 @@ class CommandRegistry { std::uint32_t value_; }; + struct Overload { + Overload(const CommandVersion &version, std::unique_ptr (*factory)()) + : version(version), factory(factory) + { + } + + CommandVersion version; // +0 + std::unique_ptr (*factory)(); // +8 + std::vector params; // +16 + std::int32_t version_offset{-1}; // +40 + std::vector params_symbol; // +48 + }; + struct Signature { std::string name; // +0 std::string description; // +32 std::vector overloads; // +64 std::vector subcommand_values; // +88 CommandPermissionLevel permission_level; // +112 - CommandRegistry::Symbol symbol; // +116 - CommandRegistry::Symbol enum_symbol; // +120 - CommandFlag command_flag; // +124 - int unknown3; // +128 - int symbol_index; // +132 - int optional_index; // +136 - char unknown6; // +140 + CommandRegistry::Symbol command_symbol; // +116 + CommandRegistry::Symbol command_alias_symbol; // +120 + CommandFlag flags; // +124 + int first_rule; // +128 + int first_factorization; // +132 + int first_optional; // +136 + bool runnable; // +140 std::int64_t unknown7; // +144 }; @@ -189,6 +186,7 @@ class CommandRegistry { using ParseRule = bool (CommandRegistry::*)(void *, const CommandRegistry::ParseToken &, const CommandOrigin &, int, std::string &, std::vector &) const; + using CommandOverrideFunctor = std::function; template bool parse(void *value, const CommandRegistry::ParseToken &parse_token, const CommandOrigin &, int, std::string &, @@ -198,11 +196,14 @@ class CommandRegistry { class Enum { public: std::string name; // +0 - Bedrock::typeid_t type_id; // +32 - ParseRule parse_rule; // +40 + Bedrock::typeid_t type; // +32 + ParseRule parse; // +40 std::vector> values; // +48 }; class SoftEnum; + class OptionalParameterChain; + class ConstrainedValue; + class RegistryState; BEDROCK_API void registerCommand(const std::string &name, char const *description, CommandPermissionLevel level, CommandFlag flag1, CommandFlag flag2); @@ -238,36 +239,36 @@ class CommandRegistry { return describe(signature, signature.name, overload, 0, nullptr, nullptr); } - std::function network_update_callback; // +0 - std::function score_callback; // +56 - std::vector unknown1; // +128 - std::map parse_tables; // +152 - std::vector optionals; // +168 - std::vector literals; // +192 - std::vector enums; // +216 - std::vector subcommands; // +240 - std::vector chained_subcommands; // +264 - std::vector symbols; // +288 - std::vector postfixes; // +312 - std::map enum_symbol_index; // +336 - std::map enum_symbols; // +352 - std::map subcommand_symbol_index; // +368 - std::map subcommand_symbols; // +384 - std::vector command_symbols; // +400 - std::map commands; // +424 - std::map, int> type_ids; // +440 - std::map aliases; // +456 - std::vector semantic_constraints; // +472 - std::map constrained_values; // +496 - std::vector constrained_value_data; // +512 - std::map, std::uint32_t> unknown8; // +536 - std::vector soft_enums; // +552 - std::map soft_enum_symbol_index; // +576 - std::vector unknown10; // +592 - char param_symbols[96]; // +616 - std::unordered_map unknown11; // +712 - std::unordered_map unknown12; // +776 - std::function command_registration_override; // +840 + std::function network_update_callback; // +0 + std::function get_score_for_objective; // +56 + std::vector rules; // +128 + std::map parse_tables; // +152 + std::vector optionals; // +168 + std::vector enum_values; // +192 + std::vector enums; // +216 + std::vector subcommands; // +240 + std::vector chained_subcommands; // +264 + std::vector symbols; // +288 + std::vector postfixes; // +312 + std::map enum_symbol_index; // +336 + std::map enum_symbols; // +352 + std::map subcommand_symbol_index; // +368 + std::map subcommand_symbols; // +384 + std::vector command_symbols; // +400 + std::map signatures; // +424 + std::map, int> type_lookup; // +440 + std::map aliases; // +456 + std::vector semantic_constraints; // +472 + std::map semantic_constraint_lookup; // +496 + std::vector constrained_values; // +512 + std::map, std::uint32_t> constrained_value_lookup; // +536 + std::vector soft_enums; // +552 + std::map soft_enum_lookup; // +576 + std::vector state_stack; // +592 + char param_symbols[96]; // +616 + std::unordered_map unknown11; // +712 + std::unordered_map unknown12; // +776 + CommandOverrideFunctor command_override_functor; // +840 private: [[nodiscard]] BEDROCK_API const CommandRegistry::Signature *findCommand(const std::string &name) const; @@ -283,39 +284,41 @@ class CommandRegistry { BEDROCK_API void registerOverloadInternal(CommandRegistry::Signature &signature, CommandRegistry::Overload &overload); }; +BEDROCK_STATIC_ASSERT_SIZE(CommandRegistry, 904, 896); -enum CommandParameterDataType : int { - Default = 0, +enum class CommandParameterDataType : int { + Basic = 0, Enum = 1, + SoftEnum = 2, + Postfix = 3 }; enum class CommandParameterOption : char { None = 0, - EnumAutocompleteExpansion = 0x01, - HasSemanticConstraint = 0x02, - EnumAsChainedCommand = 0x04 + EnumAutocompleteExpansion = 1, + HasSemanticConstraint = 2, + EnumAsChainedCommand = 4 }; struct CommandParameterData { - CommandParameterData(Bedrock::typeid_t type_id, CommandRegistry::ParseRule parse_rule, - char const *name, CommandParameterDataType type, char const *type_name, - char const *fallback_typename, int offset_value, bool optional, int offset_has_value) - : type_id(type_id), parse_rule(parse_rule), name(name), enum_name(type_name), - fallback_typename(fallback_typename), type(type), offset_value(offset_value), - offset_has_value(offset_has_value), optional(optional) + CommandParameterData(Bedrock::typeid_t type, CommandRegistry::ParseRule parse, char const *name, + CommandParameterDataType param_type, char const *enum_name, char const *fallback_typename, + int offset, bool is_optional, int set_offset) + : type(type), parse(parse), name(name), enum_name(enum_name), fallback_typename(fallback_typename), + param_type(param_type), offset(offset), set_offset(set_offset), is_optional(is_optional) { } - Bedrock::typeid_t type_id; // +0 - CommandRegistry::ParseRule parse_rule; // +8 + Bedrock::typeid_t type; // +0 + CommandRegistry::ParseRule parse; // +8 std::string name; // +16 const char *enum_name; // +48 CommandRegistry::Symbol enum_symbol{static_cast(-1)}; // +56 const char *fallback_typename; // +64 CommandRegistry::Symbol fallback_symbol{static_cast(-1)}; // +72 - CommandParameterDataType type; // +76 - int offset_value; // +80 - int offset_has_value; // +84 - bool optional; // +88 - CommandParameterOption option{0}; // +89 + CommandParameterDataType param_type; // +76 + int offset; // +80 + int set_offset; // +84 + bool is_optional; // +88 + CommandParameterOption options{0}; // +89 }; diff --git a/src/endstone_core/command/command_map.cpp b/src/endstone_core/command/command_map.cpp index b383e308b..7da318b32 100644 --- a/src/endstone_core/command/command_map.cpp +++ b/src/endstone_core/command/command_map.cpp @@ -77,7 +77,7 @@ void EndstoneCommandMap::setMinecraftCommands() it->second.push_back(alias); } - for (const auto &[command_name, signature] : registry.commands) { + for (const auto &[command_name, signature] : registry.signatures) { auto description = getI18n().get(signature.description, {}, nullptr); std::vector usages; @@ -158,9 +158,9 @@ bool EndstoneCommandMap::registerCommand(std::shared_ptr command) std::vector param_data; for (const auto ¶meter : parameters) { - auto data = CommandParameterData({0}, &CommandRegistry::parse, parameter.name.c_str(), - CommandParameterDataType::Default, nullptr, nullptr, 0, - parameter.optional, -1); + auto data = + CommandParameterData({0}, &CommandRegistry::parse, parameter.name.c_str(), + CommandParameterDataType::Basic, nullptr, nullptr, 0, parameter.optional, -1); if (parameter.is_enum) { auto symbol = static_cast(registry.addEnumValues(parameter.type, parameter.values)); @@ -169,13 +169,13 @@ bool EndstoneCommandMap::registerCommand(std::shared_ptr command) server_.getLogger().error("Unable to register enum '{}'.", parameter.type); return false; } - data.type = CommandParameterDataType::Enum; + data.param_type = CommandParameterDataType::Enum; data.enum_name = it->first.c_str(); data.enum_symbol = CommandRegistry::Symbol{symbol}; } else if (parameter.type == "bool") { static auto symbol = static_cast(registry.addEnumValues("Boolean", {})); - data.type = CommandParameterDataType::Enum; + data.param_type = CommandParameterDataType::Enum; data.enum_name = "Boolean"; data.enum_symbol = CommandRegistry::Symbol{symbol}; }