From aa8a2187baa8b5e2b389643aa54c862d67f40a68 Mon Sep 17 00:00:00 2001 From: Allanis Date: Mon, 4 Feb 2013 00:47:19 +0000 Subject: [PATCH] [Change] xml now loads from data pack. [Add] Ship xml syntax checking. --- dat/fleet.xml | 2 +- dat/ship.xml | 10 ++-- gfx/ship/{enemyship.png => ship1.png} | Bin src/main.c | 2 +- src/ship.c | 69 +++++++++++++++++--------- 5 files changed, 53 insertions(+), 30 deletions(-) rename gfx/ship/{enemyship.png => ship1.png} (100%) diff --git a/dat/fleet.xml b/dat/fleet.xml index 7a64d5d..99dc561 100644 --- a/dat/fleet.xml +++ b/dat/fleet.xml @@ -3,7 +3,7 @@ 2 - Miss. Test + Test diff --git a/dat/ship.xml b/dat/ship.xml index 8a9231e..fb83e73 100644 --- a/dat/ship.xml +++ b/dat/ship.xml @@ -26,8 +26,8 @@ laser - - enemyship.png + + ship1.png 1 180 @@ -38,9 +38,9 @@ 160 120 360 - 90 - 60 - 50 + 90 + 60 + 50 9 diff --git a/gfx/ship/enemyship.png b/gfx/ship/ship1.png similarity index 100% rename from gfx/ship/enemyship.png rename to gfx/ship/ship1.png diff --git a/src/main.c b/src/main.c index 7f80a19..f1ff217 100644 --- a/src/main.c +++ b/src/main.c @@ -202,7 +202,7 @@ int main(int argc, char** argv) { gl_bindCamera(&player->solid->pos); space_init(); - pilot_create(get_ship("Miss. Test"), NULL, NULL, NULL, 0); + pilot_create(get_ship("Test"), NULL, NULL, NULL, 0); time = SDL_GetTicks(); diff --git a/src/ship.c b/src/ship.c index 77a58f6..a036b0f 100644 --- a/src/ship.c +++ b/src/ship.c @@ -2,6 +2,7 @@ #include #include "log.h" +#include "pack.h" #include "ship.h" #define MAX_PATH_NAME 20 // Maximum size of the path. @@ -26,21 +27,24 @@ Ship* get_ship(const char* name) { int i; for(i = 0; i < ships; i++) 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; } -Ship* ship_parse(xmlNodePtr node) { - xmlNodePtr cur; +Ship* ship_parse(xmlNodePtr parent) { + xmlNodePtr cur, node; Ship* tmp = CALLOC_L(Ship); 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) { cur = node->children; if(strcmp((char*)cur->name, "text")==0) { @@ -98,34 +102,51 @@ Ship* ship_parse(xmlNodePtr node) { } 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); return tmp; } int ships_load(void) { - xmlTextReaderPtr reader; xmlNodePtr node; + + uint32_t bufsize; + char* buf = pack_readfile(DATA, SHIP_DATA, &bufsize); + xmlDocPtr doc = xmlParseMemory(buf, bufsize); + Ship* tmp = NULL; - if((reader = xmlNewTextReaderFilename(SHIP_DATA)) == NULL) { - WARN("XML error reading " SHIP_DATA); + node = doc->xmlChildrenNode; // Ships node. + if(strcmp((char*)node->name, XML_ID)) { + ERR("Malformed ships xml file: missing tag %s", XML_ID); return -1; } - // Get to the start of the "ships" section. - while(xmlTextReaderRead(reader)==1) { - if(xmlTextReaderNodeType(reader)==XML_NODE_START && - strcmp((char*)xmlTextReaderConstName(reader), XML_ID) == 0) break; + node = node->xmlChildrenNode; // First ship node. + if(node == NULL) { + ERR("Malformed ships xml file: is missing ships"); + return -1; } - - xmlTextReaderRead(reader); // At ships node. - - 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. + + do { + if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) { if(ship_stack == NULL) { ship_stack = tmp = ship_parse(node); ships = 1; @@ -136,8 +157,10 @@ int ships_load(void) { free(tmp); } } - } - xmlFreeTextReader(reader); + } while((node = node->next)); + + xmlFreeDoc(doc); + free(buf); return 0; }