Don't allow rogue files to crash game at startup.

This commit is contained in:
Steve 2016-03-07 19:03:55 +00:00
parent d042d2d02c
commit 798135eadf
6 changed files with 215 additions and 189 deletions

View File

@ -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;
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
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;
e->separationRadius = MAX(e->w, e->h);
e->action = think;
e->die = die;
loadComponents(e, cJSON_GetObjectItem(root, "components"));
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
loadGuns(e, cJSON_GetObjectItem(root, "guns"));
e->separationRadius = MAX(e->w, e->h);
loadEngines(e, cJSON_GetObjectItem(root, "engines"));
loadComponents(e, cJSON_GetObjectItem(root, "components"));
loadGuns(e, cJSON_GetObjectItem(root, "guns"));
loadEngines(e, cJSON_GetObjectItem(root, "engines"));
cJSON_Delete(root);
}
cJSON_Delete(root);
free(text);
}

View File

@ -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);
}

View File

@ -67,8 +67,12 @@ 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]);
}

View File

@ -48,74 +48,80 @@ Mission *loadMissionMeta(char *filename)
root = cJSON_Parse(text);
mission = malloc(sizeof(Mission));
memset(mission, 0, sizeof(Mission));
mission = NULL;
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;

View File

@ -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]);
}

View File

@ -363,65 +363,69 @@ 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);
}