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>
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;
all_params.push_back(this);
if (comment != NULL) m_comment = comment;
m_key_names = key_names;
m_elements = default_value;
} // MapUserConfigParam
@ -209,12 +212,13 @@ MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name,
template<typename T, typename U>
MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name,
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;
group->addChild(this);
if (comment != NULL) m_comment = comment;
m_key_names = key_names;
m_elements = default_value;
} // MapUserConfigParam
@ -224,13 +228,14 @@ void MapUserConfigParam<T, U>::write(std::stringstream& stream) const
{
// comment
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)
{
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";
} // write
@ -241,10 +246,44 @@ void MapUserConfigParam<T, U>::findYourDataInAChildOf(const XMLNode* node)
const XMLNode* child = node->getNode(m_param_name);
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;
}
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
// ----------------------------------------------------------------------------

View File

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

View File

@ -98,27 +98,6 @@ public:
int getHPR(core::vector3df *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;
/** 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>
MapServerConfigParam<T, U>::MapServerConfigParam(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)
: MapUserConfigParam<T, U>(param_name, comment)
{
m_key_names = key_names;
m_elements = default_value;
g_server_params.push_back(this);
} // MapServerConfigParam

View File

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