Skip to content

Commit

Permalink
fix: fix command empty overload
Browse files Browse the repository at this point in the history
fix: fix compatibility with old ParameterType
fix: fix LOG_ERROR_WITH_SCRIPT_INFO
feat: add EventTests
  • Loading branch information
ShrBox committed Jan 3, 2025
1 parent dcc9954 commit 0fe6d59
Show file tree
Hide file tree
Showing 22 changed files with 343 additions and 173 deletions.
32 changes: 18 additions & 14 deletions src/legacy/api/APIHelp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,36 @@ std::string ValueKindToString(const ValueKind& kind);
#if !defined(NEW_DEFINES)

// 输出脚本调用堆栈,API名称,以及插件名
inline void LOG_ERROR_WITH_SCRIPT_INFO(std::string const& msg = "") {
inline void LOG_ERROR_WITH_SCRIPT_INFO(std::string const& func = "", std::string const& msg = "") {
PrintScriptStackTrace(msg);
lse::getSelfPluginInstance().getLogger().error("In API: " __FUNCTION__);
lse::getSelfPluginInstance().getLogger().error("In API: " + func);
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
}

// 参数类型错误输出
inline void LOG_WRONG_ARG_TYPE() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong type of argument!"); }
inline void LOG_WRONG_ARG_TYPE(std::string const& func = "") {
LOG_ERROR_WITH_SCRIPT_INFO(func, "Wrong type of argument!");
}

// 参数数量错误输出
inline void LOG_TOO_FEW_ARGS() { LOG_ERROR_WITH_SCRIPT_INFO("Too Few arguments!"); }
inline void LOG_TOO_FEW_ARGS(std::string const& func = "") { LOG_ERROR_WITH_SCRIPT_INFO(func, "Too Few arguments!"); }

// 参数数量错误输出
inline void LOG_WRONG_ARGS_COUNT() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong number of arguments!"); }
inline void LOG_WRONG_ARGS_COUNT(std::string const& func = "") {
LOG_ERROR_WITH_SCRIPT_INFO(func, "Wrong number of arguments!");
}

// 至少COUNT个参数
#define CHECK_ARGS_COUNT(ARGS, COUNT) \
if (ARGS.size() < COUNT) { \
LOG_TOO_FEW_ARGS(); \
LOG_TOO_FEW_ARGS(__FUNCTION__); \
return Local<Value>(); \
}

// 检查是否TYPE类型
#define CHECK_ARG_TYPE(ARG, TYPE) \
if (ARG.getKind() != TYPE) { \
LOG_WRONG_ARG_TYPE(); \
LOG_WRONG_ARG_TYPE(__FUNCTION__); \
return Local<Value>(); \
}

Expand All @@ -75,28 +79,28 @@ inline void LOG_WRONG_ARGS_COUNT() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong number of
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
return Local<Value>(); \
}

// 至少COUNT个参数_Constructor
#define CHECK_ARGS_COUNT_C(ARGS, COUNT) \
if (ARGS.size() < COUNT) { \
LOG_TOO_FEW_ARGS(); \
LOG_TOO_FEW_ARGS(__FUNCTION__); \
return nullptr; \
}

// 检查是否TYPE类型_Constructor
#define CHECK_ARG_TYPE_C(ARG, TYPE) \
if (ARG.getKind() != TYPE) { \
LOG_WRONG_ARG_TYPE(); \
LOG_WRONG_ARG_TYPE(__FUNCTION__); \
return nullptr; \
}

// 检查是否TYPE类型_Setter
#define CHECK_ARG_TYPE_S(ARG, TYPE) \
if (ARG.getKind() != TYPE) { \
LOG_WRONG_ARG_TYPE(); \
LOG_WRONG_ARG_TYPE(__FUNCTION__); \
return; \
}

Expand All @@ -110,7 +114,7 @@ inline void LOG_WRONG_ARGS_COUNT() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong number of
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
return nullptr; \
}

Expand All @@ -124,7 +128,7 @@ inline void LOG_WRONG_ARGS_COUNT() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong number of
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
return; \
}

Expand All @@ -137,7 +141,7 @@ inline void LOG_WRONG_ARGS_COUNT() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong number of
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
}

// 截获回调函数异常
Expand Down
16 changes: 8 additions & 8 deletions src/legacy/api/BlockAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ Local<Value> McClass::getBlock(const Arguments& args) {
pos = posObj->toIntVec4();
}
} else {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
} else if (args.size() == 4) {
Expand All @@ -419,7 +419,7 @@ Local<Value> McClass::getBlock(const Arguments& args) {
args[3].asNumber().toInt32()
};
} else {
LOG_WRONG_ARGS_COUNT();
LOG_WRONG_ARGS_COUNT(__FUNCTION__);
return Local<Value>();
}

