aboutsummaryrefslogtreecommitdiff
path: root/devtools/qtable/qtable.cpp
blob: a659698688a549a1d76cd3f468a9faaeb2272457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// HACK to allow building with the SDL backend on MinGW
// see bug #1800764 "TOOLS: MinGW tools building broken"
#ifdef main
#undef main
#endif // main

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/*
  Table file format for scummvm/queen
	uint32 : 'QTBL'
	uint32 : version/tag
	repeat (number of resources tables) {
		uint16 : number of entries
		repeat (number of entries) {
			char[12] : name
			uint8    : queen.%d
			uint32   : offset in queen.%d
			uint32   : length in queen.%d
		}
	}
*/

#define MAX_VERSIONS  20
#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))

typedef unsigned char   uint8;
typedef unsigned short uint16;
typedef unsigned int   uint32;

typedef struct DataFileEntry {
	uint8 bundle;
	uint32 offset;
	uint32 length;
	char name[13];
} DataFileEntry;

typedef struct DataFileEntriesTable {
	const DataFileEntry *fileEntries;
	uint16 fileEntriesCount;
} DataFileEntriesTable;

typedef struct TableFile {
	const char *outfile;
	DataFileEntriesTable dataFileEntriesTable[MAX_VERSIONS];
	uint16 dataFileEntriesTableCount;
} TableFile;

typedef struct GameVersion {
	const char *id;
	uint32 dataSize;
	DataFileEntry *dataFileEntries;
	uint16 dataFileEntriesCount;
	uint8 queenTblVersion;
	uint32 queenTblOffset;
} GameVersion;

#include "fat_eng_floppy.h"
#include "fat_eng_cdrom.h"
#include "fat_fre_floppy.h"
#include "fat_fre_cdrom.h"
#include "fat_ger_floppy.h"
#include "fat_ger_cdrom.h"
#include "fat_ita_floppy.h"
#include "fat_ita_cdrom.h"
#include "fat_spa_cdrom.h"
#include "fat_heb_cdrom.h"
#include "fat_pc_demo_pcgames.h"
#include "fat_pc_demo.h"
#include "fat_pc_interview.h"
#include "fat_amiga_eng_floppy.h"
#include "fat_amiga_demo.h"
#include "fat_amiga_interview.h"


#define FAT(x) x, (sizeof(x)/sizeof(x[0]))

static GameVersion gameVersionsTable[] = {
	{ "PEM10",  22677657, FAT(fatEngFl),          1, 0 },
	{ "CEM10", 190787021, FAT(fatEngCd),          1, 0 },
	{ "PFM10",  22157304, FAT(fatFreFl),          1, 0 },
	{ "CFM10", 186689095, FAT(fatFreCd),          1, 0 },
	{ "PGM10",  22240013, FAT(fatGerFl),          1, 0 },
	{ "CGM10", 217648975, FAT(fatGerCd),          1, 0 },
	{ "PIM10",  22461366, FAT(fatItaFl),          1, 0 },
	{ "CIM10", 190795582, FAT(fatItaCd),          1, 0 },
	{ "CSM10", 190730602, FAT(fatSpaCd),          1, 0 },
	{ "CHM10", 190705558, FAT(fatHebCd),          1, 0 },
	{ "PE100",   3724538, FAT(fatPCDemoPcGames),  1, 0 },
	{ "PE100",   3732177, FAT(fatPCDemo),         1, 0 },
	{ "PEint",   1915913, FAT(fatPCInterview),    1, 0 },
	{ "aEM10",    351775, FAT(fatAmigaEngFl),     2, 0 },
	{ "CE101",    563335, FAT(fatAmigaDemo),      2, 0 },
	{ "PE100",    597032, FAT(fatAmigaInterview), 2, 0 }
};

static const uint32 QTBL_TAG = 0x5154424C;
static const uint32 CURRENT_VERSION = 2;

static void writeByte(FILE *fp, uint8 b) {
	fwrite(&b, 1, 1, fp);
}

static void writeUint16BE(FILE *fp, uint16 value) {
	writeByte(fp, (uint8)(value >> 8));
	writeByte(fp, (uint8)(value & 0xFF));
}

static void writeUint32BE(FILE *fp, uint32 value) {
	writeUint16BE(fp, (uint16)(value >> 16));
	writeUint16BE(fp, (uint16)(value & 0xFFFF));
}

static void writeEntry(FILE *fp, const DataFileEntry *dfe) {
	fwrite(dfe->name, 12, 1, fp);
	writeByte(fp, dfe->bundle);
	writeUint32BE(fp, dfe->offset);
	writeUint32BE(fp, dfe->length);
}

static void createTableFile(TableFile *tf) {
	FILE *out;
	uint16 i, j;
	uint32 offset; /* dump offset */

	/* setup file entries table */
	assert(ARRAYSIZE(gameVersionsTable) < MAX_VERSIONS);
	for (i = 0; i < ARRAYSIZE(gameVersionsTable); ++i) {
		const GameVersion *gv = &gameVersionsTable[i];
		tf->dataFileEntriesTable[i].fileEntries = gv->dataFileEntries;
		tf->dataFileEntriesTable[i].fileEntriesCount = gv->dataFileEntriesCount;
	}
	tf->dataFileEntriesTableCount = ARRAYSIZE(gameVersionsTable);

	/* write queen table file */
	out = fopen(tf->outfile, "wb");
	if (!out) {
		printf("ERROR: can't write output file.\n");
		return;
	}
	/* write header tag */
	writeUint32BE(out, QTBL_TAG);
	/* write version */
	writeUint32BE(out, CURRENT_VERSION);
	/* write tables */
	offset = 4 + 4;
	for (i = 0; i < tf->dataFileEntriesTableCount; ++i) {
		const DataFileEntriesTable *dfet = &tf->dataFileEntriesTable[i];
		/* write number of entries in table */
		writeUint16BE(out, dfet->fileEntriesCount);
		/* write table entries */
		for (j = 0; j < dfet->fileEntriesCount; ++j) {
			const DataFileEntry *dfe = &dfet->fileEntries[j];
			writeEntry(out, dfe);
		}
		assert(gameVersionsTable[i].queenTblVersion <= CURRENT_VERSION);
		gameVersionsTable[i].queenTblOffset = offset;
		/* update offset */
		offset += 2 + dfet->fileEntriesCount * (12 + 1 + 4 + 4);
	}
	fclose(out);
}

static void printGameVersionTable() {
	unsigned int i;
	printf("const RetailGameVersion Resource::_gameVersions[] = {\n");
	for (i = 0; i < ARRAYSIZE(gameVersionsTable); ++i) {
		const GameVersion *gv = &gameVersionsTable[i];
		printf("\t{ \"%s\", %d, 0x%08X, %9d },\n", gv->id, gv->queenTblVersion, gv->queenTblOffset, gv->dataSize);
	}
	printf("};\n");
}

int main(int argc, char *argv[]) {
	TableFile tf;
	if (argc < 2) {
		printf("syntax: %s tablefile\n", argv[0]);
		exit(0);
	}
	memset(&tf, 0, sizeof(tf));
	tf.outfile = argv[1];
	createTableFile(&tf);
	printGameVersionTable();
	return 0;
}