Subversion Repositories spk

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include "QtCatPck.h"
2
#include "../spk.h"
3
 
4
/*
5
========================================================================
6
 Cat and PCK FrontEnd Libraies using QT
7
========================================================================
8
*/
9
 
10
 
11
void UnpackFile ( const QString &filename, const QString &unpackedFile )
12
{
13
	size_t size;
14
 
15
	QString newFilename = filename;
16
	newFilename = newFilename.replace ( '/', '\\' );
17
	QString newUnpackedFile = unpackedFile;
18
	newUnpackedFile = newUnpackedFile.replace ( '/', '\\' );
19
 
20
	unsigned char *buffer = UnPCKFile ( newFilename, &size );
21
 
22
	if ( buffer )
23
	{
24
		FILE *id = fopen ( newUnpackedFile, "wb" );
25
 
26
		if ( id )
27
		{
28
			fwrite ( buffer, size, 1, id );
29
			fclose ( id );
30
		}
31
 
32
		delete buffer;
33
	}
34
}
35
 
36
void CopyCatFile ( const QString &from, const QString &to )
37
{
38
	if ( !to.contains ( "::" ) )
39
		return;
40
 
41
	QString tocatfile = to.section ( "::", 0, 0 );
42
	QString tofile = to.section ( "::", 1, 1 );
43
 
44
	if ( from.contains ( "::" ) )
45
	{
46
		CCatFile tocat;
47
		if ( tocat.Open ( tocatfile, CATREAD_CATDECRYPT, true ) )
48
			return;
49
 
50
		tocat.AppendFile ( from, tofile );
51
	}
52
}
53
 
54
 
55
bool PackFile ( unsigned char *data, size_t size, const QString &filename, bool bPCK )
56
{
57
	if ( filename.find ( "::" ) == -1 )
58
	{
59
		// delete existing file first
60
		QString newfilename = filename;
61
		QFile delfile ( newfilename );
62
		if ( delfile.exists () )
63
			delfile.remove ();
64
 
65
		// now open file for writing
66
		QFile f( filename );
67
		if ( f.open ( IO_WriteOnly ) )
68
		{
69
			size_t newsize = 0;
70
			unsigned char *d = PCKData ( data, size, &newsize );
71
 
72
			bool ret = false;
73
			if ( d )
74
			{
75
				f.writeBlock ( (const char *)d, newsize );
76
				delete d;
77
				ret = true;
78
			}
79
 
80
			f.close ();
81
 
82
			return ret;
83
		}
84
	}
85
	else
86
	{
87
		QString catfile = filename.section ( "::", 0, 0 );
88
		QString file = filename.section ( "::", 1, 1 );
89
 
90
		CCatFile cat;
91
		int err = cat.Open ( catfile, CATREAD_CATDECRYPT, true );
92
		if ( (err != CATERR_NONE) && (err != CATERR_CREATED) )
93
			return false;
94
 
95
		return cat.AppendData ( data, size, file );
96
	}
97
 
98
	return false;
99
}
100
 
101
bool PackFile ( QFile *readfile, const QString &filename, bool bPCK )
102
{
103
	if ( readfile->open ( IO_ReadOnly ) )
104
	{
105
		size_t size = QFileInfo ( *readfile ).size();
106
		unsigned char *data = new unsigned char [size];
107
		readfile->readBlock ( (char *)data, size );
108
 
109
		readfile->close();
110
 
111
		bool ret = PackFile ( data, size, filename, bPCK );
112
 
113
		delete [] data;
114
 
115
		return ret;
116
	}
117
 
118
	return false;
119
}
120
 
