-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibDevConsole-1.0.lua
168 lines (137 loc) · 6.2 KB
/
LibDevConsole-1.0.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
assert(LibStub, "LibStub not found.");
local major, minor = "LibDevConsole", 1;
--- @class LibDevConsole
local LibDevConsole = LibStub:NewLibrary(major, minor);
if not LibDevConsole then
return;
end
local commandType = Enum.ConsoleCommandType;
local commandCategory = Enum.ConsoleCategory;
local colorType = Enum.ConsoleColorType;
local GetAllBaseCommands = C_Console and C_Console.GetAllCommands or ConsoleGetAllCommands;
LibDevConsole.CommandType = commandType;
LibDevConsole.CommandCategory = commandCategory;
LibDevConsole.ConsoleColor = colorType;
---@param message string
---@param color? Enum.ConsoleColorType
--- Adds a message to the console window
function LibDevConsole.AddMessage(message, color)
if not color or not tContains(colorType, color) then
color = colorType.DefaultColor;
end
DeveloperConsole:AddMessage(message, colorType.DefaultColor);
end
---@param message string
--- Adds an error message (red) to the console window
function LibDevConsole.AddError(message)
DeveloperConsole:AddMessage(message, colorType.ErrorColor);
end
---@param message string
--- Adds an echo (green message) to the console window
function LibDevConsole.AddEcho(message)
DeveloperConsole:AddMessage(message, colorType.DefaultGreen);
end
-- under construction, nothing to see here
local function HelpCommandOverride(_, helpText)
local helpColor = colorType.WarningColor;
local self = LibDevConsole;
if not helpText then
local helpStr = "Console help categories:\n"
local categories = {}
for k, _ in pairs(self.CommandCategory) do
tinsert(categories, k);
end
strjoin(", ", helpStr, unpack(categories));
self.AddMessage(helpStr, helpColor)
end
end
LibDevConsole.CustomCommandFunctions = { -- contains the function mappings of our custom commands
libdev = function() LibDevConsole.AddMessage("Hello world!") return true; end, -- secret :p
};
LibDevConsole.CustomCommandInfo = {}; -- table where our custom commands will be kept
LibDevConsole.AllCommands = {}; -- table that will contain all base commands + our custom ones, for auto-complete
local BaseCommands = GetAllBaseCommands();
local function UpdateCommands()
for _, command in ipairs(BaseCommands) do
if not tContains(LibDevConsole.AllCommands, command) then
tinsert(LibDevConsole.AllCommands, command);
end
end
for _, command in ipairs(LibDevConsole.CustomCommandInfo) do
if not tContains(LibDevConsole.AllCommands, command) then
tinsert(LibDevConsole.AllCommands, command);
end
end
end
UpdateCommands();
local function GetAllCommandsOverride()
return LibDevConsole.AllCommands;
end
if C_Console then
C_Console.GetAllCommands = GetAllCommandsOverride;
else
ConsoleGetAllCommands = GetAllCommandsOverride;
end
-- The return values from this are important - it returns two boolean values.
-- The first return indicates success, if false, the console will attempt to `ConsoleExec` the command itself.
-- Second return indicates whether or not the command should be added to the command history - should usually be true regardless of success.
local function CommandExecuteOverride(input)
assert(not issecure(), "THIS SHOULD NOT BE SECURE??");
local inputSplit = {strsplit(" ", input)};
local command = inputSplit[1];
local args = {select(2, unpack(inputSplit))};
if LibDevConsole.CustomCommandFunctions[command] then
-- if the command exists, we pcall it with any args
local ok, result = pcall(LibDevConsole.CustomCommandFunctions[command], unpack(args));
if not ok then
LibDevConsole.AddError("Error executing " .. command .. ": " .. result);
return false, true; -- returns false for failure, and true to add to history
else
-- returns the result of the command, or true, along with true to add to history
return result or true, true;
end
else
-- not our command - return false and let the console execute it
return false, true;
end
end
DeveloperConsole:SetExecuteCommandOverrideFunction(CommandExecuteOverride);
---@class ConsoleCommandInfo
--- the keys are important!
local ExampleCommandInfo = {
help = "Say 'hello world!'", -- help text that shows up in the auto-complete window
category = Enum.ConsoleCategory.Game, -- Enum.ConsoleCategory
command = "myCommand", -- the command itself
scriptParameters = "", -- not sure what this does
scriptContents = "", -- this is a mystery too
commandType = Enum.ConsoleCommandType.Script, -- Enum.ConsoleCommandType
commandFunc = function() LibDevConsole.AddMessage("Hello World!") end, -- this is the function the command executes
}
--- Register a custom console command
---@param commandInfo ConsoleCommandInfo
---@return boolean
function LibDevConsole.RegisterCommand(commandInfo)
local commandName = commandInfo.command;
assert(type(commandName) == "string", "CommandInfo.command is not a string.");
local commandFunc = commandInfo.commandFunc;
assert(type(commandFunc) == "function", "CommandInfo.commandFunc is not a function.");
commandInfo.commandFunc = nil; -- to clean up the return for the console auto-complete/search
assert(type(commandInfo.help) == "string", "CommandInfo.help is not a valid string.");
assert(tContains(Enum.ConsoleCommandType, commandInfo.commandType), "CommandInfo.commandType must be a valid ConsoleCommandType. (Enum.ConsoleCommandType)");
assert(tContains(Enum.ConsoleCategory, commandInfo.category), "CommandInfo.category must be a valid ConsoleCategory. (Enum.ConsoleCategory)");
if not commandInfo.scriptContents then
commandInfo.scriptContents = "";
end
if not commandInfo.scriptParameters then
commandInfo.scriptParameters = "";
end
if tContains(LibDevConsole.CustomCommandInfo, commandInfo) then
LibDevConsole.AddMessage("Attempted to register existing command: " .. commandName);
return false;
end
tinsert(LibDevConsole.CustomCommandInfo, commandInfo);
LibDevConsole.CustomCommandFunctions[commandName] = commandFunc;
UpdateCommands();
LibDevConsole.AddMessage("Registered command: " .. commandName);
return true;
end