diff --git a/src/battle/capitalShips.c b/src/battle/capitalShips.c index 919a29b..e53ff85 100644 --- a/src/battle/capitalShips.c +++ b/src/battle/capitalShips.c @@ -345,37 +345,41 @@ static void loadCapitalShipDef(char *filename) text = readFile(filename); - e = malloc(sizeof(Entity)); - memset(e, 0, sizeof(Entity)); - defTail->next = e; - defTail = e; - - e->type = ET_CAPITAL_SHIP; - e->active = 1; - root = cJSON_Parse(text); - STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); - STRNCPY(e->defName, e->name, MAX_NAME_LENGTH); - e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint; - e->shieldRechargeRate = cJSON_GetObjectItem(root, "shieldRechargeRate")->valueint; - e->texture = getTexture(cJSON_GetObjectItem(root, "texture")->valuestring); - e->speed = 1; + if (root) + { + e = malloc(sizeof(Entity)); + memset(e, 0, sizeof(Entity)); + defTail->next = e; + defTail = e; - e->action = think; - e->die = die; + e->type = ET_CAPITAL_SHIP; + e->active = 1; + + STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(e->defName, e->name, MAX_NAME_LENGTH); + e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint; + e->shieldRechargeRate = cJSON_GetObjectItem(root, "shieldRechargeRate")->valueint; + e->texture = getTexture(cJSON_GetObjectItem(root, "texture")->valuestring); + e->speed = 1; - SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h); + e->action = think; + e->die = die; - e->separationRadius = MAX(e->w, e->h); + SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h); - loadComponents(e, cJSON_GetObjectItem(root, "components")); + e->separationRadius = MAX(e->w, e->h); - loadGuns(e, cJSON_GetObjectItem(root, "guns")); + loadComponents(e, cJSON_GetObjectItem(root, "components")); - loadEngines(e, cJSON_GetObjectItem(root, "engines")); + loadGuns(e, cJSON_GetObjectItem(root, "guns")); - cJSON_Delete(root); + loadEngines(e, cJSON_GetObjectItem(root, "engines")); + + cJSON_Delete(root); + } + free(text); } diff --git a/src/battle/fighters.c b/src/battle/fighters.c index 9e0b89c..c105889 100644 --- a/src/battle/fighters.c +++ b/src/battle/fighters.c @@ -649,74 +649,78 @@ static void loadFighterDef(char *filename) text = readFile(filename); - e = malloc(sizeof(Entity)); - memset(e, 0, sizeof(Entity)); - defTail->next = e; - defTail = e; - - e->type = ET_FIGHTER; - e->active = 1; - root = cJSON_Parse(text); - STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); - STRNCPY(e->defName, e->name, MAX_NAME_LENGTH); - e->health = e->maxHealth = cJSON_GetObjectItem(root, "health")->valueint; - e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint; - e->speed = cJSON_GetObjectItem(root, "speed")->valuedouble; - e->reloadTime = cJSON_GetObjectItem(root, "reloadTime")->valueint; - e->shieldRechargeRate = cJSON_GetObjectItem(root, "shieldRechargeRate")->valueint; - e->texture = getTexture(cJSON_GetObjectItem(root, "texture")->valuestring); - - SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h); - - if (cJSON_GetObjectItem(root, "guns")) + if (root) { - i = 0; + e = malloc(sizeof(Entity)); + memset(e, 0, sizeof(Entity)); + defTail->next = e; + defTail = e; - for (node = cJSON_GetObjectItem(root, "guns")->child ; node != NULL ; node = node->next) + e->type = ET_FIGHTER; + e->active = 1; + + STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(e->defName, e->name, MAX_NAME_LENGTH); + e->health = e->maxHealth = cJSON_GetObjectItem(root, "health")->valueint; + e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint; + e->speed = cJSON_GetObjectItem(root, "speed")->valuedouble; + e->reloadTime = cJSON_GetObjectItem(root, "reloadTime")->valueint; + e->shieldRechargeRate = cJSON_GetObjectItem(root, "shieldRechargeRate")->valueint; + e->texture = getTexture(cJSON_GetObjectItem(root, "texture")->valuestring); + + SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h); + + if (cJSON_GetObjectItem(root, "guns")) { - e->guns[i].type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); - e->guns[i].x = cJSON_GetObjectItem(node, "x")->valueint; - e->guns[i].y = cJSON_GetObjectItem(node, "y")->valueint; + i = 0; - i++; - - if (i >= MAX_FIGHTER_GUNS) + for (node = cJSON_GetObjectItem(root, "guns")->child ; node != NULL ; node = node->next) { - printf("ERROR: cannot assign more than %d guns to a fighter\n", MAX_FIGHTER_GUNS); - exit(1); + e->guns[i].type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); + e->guns[i].x = cJSON_GetObjectItem(node, "x")->valueint; + e->guns[i].y = cJSON_GetObjectItem(node, "y")->valueint; + + i++; + + if (i >= MAX_FIGHTER_GUNS) + { + printf("ERROR: cannot assign more than %d guns to a fighter\n", MAX_FIGHTER_GUNS); + exit(1); + } } + + e->combinedGuns = getJSONValue(root, "combinedGuns", 0); } - e->combinedGuns = getJSONValue(root, "combinedGuns", 0); + e->selectedGunType = e->guns[0].type; + + e->missiles = getJSONValue(root, "missiles", 0); + + if (cJSON_GetObjectItem(root, "flags")) + { + e->flags = flagsToLong(cJSON_GetObjectItem(root, "flags")->valuestring, NULL); + } + + if (cJSON_GetObjectItem(root, "aiFlags")) + { + e->aiFlags = flagsToLong(cJSON_GetObjectItem(root, "aiFlags")->valuestring, NULL); + } + + if (cJSON_GetObjectItem(root, "deathType")) + { + e->deathType = lookup(cJSON_GetObjectItem(root, "deathType")->valuestring); + } + + e->separationRadius = MAX(e->w, e->h) * 3; + + /* all craft default to 100 system power */ + e->systemPower = 100; + + cJSON_Delete(root); } - - e->selectedGunType = e->guns[0].type; - - e->missiles = getJSONValue(root, "missiles", 0); - - if (cJSON_GetObjectItem(root, "flags")) - { - e->flags = flagsToLong(cJSON_GetObjectItem(root, "flags")->valuestring, NULL); - } - - if (cJSON_GetObjectItem(root, "aiFlags")) - { - e->aiFlags = flagsToLong(cJSON_GetObjectItem(root, "aiFlags")->valuestring, NULL); - } - - if (cJSON_GetObjectItem(root, "deathType")) - { - e->deathType = lookup(cJSON_GetObjectItem(root, "deathType")->valuestring); - } - - e->separationRadius = MAX(e->w, e->h) * 3; - - /* all craft default to 100 system power */ - e->systemPower = 100; - - cJSON_Delete(root); + free(text); } diff --git a/src/challenges/challenges.c b/src/challenges/challenges.c index 790656f..61180ee 100644 --- a/src/challenges/challenges.c +++ b/src/challenges/challenges.c @@ -67,9 +67,13 @@ void initChallenges(void) sprintf(path, "data/challenges/%s", filenames[i]); mission = loadMissionMeta(path); - tail->next = mission; - tail = mission; - + + if (mission) + { + tail->next = mission; + tail = mission; + } + free(filenames[i]); } diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index b50eff3..08e73af 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -47,75 +47,81 @@ Mission *loadMissionMeta(char *filename) text = readFile(filename); root = cJSON_Parse(text); + + mission = NULL; - mission = malloc(sizeof(Mission)); - memset(mission, 0, sizeof(Mission)); - - STRNCPY(mission->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); - STRNCPY(mission->description, _(cJSON_GetObjectItem(root, "description")->valuestring), MAX_DESCRIPTION_LENGTH); - STRNCPY(mission->filename, filename, MAX_DESCRIPTION_LENGTH); - - mission->requires = getJSONValue(root, "requires", 0); - - if (cJSON_GetObjectItem(root, "epic")) + if (root) { - mission->epic = 1; - } + mission = malloc(sizeof(Mission)); + memset(mission, 0, sizeof(Mission)); - node = cJSON_GetObjectItem(root, "player"); + STRNCPY(mission->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(mission->description, _(cJSON_GetObjectItem(root, "description")->valuestring), MAX_DESCRIPTION_LENGTH); + STRNCPY(mission->filename, filename, MAX_DESCRIPTION_LENGTH); - if (node) - { - STRNCPY(mission->pilot, cJSON_GetObjectItem(node, "pilot")->valuestring, MAX_NAME_LENGTH); - STRNCPY(mission->squadron, cJSON_GetObjectItem(node, "squadron")->valuestring, MAX_NAME_LENGTH); - STRNCPY(mission->craft, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH); - } + mission->requires = getJSONValue(root, "requires", 0); - node = cJSON_GetObjectItem(root, "challenge"); + if (cJSON_GetObjectItem(root, "epic")) + { + mission->epic = 1; + } - if (node) - { - mission->challengeData.isChallenge = 1; - - /* limits */ - mission->challengeData.timeLimit = getJSONValue(node, "timeLimit", 0) * FPS; - mission->challengeData.killLimit = getJSONValue(node, "killLimit", 0); - mission->challengeData.escapeLimit = getJSONValue(node, "escapeLimit", 0); - mission->challengeData.waypointLimit = getJSONValue(node, "waypointLimit", 0); - mission->challengeData.itemLimit = getJSONValue(node, "itemLimit", 0); - - /* restrictions */ - mission->challengeData.noMissiles = getJSONValue(node, "noMissiles", 0); - mission->challengeData.noECM = getJSONValue(node, "noECM", 0); - mission->challengeData.noBoost = getJSONValue(node, "noBoost", 0); - mission->challengeData.noGuns = getJSONValue(node, "noGuns", 0); - - node = cJSON_GetObjectItem(node, "challenges"); + node = cJSON_GetObjectItem(root, "player"); if (node) { - node = node->child; + STRNCPY(mission->pilot, cJSON_GetObjectItem(node, "pilot")->valuestring, MAX_NAME_LENGTH); + STRNCPY(mission->squadron, cJSON_GetObjectItem(node, "squadron")->valuestring, MAX_NAME_LENGTH); + STRNCPY(mission->craft, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH); + } - i = 0; + node = cJSON_GetObjectItem(root, "challenge"); - while (node && i < MAX_CHALLENGES) + if (node) + { + mission->challengeData.isChallenge = 1; + + /* limits */ + mission->challengeData.timeLimit = getJSONValue(node, "timeLimit", 0) * FPS; + mission->challengeData.killLimit = getJSONValue(node, "killLimit", 0); + mission->challengeData.escapeLimit = getJSONValue(node, "escapeLimit", 0); + mission->challengeData.waypointLimit = getJSONValue(node, "waypointLimit", 0); + mission->challengeData.itemLimit = getJSONValue(node, "itemLimit", 0); + + /* restrictions */ + mission->challengeData.noMissiles = getJSONValue(node, "noMissiles", 0); + mission->challengeData.noECM = getJSONValue(node, "noECM", 0); + mission->challengeData.noBoost = getJSONValue(node, "noBoost", 0); + mission->challengeData.noGuns = getJSONValue(node, "noGuns", 0); + + node = cJSON_GetObjectItem(node, "challenges"); + + if (node) { - challenge = malloc(sizeof(Challenge)); - memset(challenge, 0, sizeof(Challenge)); + node = node->child; - challenge->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); - challenge->value = cJSON_GetObjectItem(node, "value")->valueint; + i = 0; - mission->challengeData.challenges[i] = challenge; + while (node && i < MAX_CHALLENGES) + { + challenge = malloc(sizeof(Challenge)); + memset(challenge, 0, sizeof(Challenge)); - node = node->next; + challenge->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); + challenge->value = cJSON_GetObjectItem(node, "value")->valueint; - i++; + mission->challengeData.challenges[i] = challenge; + + node = node->next; + + i++; + } } } - } - cJSON_Delete(root); + cJSON_Delete(root); + } + free(text); return mission; diff --git a/src/galaxy/starSystems.c b/src/galaxy/starSystems.c index a237ded..6afaeaa 100644 --- a/src/galaxy/starSystems.c +++ b/src/galaxy/starSystems.c @@ -99,8 +99,12 @@ static void loadMissions(StarSystem *starSystem) sprintf(path, "data/missions/%s/%s", name, filenames[i]); mission = loadMissionMeta(path); - tail->next = mission; - tail = mission; + + if (mission) + { + tail->next = mission; + tail = mission; + } free(filenames[i]); } diff --git a/src/system/widgets.c b/src/system/widgets.c index fb37e66..d3942b4 100644 --- a/src/system/widgets.c +++ b/src/system/widgets.c @@ -362,66 +362,70 @@ static void loadWidgetSet(char *filename) text = readFile(filename); root = cJSON_Parse(text); - - for (node = root->child ; node != NULL ; node = node->next) + + if (root) { - w = malloc(sizeof(Widget)); - memset(w, 0, sizeof(Widget)); - - w->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); - STRNCPY(w->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); - STRNCPY(w->group, cJSON_GetObjectItem(node, "group")->valuestring, MAX_NAME_LENGTH); - w->rect.x = cJSON_GetObjectItem(node, "x")->valueint; - w->rect.y = cJSON_GetObjectItem(node, "y")->valueint; - w->enabled = 1; - w->visible = 1; - - if (w->rect.x == -1) + for (node = root->child ; node != NULL ; node = node->next) { - w->rect.x = SCREEN_WIDTH / 2; + w = malloc(sizeof(Widget)); + memset(w, 0, sizeof(Widget)); + + w->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); + STRNCPY(w->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(w->group, cJSON_GetObjectItem(node, "group")->valuestring, MAX_NAME_LENGTH); + w->rect.x = cJSON_GetObjectItem(node, "x")->valueint; + w->rect.y = cJSON_GetObjectItem(node, "y")->valueint; + w->enabled = 1; + w->visible = 1; + + if (w->rect.x == -1) + { + w->rect.x = SCREEN_WIDTH / 2; + } + + switch (w->type) + { + case WT_BUTTON: + STRNCPY(w->text, _(cJSON_GetObjectItem(node, "text")->valuestring), MAX_NAME_LENGTH); + w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; + w->rect.h = cJSON_GetObjectItem(node, "h")->valueint; + w->rect.x -= w->rect.w / 2; + w->rect.y -= (w->rect.h / 2) + 8; + break; + + case WT_IMG_BUTTON: + w->texture = getTexture(cJSON_GetObjectItem(node, "texture")->valuestring); + SDL_QueryTexture(w->texture, NULL, NULL, &w->rect.w, &w->rect.h); + break; + + case WT_SELECT: + STRNCPY(w->text, cJSON_GetObjectItem(node, "text")->valuestring, MAX_NAME_LENGTH); + w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; + w->rect.h = cJSON_GetObjectItem(node, "h")->valueint; + w->rect.x -= w->rect.w / 2; + w->rect.y -= (w->rect.h / 2) + 8; + createSelectButtons(w); + createOptions(w, cJSON_GetObjectItem(node, "options")->valuestring); + break; + + case WT_CONTROL_CONFIG: + w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; + w->rect.h = cJSON_GetObjectItem(node, "h")->valueint; + break; + + default: + printf("Widget type %d not handled\n", w->type); + exit(1); + break; + } + + tail->next = w; + tail = w; } - switch (w->type) - { - case WT_BUTTON: - STRNCPY(w->text, _(cJSON_GetObjectItem(node, "text")->valuestring), MAX_NAME_LENGTH); - w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; - w->rect.h = cJSON_GetObjectItem(node, "h")->valueint; - w->rect.x -= w->rect.w / 2; - w->rect.y -= (w->rect.h / 2) + 8; - break; - - case WT_IMG_BUTTON: - w->texture = getTexture(cJSON_GetObjectItem(node, "texture")->valuestring); - SDL_QueryTexture(w->texture, NULL, NULL, &w->rect.w, &w->rect.h); - break; - - case WT_SELECT: - STRNCPY(w->text, cJSON_GetObjectItem(node, "text")->valuestring, MAX_NAME_LENGTH); - w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; - w->rect.h = cJSON_GetObjectItem(node, "h")->valueint; - w->rect.x -= w->rect.w / 2; - w->rect.y -= (w->rect.h / 2) + 8; - createSelectButtons(w); - createOptions(w, cJSON_GetObjectItem(node, "options")->valuestring); - break; - - case WT_CONTROL_CONFIG: - w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; - w->rect.h = cJSON_GetObjectItem(node, "h")->valueint; - break; - - default: - printf("Widget type %d not handled\n", w->type); - exit(1); - break; - } - - tail->next = w; - tail = w; + cJSON_Delete(root); } - - cJSON_Delete(root); + free(text); }