Skip to content

Commit

Permalink
fix: fix old plugin command compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Jan 1, 2025
1 parent e1e92f1 commit ad1033f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 70 deletions.
124 changes: 67 additions & 57 deletions src/legacy/api/CommandAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,15 @@ void onExecute(CommandOrigin const& origin, CommandOutput& output, RuntimeComman
lse::getSelfPluginInstance().getLogger().warn("Could not find {} in registered commands."_tr(commandName));
return;
}
for (std::string& name : registeredCommands[commandName]) {
for (auto& info : registeredCommands[commandName]) {
try {
auto& param = runtime[name];
args.set(name, convertResult(param, origin, output));
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 Down Expand Up @@ -373,8 +378,7 @@ Local<Value> CommandClass::newParameter(const Arguments& args) {
option = (CommandParameterOption)args[index++].asNumber().toInt32();
if (index != args.size()) throw std::runtime_error("Error Argument in newParameter");

paramMaps[name] = {type, optional, enumName}; // Stores the parameter name for overload use
getEngineOwnData()->plugin->registeredCommands[commandName].push_back(name
getEngineOwnData()->plugin->registeredCommands[commandName].push_back({name, type, optional, enumName}
); // Stores the parameter name for onExecute use

return Boolean::newBoolean(true);
Expand All @@ -400,8 +404,7 @@ Local<Value> CommandClass::mandatory(const Arguments& args) {
option = (CommandParameterOption)args[index++].asNumber().toInt32();
if (index != args.size()) throw std::runtime_error("Error Argument in newParameter");

paramMaps[name] = {type, false, enumName, option}; // Stores the parameter name for overload use
getEngineOwnData()->plugin->registeredCommands[commandName].push_back(name
getEngineOwnData()->plugin->registeredCommands[commandName].push_back({name, type, false, enumName}
); // Stores the parameter name for onExecute use

return Boolean::newBoolean(true);
Expand All @@ -427,8 +430,7 @@ Local<Value> CommandClass::optional(const Arguments& args) {
option = (CommandParameterOption)args[index++].asNumber().toInt32();
if (index != args.size()) throw std::runtime_error("Error Argument in newParameter");

paramMaps[name] = {type, true, enumName, option}; // Stores the parameter name for overload use
getEngineOwnData()->plugin->registeredCommands[commandName].push_back(name
getEngineOwnData()->plugin->registeredCommands[commandName].push_back({name, type, true, enumName}
); // Stores the parameter name for onExecute use

return Boolean::newBoolean(true);
Expand All @@ -446,19 +448,21 @@ Local<Value> CommandClass::addOverload(const Arguments& args) {
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());
if (paramMaps.find(paramName) != paramMaps.end()) {
ParamInfo& param = paramMaps[paramName];
if (param.optional) {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.optional(paramName, param.type, param.enumName).option(param.option);
} else {
cmd.optional(paramName, param.type).option(param.option);
}
} else {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.required(paramName, param.type, param.enumName).option(param.option);
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 {
cmd.required(paramName, param.type).option(param.option);
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);
}
}
}
}
Expand All @@ -469,19 +473,21 @@ Local<Value> CommandClass::addOverload(const Arguments& args) {
for (int i = 0; i < args.size(); ++i) {
CHECK_ARG_TYPE(args[i], ValueKind::kString);
std::string paramName = args[0].asString().toString();
if (paramMaps.find(paramName) != paramMaps.end()) {
ParamInfo& param = paramMaps[paramName];
if (param.optional) {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.optional(paramName, param.type, param.enumName).option(param.option);
} else {
cmd.optional(paramName, param.type).option(param.option);
}
} else {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.required(paramName, param.type, param.enumName).option(param.option);
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 {
cmd.required(paramName, param.type).option(param.option);
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);
}
}
}
}
Expand All @@ -495,19 +501,21 @@ 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());
if (paramMaps.find(paramName) != paramMaps.end()) {
ParamInfo& param = paramMaps[paramName];
if (param.optional) {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.optional(paramName, param.type, param.enumName).option(param.option);
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 {
cmd.optional(paramName, param.type).option(param.option);
}
} else {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.required(paramName, param.type, param.enumName).option(param.option);
} else {
cmd.required(paramName, param.type).option(param.option);
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);
}
}
}
}
Expand All @@ -518,19 +526,21 @@ Local<Value> CommandClass::addOverload(const Arguments& args) {
for (int i = 0; i < arr.size(); ++i) {
CHECK_ARG_TYPE(arr.get(i), ValueKind::kString);
std::string paramName = arr.get(i).asString().toString();
if (paramMaps.find(paramName) != paramMaps.end()) {
ParamInfo& param = paramMaps[paramName];
if (param.optional) {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.optional(paramName, param.type, param.enumName).option(param.option);
} else {
cmd.optional(paramName, param.type).option(param.option);
}
} else {
if (param.type == ParamKind::Kind::Enum || param.type == ParamKind::Kind::SoftEnum) {
cmd.required(paramName, param.type, param.enumName).option(param.option);
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 {
cmd.required(paramName, param.type).option(param.option);
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);
}
}
}
}
Expand Down
15 changes: 3 additions & 12 deletions src/legacy/api/CommandAPI.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "api/CommandCompatibleAPI.h"
#include "ll/api/command/CommandHandle.h"
#include "ll/api/command/runtime/ParamKind.h"

extern ClassDefine<void> ParamTypeStaticBuilder;
extern ClassDefine<void> PermissionStaticBuilder;
Expand All @@ -18,18 +17,10 @@ enum class OldCommandPermissionLevel : schar {
Internal = 0x5,
};

struct ParamInfo {
ll::command::ParamKind::Kind type;
bool optional;
std::string enumName;
CommandParameterOption option;
};

class CommandClass : public ScriptClass {
std::string commandName;
std::string description;
std::unordered_map<std::string, ParamInfo> paramMaps;
inline ll::command::CommandHandle& get() {
std::string commandName;
std::string description;
inline ll::command::CommandHandle& get() {
return ll::command::CommandRegistrar::getInstance().getOrCreateCommand(commandName);
}
inline std::vector<std::string> parseStringList(Local<Array> arr) {
Expand Down
11 changes: 10 additions & 1 deletion src/lse/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
#include "PluginManager.h"
#include "ll/api/mod/Manifest.h"
#include "ll/api/mod/Mod.h"
#include "ll/api/command/runtime/ParamKind.h"

namespace lse {

class Plugin : public ll::mod::Mod {
friend PluginManager;

public:
std::unordered_map<std::string, std::vector<std::string>> registeredCommands;
struct ParamInfo {
std::string name;
ll::command::ParamKind::Kind type;
bool optional;
std::string enumName;
CommandParameterOption option;
};

std::unordered_map<std::string, std::vector<ParamInfo>> registeredCommands;

Plugin(const ll::mod::Manifest& manifest);
~Plugin();
Expand Down

0 comments on commit ad1033f

Please sign in to comment.