Add xml reading

This commit is contained in:
Flakebi 2015-04-19 00:11:39 +02:00
parent ddb850b149
commit c1ed8790ef
3 changed files with 217 additions and 20 deletions

View File

@ -20,12 +20,12 @@
#include "io/xml_node.hpp"
XmlCharacteristics::XmlCharacteristics(const std::string &filename) :
XmlCharacteristics::XmlCharacteristics(const XMLNode *node) :
m_values(CHARACTERISTIC_COUNT),
m_skidding(nullptr)
{
if (!filename.empty())
load(filename);
if (node)
load(node);
}
const SkiddingProperties* XmlCharacteristics::getSkiddingProperties() const
@ -35,11 +35,193 @@ const SkiddingProperties* XmlCharacteristics::getSkiddingProperties() const
void XmlCharacteristics::process(CharacteristicType type, Value value, bool &isSet) const
{
//TODO
switch (getType(type))
{
case TYPE_FLOAT:
break;
case TYPE_FLOAT_VECTOR:
break;
case TYPE_INTERPOLATION_ARRAY:
break;
}
}
void XmlCharacteristics::load(const std::string &filename)
float XmlCharacteristics::processFloat(float value, std::string processor)
{
const XMLNode* root = new XMLNode(filename);
}
void XmlCharacteristics::load(const XMLNode *node)
{
// Script-generated content
if (const XMLNode *sub_node = node->getNode("Suspension"))
{
sub_node->get("stiffness", &m_values[SUSPENSION_STIFFNESS]);
sub_node->get("rest", &m_values[SUSPENSION_REST]);
sub_node->get("travel-cm", &m_values[SUSPENSION_TRAVEL_CM]);
sub_node->get("exp-spring-response", &m_values[SUSPENSION_EXP_SPRING_RESPONSE]);
sub_node->get("max-force", &m_values[SUSPENSION_MAX_FORCE]);
}
if (const XMLNode *sub_node = node->getNode("Stability"))
{
sub_node->get("roll-influence", &m_values[STABILITY_ROLL_INFLUENCE]);
sub_node->get("chassis-linear-damping", &m_values[STABILITY_CHASSIS_LINEAR_DAMPING]);
sub_node->get("chassis-angular-damping", &m_values[STABILITY_CHASSIS_ANGULAR_DAMPING]);
sub_node->get("downward-impulse-factor", &m_values[STABILITY_DOWNWARD_IMPULSE_FACTOR]);
sub_node->get("track-connection-accel", &m_values[STABILITY_TRACK_CONNECTION_ACCEL]);
sub_node->get("smooth-flying-impulse", &m_values[STABILITY_SMOOTH_FLYING_IMPULSE]);
}
if (const XMLNode *sub_node = node->getNode("Turn"))
{
sub_node->get("radius", &m_values[TURN_RADIUS]);
sub_node->get("time-reset-steer", &m_values[TURN_TIME_RESET_STEER]);
sub_node->get("time-full-steer", &m_values[TURN_TIME_FULL_STEER]);
}
if (const XMLNode *sub_node = node->getNode("Engine"))
{
sub_node->get("power", &m_values[ENGINE_POWER]);
sub_node->get("max-speed", &m_values[ENGINE_MAX_SPEED]);
sub_node->get("brake-factor", &m_values[ENGINE_BRAKE_FACTOR]);
sub_node->get("brake-time-increase", &m_values[ENGINE_BRAKE_TIME_INCREASE]);
sub_node->get("max-speed-reverse-ratio", &m_values[ENGINE_MAX_SPEED_REVERSE_RATIO]);
}
if (const XMLNode *sub_node = node->getNode("Gear"))
{
sub_node->get("switch-ratio", &m_values[GEAR_SWITCH_RATIO]);
sub_node->get("power-increase", &m_values[GEAR_POWER_INCREASE]);
}
if (const XMLNode *sub_node = node->getNode(""))
{
sub_node->get("mass", &m_values[MASS]);
}
if (const XMLNode *sub_node = node->getNode("Wheels"))
{
sub_node->get("damping-relaxation", &m_values[WHEELS_DAMPING_RELAXATION]);
sub_node->get("damping-compression", &m_values[WHEELS_DAMPING_COMPRESSION]);
sub_node->get("radius", &m_values[WHEELS_RADIUS]);
sub_node->get("position", &m_values[WHEELS_POSITION]);
}
if (const XMLNode *sub_node = node->getNode("Camera"))
{
sub_node->get("distance", &m_values[CAMERA_DISTANCE]);
sub_node->get("forward-up-angle", &m_values[CAMERA_FORWARD_UP_ANGLE]);
sub_node->get("backward-up-angle", &m_values[CAMERA_BACKWARD_UP_ANGLE]);
}
if (const XMLNode *sub_node = node->getNode("Jump"))
{
sub_node->get("animation-time", &m_values[JUMP_ANIMATION_TIME]);
}
if (const XMLNode *sub_node = node->getNode("Lean"))
{
sub_node->get("max", &m_values[LEAN_MAX]);
sub_node->get("speed", &m_values[LEAN_SPEED]);
}
if (const XMLNode *sub_node = node->getNode("Anvil"))
{
sub_node->get("duration", &m_values[ANVIL_DURATION]);
sub_node->get("weight", &m_values[ANVIL_WEIGHT]);
sub_node->get("speed-factor", &m_values[ANVIL_SPEED_FACTOR]);
}
if (const XMLNode *sub_node = node->getNode("Parachute"))
{
sub_node->get("friction", &m_values[PARACHUTE_FRICTION]);
sub_node->get("duration", &m_values[PARACHUTE_DURATION]);
sub_node->get("duration-other", &m_values[PARACHUTE_DURATION_OTHER]);
sub_node->get("lbound-franction", &m_values[PARACHUTE_LBOUND_FRANCTION]);
sub_node->get("ubound-franction", &m_values[PARACHUTE_UBOUND_FRANCTION]);
sub_node->get("max-speed", &m_values[PARACHUTE_MAX_SPEED]);
}
if (const XMLNode *sub_node = node->getNode("Bubblegum"))
{
sub_node->get("duration", &m_values[BUBBLEGUM_DURATION]);
sub_node->get("speed-fraction", &m_values[BUBBLEGUM_SPEED_FRACTION]);
sub_node->get("torque", &m_values[BUBBLEGUM_TORQUE]);
sub_node->get("fade-in-time", &m_values[BUBBLEGUM_FADE_IN_TIME]);
sub_node->get("shield-duration", &m_values[BUBBLEGUM_SHIELD_DURATION]);
}
if (const XMLNode *sub_node = node->getNode("Zipper"))
{
sub_node->get("duration", &m_values[ZIPPER_DURATION]);
sub_node->get("force", &m_values[ZIPPER_FORCE]);
sub_node->get("speed-gain", &m_values[ZIPPER_SPEED_GAIN]);
sub_node->get("speed-increase", &m_values[ZIPPER_SPEED_INCREASE]);
sub_node->get("fade-out-time", &m_values[ZIPPER_FADE_OUT_TIME]);
}
if (const XMLNode *sub_node = node->getNode("Swatter"))
{
sub_node->get("duration", &m_values[SWATTER_DURATION]);
sub_node->get("distance", &m_values[SWATTER_DISTANCE]);
sub_node->get("squash-duration", &m_values[SWATTER_SQUASH_DURATION]);
sub_node->get("squash-slowdown", &m_values[SWATTER_SQUASH_SLOWDOWN]);
}
if (const XMLNode *sub_node = node->getNode("Plunger"))
{
sub_node->get("max-length", &m_values[PLUNGER_MAX_LENGTH]);
sub_node->get("force", &m_values[PLUNGER_FORCE]);
sub_node->get("duration", &m_values[PLUNGER_DURATION]);
sub_node->get("speed-increase", &m_values[PLUNGER_SPEED_INCREASE]);
sub_node->get("fade-out-time", &m_values[PLUNGER_FADE_OUT_TIME]);
sub_node->get("in-face-time", &m_values[PLUNGER_IN_FACE_TIME]);
}
if (const XMLNode *sub_node = node->getNode("Startup"))
{
sub_node->get("time", &m_values[STARTUP_TIME]);
sub_node->get("boost", &m_values[STARTUP_BOOST]);
}
if (const XMLNode *sub_node = node->getNode("Rescue"))
{
sub_node->get("duration", &m_values[RESCUE_DURATION]);
sub_node->get("vert-offset", &m_values[RESCUE_VERT_OFFSET]);
sub_node->get("height", &m_values[RESCUE_HEIGHT]);
}
if (const XMLNode *sub_node = node->getNode("Explosion"))
{
sub_node->get("duration", &m_values[EXPLOSION_DURATION]);
sub_node->get("radius", &m_values[EXPLOSION_RADIUS]);
sub_node->get("invulnerability-time", &m_values[EXPLOSION_INVULNERABILITY_TIME]);
}
if (const XMLNode *sub_node = node->getNode("Nitro"))
{
sub_node->get("duration", &m_values[NITRO_DURATION]);
sub_node->get("engine-force", &m_values[NITRO_ENGINE_FORCE]);
sub_node->get("consumption", &m_values[NITRO_CONSUMPTION]);
sub_node->get("small-container", &m_values[NITRO_SMALL_CONTAINER]);
sub_node->get("big-container", &m_values[NITRO_BIG_CONTAINER]);
sub_node->get("max-speed-increase", &m_values[NITRO_MAX_SPEED_INCREASE]);
sub_node->get("fade-out-time", &m_values[NITRO_FADE_OUT_TIME]);
sub_node->get("max", &m_values[NITRO_MAX]);
}
if (const XMLNode *sub_node = node->getNode("Slipstream"))
{
sub_node->get("duration", &m_values[SLIPSTREAM_DURATION]);
sub_node->get("length", &m_values[SLIPSTREAM_LENGTH]);
sub_node->get("width", &m_values[SLIPSTREAM_WIDTH]);
sub_node->get("collect-time", &m_values[SLIPSTREAM_COLLECT_TIME]);
sub_node->get("use-time", &m_values[SLIPSTREAM_USE_TIME]);
sub_node->get("add-power", &m_values[SLIPSTREAM_ADD_POWER]);
sub_node->get("min-speed", &m_values[SLIPSTREAM_MIN_SPEED]);
sub_node->get("max-speed-increase", &m_values[SLIPSTREAM_MAX_SPEED_INCREASE]);
sub_node->get("fade-out-time", &m_values[SLIPSTREAM_FADE_OUT_TIME]);
}
}

