-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCExtension.cpp
1680 lines (1239 loc) · 38.6 KB
/
CExtension.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CExtension.cpp
//
// CExtension class
// Copyright (c) 2012 by Kronosaur Productions, LLC. All Rights Reserved.
//
// API VERSION HISTORY
//
// 0: Unknown version
//
// 1: 95-0.96b
// Original Extensions
//
// 2: 0.97
// Changed gStation to gSource
//
// 3: 1.1
// <SmokeTrail>: emitSpeed fixed (used in klicks per tick instead of per second)
//
// See: LoadExtensionVersion in Utilities.cpp
#include "PreComp.h"
#define ADVENTURE_DESC_TAG CONSTLIT("AdventureDesc")
#define CORE_LIBRARY_TAG CONSTLIT("CoreLibrary")
#define GLOBALS_TAG CONSTLIT("Globals")
#define IMAGE_TAG CONSTLIT("Image")
#define IMAGES_TAG CONSTLIT("Images")
#define LIBRARY_TAG CONSTLIT("Library")
#define MODULE_TAG CONSTLIT("Module")
#define MODULES_TAG CONSTLIT("Modules")
#define SOUND_TAG CONSTLIT("Sound")
#define SOUNDS_TAG CONSTLIT("Sounds")
#define STAR_SYSTEM_TOPOLOGY_TAG CONSTLIT("StarSystemTopology")
#define STATION_TYPE_RESOURCES_TAG CONSTLIT("StationTypeResources")
#define SYSTEM_TOPOLOGY_TAG CONSTLIT("SystemTopology")
#define SYSTEM_TYPES_TAG CONSTLIT("SystemTypes")
#define TABLES_TAG CONSTLIT("Tables")
#define TRANSCENDENCE_ADVENTURE_TAG CONSTLIT("TranscendenceAdventure")
#define TRANSCENDENCE_EXTENSION_TAG CONSTLIT("TranscendenceExtension")
#define TRANSCENDENCE_LIBRARY_TAG CONSTLIT("TranscendenceLibrary")
#define TRANSCENDENCE_MODULE_TAG CONSTLIT("TranscendenceModule")
#define API_VERSION_ATTRIB CONSTLIT("apiVersion")
#define AUTO_INCLUDE_ATTRIB CONSTLIT("autoInclude")
#define AUTO_INCLUDE_FOR_COMPATIBILITY_ATTRIB CONSTLIT("autoIncludeForCompatibility")
#define COVER_IMAGE_UNID_ATTRIB CONSTLIT("coverImageID")
#define CREDITS_ATTRIB CONSTLIT("credits")
#define DEBUG_ONLY_ATTRIB CONSTLIT("debugOnly")
#define EXTENDS_ATTRIB CONSTLIT("extends")
#define EXTENSION_API_VERSION_ATTRIB CONSTLIT("extensionAPIVersion")
#define FILENAME_ATTRIB CONSTLIT("filename")
#define FOLDER_ATTRIB CONSTLIT("folder")
#define HIDDEN_ATTRIB CONSTLIT("hidden")
#define NAME_ATTRIB CONSTLIT("name")
#define OPTIONAL_ATTRIB CONSTLIT("optional")
#define PRIVATE_ATTRIB CONSTLIT("private")
#define RELEASE_ATTRIB CONSTLIT("release")
#define UNID_ATTRIB CONSTLIT("UNID")
#define USES_XML_ATTRIB CONSTLIT("usesXML")
#define VERSION_ATTRIB CONSTLIT("version")
#define FILESPEC_TDB_EXTENSION CONSTLIT("tdb")
// The center of an adventure cover image is at this position relative to the
// right edge of the image.
const int RIGHT_COVER_OFFSET = 256 + 160;
CExtension::CExtension (void) :
m_dwUNID(0),
m_iGame(gameUnknown),
m_iType(extUnknown),
m_iLoadState(loadNone),
m_iFolderType(folderUnknown),
m_dwAPIVersion(0),
m_pEntities(NULL),
m_dwRelease(0),
m_pCoverImage(NULL),
m_pAdventureDesc(NULL),
m_pRootXML(NULL),
m_bMarked(false),
m_bDebugOnly(false),
m_bRegistered(false),
m_bVerified(false),
m_bPrivate(false),
m_bDisabled(false),
m_bDeleted(false),
m_bUsesXML(false),
m_bUsesCompatibilityLibrary(false),
m_bHidden(false)
// CExtension constructor
{
}
CExtension::~CExtension (void)
// CExtension destructor
{
CleanUp();
}
void CExtension::AccumulateStats (SStats &Stats) const
// AccumulateStats
//
// Accumulate memory and other stats for this extension.
{
int i;
// Add up memory used by our design type structures
for (i = 0; i < m_DesignTypes.GetCount(); i++)
{
CDesignType *pType = m_DesignTypes.GetEntry(i);
// Includes memory allocated by our member variables (but not
// any memory used by derived classes).
Stats.dwBaseTypeMemory += sizeof(CDesignType) + pType->GetAllocMemoryUsage();
// Get specific type stats
CDesignType::SStats TypeStats;
pType->GetStats(TypeStats);
Stats.dwGraphicsMemory += TypeStats.dwGraphicsMemory;
Stats.dwWreckGraphicsMemory += TypeStats.dwWreckGraphicsMemory;
// LATER: Accumulate memory used by the derived class into
// Stats.dwTotalTypeMemory.
}
// Add up XML memory usage
Stats.dwTotalXMLMemory += (m_pRootXML ? (DWORDLONG)m_pRootXML->GetMemoryUsage() : 0);
for (i = 0; i < m_ModuleXML.GetCount(); i++)
Stats.dwTotalXMLMemory += (DWORDLONG)m_ModuleXML[i]->GetMemoryUsage();
}
void CExtension::AddDefaultLibraryReferences (SDesignLoadCtx &Ctx)
// AddDefaultLibraryReferences
//
// Adds default references if we have no other libraries
{
if (GetLibraryCount() == 0)
{
// Add compatibility library if we don't load anything else
// (This should only happen for older extensions. All official
// extensions use either RPG or RTS libraries).
if (GetAPIVersion() < 26 && GetFolderType() != folderBase)
m_bUsesCompatibilityLibrary = true;
}
}
void CExtension::AddEntityNames (CExternalEntityTable *pEntities, TSortMap<DWORD, CString> *retMap) const
// AddEntityNames
//
// Adds entity names to the given map
{
int i;
for (i = 0; i < pEntities->GetCount(); i++)
{
CString sEntity, sValue;
pEntities->GetEntity(i, &sEntity, &sValue);
// Add to the list
DWORD dwUNID = strToInt(sValue, 0);
retMap->SetAt(dwUNID, sEntity);
}
}
void CExtension::AddLibraryReference (SDesignLoadCtx &Ctx, DWORD dwUNID, DWORD dwRelease, bool bOptional)
// AddLibraryReference
//
// Adds a library reference.
{
// Add the library.
//
// NOTE: We can call this function with dwUNID == 0 if we're just trying
// to add the core types library.
if (dwUNID)
{
SLibraryDesc *pLibrary = m_Libraries.Insert();
pLibrary->dwUNID = dwUNID;
pLibrary->dwRelease = dwRelease;
pLibrary->bOptional = bOptional;
}
}
bool CExtension::CanExtend (CExtension *pAdventure) const
// CanExtend
//
// Returns TRUE if this extension can extend the given adventure.
{
int i;
ASSERT(pAdventure);
// If this extension is too old for the adventure, then it can't be used.
// NOTE: We only exclude at adventure create-time. We can't exclude at
// load-time, for obvious reasons.
if (pAdventure->GetMinExtensionAPIVersion() > GetAPIVersion())
return false;
// If our extend list is empty then we extend everything. However, if an
// adventure only wants extensions >= API 33, then we assume that they only
// want extensions with explicit extend requirements. This handles the case
// of Part II not including extensions designed for Part I.
if (m_Extends.GetCount() == 0)
return (pAdventure->GetMinExtensionAPIVersion() < 33);
// If the extension is on the list, then we can extend it.
if (m_Extends.Find(pAdventure->GetUNID()))
return true;
// Otherwise, see if we extend any of the libraries used by this adventure.
for (i = 0; i < pAdventure->GetLibraryCount(); i++)
if (m_Extends.Find(pAdventure->GetLibrary(i).dwUNID))
return true;
// Any adventure that uses the compatibility library should be treated as
// if it includes Core Types, RPG, Universe, and Human Space Vol 1.
//
// NOTE: At this point, m_bUsesCompatibilityLibrary has not yet been set
// because we haven't completely loaded the adventure.
if (pAdventure->GetAPIVersion() < 26
&& (m_Extends.Find(UNID_CORE_TYPES_LIBRARY)
|| m_Extends.Find(UNID_RPG_LIBRARY)
|| m_Extends.Find(UNID_UNIVERSE_LIBRARY)
|| m_Extends.Find(UNID_HUMAN_SPACE_LIBRARY)))
return true;
// Otherwise, we don't extend it
return false;
}
bool CExtension::CanHaveAdventureDesc (void) const
// CanHaveAdventureDesc
//
// Returns TRUE if this extension type can have an adventure descriptor.
{
// Adventure extensions can always have one.
if (m_iType == extAdventure)
return true;
// For previous APIs, the extBase type can also have adventures.
if (GetAPIVersion() < 26 && m_iType == extBase)
return true;
// Otherwise, invalid
return false;
}
void CExtension::CleanUp (void)
// CleanUp
//
// Cleans up class and frees up resources.
{
int i;
// Delete entities
if (m_pEntities)
{
delete m_pEntities;
m_pEntities = NULL;
}
// Delete design types
for (i = 0; i < m_DesignTypes.GetCount(); i++)
m_DesignTypes.GetEntry(i)->Delete();
m_DesignTypes.DeleteAll();
m_Externals.DeleteAll();
// Delete global functions
CCodeChain *pCC = &g_pUniverse->GetCC();
for (i = 0; i < m_Globals.GetCount(); i++)
m_Globals[i].pCode->Discard(pCC);
m_Globals.DeleteAll();
// Delete XML representation
CleanUpXML();
// Delete other stuff
m_Topology.CleanUp();
SweepImages();
}
void CExtension::CleanUpXML (void)
// CleanUpXML
//
// Deletes XML representation.
{
int i;
if (m_pRootXML)
{
delete m_pRootXML;
m_pRootXML = NULL;
}
for (i = 0; i < m_ModuleXML.GetCount(); i++)
delete m_ModuleXML[i];
m_ModuleXML.DeleteAll();
// Remove references to XML
for (i = 0; i < m_DesignTypes.GetCount(); i++)
{
CDesignType *pType = m_DesignTypes.GetEntry(i);
pType->SetXMLElement(NULL);
}
}
ALERROR CExtension::ComposeLoadError (SDesignLoadCtx &Ctx, CString *retsError)
// ComposeLoadError
//
// Adds the filename to the load error.
{
if (retsError)
{
if (Ctx.sErrorFilespec)
*retsError = strPatternSubst(CONSTLIT("%s: %s"), Ctx.sErrorFilespec, Ctx.sError);
else
*retsError = Ctx.sError;
}
return ERR_FAIL;
}
ALERROR CExtension::CreateBaseFile (SDesignLoadCtx &Ctx, EGameTypes iGame, CXMLElement *pDesc, CExternalEntityTable *pEntities, CExtension **retpBase, TArray<CXMLElement *> *retEmbedded)
// CreateBaseFile
//
// Loads a new extension from the base file. We take ownership of pDesc and pEntities.
{
ALERROR error;
int i;
// Create an extension object
CExtension *pExtension = new CExtension;
pExtension->m_sFilespec = Ctx.sResDb;
pExtension->m_dwUNID = 0; // Base is the only extension with 0 UNID.
pExtension->m_iGame = iGame;
pExtension->m_iType = extBase;
pExtension->m_iLoadState = loadEntities;
pExtension->m_iFolderType = folderBase;
pExtension->m_pEntities = pEntities;
pExtension->m_pRootXML = pDesc;
pExtension->m_ModifiedTime = fileGetModifiedTime(Ctx.sResDb);
pExtension->m_bRegistered = true;
pExtension->m_bPrivate = true;
pExtension->m_bHidden = true;
pExtension->m_bAutoInclude = true;
pExtension->m_bUsesXML = false;
pExtension->m_bUsesCompatibilityLibrary = false;
// Load the apiVersion
CString sAPIVersion;
if (pDesc->FindAttribute(API_VERSION_ATTRIB, &sAPIVersion))
{
pExtension->m_dwAPIVersion = (DWORD)strToInt(sAPIVersion, 0);
if (pExtension->m_dwAPIVersion < 12)
pExtension->m_dwAPIVersion = 0;
}
// If this version is later than what we expect, then we fail.
if (pExtension->m_dwAPIVersion > API_VERSION)
{
pExtension->m_pEntities = NULL; // Let our parent clean up
pExtension->m_pRootXML = NULL;
delete pExtension;
Ctx.sError = strPatternSubst(CONSTLIT("Newer version of %s is required."), fileGetProductName());
return ERR_FAIL;
}
// We return the base extension
*retpBase = pExtension;
// Set up context
Ctx.pExtension = pExtension;
// Load the Main XML file
for (i = 0; i < pDesc->GetContentElementCount(); i++)
{
CXMLElement *pItem = pDesc->GetContentElement(i);
// <Images>
if (strEquals(pItem->GetTag(), IMAGES_TAG))
error = pExtension->LoadResourcesElement(Ctx, pItem);
// <Sounds>
else if (strEquals(pItem->GetTag(), SOUNDS_TAG))
error = pExtension->LoadResourcesElement(Ctx, pItem);
// <SystemTypes>
else if (strEquals(pItem->GetTag(), SYSTEM_TYPES_TAG))
error = pExtension->LoadSystemTypesElement(Ctx, pItem);
// <TranscendenceAdventure>
else if (strEquals(pItem->GetTag(), TRANSCENDENCE_ADVENTURE_TAG)
|| strEquals(pItem->GetTag(), TRANSCENDENCE_LIBRARY_TAG)
|| strEquals(pItem->GetTag(), CORE_LIBRARY_TAG))
{
// Return this as an embedded extension
retEmbedded->Insert(pItem);
error = NOERROR;
}
// Other types
else
error = pExtension->LoadDesignElement(Ctx, pItem);
// Check for error
if (error)
{
pExtension->m_pEntities = NULL; // Let our parent clean up
pExtension->m_pRootXML = NULL;
delete pExtension;
return error;
}
}
// Restore
Ctx.pExtension = NULL;
// Done
pExtension->m_iLoadState = loadComplete;
return NOERROR;
}
ALERROR CExtension::CreateExtension (SDesignLoadCtx &Ctx, CXMLElement *pDesc, EFolderTypes iFolder, CExternalEntityTable *pEntities, CExtension **retpExtension)
// CreateExtension
//
// Loads the given extension or adventure. We take ownership of pDesc and pEntities.
{
ALERROR error;
int i;
// Create an extension object
CExtension *pExtension;
if (error = CreateExtensionFromRoot(Ctx.sResDb, pDesc, iFolder, pEntities, Ctx.dwInheritAPIVersion, &pExtension, &Ctx.sError))
return error;
// Set up context
Ctx.pExtension = pExtension;
// Load all the design elements
for (i = 0; i < pDesc->GetContentElementCount(); i++)
{
CXMLElement *pItem = pDesc->GetContentElement(i);
if (error = pExtension->LoadDesignElement(Ctx, pItem))
{
pExtension->m_pEntities = NULL; // Let our parent clean up.
delete pExtension;
return error;
}
}
// If this is an adventure and we have no adventure descriptor then we
// fail.
if (pExtension->m_iType == extAdventure && pExtension->m_pAdventureDesc == NULL)
{
pExtension->m_pEntities = NULL; // Let our parent clean up.
delete pExtension;
Ctx.sError = CONSTLIT("Adventure must have an AdventureDesc type.");
return ERR_FAIL;
}
pExtension->AddDefaultLibraryReferences(Ctx);
// Restore
Ctx.pExtension = NULL;
// Done
pExtension->m_pRootXML = pDesc;
pExtension->m_iLoadState = (Ctx.bLoadAdventureDesc ? loadAdventureDesc : loadComplete);
*retpExtension = pExtension;
return NOERROR;
}
ALERROR CExtension::CreateExtensionFromRoot (const CString &sFilespec, CXMLElement *pDesc, EFolderTypes iFolder, CExternalEntityTable *pEntities, DWORD dwInheritAPIVersion, CExtension **retpExtension, CString *retsError)
// CreateExtension
//
// Loads the given extension or adventure. We take ownership of pEntities.
{
// Create an extension object
CExtension *pExtension = new CExtension;
pExtension->m_sFilespec = sFilespec;
pExtension->m_dwUNID = pDesc->GetAttributeInteger(UNID_ATTRIB);
if (pExtension->m_dwUNID == 0)
{
delete pExtension;
*retsError = CONSTLIT("Invalid UNID.");
return ERR_FAIL;
}
if (strEquals(pDesc->GetTag(), TRANSCENDENCE_ADVENTURE_TAG))
{
pExtension->m_iGame = gameTranscendence;
pExtension->m_iType = extAdventure;
}
else if (strEquals(pDesc->GetTag(), TRANSCENDENCE_LIBRARY_TAG))
{
pExtension->m_iGame = gameTranscendence;
pExtension->m_iType = extLibrary;
}
else if (strEquals(pDesc->GetTag(), TRANSCENDENCE_EXTENSION_TAG))
{
pExtension->m_iGame = gameTranscendence;
pExtension->m_iType = extExtension;
}
else if (strEquals(pDesc->GetTag(), CORE_LIBRARY_TAG))
{
// For core libraries, we don't care what game it is. It's always
// whatever game the base file is.
pExtension->m_iGame = gameUnknown;
pExtension->m_iType = extLibrary;
}
else
{
delete pExtension;
*retsError = strPatternSubst(CONSTLIT("Unknown root element: %s"), pDesc->GetTag());
return ERR_FAIL;
}
pExtension->m_iLoadState = loadEntities;
pExtension->m_iFolderType = iFolder;
pExtension->m_pEntities = pEntities;
pExtension->m_ModifiedTime = fileGetModifiedTime(sFilespec);
pExtension->m_bDebugOnly = pDesc->GetAttributeBool(DEBUG_ONLY_ATTRIB);
pExtension->m_bRegistered = IsRegisteredUNID(pExtension->m_dwUNID);
pExtension->m_bPrivate = pDesc->GetAttributeBool(PRIVATE_ATTRIB);
pExtension->m_bAutoInclude = pDesc->GetAttributeBool(AUTO_INCLUDE_ATTRIB);
pExtension->m_bUsesXML = pDesc->GetAttributeBool(USES_XML_ATTRIB);
pExtension->m_bHidden = pDesc->GetAttributeBool(HIDDEN_ATTRIB);
// API version
CString sAPIVersion;
if (pDesc->FindAttribute(API_VERSION_ATTRIB, &sAPIVersion))
{
pExtension->m_dwAPIVersion = (DWORD)strToInt(sAPIVersion, 0);
if (pExtension->m_dwAPIVersion < 12)
pExtension->m_dwAPIVersion = 0;
pExtension->m_sVersion = pDesc->GetAttribute(VERSION_ATTRIB);
}
else if (dwInheritAPIVersion)
{
pExtension->m_dwAPIVersion = dwInheritAPIVersion;
pExtension->m_sVersion = pDesc->GetAttribute(VERSION_ATTRIB);
}
else
{
sAPIVersion = pDesc->GetAttribute(VERSION_ATTRIB);
pExtension->m_dwAPIVersion = ::LoadExtensionVersion(sAPIVersion);
}
if (pExtension->m_dwAPIVersion == 0)
{
pExtension->m_pEntities = NULL; // Let our parent clean up.
delete pExtension;
*retsError = strPatternSubst(CONSTLIT("Unable to load extension: incompatible version: %s"), sAPIVersion);
return ERR_FAIL;
}
// If this is a later version, then disabled it
if (pExtension->m_dwAPIVersion > API_VERSION)
pExtension->SetDisabled(strPatternSubst(CONSTLIT("Requires a newer version of %s"), fileGetProductName()));
// Release
pExtension->m_dwRelease = pDesc->GetAttributeInteger(RELEASE_ATTRIB);
// Registered extensions default to release 1.
if (pExtension->m_dwRelease == 0 && iFolder == folderCollection)
pExtension->m_dwRelease = 1;
// Name
pExtension->m_sName = pDesc->GetAttribute(NAME_ATTRIB);
if (pExtension->m_sName.IsBlank())
pExtension->m_sName = strPatternSubst(CONSTLIT("Extension %x"), pExtension->m_dwUNID);
// Image
pExtension->m_dwCoverUNID = (DWORD)pDesc->GetAttributeInteger(COVER_IMAGE_UNID_ATTRIB);
// Load credits (we parse them into a string array)
CString sCredits = pDesc->GetAttribute(CREDITS_ATTRIB);
if (!sCredits.IsBlank())
strDelimitEx(sCredits, ';', DELIMIT_TRIM_WHITESPACE, 0, &pExtension->m_Credits);
// Load extends attrib
CString sExtends = pDesc->GetAttribute(EXTENDS_ATTRIB);
if (!sExtends.IsBlank())
ParseUNIDList(sExtends, 0, &pExtension->m_Extends);
// Other options
pExtension->m_dwAutoIncludeAPIVersion = (DWORD)pDesc->GetAttributeIntegerBounded(AUTO_INCLUDE_FOR_COMPATIBILITY_ATTRIB, 0, -1, 0);
pExtension->m_dwMinExtensionAPIVersion = (DWORD)pDesc->GetAttributeIntegerBounded(EXTENSION_API_VERSION_ATTRIB, 0, -1, 0);
// Done
*retpExtension = pExtension;
return NOERROR;
}
ALERROR CExtension::CreateExtensionStub (const CString &sFilespec, EFolderTypes iFolder, CExtension **retpExtension, CString *retsError)
// CreateExtensionStub
//
// Loads enough of the given file to get the entities and the root element.
{
ALERROR error;
// Open up the file
CResourceDb Resources(sFilespec, true);
Resources.SetDebugMode(g_pUniverse->InDebugMode());
if (error = Resources.Open(DFOPEN_FLAG_READ_ONLY, retsError))
return error;
// Create a object to receive all the entities
CExternalEntityTable *pEntities = new CExternalEntityTable;
// Load the main XML file and get the entities
CXMLElement *pGameFile;
if (error = Resources.LoadGameFileStub(&pGameFile, pEntities, retsError))
{
delete pEntities;
return error;
}
// Create the extension
//
// If sucessful then pExtension takes ownership of pEntities.
CExtension *pExtension;
error = CreateExtensionFromRoot(sFilespec, pGameFile, iFolder, pEntities, 0, &pExtension, retsError);
// Clean up
delete pGameFile;
// Error
if (error)
{
delete pEntities;
return error;
}
// Done
*retpExtension = pExtension;
return NOERROR;
}
void CExtension::CreateIcon (int cxWidth, int cyHeight, CG32bitImage **retpIcon) const
// CreateIcon
//
// Creates a cover icon for the adventure. The caller is responsible for
// freeing the result.
{
// Load the image
CG32bitImage *pBackground = GetCoverImage();
if (pBackground == NULL || pBackground->GetWidth() == 0 || pBackground->GetHeight() == 0)
{
int cxSize = Min(cxWidth, cyHeight);
*retpIcon = new CG32bitImage;
(*retpIcon)->Create(cxSize, cxSize);
return;
}
// Figure out the dimensions of the icon based on the image size and the
// desired output.
//
// If the background is larger than the icon size then we need to scale it.
CG32bitImage *pIcon;
if (pBackground->GetWidth() > cxWidth || pBackground->GetHeight() > cyHeight)
{
int xSrc, ySrc, cxSrc, cySrc;
Metric rScale;
// If we have a widescreen cover image and we want a portrait or
// square icon, then we zoom in on the key part of the cover.
if (pBackground->GetWidth() > 2 * pBackground->GetHeight())
{
rScale = (Metric)cyHeight / pBackground->GetHeight();
cxSrc = (int)(cxWidth / rScale);
xSrc = Min(pBackground->GetWidth() - cxSrc, pBackground->GetWidth() - (RIGHT_COVER_OFFSET + (cxSrc / 2)));
ySrc = 0;
cySrc = pBackground->GetHeight();
}
else
{
rScale = (Metric)cxWidth / pBackground->GetWidth();
if (rScale * pBackground->GetHeight() > (Metric)cyHeight)
rScale = (Metric)cyHeight / pBackground->GetHeight();
xSrc = 0;
ySrc = 0;
cxSrc = pBackground->GetWidth();
cySrc = pBackground->GetHeight();
}
// Create the icon
pIcon = new CG32bitImage;
pIcon->CreateFromImageTransformed(*pBackground,
xSrc,
ySrc,
cxSrc,
cySrc,
rScale,
rScale,
0.0);
}
// Otherwise we center the image on the icon
else
{
// Create the icon
pIcon = new CG32bitImage;
pIcon->Create(cxWidth, cyHeight);
// Blt
pIcon->Blt(0,
0,
pBackground->GetWidth(),
pBackground->GetHeight(),
*pBackground,
(cxWidth - pBackground->GetWidth()) / 2,
(cyHeight - pBackground->GetHeight()) / 2);
}
// Done
*retpIcon = pIcon;
}
void CExtension::DebugDump (CExtension *pExtension, bool bFull)
// DebugDump
//
// Dumps debug output for the extension.
{
if (pExtension == NULL)
{
::kernelDebugLogPattern("Null extension pointer.");
return;
}
try
{
::kernelDebugLogPattern("%08x %s [%08x]", pExtension->m_dwUNID, pExtension->m_sFilespec, (DWORD)pExtension);
if (bFull)
{
if (pExtension->m_bDeleted)
::kernelDebugLogPattern("DELETED");
if (pExtension->m_bDisabled)
::kernelDebugLogPattern("DISABLED: %s", pExtension->m_sDisabledReason);
if (pExtension->m_bVerified)
::kernelDebugLogPattern("VERIFIED");
}
}
catch (...)
{
::kernelDebugLogPattern("Invalid extension pointer.");
}
}
ALERROR CExtension::ExecuteGlobals (SDesignLoadCtx &Ctx)
// ExecuteGlobals
//
// Execute the globals
{
DEBUG_TRY
int i;
CCodeChainCtx CCCtx;
// Add a hook so that all lambda expressions defined in this global block
// are wrapped with something that sets the extension UNID to the context.
if (m_iType != extBase)
CCCtx.SetGlobalDefineWrapper(this);
// Run the code (which will likely define a bunch of functions)
for (i = 0; i < m_Globals.GetCount(); i++)
{
ICCItem *pResult = CCCtx.Run(m_Globals[i].pCode);
if (pResult->IsError())
{
Ctx.sError = strPatternSubst(CONSTLIT("%s globals: %s"), m_Globals[i].sFilespec, pResult->GetStringValue());
return ERR_FAIL;
}
CCCtx.Discard(pResult);
}
// Done
return NOERROR;
DEBUG_CATCH
}
bool CExtension::IsLibraryInUse (DWORD dwUNID) const
// IsLibraryInUse
//
// Returns TRUE if this extension is using the given library.
{
for (int i = 0; i < GetLibraryCount(); i++)
{
if (dwUNID == GetLibrary(i).dwUNID)
return true;
}
return false;
}
CG32bitImage *CExtension::GetCoverImage (void) const
// GetCoverImage
//
// Returns the cover image (or NULL if none). The caller does NOT need to free
// the image. However, the caller should not user the image past a SweepImages
// call.
{
if (m_pCoverImage)
return m_pCoverImage;
// Adventure desc overrides our UNID
DWORD dwCoverUNID = m_dwCoverUNID;
if (m_pAdventureDesc
&& m_pAdventureDesc->GetBackgroundUNID() != 0)
dwCoverUNID = m_pAdventureDesc->GetBackgroundUNID();
if (dwCoverUNID == 0)
return NULL;
// Find the image object
CObjectImage *pObjImage = CObjectImage::AsType(m_DesignTypes.FindByUNID(dwCoverUNID));
if (pObjImage == NULL)
return NULL;
// Load the image
g_pUniverse->SetLogImageLoad(false);
m_pCoverImage = pObjImage->CreateCopy();
g_pUniverse->SetLogImageLoad(true);
// Done
return m_pCoverImage;
}
CString CExtension::GetEntityName (DWORD dwUNID) const
// GetEntityName
//
// Returns the entity name of the given UNID (or NULL_STR if we don't have it).
{
// Must have entities
if (m_pEntities == NULL)
return NULL_STR;
// If we don't yet have it, create a reverse lookup
if (m_UNID2EntityName.GetCount() == 0)
AddEntityNames(m_pEntities, &m_UNID2EntityName);
// Return it
CString *pName = m_UNID2EntityName.GetAt(dwUNID);
if (pName == NULL)
return NULL_STR;
return *pName;
}
size_t CExtension::GetXMLMemoryUsage (void) const
// GetXMLMemoryUsage
//
// Returns the amount of memory used up by XML structures
{
int i;
size_t dwTotal = (m_pRootXML ? m_pRootXML->GetMemoryUsage() : 0);
for (i = 0; i < m_ModuleXML.GetCount(); i++)
dwTotal += m_ModuleXML[i]->GetMemoryUsage();