Fix names (issue)

This commit is contained in:
momo5502 2016-01-01 20:01:19 +01:00
parent 4ebf3849ab
commit 47c874d2b4
5 changed files with 51 additions and 2 deletions

View File

@ -8,6 +8,8 @@ namespace Components
Colors();
const char* GetName() { return "Colors"; };
static void Strip(const char* in, char* out, int max);
private:
static Dvar::Var NewColors;
@ -15,7 +17,5 @@ namespace Components
static char* GetClientName(int localClientNum, int index, char *buf, size_t size);
static void UpdateColorTable();
static void Strip(const char* in, char* out, int max);
};
}

View File

@ -129,6 +129,29 @@ namespace Components
{
// TODO: Register string dvars here
// Name watcher
Renderer::OnFrame([] ()
{
static std::string lastValidName = "Unknown Soldier";
std::string name = Dvar::Var("name").Get<char*>();
// Don't perform any checks if name didn't change
if (name == lastValidName) return;
char saneName[64] = { 0 };
Colors::Strip(Utils::Trim(name).data(), saneName, sizeof(saneName));
if (strlen(saneName) < 3 || (saneName[0] == '[' && saneName[1] == '{'))
{
Logger::Print("Username '%s' is invalid. It must at least be 3 characters long and not appear empty!\n", name.data());
Dvar::Var("name").Set(lastValidName);
}
else
{
lastValidName = name;
}
});
return Dvar::Register<const char*>(name, "Unknown Soldier", Dvar::Flag(flag | Game::dvar_flag::DVAR_FLAG_SAVED).val, description).Get<Game::dvar_t*>();
}

View File

@ -17,6 +17,9 @@
#include <sstream>
#include <utility>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <regex>
#include <thread>

View File

@ -77,6 +77,26 @@ namespace Utils
return hash;
}
// trim from start
std::string &LTrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
std::string &RTrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
std::string &Trim(std::string &s)
{
return LTrim(RTrim(s));
}
// Infostring class
void InfoString::Set(std::string key, std::string value)
{

View File

@ -6,6 +6,9 @@ namespace Utils
std::vector<std::string> Explode(const std::string& str, char delim);
void Replace(std::string &string, std::string find, std::string replace);
unsigned int OneAtATime(char *key, size_t len);
std::string &LTrim(std::string &s);
std::string &RTrim(std::string &s);
std::string &Trim(std::string &s);
class InfoString
{