1 /* GemRB - Infinity Engine Emulator
2  * Copyright (C) 2003 The GemRB Project
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "SAVImporter.h"
20 
21 #include "Compressor.h"
22 #include "FileCache.h"
23 #include "Interface.h"
24 #include "PluginMgr.h"
25 
26 using namespace GemRB;
27 
SAVImporter()28 SAVImporter::SAVImporter()
29 {
30 }
31 
~SAVImporter()32 SAVImporter::~SAVImporter()
33 {
34 }
35 
DecompressSaveGame(DataStream * compressed)36 int SAVImporter::DecompressSaveGame(DataStream *compressed)
37 {
38 	char Signature[8];
39 	compressed->Read( Signature, 8 );
40 	if (strncmp( Signature, "SAV V1.0", 8 ) ) {
41 		return GEM_ERROR;
42 	}
43 	int All = compressed->Remains();
44 	int Current;
45 	int percent, last_percent = 20;
46 	if (!All) return GEM_ERROR;
47 	do {
48 		ieDword fnlen, complen, declen;
49 		compressed->ReadDword( &fnlen );
50 		if (!fnlen) {
51 			Log(ERROR, "SAVImporter", "Corrupt Save Detected");
52 			return GEM_ERROR;
53 		}
54 		char* fname = ( char* ) malloc( fnlen );
55 		compressed->Read( fname, fnlen );
56 		strlwr(fname);
57 		compressed->ReadDword( &declen );
58 		compressed->ReadDword( &complen );
59 		print("Decompressing %s", fname);
60 		DataStream* cached = CacheCompressedStream(compressed, fname, complen, true);
61 		free( fname );
62 		if (!cached)
63 			return GEM_ERROR;
64 		delete cached;
65 		Current = compressed->Remains();
66 		//starting at 20% going up to 70%
67 		percent = (20 + (All - Current) * 50 / All);
68 		if (percent - last_percent > 5) {
69 			core->LoadProgress(percent);
70 			last_percent = percent;
71 		}
72 	}
73 	while(Current);
74 	return GEM_OK;
75 }
76 
77 //this one can create .sav files only
CreateArchive(DataStream * compressed)78 int SAVImporter::CreateArchive(DataStream *compressed)
79 {
80 	if (!compressed) {
81 		return GEM_ERROR;
82 	}
83 	char Signature[8];
84 
85 	memcpy(Signature,"SAV V1.0",8);
86 	compressed->Write(Signature, 8);
87 
88 	return GEM_OK;
89 }
90 
AddToSaveGame(DataStream * str,DataStream * uncompressed)91 int SAVImporter::AddToSaveGame(DataStream *str, DataStream *uncompressed)
92 {
93 	ieDword fnlen, declen, complen;
94 
95 	fnlen = strlen(uncompressed->filename)+1;
96 	declen = uncompressed->Size();
97 	str->WriteDword( &fnlen);
98 	str->Write( uncompressed->filename, fnlen);
99 	str->WriteDword( &declen);
100 	//baaah, we dump output right in the stream, we get the compressed length
101 	//only after the compressed data was written
102 	complen = 0xcdcdcdcd; //placeholder
103 	unsigned long Pos = str->GetPos(); //storing the stream position
104 	str->WriteDword( &complen);
105 
106 	PluginHolder<Compressor> comp(PLUGIN_COMPRESSION_ZLIB);
107 	comp->Compress( str, uncompressed );
108 
109 	//writing compressed length (calculated)
110 	unsigned long Pos2 = str->GetPos();
111 	complen = Pos2-Pos-sizeof(ieDword); //calculating the compressed stream size
112 	str->Seek(Pos, GEM_STREAM_START); //going back to the placeholder
113 	str->WriteDword( &complen);       //updating size
114 	str->Seek(Pos2, GEM_STREAM_START);//resuming work
115 	return GEM_OK;
116 }
117 
118 #include "plugindef.h"
119 
120 GEMRB_PLUGIN(0xCDF132C, "SAV File Importer")
121 PLUGIN_CLASS(IE_SAV_CLASS_ID, SAVImporter)
122 END_PLUGIN()
123