C client: Simplify commands slightly

This commit is contained in:
UnknownShadow200 2018-10-10 13:16:00 +11:00
parent 7f4c2b12d5
commit 0a29a21d07

View File

@ -193,11 +193,10 @@ void Chat_AddOf(const String* text, int msgType) {
*#########################################################################################################################*/ *#########################################################################################################################*/
struct ChatCommand { struct ChatCommand {
const char* Name; const char* Name;
const char* Help[5];
void (*Execute)(const String* args, int argsCount); void (*Execute)(const String* args, int argsCount);
bool SingleplayerOnly; bool SingleplayerOnly;
const char* Help[5];
}; };
typedef void (*ChatCommandConstructor)(struct ChatCommand* cmd);
#define COMMANDS_PREFIX "/client" #define COMMANDS_PREFIX "/client"
#define COMMANDS_PREFIX_SPACE "/client " #define COMMANDS_PREFIX_SPACE "/client "
@ -215,14 +214,11 @@ static bool Commands_IsCommandPrefix(const String* input) {
|| String_CaselessEquals(input, &prefix); || String_CaselessEquals(input, &prefix);
} }
static void Commands_Register(ChatCommandConstructor constructor) { NOINLINE_ static void Commands_Register(struct ChatCommand* cmd) {
if (commands_count == Array_Elems(commands_list)) { if (commands_count == Array_Elems(commands_list)) {
ErrorHandler_Fail("Commands_Register - hit max client commands"); ErrorHandler_Fail("Commands_Register - hit max client commands");
} }
commands_list[commands_count++] = *cmd;
struct ChatCommand command = { 0 };
constructor(&command);
commands_list[commands_count++] = command;
} }
static struct ChatCommand* Commands_GetMatch(const String* cmdName) { static struct ChatCommand* Commands_GetMatch(const String* cmdName) {
@ -252,7 +248,8 @@ static struct ChatCommand* Commands_GetMatch(const String* cmdName) {
return match; return match;
} }
static void Commands_PrintDefined(void) { static void Commands_PrintDefault(void) {
Chat_AddRaw("&eList of client commands:");
char strBuffer[STRING_SIZE]; char strBuffer[STRING_SIZE];
String str = String_FromArray(strBuffer); String str = String_FromArray(strBuffer);
int i; int i;
@ -270,6 +267,7 @@ static void Commands_PrintDefined(void) {
} }
if (str.length) { Chat_Add(&str); } if (str.length) { Chat_Add(&str); }
Chat_AddRaw("&eTo see help for a command, type /client help [cmd name]");
} }
static void Commands_Execute(const String* input) { static void Commands_Execute(const String* input) {
@ -288,10 +286,7 @@ static void Commands_Execute(const String* input) {
text = String_UNSAFE_Substring(&text, offset, text.length - offset); text = String_UNSAFE_Substring(&text, offset, text.length - offset);
if (!text.length) { /* only / or /client */ if (!text.length) { /* only / or /client */
Chat_AddRaw("&eList of client commands:"); Commands_PrintDefault(); return;
Commands_PrintDefined();
Chat_AddRaw("&eTo see help for a command, type &a/client help [cmd name]");
return;
} }
String args[10]; String args[10];
@ -305,36 +300,31 @@ static void Commands_Execute(const String* input) {
/*########################################################################################################################* /*########################################################################################################################*
*-------------------------------------------------------Help command------------------------------------------------------* *------------------------------------------------------Simple commands----------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static void HelpCommand_Execute(const String* args, int argsCount) { static void HelpCommand_Execute(const String* args, int argsCount) {
if (argsCount == 1) { if (argsCount == 1) {
Chat_AddRaw("&eList of client commands:"); Commands_PrintDefault(); return;
Commands_PrintDefined(); }
Chat_AddRaw("&eTo see help for a command, type /client help [cmd name]");
} else {
struct ChatCommand* cmd = Commands_GetMatch(&args[1]);
if (!cmd) return;
int i; struct ChatCommand* cmd = Commands_GetMatch(&args[1]);
for (i = 0; i < Array_Elems(cmd->Help); i++) { if (!cmd) return;
if (!cmd->Help[i]) continue;
Chat_AddRaw(cmd->Help[i]); int i;
} for (i = 0; i < Array_Elems(cmd->Help); i++) {
if (!cmd->Help[i]) continue;
Chat_AddRaw(cmd->Help[i]);
} }
} }
static void HelpCommand_Make(struct ChatCommand* cmd) { struct ChatCommand HelpCommand_Instance = {
cmd->Name = "Help"; "Help", HelpCommand_Execute, false,
cmd->Help[0] = "&a/client help [command name]"; {
cmd->Help[1] = "&eDisplays the help for the given command."; "&a/client help [command name]",
cmd->Execute = HelpCommand_Execute; "&eDisplays the help for the given command.",
} }
};
/*########################################################################################################################*
*------------------------------------------------------GpuInfo command----------------------------------------------------*
*#########################################################################################################################*/
static void GpuInfoCommand_Execute(const String* args, int argsCount) { static void GpuInfoCommand_Execute(const String* args, int argsCount) {
Gfx_UpdateApiInfo(); Gfx_UpdateApiInfo();
int i; int i;
@ -344,17 +334,14 @@ static void GpuInfoCommand_Execute(const String* args, int argsCount) {
} }
} }
static void GpuInfoCommand_Make(struct ChatCommand* cmd) { struct ChatCommand GpuInfoCommand_Instance = {
cmd->Name = "GpuInfo"; "GpuInfo", GpuInfoCommand_Execute, false,
cmd->Help[0] = "&a/client gpuinfo"; {
cmd->Help[1] = "&eDisplays information about your GPU."; "&a/client gpuinfo",
cmd->Execute = GpuInfoCommand_Execute; "&eDisplays information about your GPU.",
} }
};
/*########################################################################################################################*
*----------------------------------------------------RenderType command---------------------------------------------------*
*#########################################################################################################################*/
static void RenderTypeCommand_Execute(const String* args, int argsCount) { static void RenderTypeCommand_Execute(const String* args, int argsCount) {
if (argsCount == 1) { if (argsCount == 1) {
Chat_AddRaw("&e/client: &cYou didn't specify a new render type."); return; Chat_AddRaw("&e/client: &cYou didn't specify a new render type."); return;
@ -372,15 +359,16 @@ static void RenderTypeCommand_Execute(const String* args, int argsCount) {
} }
} }
static void RenderTypeCommand_Make(struct ChatCommand* cmd) { struct ChatCommand RenderTypeCommand_Instance = {
cmd->Name = "RenderType"; "RenderType", RenderTypeCommand_Execute, false,
cmd->Help[0] = "&a/client rendertype [normal/legacy/legacyfast]"; {
cmd->Help[1] = "&bnormal: &eDefault renderer, with all environmental effects enabled."; "&a/client rendertype [normal/legacy/legacyfast]",
cmd->Help[2] = "&blegacy: &eMay be slightly slower than normal, but produces the same environmental effects."; "&bnormal: &eDefault renderer, with all environmental effects enabled.",
cmd->Help[3] = "&blegacyfast: &eSacrifices clouds, fog and overhead sky for faster performance."; "&blegacy: &eMay be slightly slower than normal, but produces the same environmental effects.",
cmd->Help[4] = "&bnormalfast: &eSacrifices clouds, fog and overhead sky for faster performance."; "&blegacyfast: &eSacrifices clouds, fog and overhead sky for faster performance.",
cmd->Execute = RenderTypeCommand_Execute; "&bnormalfast: &eSacrifices clouds, fog and overhead sky for faster performance.",
} }
};
static void ResolutionCommand_Execute(const String* args, int argsCount) { static void ResolutionCommand_Execute(const String* args, int argsCount) {
int width, height; int width, height;
@ -397,32 +385,30 @@ static void ResolutionCommand_Execute(const String* args, int argsCount) {
} }
} }
static void ResolutionCommand_Make(struct ChatCommand* cmd) { struct ChatCommand ResolutionCommand_Instance = {
cmd->Name = "Resolution"; "Resolution", ResolutionCommand_Execute, false,
cmd->Help[0] = "&a/client resolution [width] [height]"; {
cmd->Help[1] = "&ePrecisely sets the size of the rendered window."; "&a/client resolution [width] [height]",
cmd->Execute = ResolutionCommand_Execute; "&ePrecisely sets the size of the rendered window.",
} }
};
static void ModelCommand_Execute(const String* args, int argsCount) { static void ModelCommand_Execute(const String* args, int argsCount) {
if (argsCount == 1) { if (argsCount == 1) {
Chat_AddRaw("&e/client model: &cYou didn't specify a model name."); Chat_AddRaw("&e/client model: &cYou didn't specify a model name.");
} else { } else {
char modelBuffer[STRING_SIZE]; Entity_SetModel(&LocalPlayer_Instance.Base, &args[1]);
String model = String_FromArray(modelBuffer);
String_AppendString(&model, &args[1]);
Entity_SetModel(&LocalPlayer_Instance.Base, &model);
} }
} }
static void ModelCommand_Make(struct ChatCommand* cmd) { struct ChatCommand ModelCommand_Instance = {
cmd->Name = "Model"; "Model", ModelCommand_Execute, true,
cmd->Help[0] = "&a/client model [name]"; {
cmd->Help[1] = "&bnames: &echibi, chicken, creeper, human, pig, sheep"; "&a/client model [name]",
cmd->Help[2] = "&e skeleton, spider, zombie, sitting, <numerical block id>"; "&bnames: &echibi, chicken, creeper, human, pig, sheep",
cmd->SingleplayerOnly = true; "&e skeleton, spider, zombie, sitting, <numerical block id>",
cmd->Execute = ModelCommand_Execute; }
} };
/*########################################################################################################################* /*########################################################################################################################*
@ -516,16 +502,16 @@ static void CuboidCommand_Execute(const String* args, int argsCount) {
cuboid_hooked = true; cuboid_hooked = true;
} }
static void CuboidCommand_Make(struct ChatCommand* cmd) { struct ChatCommand CuboidCommand_Instance = {
cmd->Name = "Cuboid"; "Cuboid", CuboidCommand_Execute, true,
cmd->Help[0] = "&a/client cuboid [block] [persist]"; {
cmd->Help[1] = "&eFills the 3D rectangle between two points with [block]."; "&a/client cuboid [block] [persist]",
cmd->Help[2] = "&eIf no block is given, uses your currently held block."; "&eFills the 3D rectangle between two points with [block].",
cmd->Help[3] = "&e If persist is given and is \"yes\", then the command"; "&eIf no block is given, uses your currently held block.",
cmd->Help[4] = "&e will repeatedly cuboid, without needing to be typed in again."; "&e If persist is given and is \"yes\", then the command",
cmd->SingleplayerOnly = true; "&e will repeatedly cuboid, without needing to be typed in again.",
cmd->Execute = CuboidCommand_Execute; }
} };
/*########################################################################################################################* /*########################################################################################################################*
@ -548,13 +534,13 @@ static void TeleportCommand_Execute(const String* args, int argsCount) {
} }
} }
static void TeleportCommand_Make(struct ChatCommand* cmd) { struct ChatCommand TeleportCommand_Instance = {
cmd->Name = "TP"; "TP", TeleportCommand_Execute, true,
cmd->Help[0] = "&a/client tp [x y z]"; {
cmd->Help[1] = "&eMoves you to the given coordinates."; "&a/client tp [x y z]",
cmd->SingleplayerOnly = true; "&eMoves you to the given coordinates.",
cmd->Execute = TeleportCommand_Execute; }
} };
/*########################################################################################################################* /*########################################################################################################################*
@ -573,13 +559,13 @@ void Chat_Send(const String* text, bool logUsage) {
} }
static void Chat_Init(void) { static void Chat_Init(void) {
Commands_Register(GpuInfoCommand_Make); Commands_Register(&GpuInfoCommand_Instance);
Commands_Register(HelpCommand_Make); Commands_Register(&HelpCommand_Instance);
Commands_Register(RenderTypeCommand_Make); Commands_Register(&RenderTypeCommand_Instance);
Commands_Register(ResolutionCommand_Make); Commands_Register(&ResolutionCommand_Instance);
Commands_Register(ModelCommand_Make); Commands_Register(&ModelCommand_Instance);
Commands_Register(CuboidCommand_Make); Commands_Register(&CuboidCommand_Instance);
Commands_Register(TeleportCommand_Make); Commands_Register(&TeleportCommand_Instance);
} }
static void Chat_Reset(void) { static void Chat_Reset(void) {