Skip to content

Commit

Permalink
refactor: add mutex to command registrar
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Feb 5, 2024
1 parent 96bc124 commit a70e453
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/ll/api/command/CommandHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ struct CommandHandle::Impl {
gsl::not_null<CommandRegistrar*> registrar;
gsl::not_null<CommandRegistry::Signature*> signature;
bool owned;
std::recursive_mutex mutex;
};


CommandHandle::CommandHandle(CommandRegistrar& registrar, CommandRegistry::Signature* signature, bool owned)
: impl(std::make_unique<Impl>(&registrar, signature, owned)) {}

Expand All @@ -26,8 +26,9 @@ CommandHandle::~CommandHandle() = default;
CommandRegistrar& CommandHandle::getRegistrar() { return *impl->registrar; }

void CommandHandle::registerOverload(OverloadData&& data) {
auto& overload = impl->signature->overloads.emplace_back(CommandVersion{}, data.getFactory());
overload.params = data.moveParams();
std::lock_guard lock{impl->mutex};
auto& overload = impl->signature->overloads.emplace_back(CommandVersion{}, data.getFactory());
overload.params = data.moveParams();
impl->registrar->getRegistry().registerOverloadInternal(*impl->signature, overload);
}
char const* CommandHandle::addText(std::string_view text) { return impl->registrar->addText(*this, text); }
Expand Down
12 changes: 9 additions & 3 deletions src/ll/api/command/CommandRegistrar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace ll::command {
struct CommandRegistrar::Impl {
std::unordered_map<std::string, CommandHandle> commands;
std::unordered_map<std::string, uint64> textWithRef;
std::recursive_mutex mutex;
};

CommandRegistrar::CommandRegistrar() : impl(std::make_unique<Impl>()) {}
Expand All @@ -38,11 +39,15 @@ CommandHandle& CommandRegistrar::getOrCreateCommand(
CommandFlag flag,
std::weak_ptr<plugin::Plugin> /*plugin*/
) {
auto& registry = getRegistry();
auto signature = registry.findCommand(name);
std::lock_guard lock{impl->mutex};
auto& registry = getRegistry();
auto signature = registry.findCommand(name);
if (!signature) {
registry.registerCommand(name, description.c_str(), requirement, flag);
signature = registry.findCommand(name);
if (!signature) {
throw std::runtime_error{"failed to register command " + name};
}
return impl->commands.try_emplace(name, *this, signature, true).first->second;
} else if (impl->commands.contains(signature->name)) {
return impl->commands.at(signature->name);
Expand Down Expand Up @@ -124,7 +129,8 @@ bool CommandRegistrar::setSoftEnumValues(std::string const& name, std::vector<st
}

char const* CommandRegistrar::addText(CommandHandle& /*handle*/, std::string_view text) {
std::string storedName{"ll_text_enum_name_"};
std::lock_guard lock{impl->mutex};
std::string storedName{"ll_text_enum_name_"};
storedName += text;
if (impl->textWithRef.contains(storedName)) {
impl->textWithRef.at(storedName)++;
Expand Down
17 changes: 14 additions & 3 deletions src/ll/api/command/OverloadData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ struct OverloadData::Impl {
gsl::not_null<CommandHandle*> handle;
CommandRegistry::FactoryFn factory{};
std::vector<CommandParameterData> params;
std::recursive_mutex mutex;
};

OverloadData::OverloadData(CommandHandle& handle) : impl(std::make_unique<Impl>(&handle)) {}

OverloadData::~OverloadData() = default;

CommandRegistry::FactoryFn OverloadData::getFactory() { return impl->factory; }
std::vector<CommandParameterData> OverloadData::moveParams() { return std::move(impl->params); }
CommandRegistry::FactoryFn OverloadData::getFactory() {
std::lock_guard lock{impl->mutex};
return impl->factory;
}
std::vector<CommandParameterData> OverloadData::moveParams() {
std::lock_guard lock{impl->mutex};
return std::move(impl->params);
}

CommandParameterData& OverloadData::back() {
std::lock_guard lock{impl->mutex};
if (!impl->params.empty()) {
return impl->params.back();
} else {
Expand All @@ -49,7 +57,8 @@ CommandParameterData& OverloadData::addParamImpl(
int flagOffset,
bool optional
) {
auto& param =
std::lock_guard lock{impl->mutex};
auto& param =
impl->params.emplace_back(id, parser, std::string{name}, type, enumNameOrPostfix, offset, optional, flagOffset);
if (id == Bedrock::type_id<CommandRegistry, CommandBlockName>()
|| id == Bedrock::type_id<CommandRegistry, CommandItem>()) {
Expand All @@ -61,6 +70,7 @@ CommandParameterData& OverloadData::addParamImpl(
return param;
}
CommandParameterData& OverloadData::addTextImpl(std::string_view text, int offset) {
std::lock_guard lock{impl->mutex};
return addParamImpl(
Bedrock::type_id<CommandRegistry, Placeholder>(),
&CommandRegistry::parse<Placeholder>,
Expand All @@ -74,6 +84,7 @@ CommandParameterData& OverloadData::addTextImpl(std::string_view text, int offse
}

void OverloadData::setFactory(CommandRegistry::FactoryFn fn) {
std::lock_guard lock{impl->mutex};
impl->factory = fn;
impl->handle->registerOverload(std::move(*this));
}
Expand Down
2 changes: 1 addition & 1 deletion src/ll/core/dimension/CustomDimensionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void setDimensionConfigPath() {
if (!ll::service::getLevel()) {
throw std::runtime_error("Level nullptr");
}
dimensionConfigPath /= string_utils::str2u8str(ll::service::getLevel()->getLevelName());
dimensionConfigPath /= string_utils::str2u8str(ll::service::getLevel()->getLevelData().getLevelName());
dimensionConfigPath /= u8"dimension_config.json";
}

Expand Down
6 changes: 6 additions & 0 deletions src/mc/world/actor/Mob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "mc/world/actor/Mob.h"

void Mob::refreshInventory() {
sendInventory(true);
sendArmor(std::bitset<4>{"1111"});
}
5 changes: 1 addition & 4 deletions src/mc/world/actor/Mob.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ class BodyControl;

class Mob : public ::Actor {
public:
inline void refreshInventory() {
sendInventory(true);
sendArmor(std::bitset<4>{"1111"});
}
LLAPI void refreshInventory();

// prevent constructor by default
Mob& operator=(Mob const&);
Expand Down
12 changes: 12 additions & 0 deletions src/mc/world/actor/player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ optional_ref<ConnectionRequest const> Player::getConnectionRequest() const {
return ll::service::getServerNetworkHandler()->fetchConnectionRequest(getNetworkIdentifier());
}

NetworkIdentifier const& Player::getNetworkIdentifier() const { return getUserEntityIdentifier().mNetworkId; }

optional_ref<Certificate const> Player::getCertificate() const { return getUserEntityIdentifier().mCertificate.get(); }

SubClientId const& Player::getClientSubId() const { return getUserEntityIdentifier().mClientSubId; }

mce::UUID const& Player::getUuid() const { return getUserEntityIdentifier().mClientUUID; }

std::string Player::getIPAndPort() const { return getNetworkIdentifier().getIPAndPort(); }

bool Player::isOperator() const { return getPlayerPermissionLevel() == PlayerPermissionLevel::Operator; }

std::string Player::getLocaleName() const {
if (auto request = getConnectionRequest()) {
return request->mRawToken->mDataInfo["LanguageCode"].asString("");
Expand Down
14 changes: 6 additions & 8 deletions src/mc/world/actor/player/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,30 @@ class Player : public ::Mob {

LLNDAPI optional_ref<ConnectionRequest const> getConnectionRequest() const;

[[nodiscard]] NetworkIdentifier const& getNetworkIdentifier() const { return getUserEntityIdentifier().mNetworkId; }
LLNDAPI NetworkIdentifier const& getNetworkIdentifier() const;

[[nodiscard]] optional_ref<Certificate const> getCertificate() const {
return getUserEntityIdentifier().mCertificate.get();
}
LLNDAPI optional_ref<Certificate const> getCertificate() const;

[[nodiscard]] SubClientId const& getClientSubId() const { return getUserEntityIdentifier().mClientSubId; }
LLNDAPI SubClientId const& getClientSubId() const;

/**
* @brief Get the player's uuid
* @return Player's uuid
*/
[[nodiscard]] mce::UUID const& getUuid() const { return getUserEntityIdentifier().mClientUUID; }
LLNDAPI mce::UUID const& getUuid() const;

/**
* @brief Get the player's IP and port
* @return player's IP and port
*/
[[nodiscard]] std::string getIPAndPort() const { return getNetworkIdentifier().getIPAndPort(); }
LLNDAPI std::string getIPAndPort() const;

/**
* @brief Determine if a player is an administrator of the server
* @return Returns true if the player is an administrator of the server; otherwise returns false
* @warning Custom permissions are not considered administrators
*/
[[nodiscard]] bool isOperator() const { return getPlayerPermissionLevel() == PlayerPermissionLevel::Operator; }
LLNDAPI bool isOperator() const;

/**
* @brief Get the player's real in-game nickname
Expand Down
2 changes: 2 additions & 0 deletions src/mc/world/events/ServerInstanceEventCoordinator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "mc/events/ServerInstanceGameplayEvent.h"
#include "mc/world/events/EventRef.h"

class ServerInstance;

class ServerInstanceEventCoordinator {
public:
// prevent constructor by default
Expand Down
2 changes: 0 additions & 2 deletions src/mc/world/level/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class ServerLevel;

class Level : public ILevel, public BlockSourceListener, public IWorldRegistriesProvider {
public:
[[nodiscard]] std::string const& getLevelName() const { return getLevelData().getLevelName(); }

[[nodiscard]] ServerLevel& asServer() { return *reinterpret_cast<ServerLevel*>(this); }

// prevent constructor by default
Expand Down

0 comments on commit a70e453

Please sign in to comment.