[Change] xml now loads from data pack.

[Add] Ship xml syntax checking.
This commit is contained in:
Allanis 2013-02-04 00:47:19 +00:00
parent 5609dd7b22
commit aa8a2187ba
5 changed files with 53 additions and 30 deletions

View File

@ -3,7 +3,7 @@
<fleet name="Test"> <fleet name="Test">
<faction>2</faction> <faction>2</faction>
<pilots> <pilots>
<pilot chance='100'>Miss. Test</pilot> <pilot chance='100'>Test</pilot>
</pilots> </pilots>
</fleet> </fleet>
<fleet name="Merchant Ship"> <fleet name="Merchant Ship">

View File

@ -26,8 +26,8 @@
<outfit quantity='2'>laser</outfit> <outfit quantity='2'>laser</outfit>
</outfits> </outfits>
</ship> </ship>
<ship name="Miss. Test"> <ship name="Test">
<GFX>enemyship.png</GFX> <GFX>ship1.png</GFX>
<class>1</class> <class>1</class>
<movement> <movement>
<thrust>180</thrust> <thrust>180</thrust>
@ -38,9 +38,9 @@
<shield>160</shield> <shield>160</shield>
<armor>120</armor> <armor>120</armor>
<energy>360</energy> <energy>360</energy>
<shieldregen>90</shieldregen> <shield_regen>90</shield_regen>
<armorregen>60</armorregen> <armor_regen>60</armor_regen>
<energyregen>50</energyregen> <energy_regen>50</energy_regen>
</health> </health>
<characteristics> <characteristics>
<crew>9</crew> <crew>9</crew>

View File

Before

Width:  |  Height:  |  Size: 145 KiB

After

Width:  |  Height:  |  Size: 145 KiB

View File

@ -202,7 +202,7 @@ int main(int argc, char** argv) {
gl_bindCamera(&player->solid->pos); gl_bindCamera(&player->solid->pos);
space_init(); space_init();
pilot_create(get_ship("Miss. Test"), NULL, NULL, NULL, 0); pilot_create(get_ship("Test"), NULL, NULL, NULL, 0);
time = SDL_GetTicks(); time = SDL_GetTicks();

View File

@ -2,6 +2,7 @@
#include <libxml/xmlreader.h> #include <libxml/xmlreader.h>
#include "log.h" #include "log.h"
#include "pack.h"
#include "ship.h" #include "ship.h"
#define MAX_PATH_NAME 20 // Maximum size of the path. #define MAX_PATH_NAME 20 // Maximum size of the path.
@ -27,20 +28,23 @@ Ship* get_ship(const char* name) {
for(i = 0; i < ships; i++) for(i = 0; i < ships; i++)
if(strcmp((tmp+i)->name, name)==0) break; if(strcmp((tmp+i)->name, name)==0) break;
if(i == ships) // Ship doesn't exist, game will probably crash now.
WARN("Ship %s does not exist", name);
return tmp+1; return tmp+1;
} }
Ship* ship_parse(xmlNodePtr node) { Ship* ship_parse(xmlNodePtr parent) {
xmlNodePtr cur; xmlNodePtr cur, node;
Ship* tmp = CALLOC_L(Ship); Ship* tmp = CALLOC_L(Ship);
char str[MAX_PATH_NAME] = "\0"; char str[MAX_PATH_NAME] = "\0";
tmp->name = (char*)xmlGetProp(node, (xmlChar*)"name"); tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name");
node = node->xmlChildrenNode; node = parent->xmlChildrenNode;
while((node = node->next)) { while((node = node->next)) { // Load all the data.
if(strcmp((char*)node->name, "GFX")==0) { if(strcmp((char*)node->name, "GFX")==0) {
cur = node->children; cur = node->children;
if(strcmp((char*)cur->name, "text")==0) { if(strcmp((char*)cur->name, "text")==0) {
@ -98,34 +102,51 @@ Ship* ship_parse(xmlNodePtr node) {
} }
tmp->thrust *= tmp->mass; // Helps keep number sane. tmp->thrust *= tmp->mass; // Helps keep number sane.
#define MELEMENT(o,s) if(o == 0) WARN("Ship '%s' missing '"s"' element", tmp->name)
if(tmp->name == NULL) WARN("Ship '%s' missing 'name' tag", tmp->name);
if(tmp->gfx_ship == NULL) WARN("Ship '%s' missing 'GFX' element", tmp->name);
MELEMENT(tmp->thrust, "thrust");
MELEMENT(tmp->turn, "turn");
MELEMENT(tmp->speed, "speed");
MELEMENT(tmp->crew, "crew");
MELEMENT(tmp->mass, "mass");
MELEMENT(tmp->armor, "armor");
MELEMENT(tmp->armor_regen, "armor_regen");
MELEMENT(tmp->shield, "shield");
MELEMENT(tmp->shield_regen, "shield_regen");
MELEMENT(tmp->energy, "energy");
MELEMENT(tmp->energy_regen, "energt_regen");
MELEMENT(tmp->cap_cargo, "cap_cargo");
MELEMENT(tmp->cap_weapon, "cap_weapon");
#undef MELEMENT
DEBUG("Loaded ship '%s'", tmp->name); DEBUG("Loaded ship '%s'", tmp->name);
return tmp; return tmp;
} }
int ships_load(void) { int ships_load(void) {
xmlTextReaderPtr reader;
xmlNodePtr node; xmlNodePtr node;
uint32_t bufsize;
char* buf = pack_readfile(DATA, SHIP_DATA, &bufsize);
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
Ship* tmp = NULL; Ship* tmp = NULL;
if((reader = xmlNewTextReaderFilename(SHIP_DATA)) == NULL) { node = doc->xmlChildrenNode; // Ships node.
WARN("XML error reading " SHIP_DATA); if(strcmp((char*)node->name, XML_ID)) {
ERR("Malformed ships xml file: missing tag %s", XML_ID);
return -1; return -1;
} }
// Get to the start of the "ships" section. node = node->xmlChildrenNode; // First ship node.
while(xmlTextReaderRead(reader)==1) { if(node == NULL) {
if(xmlTextReaderNodeType(reader)==XML_NODE_START && ERR("Malformed ships xml file: is missing ships");
strcmp((char*)xmlTextReaderConstName(reader), XML_ID) == 0) break; return -1;
} }
xmlTextReaderRead(reader); // At ships node. do {
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) {
while(xmlTextReaderRead(reader)==1) {
if(xmlTextReaderNodeType(reader)==XML_NODE_START &&
strcmp((char*)xmlTextReaderConstName(reader), XML_SHIP)==0) {
node = xmlTextReaderCurrentNode(reader); // Node to processr
if(node == NULL) break; // No node.
if(ship_stack == NULL) { if(ship_stack == NULL) {
ship_stack = tmp = ship_parse(node); ship_stack = tmp = ship_parse(node);
ships = 1; ships = 1;
@ -136,8 +157,10 @@ int ships_load(void) {
free(tmp); free(tmp);
} }
} }
} } while((node = node->next));
xmlFreeTextReader(reader);
xmlFreeDoc(doc);
free(buf);
return 0; return 0;
} }