Skip to content

Commit

Permalink
refactor: replace templates with MCTAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Oct 14, 2023
1 parent e6dcd12 commit 7df9474
Show file tree
Hide file tree
Showing 30 changed files with 195 additions and 2,506 deletions.
6 changes: 5 additions & 1 deletion src/ll/api/base/Meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ struct max_type<T, U> {
};

template <typename... TL>
struct TypeList {
class TypeList {
public:
template <typename T>
static constexpr bool contains = (std::is_same_v<T, TL> || ...);

template <template <typename> class W>
using wrap = TypeList<W<TL>...>;

Expand Down
34 changes: 4 additions & 30 deletions src/ll/api/command/DynamicCommandAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,10 @@ typedef CommandItem Item;
typedef CommandBlockName Block;
typedef std::vector<BlockStateCommandParam> BlockState;
typedef MobEffect const* Effect;

#ifdef USE_PARSE_ENUM_STRING
typedef std::pair<std::string, int> Enum;
#else
typedef int Enum;
#endif // USE_PARSE_ENUM_STRING
typedef std::string SoftEnum;
typedef ActorDefinitionIdentifier const* ActorType;
typedef std::unique_ptr<Command> Command;
typedef std::pair<std::string, int> Enum;
typedef std::string SoftEnum;
typedef ActorDefinitionIdentifier const* ActorType;
typedef std::unique_ptr<Command> Command;
#ifdef ENABLE_PARAMETER_TYPE_POSTFIX
typedef int Postfix;
#endif // ENABLE_PARAMETER_TYPE_POSTFIX
Expand Down Expand Up @@ -543,9 +538,6 @@ std::unique_ptr<Command>* DynamicCommand::commandBuilder(std::unique_ptr<Command
DynamicCommandInstance* DynamicCommand::_setup(std::unique_ptr<class DynamicCommandInstance> commandInstance) {
std::string name = commandInstance->getCommandName();
logger.info("Setting up command \"{}\"", name);
#ifdef DEBUG
logger.info("Setting up command \"{}\"", name);
#endif // DEBUG

// Check if there is another command with the same name
auto signature = ll::Global<CommandRegistry>->findCommand(name);
Expand All @@ -566,9 +558,7 @@ DynamicCommandInstance* DynamicCommand::_setup(std::unique_ptr<class DynamicComm
throw std::runtime_error(
"Enum " + std::string(param.description) + "not found in command and BDS"
);
#ifndef USE_PARSE_ENUM_STRING_ // fix Enum
commandInstance->setEnum(*iter, ll::Global<CommandRegistry>->getEnumValues(*iter));
#endif // USE_PARSE_ENUM_STRING
}
} else if (param.type == ParameterType::SoftEnum) {
// add empty Soft Enum if not found in command and BDS
Expand Down Expand Up @@ -609,11 +599,7 @@ DynamicCommandInstance* DynamicCommand::_setup(std::unique_ptr<class DynamicComm
values.emplace_back(*iter, index);
++index;
}
#ifdef USE_PARSE_ENUM_STRING
ll::Global<CommandRegistry>->_addEnumValuesInternal(fixedView.data(), values, Bedrock::typeid_t<CommandRegistry>::_getCounter().fetch_add(1), &CommandRegistry::parseEnumStringAndInt).mValue;
#else
ll::Global<CommandRegistry>->_addEnumValuesInternal(fixedView.data(), values, Bedrock::typeid_t<CommandRegistry>::count++, &CommandRegistry::parseEnum<int>).mValue;
#endif // USE_PARSE_ENUM_STRING
}
commandInstance->enumRanges.swap(convertedEnumRanges);

Expand Down Expand Up @@ -757,12 +743,6 @@ std::unique_ptr<class DynamicCommandInstance> DynamicCommand::createCommand(
}

bool DynamicCommand::unregisterCommand(std::string const& name) {
#ifdef DEBUG
Schedule::nextTick([tid = std::this_thread::get_id()]() {
// Call DynamicCommand::unregisterCommand in other thread is not allowed!
assert(tid == std::this_thread::get_id());
});
#endif // DEBUG
if (ll::Global<CommandRegistry>->unregisterCommand(name)) {
dynamicCommandInstances.erase(name);
updateAvailableCommands();
Expand All @@ -772,12 +752,6 @@ bool DynamicCommand::unregisterCommand(std::string const& name) {
}

inline bool DynamicCommand::updateAvailableCommands() {
#ifdef DEBUG
Schedule::nextTick([tid = std::this_thread::get_id()]() {
// Call DynamicCommand::updateAvailableCommands in other thread is not allowed!
assert(tid == std::this_thread::get_id());
});
#endif // DEBUG

if (!ll::Global<CommandRegistry> || !ll::Global<Level>) return false;
auto packet = ll::Global<CommandRegistry>->serializeAvailableCommands();
Expand Down
15 changes: 2 additions & 13 deletions src/ll/api/command/DynamicCommandAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ copies or substantial portions of the Software.

#pragma once

#define USE_PARSE_ENUM_STRING
// #define ENABLE_PARAMETER_TYPE_POSTFIX

#include "magic_enum.hpp"
Expand Down Expand Up @@ -274,7 +273,7 @@ class DynamicCommand : public Command {

template <typename T>
inline enable_if_supported_t<T, T const&> getRaw() const {
#ifdef USE_PARSE_ENUM_STRING

if (type == ParameterType::Enum) {
auto& val = ll::memory::dAccess<std::pair<std::string, int>>(command, offset);
if constexpr (std::is_same_v<std::remove_cv_t<T>, int> || std::is_enum_v<T>) {
Expand All @@ -283,11 +282,6 @@ class DynamicCommand : public Command {
return static_cast<T const&>(val.first);
}
}
#else
if constexpr (std::is_same_v<std::remove_cv_t<T>, std::string>) {
if (type == ParameterType::Enum) return getEnumValue();
}
#endif // USE_PARSE_ENUM_STRING
if (checkTempateType<T>(type)) return ll::memory::dAccess<T>(command, offset);
throw std::runtime_error(fmt::format(
"Raw type not match, parameter Type: {}, data type: {}",
Expand All @@ -314,11 +308,6 @@ class DynamicCommand : public Command {
std::add_lvalue_reference_t<std::add_const_t<std::remove_reference_t<T>>>,
T>
get() const {
static_assert(
is_supported_result_type_v<T>
|| (std::is_lvalue_reference_v<T> && is_supported_result_type_v<std::remove_reference_t<T>>),
"Unsupported Result Type in " __FUNCTION__
);
if constexpr (std::is_lvalue_reference_v<T>) return getRaw<std::remove_reference_t<T>>();
else return getRaw<T>();
}
Expand Down Expand Up @@ -450,7 +439,7 @@ class DynamicCommand : public Command {
this->description = enumOptions;
return true;
}
inline bool setParameterOption(CommandParameterOption parameterOption) { this->option = parameterOption; }
inline void setParameterOption(CommandParameterOption parameterOption) { this->option = parameterOption; }

inline ParameterData(
std::string const& name,
Expand Down
2 changes: 0 additions & 2 deletions src/ll/core/BuiltinCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ class TeleportDimensionCommand : public Command {

registry->registerOverload<TeleportDimensionCommand>("tpdim", victimParam, dimensionTypeParam, positionParam);
registry->registerOverload<TeleportDimensionCommand>("tpdim", victimParam, dimensionIdParam, positionParam);
// registry->registerOverload<TeleportDimensionCommand>(
// "tpdim", dimensionTypeParam, positionParam);
registry->registerOverload<TeleportDimensionCommand>("tpdim", dimensionIdParam, positionParam);
}
};
Expand Down
8 changes: 7 additions & 1 deletion src/mc/_HeaderOutputPredefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#pragma warning(disable : 4201)

#define MCAPI __declspec(dllimport)
#define MCVAPI __declspec(dllimport)
#define MCVAPI MCAPI

#define MCTAPI \
template <> \
MCAPI

#include <algorithm> // STL general algorithms
#include <array> // STL array container
Expand Down Expand Up @@ -110,6 +114,8 @@ using uint64 = uint64_t;
using schar = int8_t;
using int64 = int64_t;

using FacingID = uchar;

// clang-format off
template <typename T0, typename T1>
class AutomaticID;
Expand Down
3 changes: 2 additions & 1 deletion src/mc/deps/core/common/bedrock/pubsub/Subscription.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "mc/_HeaderOutputPredefine.h"
#include "mc/deps/core/common/bedrock/pubsub/SubscriptionBase.h"

namespace Bedrock::PubSub {

class Subscription {
class Subscription : public SubscriptionBase {
public:
// prevent constructor by default
Subscription& operator=(Subscription const&);
Expand Down
2 changes: 2 additions & 0 deletions src/mc/deps/core/common/bedrock/pubsub/SubscriptionBase.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include "mc/_HeaderOutputPredefine.h"
#include "mc/deps/core/common/bedrock/pubsub/SubscriptionBodyBase.h"

namespace Bedrock::PubSub {

class SubscriptionBase {
public:
std::weak_ptr<Detail::SubscriptionBodyBase> mBody; // this+0x0
// prevent constructor by default
SubscriptionBase& operator=(SubscriptionBase const&);
SubscriptionBase(SubscriptionBase const&);
Expand Down
11 changes: 10 additions & 1 deletion src/mc/deps/core/common/bedrock/pubsub/SubscriptionBodyBase.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include "mc/_HeaderOutputPredefine.h"
#include "mc/deps/core/common/bedrock/pubsub/PublisherDisconnector.h"
#include "mc/deps/core/common/bedrock/pubsub/SubscriptionContext.h"

// auto generated forward declare list
// clang-format off
Expand All @@ -16,10 +18,17 @@ class SubscriptionBodyBase {
SubscriptionBodyBase(SubscriptionBodyBase const&);
SubscriptionBodyBase();

SubscriptionBodyBase* mNext; // this+0x0
SubscriptionBodyBase* mPrev; // this+0x8

std::shared_ptr<SubscriptionBodyBase> mStrongSelf; // this+0x18
PublisherDisconnector* mDisconnector; // this+0x28
std::unique_ptr<Bedrock::PubSub::SubscriptionContext> mContext; // this+0x30
int mGroup; // this+0x38
public:
// NOLINTBEGIN
// symbol: ??1SubscriptionBodyBase@Detail@PubSub@Bedrock@@UEAA@XZ
MCVAPI ~SubscriptionBodyBase();
virtual ~SubscriptionBodyBase();

// symbol:
// ??0SubscriptionBodyBase@Detail@PubSub@Bedrock@@QEAA@$$QEAV?$unique_ptr@VSubscriptionContext@PubSub@Bedrock@@U?$default_delete@VSubscriptionContext@PubSub@Bedrock@@@std@@@std@@@Z
Expand Down
76 changes: 25 additions & 51 deletions src/mc/deps/core/common/bedrock/typeid_t.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#pragma once

#include "mc/_HeaderOutputPredefine.h"
#include "mc/entity/utilities/ActorDamageCause.h"
#include "mc/enums/BlockSlot.h"
#include "mc/enums/ObjectiveSortOrder.h"
#include "mc/server/commands/CommandOperator.h"
#include <atomic>

namespace Json {
class Value;
Expand Down Expand Up @@ -46,50 +41,29 @@ typeid_t<T0> type_id() {
return id;
}

template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, bool>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, int>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, float>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, std::string>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, ActorDamageCause>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, DimensionType>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandBlockName>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandMessage>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, CommandOperator>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandPosition>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandPositionFloat>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandSelector<class Player>>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, Json::Value>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class MobEffect const*>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class RelativeFloat>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, std::unique_ptr<class Command>>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class WildcardCommandSelector<class Actor>>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandItem>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, struct ActorDefinitionIdentifier const*>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandRawText>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, class CommandWildcardInt>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, ObjectiveSortOrder>();
template <>
MCAPI typeid_t<CommandRegistry> type_id<CommandRegistry, BlockSlot>();
}; // namespace Bedrock

// clang-format off
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, int>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, bool>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, float>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, DimensionType>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, std::string>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, std::unique_ptr<class Command>>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, std::vector<class BlockStateCommandParam>>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandBlockName>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandFilePath>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandItem>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandMessage>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandPosition>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandPositionFloat>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandRawText>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandSelector<class Actor>>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandSelector<class Player>>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class CommandWildcardInt>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class Json::Value>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class MobEffect const *>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class RelativeFloat>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, class WildcardCommandSelector<class Actor>>();
MCTAPI Bedrock::typeid_t<CommandRegistry> Bedrock::type_id<CommandRegistry, struct ActorDefinitionIdentifier const *>();
// clang-format on
21 changes: 7 additions & 14 deletions src/mc/deps/core/utility/BinaryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@ class BinaryStream : public ::ReadOnlyBinaryStream {
serialize<T>::write(x, *this);
}

template <>
MCAPI void writeType(struct CommandOriginData const&);
template <>
MCAPI void writeType(std::vector<std::unique_ptr<class DataItem>> const&);
template <>
MCAPI void writeType(class NetworkItemStackDescriptor const&);
template <>
MCAPI void writeType(class MoveActorAbsoluteData const&);
template <>
MCAPI void writeType(class NetworkItemInstanceDescriptor const&);
template <>
MCAPI void writeType(struct ItemStackRequestSlotInfo const&);
template <>
MCAPI void writeType(class RecipeIngredient const&);
MCTAPI void writeType(struct CommandOriginData const&);
MCTAPI void writeType(std::vector<std::unique_ptr<class DataItem>> const&);
MCTAPI void writeType(class NetworkItemStackDescriptor const&);
MCTAPI void writeType(class MoveActorAbsoluteData const&);
MCTAPI void writeType(class NetworkItemInstanceDescriptor const&);
MCTAPI void writeType(struct ItemStackRequestSlotInfo const&);
MCTAPI void writeType(class RecipeIngredient const&);

public:
// NOLINTBEGIN
Expand Down
24 changes: 8 additions & 16 deletions src/mc/deps/core/utility/ReadOnlyBinaryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,14 @@ class ReadOnlyBinaryStream {
}
return res;
}
template <>
MCAPI Bedrock::Result<void> readType(class CompoundTag&);
template <>
MCAPI Bedrock::Result<void> readType(struct CommandOriginData&);
template <>
MCAPI Bedrock::Result<void> readType(class Experiments&);
template <>
MCAPI Bedrock::Result<void> readType(struct ItemStackRequestSlotInfo&);
template <>
MCAPI Bedrock::Result<void> readType(class MoveActorAbsoluteData&);
template <>
MCAPI Bedrock::Result<void> readType(class NetworkItemStackDescriptor&);
template <>
MCAPI Bedrock::Result<void> readType(class StructureSettings&);
template <>
MCAPI Bedrock::Result<void> readType(std::vector<std::unique_ptr<class DataItem>>&);
MCTAPI Bedrock::Result<void> readType(class CompoundTag&);
MCTAPI Bedrock::Result<void> readType(struct CommandOriginData&);
MCTAPI Bedrock::Result<void> readType(class Experiments&);
MCTAPI Bedrock::Result<void> readType(struct ItemStackRequestSlotInfo&);
MCTAPI Bedrock::Result<void> readType(class MoveActorAbsoluteData&);
MCTAPI Bedrock::Result<void> readType(class NetworkItemStackDescriptor&);
MCTAPI Bedrock::Result<void> readType(class StructureSettings&);
MCTAPI Bedrock::Result<void> readType(std::vector<std::unique_ptr<class DataItem>>&);

// prevent constructor by default
ReadOnlyBinaryStream& operator=(ReadOnlyBinaryStream const&);
Expand Down
Loading

0 comments on commit 7df9474

Please sign in to comment.