View File

@ -23,6 +23,8 @@
#include <string>
class XMLNode;
class XmlCharacteristics : public AbstractCharacteristics
{
private:
@ -31,12 +33,15 @@ private:
SkiddingProperties *m_skidding;
public:
XmlCharacteristics(const std::string &filename = "");
XmlCharacteristics(const XMLNode *node = nullptr);
virtual const SkiddingProperties* getSkiddingProperties() const;
virtual void process(CharacteristicType type, Value value, bool &isSet) const;
void load(const std::string &filename);
void load(const XMLNode *node);
private:
static float processFloat(float value, std::string processor);
};
#endif

View File

@ -19,7 +19,7 @@
# This script creates the output for the AbstractCharacteristics
# It takes an argument that specifies what the output of the script should be.
# Possibilities are enum, defs, getter and getType
# The output options can be seen by running this script without arguments.
import sys
@ -132,23 +132,23 @@ def joinSubName(group, member, titleCase):
def main():
# Find out what to do
if len(sys.argv) == 1:
print("Please specify what you want to know [enum/defs/getter/getType]")
print("Please specify what you want to know [enum/defs/getter/getType/getXml]")
return
task = sys.argv[1]
groups = [Group.parse(line) for line in characteristics.split("\n")]
# Find longest name to align the function bodies
#nameLengthTitle = 0
#nameLengthUnderscore = 0
#for g in groups:
# for m in g.members:
# l = len(joinSubName(g, m, True))
# if l > nameLengthTitle:
# nameLengthTitle = l
# l = len(joinSubName(g, m, False))
# if l > nameLengthUnderscore:
# nameLengthUnderscore = l
nameLengthTitle = 0
nameLengthUnderscore = 0
for g in groups:
for m in g.members:
l = len(joinSubName(g, m, True))
if l > nameLengthTitle:
nameLengthTitle = l
l = len(joinSubName(g, m, False))
if l > nameLengthUnderscore:
nameLengthUnderscore = l
# Print the results
if task == "enum":
@ -199,6 +199,16 @@ def main():
nameUnderscore = joinSubName(g, m, False)
print(""" case {0}:\n return TYPE_{1};""".
format(nameUnderscore.upper(), "_".join(toList(m.typeStr)).upper()))
elif task == "getXml":
for g in groups:
print(" if (const XMLNode *sub_node = node->getNode(\"{0}\"))\n {{".
format(g.baseName))
for m in g.members:
nameUnderscore = joinSubName(g, m, False)
nameMinus = "-".join(toList(m.name))
print(" sub_node->get(\"{0}\", &m_values[{1}]);".
format(nameMinus, nameUnderscore.upper()))
print(" }\n")
else:
print("Unknown task")