[059f52]: gemrb / plugins / KEYImporter / KEYImporter.cpp  Maximize  Restore  History

Download this file

260 lines (225 with data), 7.0 kB

  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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
#include "KEYImporter.h"
#include "win32def.h"
#include "globals.h"
#include "IndexedArchive.h"
#include "Interface.h"
#include "ResourceDesc.h"
#include "System/FileStream.h"
KEYImporter::KEYImporter(void)
{
description = NULL;
}
KEYImporter::~KEYImporter(void)
{
free(description);
for (unsigned int i = 0; i < biffiles.size(); i++) {
free( biffiles[i].name );
}
}
static char* AddCBF(char *file)
{
// This is safe in single-threaded, since the
// return value is passed straight to PathJoin.
static char cbf[_MAX_PATH];
strcpy(cbf,file);
char *dot = strrchr(cbf, '.');
if (dot)
strcpy(dot, ".cbf");
else
strcat(cbf, ".cbf");
return cbf;
}
static bool PathExists(BIFEntry *entry, const char *path)
{
PathJoin(entry->path, path, entry->name, NULL);
if (file_exists(entry->path)) {
return true;
}
PathJoin(entry->path, path, AddCBF(entry->name), NULL);
if (file_exists(entry->path)) {
return true;
}
return false;
}
static bool PathExists(BIFEntry *entry, const std::vector<std::string> &pathlist)
{
size_t i;
for(i=0;i<pathlist.size();i++) {
if (PathExists(entry, pathlist[i].c_str() )) {
return true;
}
}
return false;
}
static void FindBIF(BIFEntry *entry)
{
entry->cd = 0;
entry->found = PathExists(entry, core->GamePath);
if (entry->found) {
return;
}
for (int i = 0; i < MAX_CD; i++) {
if (PathExists(entry, core->CD[i]) ) {
entry->found = true;
entry->cd = i;
return;
}
}
printMessage("KEYImporter", "Cannot find %s...", WHITE, entry->name);
printStatus( "ERROR", LIGHT_RED );
}
bool KEYImporter::Open(const char *resfile, const char *desc)
{
free(description);
description = strdup(desc);
if (!core->IsAvailable( IE_BIF_CLASS_ID )) {
print( "[ERROR]\nAn Archive Plug-in is not Available\n" );
return false;
}
unsigned int i;
// NOTE: Interface::Init has already resolved resfile.
printMessage("KEYImporter", "Opening %s...", WHITE, resfile);
FileStream* f = FileStream::OpenFile(resfile);
if (!f) {
// Check for backslashes (false escape characters)
// this check probably belongs elsewhere (e.g. ResolveFilePath)
if (strstr( resfile, "\\ " )) {
print("%s", "\nEscaped space(s) detected in path!. Do not escape spaces in your GamePath! " );
}
printStatus( "ERROR", LIGHT_RED );
printMessage( "KEYImporter", "Cannot open Chitin.key\n", LIGHT_RED );
textcolor( WHITE );
print("This means you set the GamePath config variable incorrectly.\n");
print("It must point to the directory that holds a readable Chitin.key\n");
return false;
}
printStatus( "OK", LIGHT_GREEN );
printMessage( "KEYImporter", "Checking file type...", WHITE );
char Signature[8];
f->Read( Signature, 8 );
if (strncmp( Signature, "KEY V1 ", 8 ) != 0) {
printStatus( "ERROR", LIGHT_RED );
printMessage( "KEYImporter", "File has an Invalid Signature.\n", LIGHT_RED );
textcolor( WHITE );
delete( f );
return false;
}
printStatus( "OK", LIGHT_GREEN );
printMessage( "KEYImporter", "Reading Resources...\n", WHITE );
ieDword BifCount, ResCount, BifOffset, ResOffset;
f->ReadDword( &BifCount );
f->ReadDword( &ResCount );
f->ReadDword( &BifOffset );
f->ReadDword( &ResOffset );
printMessage( "KEYImporter", " ", WHITE );
print( "BIF Files Count: %d (Starting at %d Bytes)\n", BifCount,
BifOffset );
printMessage("KEYImporter", "RES Count: %d (Starting at %d Bytes)\n", WHITE,
ResCount, ResOffset);
f->Seek( BifOffset, GEM_STREAM_START );
ieDword BifLen, ASCIIZOffset;
ieWord ASCIIZLen;
for (i = 0; i < BifCount; i++) {
BIFEntry be;
f->Seek( BifOffset + ( 12 * i ), GEM_STREAM_START );
f->ReadDword( &BifLen );
f->ReadDword( &ASCIIZOffset );
f->ReadWord( &ASCIIZLen );
f->ReadWord( &be.BIFLocator );
be.name = ( char * ) malloc( ASCIIZLen );
f->Seek( ASCIIZOffset, GEM_STREAM_START );
f->Read( be.name, ASCIIZLen );
for (int p = 0; p < ASCIIZLen; p++) {
//some MAC versions use : as delimiter
if (be.name[p] == '\\' || be.name[p] == ':')
be.name[p] = PathDelimiter;
}
FindBIF(&be);
biffiles.push_back( be );
}
f->Seek( ResOffset, GEM_STREAM_START );
MapKey key;
ieDword ResLocator;
// limit to 32k buckets
// only ~1% of the bg2 entries are of bucket lenght >4
resources.init(ResCount > 32 * 1024 ? 32 * 1024 : ResCount, ResCount);
for (i = 0; i < ResCount; i++) {
f->ReadResRef(key.ref);
f->ReadWord(&key.type);
f->ReadDword(&ResLocator);
// seems to be always the last entry?
if (key.ref[0] != 0)
resources.set(key, ResLocator);
}
printMessage( "KEYImporter", "Resources Loaded...", WHITE );
printStatus( "OK", LIGHT_GREEN );
delete( f );
return true;
}
bool KEYImporter::HasResource(const char* resname, SClass_ID type)
{
return resources.has(resname, type);
}
bool KEYImporter::HasResource(const char* resname, const ResourceDesc &type)
{
return HasResource(resname, type.GetKeyType());
}
DataStream* KEYImporter::GetStream(const char *resname, ieWord type)
{
if (type == 0)
return NULL;
const ieDword *ResLocator = resources.get(resname, type);
if (!ResLocator)
return 0;
unsigned int bifnum = ( *ResLocator & 0xFFF00000 ) >> 20;
if (!biffiles[bifnum].found) {
print( "Cannot find %s... Resource unavailable.\n",
biffiles[bifnum].name );
return NULL;
}
PluginHolder<IndexedArchive> ai(IE_BIF_CLASS_ID);
if (ai->OpenArchive( biffiles[bifnum].path ) == GEM_ERROR) {
print("Cannot open archive %s\n", biffiles[bifnum].path );
return NULL;
}
DataStream* ret = ai->GetStream( *ResLocator, type );
if (ret) {
strnlwrcpy( ret->filename, resname, 8 );
strcat( ret->filename, "." );
strcat( ret->filename, core->TypeExt( type ) );
return ret;
}
return NULL;
}
DataStream* KEYImporter::GetResource(const char* resname, SClass_ID type)
{
//the word masking is a hack for synonyms, currently used for bcs==bs
return GetStream(resname, type&0xFFFF);
}
DataStream* KEYImporter::GetResource(const char* resname, const ResourceDesc &type)
{
return GetStream(resname, type.GetKeyType());
}
#include "plugindef.h"
GEMRB_PLUGIN(0x1DFDEF80, "KEY File Importer")
PLUGIN_CLASS(PLUGIN_RESOURCE_KEY, KEYImporter)
END_PLUGIN()

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks