2011-08-24 14:14:44 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003 Parallel Realities
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-08-26 21:29:04 +02:00
|
|
|
#include "Starfighter.h"
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Searches the pak file for the required data. When
|
|
|
|
it is found, the data is read into a character buffer.
|
|
|
|
*/
|
2012-02-26 22:22:27 +01:00
|
|
|
bool unpack(const char *file, signed char fileType)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
unsigned char *packBuffer;
|
|
|
|
char packFilename[60];
|
|
|
|
int packFSize;
|
|
|
|
|
|
|
|
FILE *pak;
|
|
|
|
FILE *fp = NULL; // music has to be read-written-read!
|
|
|
|
char musicFilename[PATH_MAX];
|
|
|
|
|
|
|
|
strcpy(packFilename, "");
|
|
|
|
|
|
|
|
pak = fopen(PACKLOCATION, "rb");
|
|
|
|
if (pak == NULL)
|
|
|
|
{
|
|
|
|
printf("Couldn't access the Project: Starfighter data file!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fseek(pak, 4, SEEK_SET);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!fread(packFilename, 1, 56, pak))
|
|
|
|
{
|
|
|
|
fclose(pak);
|
2012-02-26 22:22:27 +01:00
|
|
|
|
|
|
|
if (fileType == PAK_MOD || fileType == PAK_S3M)
|
|
|
|
return false;
|
|
|
|
|
2011-08-24 14:14:44 +02:00
|
|
|
if (fileType != PAK_FONT)
|
|
|
|
{
|
|
|
|
showErrorAndExit(0, file);
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fread(&packFSize, 4, 1, pak);
|
|
|
|
packFSize = SDL_SwapLE32(packFSize);
|
|
|
|
|
|
|
|
if (strcmp(packFilename, file) == 0)
|
|
|
|
{
|
|
|
|
if ((fileType == PAK_MOD) || (fileType == PAK_S3M))
|
|
|
|
{
|
|
|
|
if (fileType == PAK_MOD)
|
|
|
|
{
|
|
|
|
sprintf(musicFilename, "%smusic.mod", engine.userHomeDirectory);
|
|
|
|
fp = fopen(musicFilename, "wb");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(musicFilename, "%smusic.s3m", engine.userHomeDirectory);
|
|
|
|
fp = fopen(musicFilename, "wb");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
showErrorAndExit(1, "");
|
|
|
|
}
|
|
|
|
packBuffer = (unsigned char*) malloc(packFSize);
|
|
|
|
fread(packBuffer, 1, packFSize, pak);
|
|
|
|
if ((fileType == PAK_MOD) || (fileType == PAK_S3M))
|
|
|
|
{
|
|
|
|
fwrite(packBuffer, 1, packFSize, fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fseek(pak, packFSize, SEEK_CUR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fileType != PAK_MOD) && (fileType != PAK_S3M))
|
|
|
|
engine.sdlrw = SDL_RWFromMem(packBuffer, packFSize);
|
|
|
|
|
|
|
|
fclose(pak);
|
2012-02-26 22:22:27 +01:00
|
|
|
return true;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Search the data package for the required file.
|
|
|
|
When it is found, return the location.
|
|
|
|
*/
|
2011-08-26 16:55:46 +02:00
|
|
|
int locateDataInPak(const char *file, bool required)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2012-03-11 15:16:19 +01:00
|
|
|
char packFilename[60] = "";
|
2011-08-24 14:14:44 +02:00
|
|
|
int packFSize;
|
|
|
|
int location = 0;
|
|
|
|
|
|
|
|
FILE *pak;
|
|
|
|
|
|
|
|
pak = fopen(PACKLOCATION, "rb");
|
|
|
|
if (pak == NULL)
|
|
|
|
{
|
|
|
|
printf("Couldn't access the Project: Starfighter data file!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fseek(pak, 4, SEEK_SET);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!fread(packFilename, 1, 56, pak))
|
|
|
|
{
|
|
|
|
fclose(pak);
|
|
|
|
if (required)
|
|
|
|
{
|
|
|
|
showErrorAndExit(0, file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fread(&packFSize, 4, 1, pak);
|
|
|
|
packFSize = SDL_SwapLE32(packFSize);
|
|
|
|
|
|
|
|
if (strcmp(packFilename, file) == 0)
|
|
|
|
{
|
|
|
|
location = ftell(pak);
|
|
|
|
fclose(pak);
|
|
|
|
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fseek(pak, packFSize, SEEK_CUR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1; // we only get this if it isn't required
|
|
|
|
}
|