Expand All @@ -435,7 +435,7 @@ Local<Value> McClass::getBlock(const Arguments& args) {
ChunkBlockPos cbpos = ChunkBlockPos(pos.getBlockPos(), minHeight);
auto block = &const_cast<Block&>(lc->getBlock(cbpos));
if (!block) {
// LOG_WRONG_ARG_TYPE();
// LOG_WRONG_ARG_TYPE(__FUNCTION__);
return {};
}
BlockPos bp{pos.x, pos.y, pos.z};
Expand Down Expand Up @@ -474,7 +474,7 @@ Local<Value> McClass::setBlock(const Arguments& args) {
block = args[1];
}
} else {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
} else if (args.size() == 5 || args.size() == 6) {
Expand All @@ -496,7 +496,7 @@ Local<Value> McClass::setBlock(const Arguments& args) {
tileData = args[5].asNumber().toInt32();
}
} else {
LOG_WRONG_ARGS_COUNT();
LOG_WRONG_ARGS_COUNT(__FUNCTION__);
return Local<Value>();
}

Expand All @@ -520,7 +520,7 @@ Local<Value> McClass::setBlock(const Arguments& args) {
// other block object
Block* bl = BlockClass::extract(block);
if (!bl) {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
BlockSource& bs = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
Expand Down Expand Up @@ -561,7 +561,7 @@ Local<Value> McClass::spawnParticle(const Arguments& args) {
type = args[1];
}
} else {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
} else if (args.size() == 5) {
Expand All @@ -580,7 +580,7 @@ Local<Value> McClass::spawnParticle(const Arguments& args) {
};
type = args[4];
} else {
LOG_WRONG_ARGS_COUNT();
LOG_WRONG_ARGS_COUNT(__FUNCTION__);
return Local<Value>();
}

Expand Down
27 changes: 15 additions & 12 deletions src/legacy/api/CommandAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ using ll::event::ServerStartedEvent;
//////////////////// Class Definition ////////////////////

ClassDefine<void> PermissionStaticBuilder = EnumDefineBuilder<OldCommandPermissionLevel>::build("PermType");
ClassDefine<void> ParamTypeStaticBuilder = EnumDefineBuilder<ParamKind::Kind>::build("ParamType");
ClassDefine<void> ParamTypeStaticBuilder = EnumDefineBuilder<OldParameterType>::build("ParamType");
ClassDefine<void> ParamOptionStaticBuilder = EnumDefineBuilder<CommandParameterOption>::build("ParamOption");

ClassDefine<CommandClass> CommandClassBuilder =
Expand Down Expand Up @@ -370,12 +370,14 @@ void onExecute(CommandOrigin const& origin, CommandOutput& output, RuntimeComman
}
for (auto& info : registeredCommands[commandName]) {
try {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
auto& param = runtime[info.enumName];
args.set(info.name, convertResult(param, origin, output));
} else {
auto& param = runtime[info.name];
args.set(info.name, convertResult(param, origin, output));
if (!info.name.empty()) {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
auto& param = runtime[info.enumName];
args.set(info.name, convertResult(param, origin, output));
} else {
auto& param = runtime[info.name];
args.set(info.name, convertResult(param, origin, output));
}
}
} catch (std::out_of_range&) {
continue;
Expand All @@ -395,7 +397,7 @@ Local<Value> CommandClass::newParameter(const Arguments& args) {
CHECK_ARG_TYPE(args[0], ValueKind::kString);
try {
auto name = args[0].asString().toString();
ParamKind::Kind type = parseEnum<ParamKind::Kind>(args[1]);
ParamKind::Kind type = (ParamKind::Kind)parseEnum<OldParameterType>(args[1]);
std::string enumName = "";
bool optional = false;
std::string identifier = "";
Expand Down Expand Up @@ -423,7 +425,7 @@ Local<Value> CommandClass::mandatory(const Arguments& args) {
CHECK_ARG_TYPE(args[0], ValueKind::kString);
try {
auto name = args[0].asString().toString();
ParamKind::Kind type = parseEnum<ParamKind::Kind>(args[1]);
ParamKind::Kind type = (ParamKind::Kind)parseEnum<OldParameterType>(args[1]);
std::string enumName = "";
std::string identifier = "";
size_t index = 2;
Expand All @@ -449,7 +451,7 @@ Local<Value> CommandClass::optional(const Arguments& args) {
CHECK_ARG_TYPE(args[0], ValueKind::kString);
try {
auto name = args[0].asString().toString();
ParamKind::Kind type = parseEnum<ParamKind::Kind>(args[1]);
ParamKind::Kind type = (ParamKind::Kind)parseEnum<OldParameterType>(args[1]);
std::string enumName = "";
std::string identifier = "";
size_t index = 2;
Expand All @@ -472,7 +474,6 @@ Local<Value> CommandClass::optional(const Arguments& args) {
// vector<index>
Local<Value> CommandClass::addOverload(const Arguments& args) {
try {
if (args.size() == 0) return Boolean::newBoolean(true);
auto overloadFunc = [e(EngineScope::currentEngine()
)](RuntimeOverload& cmd, std::string const& commandName, std::string const& paramName) {
auto& paramList = getEngineData(e)->plugin->registeredCommands[commandName];
Expand Down Expand Up @@ -552,13 +553,15 @@ Local<Value> CommandClass::addOverload(const Arguments& args) {
if (ll::getGamingStatus() == ll::GamingStatus::Starting) {
EventBus::getInstance().emplaceListener<ServerStartedEvent>(
[commandName(commandName), e(EngineScope::currentEngine())](ServerStartedEvent&) {
getEngineData(e)->plugin->registeredCommands[commandName].push_back({});
auto cmd = ll::command::CommandRegistrar::getInstance()
.getOrCreateCommand(commandName)
.runtimeOverload(getEngineData(e)->plugin);
cmd.execute(onExecute);
}
);
} else {
getEngineOwnData()->plugin->registeredCommands[commandName].push_back({});
auto cmd = get().runtimeOverload(getEngineOwnData()->plugin);
cmd.execute(onExecute);
}
Expand Down Expand Up @@ -602,7 +605,7 @@ Local<Value> CommandClass::addOverload(const Arguments& args) {
return Boolean::newBoolean(true);
}
}
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
CATCH("Fail in addOverload!")
Expand Down
30 changes: 30 additions & 0 deletions src/legacy/api/CommandAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@ enum class OldCommandPermissionLevel : schar {
Internal = 0x5,
};

enum class OldParameterType {
Int,
Bool,
Float,
Dimension,
String,
Enum,
SoftEnum,
Actor,
Player,
BlockPos,
Vec3,
RawText,
Message,
JsonValue,
Item,
Block,
BlockState,
Effect,
ActorType,
Command,
RelativeFloat,
IntegerRange,
FilePath,
WildcardInt,
WildcardActor,
// New types can
Count,
};

class CommandClass : public ScriptClass {
std::string commandName;
std::string description;
Expand Down
10 changes: 5 additions & 5 deletions src/legacy/api/ContainerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Local<Value> ContainerClass::addItem(const Arguments& args) {
try {
ItemStack* item = ItemClass::extract(args[0]);
if (!item) {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
if (args.size() >= 2) {
Expand All @@ -88,7 +88,7 @@ Local<Value> ContainerClass::addItemToFirstEmptySlot(const Arguments& args) {
try {
ItemStack* item = ItemClass::extract(args[0]);
if (!item) {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
return Boolean::newBoolean(container->addItemToFirstEmptySlot(*item));
Expand All @@ -102,7 +102,7 @@ Local<Value> ContainerClass::hasRoomFor(const Arguments& args) {
try {
ItemStack* item = ItemClass::extract(args[0]);
if (!item) {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}
return Boolean::newBoolean(container->hasRoomForItem(*item));
Expand All @@ -129,7 +129,7 @@ Local<Value> ContainerClass::getItem(const Arguments& args) {
try {
ItemStack* item = &const_cast<ItemStack&>(container->getItem(args[0].asNumber().toInt32()));
if (!item) {
LOG_ERROR_WITH_SCRIPT_INFO("Fail to get slot from container!");
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, "Fail to get slot from container!");
return Local<Value>();
}
return ItemClass::newItem(item);
Expand All @@ -144,7 +144,7 @@ Local<Value> ContainerClass::setItem(const Arguments& args) {
try {
ItemStack* item = ItemClass::extract(args[1]);
if (!item) {
LOG_WRONG_ARG_TYPE();
LOG_WRONG_ARG_TYPE(__FUNCTION__);
return Local<Value>();
}

Expand Down
Loading

0 comments on commit 0fe6d59

Please sign in to comment.