Add more to xml reading

This commit is contained in:
Flakebi 2015-04-19 02:56:15 +02:00
parent cedd816ca1
commit c22d5c8e6e
9 changed files with 406 additions and 282 deletions

File diff suppressed because it is too large Load Diff

View File

@ -209,10 +209,10 @@ public:
* *
* \param type The characteristic that should be modified. * \param type The characteristic that should be modified.
* \param value The current value and result at the same time. * \param value The current value and result at the same time.
* \param isSet If the current value was already set (so it can be used * \param is_set If the current value was already set (so it can be used
* for computations. * for computations.
*/ */
virtual void process(CharacteristicType type, Value value, bool *isSet) const; virtual void process(CharacteristicType type, Value value, bool *is_set) const;
static ValueType getType(CharacteristicType type); static ValueType getType(CharacteristicType type);
static std::string getName(CharacteristicType type); static std::string getName(CharacteristicType type);

View File

@ -58,15 +58,15 @@ void CachedCharacteristics::updateSource()
{ {
SaveValue &v = m_values[i]; SaveValue &v = m_values[i];
bool isSet = false; bool is_set = false;
switch (getType(static_cast<CharacteristicType>(i))) switch (getType(static_cast<CharacteristicType>(i)))
{ {
case TYPE_FLOAT: case TYPE_FLOAT:
{ {
float value; float value;
float *ptr = static_cast<float*>(v.content); float *ptr = static_cast<float*>(v.content);
m_origin->process(static_cast<CharacteristicType>(i), &value, &isSet); m_origin->process(static_cast<CharacteristicType>(i), &value, &is_set);
if (isSet) if (is_set)
{ {
if (!ptr) if (!ptr)
{ {
@ -90,8 +90,8 @@ void CachedCharacteristics::updateSource()
{ {
std::vector<float> value; std::vector<float> value;
std::vector<float> *ptr = static_cast<std::vector<float>*>(v.content); std::vector<float> *ptr = static_cast<std::vector<float>*>(v.content);
m_origin->process(static_cast<CharacteristicType>(i), &value, &isSet); m_origin->process(static_cast<CharacteristicType>(i), &value, &is_set);
if (isSet) if (is_set)
{ {
if (!ptr) if (!ptr)
{ {
@ -116,8 +116,8 @@ void CachedCharacteristics::updateSource()
{ {
InterpolationArray value; InterpolationArray value;
InterpolationArray *ptr = static_cast<InterpolationArray*>(v.content); InterpolationArray *ptr = static_cast<InterpolationArray*>(v.content);
m_origin->process(static_cast<CharacteristicType>(i), &value, &isSet); m_origin->process(static_cast<CharacteristicType>(i), &value, &is_set);
if (isSet) if (is_set)
{ {
if (!ptr) if (!ptr)
{ {
@ -147,7 +147,7 @@ const SkiddingProperties* CachedCharacteristics::getSkiddingProperties() const
return m_origin->getSkiddingProperties(); return m_origin->getSkiddingProperties();
} }
void CachedCharacteristics::process(CharacteristicType type, Value value, bool *isSet) const void CachedCharacteristics::process(CharacteristicType type, Value value, bool *is_set) const
{ {
void *v = m_values[type].content; void *v = m_values[type].content;
if (v) if (v)
@ -164,7 +164,7 @@ void CachedCharacteristics::process(CharacteristicType type, Value value, bool *
*value.ia = *static_cast<InterpolationArray*>(v); *value.ia = *static_cast<InterpolationArray*>(v);
break; break;
} }
*isSet = true; *is_set = true;
} }
} }

View File

@ -48,7 +48,7 @@ public:
void updateSource(); void updateSource();
virtual const SkiddingProperties* getSkiddingProperties() const; virtual const SkiddingProperties* getSkiddingProperties() const;
virtual void process(CharacteristicType type, Value value, bool *isSet) const; virtual void process(CharacteristicType type, Value value, bool *is_set) const;
}; };
#endif #endif

View File

@ -34,9 +34,9 @@ const SkiddingProperties* CombinedCharacteristics::getSkiddingProperties() const
return nullptr; return nullptr;
} }
void CombinedCharacteristics::process(CharacteristicType type, Value value, bool *isSet) const void CombinedCharacteristics::process(CharacteristicType type, Value value, bool *is_set) const
{ {
for (const AbstractCharacteristics *characteristic : m_childs) for (const AbstractCharacteristics *characteristic : m_childs)
characteristic->process(type, value, isSet); characteristic->process(type, value, is_set);
} }

View File

@ -30,7 +30,7 @@ public:
void addCharacteristic(const AbstractCharacteristics *characteristic); void addCharacteristic(const AbstractCharacteristics *characteristic);
virtual const SkiddingProperties* getSkiddingProperties() const; virtual const SkiddingProperties* getSkiddingProperties() const;
virtual void process(CharacteristicType type, Value value, bool *isSet) const; virtual void process(CharacteristicType type, Value value, bool *is_set) const;
}; };
#endif #endif

View File

@ -18,6 +18,10 @@
#include "karts/xml_characteristics.hpp" #include "karts/xml_characteristics.hpp"
#include "utils/interpolation_array.hpp"
#include "utils/log.hpp"
#include "utils/string_utils.hpp"
#include "io/xml_node.hpp" #include "io/xml_node.hpp"
XmlCharacteristics::XmlCharacteristics(const XMLNode *node) : XmlCharacteristics::XmlCharacteristics(const XMLNode *node) :
@ -33,21 +37,141 @@ const SkiddingProperties* XmlCharacteristics::getSkiddingProperties() const
return m_skidding; return m_skidding;
} }
void XmlCharacteristics::process(CharacteristicType type, Value value, bool *isSet) const void XmlCharacteristics::process(CharacteristicType type, Value value, bool *is_set) const
{ {
switch (getType(type)) switch (getType(type))
{ {
case TYPE_FLOAT: case TYPE_FLOAT:
processFloat(m_values[type], value.f, isSet); processFloat(m_values[type], value.f, is_set);
break; break;
case TYPE_FLOAT_VECTOR: case TYPE_FLOAT_VECTOR:
{
const std::vector<std::string> processors =
StringUtils::split(m_values[type], ' ');
if (*is_set)
{
if (processors.size() != value.fv->size())
{
Log::error("XmlCharacteristics::process",
"FloatVectors have different sizes for %s",
getName(type).c_str());
break; break;
}
std::vector<float>::iterator fit = value.fv->begin();
for (std::vector<std::string>::const_iterator it = processors.begin();
it != processors.end(); it++, fit++)
processFloat(*it, &*fit, is_set);
}
else
{
value.fv->resize(processors.size());
std::vector<float>::iterator fit = value.fv->begin();
for (std::vector<std::string>::const_iterator it = processors.begin();
it != processors.end(); it++, fit++)
{
*is_set = false;
processFloat(*it, &*fit, is_set);
if (!*is_set)
{
Log::error("XmlCharacteristics::process", "Can't process %s",
getName(type).c_str());
value.fv->clear();
break;
}
}
}
break;
}
case TYPE_INTERPOLATION_ARRAY: case TYPE_INTERPOLATION_ARRAY:
{
const std::vector<std::string> processors =
StringUtils::split(m_values[type], ' ');
if (*is_set)
{
if (processors.size() != value.fv->size())
{
Log::error("XmlCharacteristics::process",
"InterpolationArrays have different sizes for %s",
getName(type).c_str());
break; break;
} }
else
{
for (std::vector<std::string>::const_iterator it = processors.begin();
it != processors.end(); it++)
{
std::vector<std::string> pair = StringUtils::split(*it,':');
if (!pair.size() == 2)
Log::error("XmlCharacteristics::process",
"Can't process %s: Wrong format", getName(type).c_str());
else
{
float x;
if (!StringUtils::fromString(pair[0], x))
Log::error("XmlCharacteristics::process",
"Can't process %s: Not a float", getName(type).c_str());
else
{
// Search the index of this x value
bool found = false;
for (unsigned int i = 0; i < value.ia->size(); i++)
{
if (value.ia->getX(i) == x)
{
float val;
processFloat(pair[1], &val, is_set);
value.ia->setY(i, val);
break;
}
}
if (!found)
Log::error("XmlCharacteristics::process",
"Can't find the %f in %s", x,
getName(type).c_str());
}
}
}
}
}
else
{
for (std::vector<std::string>::const_iterator it = processors.begin();
it != processors.end(); it++)
{
std::vector<std::string> pair = StringUtils::split(*it,':');
if (!pair.size() == 2)
Log::error("XmlCharacteristics::process",
"Can't process %s: Wrong format", getName(type).c_str());
else
{
float x;
if (!StringUtils::fromString(pair[0], x))
Log::error("XmlCharacteristics::process",
"Can't process %s: Not a float", getName(type).c_str());
else
{
float val;
*is_set = false;
processFloat(pair[1], &val, is_set);
if (!*is_set)
{
Log::error("XmlCharacteristics::process", "Can't process %s",
getName(type).c_str());
value.ia->clear();
break;
}
value.ia->push_back(x, val);
}
}
}
}
break;
}
}
} }
void XmlCharacteristics::processFloat(const std::string &processor, float *value, bool *isSet) void XmlCharacteristics::processFloat(const std::string &processor, float *value, bool *is_set)
{ {
} }

View File

@ -36,12 +36,12 @@ public:
XmlCharacteristics(const XMLNode *node = nullptr); XmlCharacteristics(const XMLNode *node = nullptr);
virtual const SkiddingProperties* getSkiddingProperties() const; virtual const SkiddingProperties* getSkiddingProperties() const;
virtual void process(CharacteristicType type, Value value, bool *isSet) const; virtual void process(CharacteristicType type, Value value, bool *is_set) const;
void load(const XMLNode *node); void load(const XMLNode *node);
private: private:
static void processFloat(const std::string &processor, float *value, bool *isSet); static void processFloat(const std::string &processor, float *value, bool *is_set);
}; };
#endif #endif

View File

@ -185,9 +185,9 @@ def main():
print("""{3} AbstractCharacteristics::get{1}() const print("""{3} AbstractCharacteristics::get{1}() const
{{ {{
{0} result; {0} result;
bool isSet = false; bool is_set = false;
process({2}, &result, &isSet); process({2}, &result, &is_set);
if (!isSet) if (!is_set)
Log::fatal("AbstractCharacteristics", "Can't get characteristic %s", getName({2}).c_str()); Log::fatal("AbstractCharacteristics", "Can't get characteristic %s", getName({2}).c_str());
return {4}; return {4};
}} }}