Skip to content

Commit

Permalink
feat: add PermissibleFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Oct 1, 2024
1 parent 65ff3f7 commit e848e6f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 23 deletions.
46 changes: 46 additions & 0 deletions include/endstone/detail/permissions/permissible.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

namespace endstone::detail {

class PermissibleFactory {
public:
enum class Option {
Normal,
Lazy
};

template <Option Option, typename Derived, typename... Args>
static std::shared_ptr<Derived> create(Args &&...args)
{
struct Impl : Derived {
explicit Impl(Args &&...args) : Derived(std::forward<Args>(args)...) {}
};
auto result = std::make_shared<Impl>(std::forward<Args>(args)...);
if constexpr (Option == Option::Normal) {
result->recalculatePermissions();
}
return result;
}

template <typename Derived, typename... Args>
static std::shared_ptr<Derived> create(Args &&...args)
{
return create<Option::Normal, Derived>(std::forward<Args>(args)...);
}
};

} // namespace endstone::detail
6 changes: 2 additions & 4 deletions include/endstone/detail/permissions/permissible_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ namespace endstone::detail {
* Base Permissible for use in any Permissible object via proxy or extension
*/
class PermissibleBase : public Permissible {
struct Private {
explicit Private() = default;
};
protected:
explicit PermissibleBase(Permissible *opable);

public:
explicit PermissibleBase(Private, Permissible *opable);
[[nodiscard]] bool isOp() const override;
void setOp(bool value) override;
[[nodiscard]] bool isPermissionSet(std::string name) const override;
Expand Down
12 changes: 0 additions & 12 deletions include/endstone/permissions/permissible.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,5 @@ class Permissible {
[[nodiscard]] virtual std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const = 0;

[[nodiscard]] virtual CommandSender *asCommandSender() const = 0;

protected:
template <typename Derived, typename... Args>
static std::shared_ptr<Derived> create0(Args &&...args)
{
struct make_shared_enabler : Derived {
explicit make_shared_enabler(Args &&...args) : Derived(std::forward<Args>(args)...) {}
};
auto result = std::make_shared<make_shared_enabler>(std::forward<Args>(args)...);
result->recalculatePermissions();
return result;
}
};
} // namespace endstone
3 changes: 2 additions & 1 deletion src/endstone_core/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "bedrock/world/level/dimension/vanilla_dimensions.h"
#include "endstone/detail/level/dimension.h"
#include "endstone/detail/level/level.h"
#include "endstone/detail/permissions/permissible.h"

namespace endstone::detail {

Expand Down Expand Up @@ -209,7 +210,7 @@ ::Actor &EndstoneActor::getActor() const

std::shared_ptr<EndstoneActor> EndstoneActor::create(EndstoneServer &server, ::Actor &actor)
{
return create0<EndstoneActor>(server, actor);
return PermissibleFactory::create<EndstoneActor>(server, actor);
}

} // namespace endstone::detail
3 changes: 2 additions & 1 deletion src/endstone_core/actor/mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "endstone/detail/actor/mob.h"

#include "endstone/detail/permissions/permissible.h"
#include "endstone/detail/server.h"

namespace endstone::detail {
Expand Down Expand Up @@ -168,7 +169,7 @@ bool EndstoneMob::isGliding() const

std::shared_ptr<EndstoneMob> EndstoneMob::create(EndstoneServer &server, ::Mob &mob)
{
return create0<EndstoneMob>(server, mob);
return PermissibleFactory::create<EndstoneMob>(server, mob);
}

} // namespace endstone::detail
3 changes: 2 additions & 1 deletion src/endstone_core/command/command_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <entt/entt.hpp>

#include "endstone/detail/message.h"
#include "endstone/detail/permissions/permissible.h"

namespace endstone::detail {
CommandSenderAdapter::CommandSenderAdapter(const CommandOrigin &origin, CommandOutput &output)
Expand Down Expand Up @@ -70,7 +71,7 @@ void CommandSenderAdapter::setOp(bool value)

std::shared_ptr<CommandSenderAdapter> CommandSenderAdapter::create(const CommandOrigin &origin, CommandOutput &output)
{
return Permissible::create0<CommandSenderAdapter>(origin, output);
return PermissibleFactory::create<CommandSenderAdapter>(origin, output);
}

void CommandAdapter::execute(const CommandOrigin &origin, CommandOutput &output) const
Expand Down
3 changes: 2 additions & 1 deletion src/endstone_core/command/console_command_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "endstone/detail/command/console_command_sender.h"

#include "endstone/detail/message.h"
#include "endstone/detail/permissions/permissible.h"
#include "endstone/detail/server.h"

namespace endstone::detail {
Expand Down Expand Up @@ -102,7 +103,7 @@ std::unordered_set<PermissionAttachmentInfo *> EndstoneConsoleCommandSender::get

std::shared_ptr<EndstoneConsoleCommandSender> EndstoneConsoleCommandSender::create()
{
return create0<EndstoneConsoleCommandSender>();
return PermissibleFactory::create<EndstoneConsoleCommandSender>();
}

} // namespace endstone::detail
6 changes: 4 additions & 2 deletions src/endstone_core/permissions/permissible_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

#include <memory>

#include <endstone/detail/permissions/permissible.h>

#include "endstone/detail/server.h"
#include "endstone/detail/util/error.h"
#include "endstone/permissions/permission.h"
#include "endstone/permissions/permission_attachment_info.h"

namespace endstone::detail {

PermissibleBase::PermissibleBase(Private, Permissible *opable) : opable_(opable), parent_(opable ? *opable : *this) {}
PermissibleBase::PermissibleBase(Permissible *opable) : opable_(opable), parent_(opable ? *opable : *this) {}

bool PermissibleBase::isOp() const
{
Expand Down Expand Up @@ -216,7 +218,7 @@ void PermissibleBase::clearPermissions()

std::shared_ptr<PermissibleBase> PermissibleBase::create(Permissible *opable)
{
return std::make_shared<PermissibleBase>(Private(), opable);
return PermissibleFactory::create<PermissibleFactory::Option::Lazy, PermissibleBase>(opable);
}

} // namespace endstone::detail
3 changes: 2 additions & 1 deletion src/endstone_core/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "endstone/detail/base64.h"
#include "endstone/detail/form/form_codec.h"
#include "endstone/detail/network/packet_adapter.h"
#include "endstone/detail/permissions/permissible.h"
#include "endstone/detail/server.h"
#include "endstone/detail/util/error.h"
#include "endstone/detail/util/uuid.h"
Expand Down Expand Up @@ -741,7 +742,7 @@ ::Player &EndstonePlayer::getHandle() const

std::shared_ptr<EndstonePlayer> EndstonePlayer::create(EndstoneServer &server, ::Player &player)
{
return create0<EndstonePlayer>(server, player);
return PermissibleFactory::create<EndstonePlayer>(server, player);
}

} // namespace endstone::detail

0 comments on commit e848e6f

Please sign in to comment.