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,6 +345,10 @@ static void loadCapitalShipDef(char *filename)
text = readFile(filename); text = readFile(filename);
root = cJSON_Parse(text);
if (root)
{
e = malloc(sizeof(Entity)); e = malloc(sizeof(Entity));
memset(e, 0, sizeof(Entity)); memset(e, 0, sizeof(Entity));
defTail->next = e; defTail->next = e;
@ -353,8 +357,6 @@ static void loadCapitalShipDef(char *filename)
e->type = ET_CAPITAL_SHIP; e->type = ET_CAPITAL_SHIP;
e->active = 1; e->active = 1;
root = cJSON_Parse(text);
STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH);
STRNCPY(e->defName, e->name, MAX_NAME_LENGTH); STRNCPY(e->defName, e->name, MAX_NAME_LENGTH);
e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint; e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint;
@ -376,6 +378,8 @@ static void loadCapitalShipDef(char *filename)
loadEngines(e, cJSON_GetObjectItem(root, "engines")); loadEngines(e, cJSON_GetObjectItem(root, "engines"));
cJSON_Delete(root); cJSON_Delete(root);
}
free(text); free(text);
} }

View File

@ -649,6 +649,10 @@ static void loadFighterDef(char *filename)
text = readFile(filename); text = readFile(filename);
root = cJSON_Parse(text);
if (root)
{
e = malloc(sizeof(Entity)); e = malloc(sizeof(Entity));
memset(e, 0, sizeof(Entity)); memset(e, 0, sizeof(Entity));
defTail->next = e; defTail->next = e;
@ -657,8 +661,6 @@ static void loadFighterDef(char *filename)
e->type = ET_FIGHTER; e->type = ET_FIGHTER;
e->active = 1; e->active = 1;
root = cJSON_Parse(text);
STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH);
STRNCPY(e->defName, e->name, MAX_NAME_LENGTH); STRNCPY(e->defName, e->name, MAX_NAME_LENGTH);
e->health = e->maxHealth = cJSON_GetObjectItem(root, "health")->valueint; e->health = e->maxHealth = cJSON_GetObjectItem(root, "health")->valueint;
@ -717,6 +719,8 @@ static void loadFighterDef(char *filename)
e->systemPower = 100; e->systemPower = 100;
cJSON_Delete(root); cJSON_Delete(root);
}
free(text); free(text);
} }

View File

@ -67,8 +67,12 @@ void initChallenges(void)
sprintf(path, "data/challenges/%s", filenames[i]); sprintf(path, "data/challenges/%s", filenames[i]);
mission = loadMissionMeta(path); mission = loadMissionMeta(path);
if (mission)
{
tail->next = mission; tail->next = mission;
tail = mission; tail = mission;
}
free(filenames[i]); free(filenames[i]);
} }

View File

@ -48,6 +48,10 @@ Mission *loadMissionMeta(char *filename)
root = cJSON_Parse(text); root = cJSON_Parse(text);
mission = NULL;
if (root)
{
mission = malloc(sizeof(Mission)); mission = malloc(sizeof(Mission));
memset(mission, 0, sizeof(Mission)); memset(mission, 0, sizeof(Mission));
@ -116,6 +120,8 @@ Mission *loadMissionMeta(char *filename)
} }
cJSON_Delete(root); cJSON_Delete(root);
}
free(text); free(text);
return mission; return mission;

View File

@ -99,8 +99,12 @@ static void loadMissions(StarSystem *starSystem)
sprintf(path, "data/missions/%s/%s", name, filenames[i]); sprintf(path, "data/missions/%s/%s", name, filenames[i]);
mission = loadMissionMeta(path); mission = loadMissionMeta(path);
if (mission)
{
tail->next = mission; tail->next = mission;
tail = mission; tail = mission;
}
free(filenames[i]); free(filenames[i]);
} }

View File

@ -363,6 +363,8 @@ static void loadWidgetSet(char *filename)
text = readFile(filename); text = readFile(filename);
root = cJSON_Parse(text); root = cJSON_Parse(text);
if (root)
{
for (node = root->child ; node != NULL ; node = node->next) for (node = root->child ; node != NULL ; node = node->next)
{ {
w = malloc(sizeof(Widget)); w = malloc(sizeof(Widget));
@ -422,6 +424,8 @@ static void loadWidgetSet(char *filename)
} }
cJSON_Delete(root); cJSON_Delete(root);
}
free(text); free(text);
} }