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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
// TODO: replace 'WordProperties' with regex/glob expressions
// ex: '/give [0-9]* [_0-9a-zA-Z]+'
// or even create a domain specific language
// ex: '/give <ammount> {unitname} <team-ID>'
// user-input: '/give 10 armcom 1'
#include "WordCompletion.h"
#include "System/Log/ILog.h"
#include <stdexcept>
CWordCompletion::CWordCompletion()
{
Reset();
}
CWordCompletion::~CWordCompletion()
{
}
CWordCompletion* CWordCompletion::singleton = NULL;
void CWordCompletion::CreateInstance() {
if (singleton == NULL) {
singleton = new CWordCompletion();
} else {
throw std::logic_error(
"CWordCompletion singleton is already initialized");
}
}
void CWordCompletion::DestroyInstance() {
if (singleton != NULL) {
// SafeDelete
CWordCompletion* tmp = singleton;
singleton = NULL;
delete tmp;
} else {
// this might happen during shutdown after an unclean init
LOG_L(L_WARNING, "CWordCompletion singleton was not initialized"
" or is already destroyed");
}
}
void CWordCompletion::Reset()
{
words.clear();
WordProperties sl(true, false, false);
// key bindings (Game/UI/KeyBindings.cpp)
words["/bind "] = sl;
words["/unbind "] = sl;
words["/unbindall "] = sl;
words["/unbindaction "] = sl;
words["/unbindkeyset "] = sl;
words["/keyload "] = sl;
words["/keyreload "] = sl;
words["/keysave "] = sl;
words["/keysyms "] = sl;
words["/keycodes "] = sl;
words["/keyprint "] = sl;
words["/keydebug "] = sl;
words["/fakemeta "] = sl;
// camera handler (Game/CameraHandler.cpp)
words["/viewtaflip "] = sl;
// mini-map (Game/UI/MiniMap.cpp)
WordProperties mm(false, false, true);
words["fullproxy "] = mm;
words["drawcommands "] = mm;
words["drawprojectiles "] = mm;
words["icons "] = mm;
words["unitexp "] = mm;
words["unitsize "] = mm;
words["simplecolors "] = mm;
words["geometry "] = mm;
words["minimize "] = mm;
words["maximize "] = mm;
words["maxspect "] = mm;
// remote commands (Game/GameServer.cpp)
// TODO those commans are registered in Console, get the list from there
// This is best done with a new command class (eg. ConsoleCommand)
// deprecated idea, use *ActionExecutor's instead
words["/atm"] = sl;
words["/devlua "] = sl;
words["/editdefs "] = sl;
words["/give "] = sl;
words["/luagaia "] = sl;
words["/luarules "] = sl;
words["/nocost"] = sl;
words["/nospectatorchat "] = sl;
words["/reloadcob "] = sl;
words["/reloadcegs "] = sl;
words["/save "] = sl;
words["/skip "] = sl;
words["/skip +"] = sl;
words["/skip f"] = sl;
words["/skip f+"] = sl;
words["/spectator "] = sl;
words["/take "] = sl;
words["/team "] = sl;
words["/cheat "] = sl;
words["/nohelp "] = sl;
words["/nopause "] = sl;
words["/setmaxspeed "] = sl;
words["/setminspeed "] = sl;
words["/kick "] = sl;
words["/kickbynum "] = sl;
words["/mute "] = sl;
words["/mutebynum "] = sl;
}
void CWordCompletion::AddWord(const std::string& word, bool startOfLine,
bool unitName, bool miniMap)
{
if (!word.empty()) {
if (words.find(word) != words.end()) {
LOG_SL("WordCompletion", L_DEBUG,
"Tried to add already present word: %s", word.c_str());
return;
}
words[word] = WordProperties(startOfLine, unitName, miniMap);
}
}
void CWordCompletion::RemoveWord(const std::string& word)
{
words.erase(word);
}
std::vector<std::string> CWordCompletion::Complete(std::string& msg) const
{
std::vector<std::string> partials;
const bool unitName = (msg.find("/give ") == 0);
const bool miniMap = (msg.find("/minimap ") == 0);
// strip "a:" and "s:" prefixes
std::string prefix, rawmsg;
if ((msg.find_first_of("aAsS") == 0) && (msg[1] == ':')) {
prefix = msg.substr(0, 2);
rawmsg = msg.substr(2);
} else {
rawmsg = msg;
}
// the word fragment
std::string::size_type startPos = rawmsg.find_last_of(' ');
const bool startOfLine = (startPos == std::string::npos);
startPos = startOfLine ? 0 : startPos + 1;
const std::string fragment(rawmsg, startPos, std::string::npos);
if (fragment.empty()) {
return partials;
}
const std::string::size_type fragLen = fragment.size();
// pick a decent spot to start scanning
std::map<std::string, WordProperties>::const_iterator start;
start = words.lower_bound(fragment);
// make a list of valid possible matches
std::map<std::string, WordProperties>::const_iterator it;
for (it = start; it != words.end(); ++it) {
const int cmp = it->first.compare(0, fragLen, fragment);
if (cmp < 0) continue;
if (cmp > 0) break;
if ((!it->second.startOfLine || startOfLine) &&
(!it->second.unitName || unitName) &&
(!it->second.miniMap || miniMap)) {
partials.push_back(it->first);
}
}
if (partials.empty()) {
return partials; // no possible matches
}
if (partials.size() == 1) {
msg = prefix + rawmsg.substr(0, startPos) + partials[0];
partials.clear();
return partials; // single match
}
// add as much of the match as possible
const std::string& firstStr = partials[0];
const std::string& lastStr = partials[partials.size() - 1];
unsigned int i = 0;
unsigned int least = std::min(firstStr.size(), lastStr.size());
for (i = 0; i < least; i++) {
if (firstStr[i] != lastStr[i]) {
break;
}
}
msg = prefix + rawmsg.substr(0, startPos) + partials[0].substr(0, i);
return partials;
}
|