121
bool ExtractFromCat ( const QString &filename, const QString &savefile, bool convert, bool plain )
122
{
123
	if ( filename.find ( "::" ) == -1 )
124
		return false;
125
 
126
 
127
	QString catfile = filename.section ( "::", 0, 0 );
128
	QString file = filename.section ( "::", 1, 1 );
129
 
130
	// open the cat file
131
	CCatFile cat;
132
	if ( cat.Open ( catfile, CATREAD_CATDECRYPT ) )
133
		return false;
134
 
135
	// find the file to extract
136
	SInCatFile *f = cat.FindData ( file );
137
	if ( !f )
138
		return false;
139
 
140
	// read the data to memory
141
	if ( !cat.ReadFileToData ( f ) )
142
		return false;
143
 
144
	// check for extract
145
	QString sSaveFile = savefile;
146
 
147
	unsigned char *d = f->sData;
148
	size_t size = f->lSize;
149
 
150
	if ( (plain) && (IsDataPCK ( f->sData, f->lSize )) )
151
	{
152
		if ( convert )
153
		{
154
			QString ext = QFileInfo(sSaveFile).extension(false).lower();
155
			if ( ext == "pbb" )
156
				sSaveFile = QFileInfo(sSaveFile).dirPath() + "/" + QFileInfo(sSaveFile).baseName(true) + ".bob";
157
			else if ( ext == "pbd" )
158
				sSaveFile = QFileInfo(sSaveFile).dirPath() + "/" + QFileInfo(sSaveFile).baseName(true) + ".bod";
159
			else if ( ext == "pck" )
160
			{
161
				if ( file.left(5).lower() == "types" )
162
					sSaveFile = QFileInfo(sSaveFile).dirPath() + "/" + QFileInfo(sSaveFile).baseName(true) + ".txt";
163
				else
164
					sSaveFile = QFileInfo(sSaveFile).dirPath() + "/" + QFileInfo(sSaveFile).baseName(true) + ".xml";
165
			}
166
		}
167
 
168
		d = UnPCKData ( f->sData, f->lSize, &size );
169
	}
170
 
171
	if ( !d )
172
		return false;
173
 
174
	// now write to file
175
	QFile wfile ( sSaveFile );
176
	if ( wfile.open ( IO_WriteOnly ) )
177
	{
178
		wfile.writeBlock ( (const char *)d, size );
179
		wfile.close();
180
	}
181
 
182
	return true;
183
}
184
 
185
bool RemoveFromCat ( const QString &file )
186
{
187
	if ( !file.contains ( "::" ) )
188
		return false;
189
 
190
	QString catfile = file.section ( "::", 0, 0 );
191
	QString infile = file.section ( "::", 1, 1 );
192
 
193
	CCatFile cat;
194
	if ( cat.Open ( catfile, CATREAD_CATDECRYPT, false ) )
195
		return false;
196
 
197
	if ( !cat.RemoveFile ( infile ) )
198
		return false;
199
 
200
	return true;
201
}
202
 
203
QPtrList<SCatFile> GetCatList ( const QString &file )
204
{
205
	QPtrList<SCatFile> list;
206
 
207
	CCatFile cat;
208
	if ( !cat.Open ( file, CATREAD_CATDECRYPT, false ) )
209
	{
210
		for ( int i = 0; i < cat.GetNumFiles(); i++ )
211
		{
212
			SInCatFile *cf = cat.GetFile (i);
213
			SCatFile *f = new SCatFile;
214
			f->iSize = cf->lSize;
215
			f->sName = cf->sFile.c_str();
216
			f->sName = f->sName.replace ( "\\", "/" );
217
			list.append ( f );			
218
		}
219
	}
220
 
221
	return list;
222
}
223
 
224
 
225
bool ExtractFile ( const QString &dir, const QString &file, const QString &to )
226
{
227
	bool done = false;
228
 
229
	// rmeove old file first
230
	if ( QFile ( to ).exists() )
231
		QFile ( to ).remove ();
232
 
233
 
234
	if ( file.contains ( "::" ) )
235
	{
236
		if ( ExtractFromCat ( file, to, true, true ) )
237
			done = true;
238
	}
239
	else
240
	{
241
		// find first file to check
242
		QString sFile;
243
		int num;
244
		for ( num = 1; num <= 99; num++ )
245
		{
246
			if ( num < 10 )
247
				sFile.sprintf ( "0%d.cat", num );
248
			else
249
				sFile.sprintf ( "%d.cat", num );
250
 
251
			if ( !QFile ( dir + "/" + sFile ).exists() )
252
				break;
253
		}
254
 
255
		--num;
256
		for ( ; num; num-- )
257
		{
258
			if ( num < 10 )
259
				sFile.sprintf ( "0%d", num );
260
			else
261
				sFile.sprintf ( "%d", num );
262
 
263
			if ( ExtractFromCat ( dir + "/" + sFile + ".cat::" + file, to, true, true ) )
264
			{
265
				done = true;
266
				break;
267
			}
268
		}
269
	}
270
 
271
	return done;
272
}
273
 
274
 
275
/*
276
  ================================================================
277
  =====            End of Cat/Pck Utils                      =====
278
  ================================================================
279
*/
280