Use a correct xml syntax for map config

This commit is contained in:
Benau 2018-09-11 20:57:19 +08:00
parent 300cdff07f
commit c778a22a3a
5 changed files with 66 additions and 37 deletions

View File

@ -186,11 +186,14 @@ void GroupUserConfigParam::addChild(UserConfigParam* child)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
template<typename T, typename U> template<typename T, typename U>
MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name, MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name,
const char* comment, std::map<T, U> default_value) const char* comment, std::array<std::string, 3> key_names,
std::map<T, U> default_value)
{ {
m_param_name = param_name; m_param_name = param_name;
all_params.push_back(this); all_params.push_back(this);
if (comment != NULL) m_comment = comment; if (comment != NULL) m_comment = comment;
m_key_names = key_names;
m_elements = default_value; m_elements = default_value;
} // MapUserConfigParam } // MapUserConfigParam
@ -209,12 +212,13 @@ MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name,
template<typename T, typename U> template<typename T, typename U>
MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name, MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name,
GroupUserConfigParam* group, const char* comment, GroupUserConfigParam* group, const char* comment,
std::map<T, U> default_value) std::array<std::string, 3> key_names, std::map<T, U> default_value)
{ {
m_param_name = param_name; m_param_name = param_name;
group->addChild(this); group->addChild(this);
if (comment != NULL) m_comment = comment; if (comment != NULL) m_comment = comment;
m_key_names = key_names;
m_elements = default_value; m_elements = default_value;
} // MapUserConfigParam } // MapUserConfigParam
@ -224,13 +228,14 @@ void MapUserConfigParam<T, U>::write(std::stringstream& stream) const
{ {
// comment // comment
if (m_comment.size() > 0) stream << " <!-- " << m_comment.c_str(); if (m_comment.size() > 0) stream << " <!-- " << m_comment.c_str();
stream << " -->\n <" << m_param_name.c_str() << "\n"; stream << " -->\n <" << m_param_name.c_str() << ">\n";
for (const auto& kv : m_elements) for (const auto& kv : m_elements)
{ {
stream << " " << kv.first << "=\"" << kv.second << "\"\n"; stream << " <" << m_key_names[0] << " " << m_key_names[1] <<
"=\"" << kv.first << "\" " << m_key_names[2] << "=\"" <<
kv.second << "\"/>\n";
} }
stream << " >\n";
stream << " </" << m_param_name.c_str() << ">\n\n"; stream << " </" << m_param_name.c_str() << ">\n\n";
} // write } // write
@ -241,10 +246,44 @@ void MapUserConfigParam<T, U>::findYourDataInAChildOf(const XMLNode* node)
const XMLNode* child = node->getNode(m_param_name); const XMLNode* child = node->getNode(m_param_name);
if (child == NULL) if (child == NULL)
{ {
//Log::error("User Config", "Couldn't find parameter group %s", m_param_name.c_str()); Log::error("User Config", "Couldn't find parameter group %s",
m_param_name.c_str());
return; return;
} }
child->get(&m_elements); for (unsigned i = 0; i < child->getNumNodes(); i++)
{
const std::string& map_name = m_key_names[0];
const std::string& key_name = m_key_names[1];
const std::string& value_name = m_key_names[2];
const XMLNode* map = child->getNode(i);
if (map->getName() != map_name)
{
Log::error("User Config", "Invalid name %s",
map->getName().c_str());
continue;
}
T key;
std::string key_string;
if (!map->get(key_name, &key_string) ||
!StringUtils::fromString(key_string, key))
{
Log::error("User Config", "Invalid key name %s",
key_name.c_str());
continue;
}
U value;
std::string value_string;
if (!map->get(value_name, &value_string) ||
!StringUtils::fromString(value_string, value))
{
Log::error("User Config", "Invalid value name %s",
value_name.c_str());
continue;
}
m_elements[key] = value;
}
} // findYourDataInAChildOf } // findYourDataInAChildOf
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -37,6 +37,7 @@
cause an undefined game action now cause an undefined game action now
6: Added stick configurations. 6: Added stick configurations.
*/ */
#include <array>
#include <iterator> #include <iterator>
#include <string> #include <string>
#include <map> #include <map>
@ -99,10 +100,13 @@ public:
}; // GroupUserConfigParam }; // GroupUserConfigParam
// ============================================================================ // ============================================================================
/** ATM only map with 1 key and 1 value is supported
*/
template<typename T, typename U> template<typename T, typename U>
class MapUserConfigParam : public UserConfigParam class MapUserConfigParam : public UserConfigParam
{ {
protected: protected:
std::array<std::string, 3> m_key_names;
std::map<T, U> m_elements; std::map<T, U> m_elements;
MapUserConfigParam(const char* param_name, MapUserConfigParam(const char* param_name,
const char* comment) const char* comment)
@ -115,13 +119,14 @@ protected:
public: public:
MapUserConfigParam(const char* param_name, MapUserConfigParam(const char* param_name,
const char* comment, const char* comment,
std::array<std::string, 3> key_names,
std::map<T, U> default_value); std::map<T, U> default_value);
MapUserConfigParam(const char* param_name, MapUserConfigParam(const char* param_name,
GroupUserConfigParam* group, GroupUserConfigParam* group,
const char* comment = NULL); const char* comment = NULL);
MapUserConfigParam(const char* param_name, MapUserConfigParam(const char* param_name,
GroupUserConfigParam* group, GroupUserConfigParam* group,
const char* comment, const char* comment, std::array<std::string, 3> key_names,
std::map<T, U> default_value); std::map<T, U> default_value);
void write(std::stringstream& stream) const; void write(std::stringstream& stream) const;
@ -722,9 +727,9 @@ namespace UserConfigParams
// ---- Networking // ---- Networking
PARAM_PREFIX StringToUIntUserConfigParam m_stun_servers PARAM_PREFIX StringToUIntUserConfigParam m_stun_servers
PARAM_DEFAULT(StringToUIntUserConfigParam("stun_servers", PARAM_DEFAULT(StringToUIntUserConfigParam("stun-servers",
"The stun servers that will be used to know the public address with" "The stun servers that will be used to know the public address with"
" port, LHS: server address, RHS: ping time.", " port", {{ "stun-server", "address", "ping" }},
{ {
{ "stun.12connect.com:3478", 0u }, { "stun.12connect.com:3478", 0u },
{ "stun.callwithus.com:3478", 0u }, { "stun.callwithus.com:3478", 0u },
@ -771,6 +776,7 @@ namespace UserConfigParams
PARAM_PREFIX UIntToUIntUserConfigParam m_num_karts_per_gamemode PARAM_PREFIX UIntToUIntUserConfigParam m_num_karts_per_gamemode
PARAM_DEFAULT(UIntToUIntUserConfigParam("num-karts-per-gamemode", PARAM_DEFAULT(UIntToUIntUserConfigParam("num-karts-per-gamemode",
"The Number of karts per gamemode.", "The Number of karts per gamemode.",
{{ "gamemode-list", "gamemode", "num-karts" }},
{ {
{ 0u, 4u }, { 0u, 4u },
{ 1002u, 5u }, { 1002u, 5u },

View File

@ -98,27 +98,6 @@ public:
int getHPR(core::vector3df *value) const; int getHPR(core::vector3df *value) const;
int getHPR(Vec3 *value) const; int getHPR(Vec3 *value) const;
template<typename T, typename U>
int get(std::map<T, U>* out_map) const
{
using namespace StringUtils;
for (auto& p : m_attributes)
{
T val_1;
if (!fromString(p.first, val_1))
{
return 0;
}
U val_2;
if (!fromString(wideToUtf8(p.second), val_2))
{
return 0;
}
(*out_map)[val_1] = val_2;
}
return (int)m_attributes.size();
}
bool hasChildNamed(const char* name) const; bool hasChildNamed(const char* name) const;
/** Handy functions to test the bit pattern returned by get(vector3df*).*/ /** Handy functions to test the bit pattern returned by get(vector3df*).*/

View File

@ -92,10 +92,11 @@ StringServerConfigParam::StringServerConfigParam(std::string default_value,
// ============================================================================ // ============================================================================
template<typename T, typename U> template<typename T, typename U>
MapServerConfigParam<T, U>::MapServerConfigParam(const char* param_name, MapServerConfigParam<T, U>::MapServerConfigParam(const char* param_name,
const char* comment, const char* comment, std::array<std::string, 3> key_names,
std::map<T, U> default_value) std::map<T, U> default_value)
: MapUserConfigParam<T, U>(param_name, comment) : MapUserConfigParam<T, U>(param_name, comment)
{ {
m_key_names = key_names;
m_elements = default_value; m_elements = default_value;
g_server_params.push_back(this); g_server_params.push_back(this);
} // MapServerConfigParam } // MapServerConfigParam

View File

@ -74,9 +74,11 @@ namespace ServerConfig
class MapServerConfigParam : public MapUserConfigParam<T, U> class MapServerConfigParam : public MapUserConfigParam<T, U>
{ {
private: private:
using MapUserConfigParam<T, U>::m_key_names;
using MapUserConfigParam<T, U>::m_elements; using MapUserConfigParam<T, U>::m_elements;
public: public:
MapServerConfigParam(const char* param_name, const char* comment, MapServerConfigParam(const char* param_name, const char* comment,
std::array<std::string, 3> key_names,
std::map<T, U> default_value); std::map<T, U> default_value);
using MapUserConfigParam<T, U>::operator=; using MapUserConfigParam<T, U>::operator=;
}; };
@ -251,16 +253,18 @@ namespace ServerConfig
SERVER_CFG_PREFIX StringToUIntServerConfigParam m_server_ip_ban_list SERVER_CFG_PREFIX StringToUIntServerConfigParam m_server_ip_ban_list
SERVER_CFG_DEFAULT(StringToUIntServerConfigParam("server-ip-ban-list", SERVER_CFG_DEFAULT(StringToUIntServerConfigParam("server-ip-ban-list",
"LHS: IP in X.X.X.X/Y (CIDR) format, use Y of 32 for a specific ip, " "ip: IP in X.X.X.X/Y (CIDR) format for banning, use Y of 32 for a "
"RHS: time epoch to expire, " "specific ip, expired-time: unix timestamp to expire, "
"if -1 (uint32_t max) than a permanent ban.", "if -1 (uint32_t max) than a permanent ban.",
{{ "ban", "ip", "expired-time" }},
{ { "0.0.0.0/0", 0u } })); { { "0.0.0.0/0", 0u } }));
SERVER_CFG_PREFIX UIntToUIntServerConfigParam m_server_online_id_ban_list SERVER_CFG_PREFIX UIntToUIntServerConfigParam m_server_online_id_ban_list
SERVER_CFG_DEFAULT(UIntToUIntServerConfigParam( SERVER_CFG_DEFAULT(UIntToUIntServerConfigParam(
"server-online-id-ban-list", "server-online-id-ban-list",
"LHS: online id, RHS: time epoch to expire, " "online-id: online id for banning, expired-time: unix timestamp to "
"if -1 (uint32_t max) than a permanent ban.", "expire, if -1 (uint32_t max) than a permanent ban.",
{{ "ban", "online-id", "expired-time" }},
{ { 0u, 0u } })); { { 0u, 0u } }));
// ======================================================================== // ========================================================================