mirror of
https://github.com/thunderbrewhq/thunderbrew
synced 2025-04-15 17:46:03 +03:00
feat(console): partially implement console command functions
This commit is contained in:
parent
e18afec28d
commit
23d537103e
86
src/console/Command.cpp
Normal file
86
src/console/Command.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "console/Command.hpp"
|
||||
#include <storm/Error.hpp>
|
||||
|
||||
int32_t ValidateFileName(const char* filename) {
|
||||
if (SStrStr(filename, "..") || SStrStr(filename, "\\")) {
|
||||
// TODO
|
||||
// ConsoleWrite("File Name cannot contain '\\' or '..'", ERROR_COLOR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* extension = SStrChrR(filename, '.');
|
||||
|
||||
if (extension && SStrCmpI(extension, ".wtf", -1)) {
|
||||
// TODO
|
||||
// ConsoleWrite("Only .wtf extensions are allowed", ERROR_COLOR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
TSHashTable<CONSOLECOMMAND, HASHKEY_STRI> g_consoleCommandHash;
|
||||
char g_commandHistory[HISTORY_DEPTH][CMD_BUFFER_SIZE];
|
||||
uint32_t g_commandHistoryIndex;
|
||||
|
||||
void ConsoleCommandDestroy() {
|
||||
g_consoleCommandHash.Clear();
|
||||
}
|
||||
|
||||
char* ConsoleCommandHistory(uint32_t index) {
|
||||
// Return a pointer to the buffer at the specified index
|
||||
return g_commandHistory[((g_commandHistoryIndex + (HISTORY_DEPTH - 1) - index) & (HISTORY_DEPTH - 1))];
|
||||
}
|
||||
|
||||
uint32_t ConsoleCommandHistoryDepth() {
|
||||
return HISTORY_DEPTH;
|
||||
}
|
||||
|
||||
void ConsoleCommandInitialize() {
|
||||
ConsoleCommandRegister("help", ConsoleCommand_Help, CONSOLE, "Provides help information about a command.");
|
||||
}
|
||||
|
||||
int32_t ConsoleCommandRegister(const char* command, int32_t (*handler)(const char*, const char*), CATEGORY category, const char* helpText) {
|
||||
STORM_ASSERT(command);
|
||||
STORM_ASSERT(handler);
|
||||
|
||||
if (SStrLen(command) > (MAX_CMD_LENGTH - 1) || g_consoleCommandHash.Ptr(command)) {
|
||||
// The command name exceeds MAX_CMD_LENGTH, minus the null terminator
|
||||
// or it has already been registered
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Register the new command
|
||||
auto commandPtr = g_consoleCommandHash.New(command, 0, 0);
|
||||
commandPtr->command = command;
|
||||
commandPtr->handler = handler;
|
||||
commandPtr->helpText = helpText;
|
||||
commandPtr->category = category;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ConsoleCommandUnregister(const char* command) {
|
||||
if (command) {
|
||||
auto commandPtr = g_consoleCommandHash.Ptr(command);
|
||||
if (commandPtr) {
|
||||
g_consoleCommandHash.Delete(commandPtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int32_t ConsoleCommand_Help(const char* command, const char* arguments) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t ConsoleCommand_Quit(const char* command, const char* arguments) {
|
||||
// TODO
|
||||
// ConsolePostClose()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t ConsoleCommand_Ver(const char* command, const char* arguments) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
57
src/console/Command.hpp
Normal file
57
src/console/Command.hpp
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef CONSOLE_COMMAND_HPP
|
||||
#define CONSOLE_COMMAND_HPP
|
||||
|
||||
#include <storm/Hash.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
#define EXEC_BUFFER_SIZE 8192
|
||||
#define CMD_BUFFER_SIZE 1024
|
||||
#define MAX_CMD_LENGTH 64
|
||||
#define HISTORY_DEPTH 32
|
||||
#define NOHELP nullptr
|
||||
|
||||
enum CATEGORY {
|
||||
DEBUG,
|
||||
GRAPHICS,
|
||||
CONSOLE,
|
||||
COMBAT,
|
||||
GAME,
|
||||
DEFAULT,
|
||||
NET,
|
||||
SOUND,
|
||||
GM,
|
||||
NONE,
|
||||
LAST
|
||||
};
|
||||
|
||||
struct CONSOLECOMMAND : TSHashObject<CONSOLECOMMAND, HASHKEY_STRI> {
|
||||
const char* command;
|
||||
int32_t (*handler)(const char*, const char*);
|
||||
const char* helpText;
|
||||
CATEGORY category;
|
||||
};
|
||||
|
||||
extern TSHashTable<CONSOLECOMMAND, HASHKEY_STRI> g_consoleCommandHash;
|
||||
extern char g_commandHistory[HISTORY_DEPTH][CMD_BUFFER_SIZE];
|
||||
extern uint32_t g_commandHistoryIndex;
|
||||
extern char g_ExecBuffer[EXEC_BUFFER_SIZE];
|
||||
|
||||
void ConsoleCommandDestroy();
|
||||
|
||||
char* ConsoleCommandHistory(uint32_t index);
|
||||
|
||||
uint32_t ConsoleCommandHistoryDepth();
|
||||
|
||||
void ConsoleCommandInitialize();
|
||||
|
||||
int32_t ConsoleCommandRegister(const char* command, int32_t (*handler)(const char*, const char*), CATEGORY category, const char* helpText);
|
||||
|
||||
void ConsoleCommandUnregister(const char* command);
|
||||
|
||||
int32_t ConsoleCommand_Help(const char* command, const char* arguments);
|
||||
|
||||
int32_t ConsoleCommand_Quit(const char* command, const char* arguments);
|
||||
|
||||
int32_t ConsoleCommand_Ver(const char* command, const char* arguments);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user