Skip to content

Commit

Permalink
refactor: code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Jan 1, 2025
1 parent c6b81ad commit a08cf76
Showing 1 changed file with 25 additions and 73 deletions.
98 changes: 25 additions & 73 deletions src/legacy/api/CommandAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,54 +443,40 @@ Local<Value> CommandClass::optional(const Arguments& args) {
Local<Value> CommandClass::addOverload(const Arguments& args) {
try {
if (args.size() == 0) return Boolean::newBoolean(true);
auto cmd = get().runtimeOverload(getEngineOwnData()->plugin);
if (args[0].isNumber()) {
for (int i = 0; i < args.size(); ++i) {
CHECK_ARG_TYPE(args[i], ValueKind::kNumber);
std::string paramName = std::to_string(args[i].asNumber().toInt32());
auto& paramList = getEngineOwnData()->plugin->registeredCommands[commandName];
for (auto& info : paramList) {
if (info.name == paramName || info.enumName == paramName) {
if (info.optional) {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.optional(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.optional(info.name, info.type).option(info.option);
}
auto cmd = get().runtimeOverload(getEngineOwnData()->plugin);
auto overloadFunc = [](RuntimeOverload& cmd, std::string const& commandName, std::string const& paramName) {
auto& paramList = getEngineOwnData()->plugin->registeredCommands[commandName];
for (auto& info : paramList) {
if (info.name == paramName || info.enumName == paramName) {
if (info.optional) {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.optional(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.optional(info.name, info.type).option(info.option);
}
} else {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.required(info.enumName, info.type, info.enumName).option(info.option);
} else {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.required(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.required(info.name, info.type).option(info.option);
}
cmd.required(info.name, info.type).option(info.option);
}
}
}
}
};
if (args[0].isNumber()) {
for (int i = 0; i < args.size(); ++i) {
CHECK_ARG_TYPE(args[i], ValueKind::kNumber);
std::string paramName = std::to_string(args[i].asNumber().toInt32());
overloadFunc(cmd, commandName, paramName);
}
cmd.execute(onExecute);
return Boolean::newBoolean(true);
} else if (args[0].isString()) {
for (int i = 0; i < args.size(); ++i) {
CHECK_ARG_TYPE(args[i], ValueKind::kString);
std::string paramName = args[0].asString().toString();
auto& paramList = getEngineOwnData()->plugin->registeredCommands[commandName];
for (auto& info : paramList) {
if (info.name == paramName || info.enumName == paramName) {
if (info.optional) {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.optional(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.optional(info.name, info.type).option(info.option);
}
} else {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.required(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.required(info.name, info.type).option(info.option);
}
}
}
}
overloadFunc(cmd, commandName, paramName);
}
cmd.execute(onExecute);
return Boolean::newBoolean(true);
Expand All @@ -501,49 +487,15 @@ Local<Value> CommandClass::addOverload(const Arguments& args) {
for (int i = 0; i < arr.size(); ++i) {
CHECK_ARG_TYPE(arr.get(i), ValueKind::kNumber);
std::string paramName = std::to_string(arr.get(i).asNumber().toInt32());
auto& paramList = getEngineOwnData()->plugin->registeredCommands[commandName];
for (auto& info : paramList) {
if (info.name == paramName || info.enumName == paramName) {
if (info.optional) {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.optional(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.optional(info.name, info.type).option(info.option);
}
} else {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.required(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.required(info.name, info.type).option(info.option);
}
}
}
}
overloadFunc(cmd, commandName, paramName);
}
cmd.execute(onExecute);
return Boolean::newBoolean(true);
} else if (arr.get(0).isString()) {
for (int i = 0; i < arr.size(); ++i) {
CHECK_ARG_TYPE(arr.get(i), ValueKind::kString);
std::string paramName = arr.get(i).asString().toString();
auto& paramList = getEngineOwnData()->plugin->registeredCommands[commandName];
for (auto& info : paramList) {
if (info.name == paramName || info.enumName == paramName) {
if (info.optional) {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.optional(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.optional(info.name, info.type).option(info.option);
}
} else {
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
cmd.required(info.enumName, info.type, info.enumName).option(info.option);
} else {
cmd.required(info.name, info.type).option(info.option);
}
}
}
}
overloadFunc(cmd, commandName, paramName);
}
cmd.execute(onExecute);
return Boolean::newBoolean(true);
Expand Down

0 comments on commit a08cf76

Please sign in to comment.