-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCResourceManager.cpp
653 lines (516 loc) · 13.9 KB
/
CResourceManager.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include "stdafx.h"
#include "CResourceManager.h"
#include "CFileStreamReader.h"
#include "CFileStreamWriter.h"
#include "CVolReader.h"
#include "CVolWriter.h"
#include "CClmReader.h"
#include "CClmWriter.h"
#include "CMapFile.h"
#include "GlobalFunctions.h"
extern int g_cLocks;
ULONG __stdcall CResourceManager::AddRef()
{
return ++m_cRef;
}
ULONG __stdcall CResourceManager::Release()
{
if (--m_cRef != 0)
return m_cRef;
delete this;
return 0;
}
HRESULT __stdcall CResourceManager::QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_IUnknown)
*ppv = (IUnknown*)(ResourceManager*)this;
else if (riid == IID_IResourceManager)
*ppv = (IResourceManager*)this;
else if (riid == IID_ISupportErrorInfo)
*ppv = (ISupportErrorInfo*)this;
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
CResourceManager::CResourceManager(BSTR gamePath) : m_cRef(1)
{
// Initilize linked list of archives in search path
head = NULL;
tail = NULL;
int stringLength;
// Cache string length
stringLength = SysStringLen(gamePath);
// Add a trailing "\" if none exists
if ((stringLength > 0) && (gamePath[stringLength-1] != L'\\'))
{
// Allocate space for string + "\"
path = SysAllocStringLen(NULL, stringLength+1);
// Copy the string
memcpy(path, gamePath, stringLength*2);
// Append a trailing "\"
path[stringLength] = L'\\';
}
else
{
// Save the path to the game folder
path = SysAllocString(gamePath);
}
// Create a new tile set source object
tileSetSource = new CTileSetSource(this);
g_cLocks++;
}
CResourceManager::~CResourceManager()
{
LinkedListNode *current, *next;
g_cLocks--;
// Unload all archives in search path and delete linked list nodes
current = head;
while (current)
{
current->archive->Release();
next = current->next;
delete current;
current = next;
}
// Free the tile set source object
tileSetSource->Release();
// Free the game path BSTR
SysFreeString(path);
}
HRESULT CResourceManager::get_RootPath(BSTR *path)
{
SysReAllocString(path, this->path);
return S_OK;
}
HRESULT CResourceManager::put_RootPath(BSTR newPath)
{
int stringLength;
// Cache string length
stringLength = SysStringLen(newPath);
// Add a trailing "\" if none exists
if ((stringLength > 0) && (newPath[stringLength-1] != L'\\'))
{
// Allocate space for string + "\"
SysReAllocStringLen(&path, NULL, stringLength+1);
// Copy the string
memcpy(path, newPath, stringLength*2);
// Append a trailing "\"
path[stringLength] = L'\\';
}
else
{
// Save the path to the game folder
SysReAllocString(&path, newPath);
}
// Replace the resource sources
delete tileSetSource;
tileSetSource = new CTileSetSource(this);
return S_OK;
}
HRESULT CResourceManager::OpenStreamRead(
/* [in] */ BSTR fileName,
/* [retval][out] */ StreamReader **stream)
{
int lenFileName = SysStringLen(fileName);
int lenPath = SysStringLen(path);
BSTR pathAndFile = NULL;
// Initialize returned stream pointer to NULL
*stream = NULL;
// Check if relative path given
if (IsRelative(fileName))
{
// Form the full path name rooted at the desired directory
pathAndFile = SysAllocStringLen(NULL, lenFileName + lenPath);
memcpy(pathAndFile, path, lenPath*2);
memcpy((char*)pathAndFile + (lenPath * 2), fileName, lenFileName*2);
}
else
{
// Absolute path. Copy the string
pathAndFile = SysAllocString(fileName);
}
// Check if file exists in directory rooted at the game path
try
{
*stream = new CFileStreamReader(pathAndFile);
}
catch(...)
{
}
// Release the path string (Not needed for searching archives)
SysFreeString(pathAndFile);
if (*stream)
{
// Stream successfully opened
return S_OK;
}
// Failed to open a file. Search the loaded vol files for this resource.
LinkedListNode *current = head;
while (current)
{
// Try to open the stream from each archive
// Note: Don't use full path. Only resource name.
current->archive->OpenStreamRead(fileName, stream);
// Check if successful
if (*stream != NULL)
{
// Stream opened successfully
return S_OK;
}
current = current->next;
}
// Could not find resource.
PostErrorMsg(L"Could not find resource.");
return E_FAIL;
}
HRESULT CResourceManager::OpenStreamWrite(
/* [in] */ BSTR fileName,
/* [retval][out] */ StreamWriter **stream)
{
int lenFileName = SysStringLen(fileName);
int lenPath = SysStringLen(path);
BSTR pathAndFile = NULL;
// Initialize returned stream pointer to NULL
*stream = NULL;
// Check if relative path given
if (IsRelative(fileName))
{
// Form the full path name rooted at the desired directory
pathAndFile = SysAllocStringLen(NULL, lenFileName + lenPath);
memcpy(pathAndFile, path, lenPath*2);
memcpy((char*)pathAndFile + (lenPath * 2), fileName, lenFileName*2);
}
else
{
// Absolute path. Copy the string
pathAndFile = SysAllocString(fileName);
}
// Try to open the file for writing
try
{
*stream = new CFileStreamWriter(pathAndFile);
if (stream == NULL)
{
SysFreeString(pathAndFile);
PostErrorMsg(L"Failed to create stream writer.");
return E_OUTOFMEMORY;
}
}
catch(...)
{
}
if (*stream)
{
// Stream successfully opened
SysFreeString(pathAndFile);
return S_OK;
}
// **Possible future expansion**
// See if an existing archive file can accept the new data
// Could not open the file for writing
SysFreeString(pathAndFile);
PostErrorMsg(L"Failed to create stream writer.");
return E_FAIL;
}
HRESULT CResourceManager::LoadMapFile(BSTR fileName, enum MapLoadSaveFormat loadFlags, MapFile **mapFile)
{
StreamReader *inStream;
// Initialize returned mapFile pointer to NULL
*mapFile = NULL;
// Try to open the file as a stream
OpenStreamRead(fileName, &inStream);
// Check if a stream was constructed
if (inStream == NULL)
{
// Failed to open the file as a stream
return E_INVALIDARG;
}
// Read in the map data
HRESULT hr = LoadMap(inStream, loadFlags, mapFile);
// Release the input stream
inStream->Release();
return hr;
}
HRESULT CResourceManager::LoadMap(StreamReader *inStream, enum MapLoadSaveFormat loadFlags, MapFile **mapFile)
{
CMapFile *pMapFile;
// Initialize returned mapFile pointer to NULL
*mapFile = NULL;
try
{
pMapFile = new CMapFile(tileSetSource, inStream, loadFlags);
if (pMapFile == 0) return E_OUTOFMEMORY;
}
catch(...)
{
// Error constructing object
return E_FAIL;
}
HRESULT hr = pMapFile->QueryInterface(IID_MapFile, (void**)mapFile);
pMapFile->Release();
return hr;
}
HRESULT CResourceManager::CreateNewMap(int width, int height, MapFile** newMap)
{
CMapFile *pMapFile;
// Initialize returned mapFile pointer to NULL
*newMap = NULL;
try
{
pMapFile = new CMapFile(tileSetSource, width, height);
if (pMapFile == 0) return E_OUTOFMEMORY;
}
catch(...)
{
// Error constructing object
return E_FAIL;
}
HRESULT hr = pMapFile->QueryInterface(IID_MapFile, (void**)newMap);
pMapFile->Release();
return hr;
}
HRESULT CResourceManager::LoadTileSetFile(BSTR fileName, TileSet **tileSet)
{
StreamReader *inStream;
// Initialize returned tileSet pointer to NULL
*tileSet = NULL;
// Try to open the file as a stream
OpenStreamRead(fileName, &inStream);
// Check if a stream was constructed
if (inStream == NULL)
{
// Failed to open the file as a stream
return E_FAIL;
}
// Read in the tile set data
HRESULT hr = LoadTileSet(inStream, tileSet);
// Release the input stream
inStream->Release();
return hr;
}
HRESULT CResourceManager::LoadTileSet(StreamReader *stream, TileSet **tileSet)
{
CTileSet *pTileSet;
// Initialize returned tileSet pointer to NULL
*tileSet = NULL;
try
{
pTileSet = new CTileSet(stream);
if (pTileSet == 0) return E_OUTOFMEMORY;
}
catch(...)
{
// Error constructing object
return E_FAIL;
}
HRESULT hr = pTileSet->QueryInterface(IID_TileSet, (void**)tileSet);
pTileSet->Release();
return hr;
}
HRESULT CResourceManager::CreateTileSet(int numTiles, int bitDepth, int width, TileSet **newTileSet)
{
CTileSet *pTileSet;
// Initialize returned tileSet pointer to NULL
*newTileSet = NULL;
try
{
pTileSet = new CTileSet(numTiles, bitDepth, width);
if (pTileSet == 0) return E_OUTOFMEMORY;
}
catch(...)
{
// Error constructing object
return E_FAIL;
}
HRESULT hr = pTileSet->QueryInterface(IID_TileSet, (void**)newTileSet);
pTileSet->Release();
return hr;
}
HRESULT CResourceManager::LoadVolFile(BSTR fileName, int bAttachToStream, ArchiveReader **volReader)
{
StreamReader *inStream;
SeekableStreamReader *inSeekStream;
// Initialize returned ArchiveReader to NULL
*volReader = NULL;
// Try to open the file as a stream
OpenStreamRead(fileName, &inStream);
// Check if a stream was constructed
if (inStream == NULL)
{
// Failed to open the file as a stream
return E_FAIL;
}
// Obtain the SeekableStreamReader interface
inStream->QueryInterface(IID_SeekableStreamReader, (void**)&inSeekStream);
inStream->Release();
// Check if the interface was obtained
if (inSeekStream == NULL)
{
// Failed to obtain a SeekableStreamReader interface
return E_FAIL;
}
// Read in the vol data
HRESULT hr = LoadVol(inSeekStream, bAttachToStream, volReader);
// Release the stream
inSeekStream->Release();
return hr;
}
HRESULT CResourceManager::LoadVol(SeekableStreamReader *inStream, int bAttachToStream, ArchiveReader **volReader)
{
CVolReader *pVolReader;
// Initialize returned ArchiveReader to NULL
*volReader = NULL;
// Try to create a VOL reader
try
{
pVolReader = new CVolReader(inStream, bAttachToStream);
if (pVolReader == NULL) return E_OUTOFMEMORY;
}
catch(...)
{
return E_FAIL;
}
// Obtain the ArchiveReader interface
HRESULT hr = pVolReader->QueryInterface(IID_ArchiveReader, (void**)volReader);
pVolReader->Release();
return hr;
}
HRESULT CResourceManager::CreateVolFile(ArchiveWriter **volWriter)
{
CVolWriter *pVolWriter;
// Initialize returned ArchiveWriter to NULL
*volWriter = NULL;
try
{
pVolWriter = new CVolWriter();
if (pVolWriter == NULL) return E_OUTOFMEMORY;
}
catch(...)
{
// Failed to create CVolWriter object
return E_FAIL;
}
// Obtain the ArchiveWriter interface
HRESULT hr = pVolWriter->QueryInterface(IID_ArchiveWriter, (void**)volWriter);
pVolWriter->Release();
return hr;
}
HRESULT CResourceManager::LoadClmFile(BSTR fileName, int bAttachToStream, ArchiveReader **clmReader)
{
StreamReader *inStream;
SeekableStreamReader *inSeekStream;
// Initialize returned ArchiveReader to NULL
*clmReader = NULL;
// Try to open the file as a stream
OpenStreamRead(fileName, &inStream);
// Check if a stream was constructed
if (inStream == NULL)
{
// Failed to open the file as a stream
PostErrorMsg(L"Error. Could not open stream.");
return E_FAIL;
}
// Obtain the SeekableStreamReader interface
inStream->QueryInterface(IID_SeekableStreamReader, (void**)&inSeekStream);
inStream->Release();
// Check if the interface was obtained
if (inSeekStream == NULL)
{
// Failed to obtain a SeekableStreamReader interface
PostErrorMsg(L"Error. Stream was not seekable.");
return E_FAIL;
}
// Read in the vol data
HRESULT hr = LoadClm(inSeekStream, bAttachToStream, clmReader);
// Release the stream
inSeekStream->Release();
return hr;
}
HRESULT CResourceManager::LoadClm(SeekableStreamReader *inStream, int bAttachToStream, ArchiveReader **clmReader)
{
CClmReader *pClmReader;
// Initialize returned ArchiveReader to NULL
*clmReader = NULL;
// Try to create a CLM reader
try
{
pClmReader = new CClmReader(inStream, bAttachToStream);
if (pClmReader == NULL) return E_OUTOFMEMORY;
}
catch(...)
{
return E_FAIL;
}
// Obtain the ArchiveReader interface
HRESULT hr = pClmReader->QueryInterface(IID_ArchiveReader, (void**)clmReader);
pClmReader->Release();
return hr;
}
HRESULT CResourceManager::CreateClmFile(ArchiveWriter **clmWriter)
{
CClmWriter *pClmWriter;
// Initialize returned ArchiveWriter to NULL
*clmWriter = NULL;
try
{
pClmWriter = new CClmWriter();
if (pClmWriter == NULL) return E_OUTOFMEMORY;
}
catch(...)
{
// Failed to create CVolWriter object
return E_FAIL;
}
// Obtain the ArchiveWriter interface
HRESULT hr = pClmWriter->QueryInterface(IID_ArchiveWriter, (void**)clmWriter);
pClmWriter->Release();
return hr;
}
HRESULT CResourceManager::AddArchiveToSearch(ArchiveReader *volReader)
{
LinkedListNode *newNode;
// Create a new linked list node
newNode = new LinkedListNode;
newNode->next = NULL;
newNode->archive = volReader;
// Increment the reference count
volReader->AddRef();
// Check if list is empty
if (head == NULL)
head = newNode;
// Add archive to tail of linked list
if (tail != NULL)
tail->next = newNode;
// Update tail of linked list
tail = newNode;
return S_OK;
}
HRESULT CResourceManager::ClearSearchPath()
{
LinkedListNode *current, *next;
// Clear linked list of archives in search path
current = head;
while (current)
{
current->archive->Release(); // Release the archive
next = current->next; // Get the next linked list node
delete current; // Release the current linked list node
current = next; // Process next node
}
head = NULL;
tail = NULL;
return S_OK;
}
// ISupportErrorInfo
// *****************
HRESULT CResourceManager::InterfaceSupportsErrorInfo(REFIID riid)
{
if (riid == IID_IResourceManager)
return S_OK;
else
return S_FALSE;
}