aboutsummaryrefslogtreecommitdiff
path: root/sky/disk.cpp
blob: 6fff6b13827ef1e51913808dfb2f2df880f36074 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003 The ScummVM project
 *
 * 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.
 *
 * $Header$
 *
 */

#include "stdafx.h"
#include "common/scummsys.h"
#include "common/engine.h"
#include "common/file.h"
#include "sky/skydefs.h"
#include "sky/sky.h"
#include "sky/rnc_deco.h"

#define MAX_FILES_IN_LIST		60

const char *dataFilename = "sky.dsk";
const char *dinnerFilename = "sky.dnr";
uint8 *dinnerTableArea, *fixedDest, *fileDest, *compDest;
uint32 dinnerTableEntries, fileFlags, fileOffset, fileSize, decompSize, compFile;
uint16 buildList[MAX_FILES_IN_LIST];
uint32 loadedFileList[MAX_FILES_IN_LIST];

File *dataDiskHandle = new File();
File *dnrHandle = new File();

void SkyState::initialiseDisk() {

	uint32 entriesRead;

	dnrHandle->open(dinnerFilename, _gameDataPath);
	if (dnrHandle->isOpen() == false)
			error("Could not open %s%s!\n", _gameDataPath, dinnerFilename);

	if (!(dinnerTableEntries = dnrHandle->readUint32LE()))
		error("Error reading from sky.dnr!\n"); //even though it was opened correctly?!
	
	debug(1, "Entries in dinner table: %d", dinnerTableEntries);

	if (dinnerTableEntries < 400)
		_isDemo = true;
	else
		_isDemo = false;
	
	if (dinnerTableEntries > 1600) 
		_isCDVersion = true;
	else
		_isCDVersion = false;
		
	dinnerTableArea = (uint8 *)malloc(dinnerTableEntries * 8);
	entriesRead = dnrHandle->read(dinnerTableArea, 8 * dinnerTableEntries) / 8;

	if (entriesRead != dinnerTableEntries)
		warning("entriesRead != dinnerTableEntries. [%d/%d]\n", entriesRead, dinnerTableEntries);

	dataDiskHandle->open(dataFilename, _gameDataPath);
	if (dataDiskHandle->isOpen() == false) 
		error("Error opening %s%s!\n", _gameDataPath, dataFilename);
}

//load in file file_nr to address dest
//if dest == NULL, then allocate memory for this file
uint16 *SkyState::loadFile(uint16 fileNr, uint8 *dest) {
	
	uint8 cflag;
	int32 bytesRead;
	uint8 *filePtr, *inputPtr, *outputPtr;
	dataFileHeader fileHeader;

	#ifdef file_order_chk
		warning("File order checking not implemented yet!\n");
	#endif

	compFile = fileNr;
	debug(2, "load file %d,%d (%d)", (fileNr >> 11), (fileNr & 2047), fileNr); 

	filePtr = (uint8 *)getFileInfo(fileNr);
	if (filePtr == NULL) {
		printf("File %d not found!\n", fileNr);
		return NULL;
	}

	fileFlags = READ_LE_UINT32((filePtr + 5));
	fileSize = fileFlags & 0x03fffff;
	_lastLoadedFileSize = fileSize;
	
	fileOffset = READ_LE_UINT32((filePtr + 2)) & 0x0ffffff;

	cflag = (uint8)((fileOffset >> (23)) & 0x1);
	fileOffset = (((1 << (23)) ^ 0xFFFFFFFF) & fileOffset);

	if (cflag)
		fileOffset <<= 4;

	fixedDest = dest;
	fileDest = dest;
	compDest = dest;

	if (dest == NULL) //we need to allocate memory for this file
		fileDest = (uint8 *)malloc(fileSize);

	dataDiskHandle->seek(fileOffset, SEEK_SET);

	#ifdef file_order_chk
		warning("File order checking not implemented yet!\n");
	#endif

	//now read in the data
	bytesRead = dataDiskHandle->read(fileDest, 1 * fileSize);

	if (bytesRead != (int32)fileSize)
		printf("ERROR: Unable to read %d bytes from datadisk (%d bytes read)\n", fileSize, bytesRead);

	cflag = (uint8)((fileFlags >> (23)) & 0x1);

	//if cflag == 0 then file is compressed, 1 == uncompressed

	if (!cflag) {
		debug(2, "File is compressed...");

		memcpy(&fileHeader, fileDest, sizeof(struct dataFileHeader));
		if ( (uint8)((FROM_LE_16(fileHeader.flag) >> 7) & 0x1)	 ) {
			debug(2, "with RNC!");

			decompSize = (FROM_LE_16(fileHeader.flag) & 0xFFFFFF00) << 8;
			decompSize |= FROM_LE_16((uint16)fileHeader.s_tot_size);

			if (fixedDest == NULL) // is this valid?
				compDest = (uint8 *)malloc(decompSize);

			inputPtr = fileDest;
			outputPtr = compDest;

			if ( (uint8)(fileFlags >> (22) & 0x1) ) //do we include the header?
				inputPtr += sizeof(struct dataFileHeader);
			else {
				memcpy(outputPtr, inputPtr, sizeof(struct dataFileHeader));
				inputPtr += sizeof(struct dataFileHeader);
				outputPtr += sizeof(struct dataFileHeader);
			}

			RncDecoder rncDecoder;
			int32 unPackLen = rncDecoder.unpackM1(inputPtr, outputPtr, 0);

			debug(3, "UnpackM1 returned: %d", unPackLen);

			if (unPackLen == 0) { //Unpack returned 0: file was probably not packed.
				if (fixedDest == NULL)
					free(compDest);
			
				return (uint16 *)fileDest;
			}

			if (! (uint8)(fileFlags >> (22) & 0x1) ) { // include header?
				unPackLen += sizeof(struct dataFileHeader);

				if (unPackLen != (int32)decompSize) {
					debug(1, "ERROR: invalid decomp size! (was: %d, should be: %d)", unPackLen, decompSize);
				}
			}

			_lastLoadedFileSize = decompSize; //including header
			
			if (fixedDest == NULL)
				free(fileDest);

		} else
			debug(2, "but not with RNC! (?!)");
	} else
		return (uint16 *)fileDest;

	return (uint16 *)compDest;
}

uint16 *SkyState::getFileInfo(uint16 fileNr) {
	
	uint16 i;
	uint16 *dnrTbl16Ptr = (uint16 *)dinnerTableArea;

	for (i = 0; i < dinnerTableEntries; i++) {
		if (READ_LE_UINT16(dnrTbl16Ptr + (i * 4)) == fileNr) {
			debug(2, "file %d found!", fileNr);
			return (dnrTbl16Ptr + (i * 4));
		}
	}

	// if file not found return NULL
	return (uint16 *)NULL;
}

void SkyState::dumpFile(uint16 fileNr) {
	char buf[128];
	File out;
	byte* filePtr;

	filePtr = (byte *)loadFile(fileNr, NULL);
	sprintf(buf, "dumps/file-%d.dmp", fileNr);
	
	out.open(buf, "", 1);
	if (out.isOpen() == false) {
		out.open(buf, "", 2);
		if (out.isOpen() == false)
			return;
		out.write(filePtr, _lastLoadedFileSize);
	}
	out.close();
	free(filePtr);
}