⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unpack.cpp

📁 打飞机的过关游戏(linux)
💻 CPP
字号:
#include "unpack.h"

/*
Searches the pak file for the required data. When
it is found, the data is read into a character buffer.
*/
void unpack(char *file, signed char fileType)
{
	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);
			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);

	//printf("Extracted: %s\n", file);

	fclose(pak);
}

/*
Search the data package for the required file.
When it is found, return the location.
*/
int locateDataInPak(char *file, signed char required)
{
	//printf("Looking for %s...", file);

	char packFilename[60];
	int packFSize;
	int location = 0;

	FILE *pak;

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

			//printf("found it!\n");

			return location;
		}
		else
		{
			fseek(pak, packFSize, SEEK_CUR);
		}
	}

	//printf("not found (skipping)\n");

	return -1; // we only get this if it isn't required
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -