-
Notifications
You must be signed in to change notification settings - Fork 57
/
CResourceBase.cpp
1498 lines (1285 loc) · 37.7 KB
/
CResourceBase.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
#include "../graysvr/graysvr.h"
#include "CFileList.h"
#include "CResourceBase.h"
//***************************************************
// CResourceBase
LPCTSTR const CResourceBase::sm_szResourceBlocks[RES_QTY] = // static
{
"AAAUNUSED", // unused / unknown.
"ACCOUNT", // Define an account instance.
"ADVANCE", // Define the advance rates for stats.
"AREA", // Complex region. (w/ extra tags)
"BLOCKIP", // (SL) A list of IP's to block.
"BOOK", // A book or a page from a book.
"CHARDEF", // Define a char type.
"COMMENT", // A commented out block type.
"DEFNAME", // (SL) Just add a bunch of new defs and equivs str/values.
"DIALOG", // A scriptable gump dialog", text or handler block.
"EVENTS", // (SL) Preload these Event files.
"FAME",
"FUNCTION", // Define a new command verb script that applies to a char.
"GMPAGE", // A GM page. (SAVED in World)
"ITEMDEF", // Define an item type
"KARMA",
"KRDIALOGLIST", // mapping of dialog<->kr ids
"MENU", // General scriptable menus.
"MOONGATES", // (SL) Define where the moongates are.
"NAMES", // A block of possible names for a NPC type. (read as needed)
"NEWBIE", // Triggers to execute on Player creation (based on skills selected)
"NOTOTITLES", // (SI) Define the noto titles used.
"OBSCENE", // (SL) A list of obscene words.
"PLEVEL", // Define the list of commands that a PLEVEL can access. (or not access)
"REGIONRESOURCE", // Define Ore types.
"REGIONTYPE", // Triggers etc. that can be assinged to a "AREA
"RESOURCELIST",
"RESOURCES", // (SL) list of all the resource files we should index !
"ROOM", // Non-complex region. (no extra tags)
"RUNES", // (SI) Define list of the magic runes.
"SCROLL", // SCROLL_GUEST=message scroll sent to player at guest login. SCROLL_MOTD", SCROLL_NEWBIE
"SECTOR", // Make changes to a sector. (SAVED in World)
"SERVERS", // List a number of servers in 3 line format.
"SKILL", // Define attributes for a skill (how fast it raises etc)
"SKILLCLASS", // Define class specifics for a char with this skill class.
"SKILLMENU", // A menu that is attached to a skill. special arguments over other menus.
"SPAWN", // Define a list of NPC's and how often they may spawn.
"SPEECH", // (SL) Preload these speech files.
"SPELL", // Define a magic spell. (0-64 are reserved)
"SPHERE", // Main Server INI block
"SPHERECRYPT", // Encryption keys
"STARTS", // (SI) List of starting locations for newbies.
"STAT", // Stats elements like KARMA,STR,DEX,FOOD,FAME,CRIMINAL etc. Used for resource and desire scripts.
"TELEPORTERS", // (SL) Where are the teleporteres in the world ?
"TEMPLATE", // Define a list of items. (for filling loot etc)
"TIMERF",
"TIP", // Tips that can come up at startup.
"TYPEDEF", // Define a trigger block for a "WORLDITEM m_type.
"TYPEDEFS",
"WC", // =WORLDCHAR
"WEBPAGE", // Define a web page template.
"WI", // =WORLDITEM
"WORLDCHAR", // Define instance of char in the world. (SAVED in World)
"WORLDITEM", // Define instance of item in the world. (SAVED in World)
"WORLDLISTS", // Define instance of list in the world. (SAVED in World)
"WORLDSCRIPT", // Define instance of resource in the world. (SAVED in World)
"WORLDVARS", // block of global variables
"WS", // =WORLDSCRIPT
};
//*********************************************************
// Resource Files
CResourceScript * CResourceBase::FindResourceFile( LPCTSTR pszPath )
{
ADDTOCALLSTACK("CResourceBase::FindResourceFile");
// Just match the titles ( not the whole path)
LPCTSTR pszTitle = CScript::GetFilesTitle( pszPath );
for ( size_t i = 0; ; i++ )
{
CResourceScript * pResFile = GetResourceFile(i);
if ( pResFile == NULL )
break;
LPCTSTR pszTitle2 = pResFile->GetFileTitle();
if ( ! strcmpi( pszTitle2, pszTitle ))
return( pResFile );
}
return( NULL );
}
CResourceScript * CResourceBase::AddResourceFile( LPCTSTR pszName )
{
ADDTOCALLSTACK("CResourceBase::AddResourceFile");
ASSERT(pszName != NULL);
// Is this really just a dir name ?
TCHAR szName[_MAX_PATH];
ASSERT(strlen(pszName) < COUNTOF(szName));
strcpy(szName, pszName);
TCHAR szTitle[_MAX_PATH];
strcpy(szTitle, CScript::GetFilesTitle( szName ));
if ( szTitle[0] == '\0' )
{
AddResourceDir( pszName );
return NULL;
}
LPCTSTR pszExt = CScript::GetFilesExt( szTitle );
if ( pszExt == NULL )
{
// No file extension provided, so append .scp to the filename
strcat( szName, GRAY_SCRIPT );
strcat( szTitle, GRAY_SCRIPT );
}
if ( ! strnicmp( szTitle, GRAY_FILE "tables", strlen(GRAY_FILE "tables")))
{
// Don't dupe this.
return NULL;
}
// Try to prevent dupes
CResourceScript * pNewRes = FindResourceFile(szTitle);
if ( pNewRes )
return( pNewRes );
// Find correct path
CScript s;
if ( ! OpenResourceFind( s, szName ))
{
return( NULL );
}
pNewRes = new CResourceScript( s.GetFilePath() );
m_ResourceFiles.Add(pNewRes);
return( pNewRes );
}
void CResourceBase::AddResourceDir( LPCTSTR pszDirName )
{
ADDTOCALLSTACK("CResourceBase::AddResourceDir");
if ( pszDirName[0] == '\0' )
return;
CGString sFilePath = CGFile::GetMergedFileName( pszDirName, "*" GRAY_SCRIPT );
CFileList filelist;
int iRet = filelist.ReadDir( sFilePath, false );
if ( iRet < 0 )
{
// also check script file path
sFilePath = CGFile::GetMergedFileName(m_sSCPBaseDir, sFilePath.GetPtr());
iRet = filelist.ReadDir( sFilePath, true );
if ( iRet < 0 )
{
DEBUG_ERR(( "DirList=%d for '%s'\n", iRet, static_cast<LPCTSTR>(pszDirName) ));
return;
}
}
if ( iRet <= 0 ) // no files here.
{
return;
}
CGStringListRec * psFile = filelist.GetHead();
for ( ; psFile; psFile = psFile->GetNext())
{
sFilePath = CGFile::GetMergedFileName( pszDirName, *psFile );
AddResourceFile( sFilePath );
}
}
void CResourceBase::LoadResourcesOpen( CScript * pScript )
{
ADDTOCALLSTACK("CResourceBase::LoadResourcesOpen");
// Load an already open resource file.
ASSERT(pScript);
ASSERT( pScript->IsFileOpen());
int iSections = 0;
while ( pScript->FindNextSection())
{
LoadResourceSection( pScript );
iSections ++;
}
if ( ! iSections )
{
DEBUG_WARN(( "No resource sections in '%s'\n", (LPCTSTR)pScript->GetFilePath()));
}
}
bool CResourceBase::LoadResources( CResourceScript * pScript )
{
ADDTOCALLSTACK("CResourceBase::LoadResources");
// Open the file then load it.
if ( pScript == NULL )
return( false );
if ( ! pScript->Open())
{
g_Log.Event(LOGL_CRIT|LOGM_INIT, "[RESOURCES] '%s' not found...\n", static_cast<LPCTSTR>(pScript->GetFilePath()));
return( false );
}
g_Log.Event(LOGM_INIT, "Loading %s\n", static_cast<LPCTSTR>(pScript->GetFilePath()));
LoadResourcesOpen( pScript );
pScript->Close();
pScript->CloseForce();
return( true );
}
CResourceScript * CResourceBase::LoadResourcesAdd( LPCTSTR pszNewFileName )
{
ADDTOCALLSTACK("CResourceBase::LoadResourcesAdd");
// Make sure this is added to my list of resource files
// And load it now.
CResourceScript * pScript = AddResourceFile( pszNewFileName );
if ( ! LoadResources(pScript))
return( NULL );
return( pScript );
}
bool CResourceBase::OpenResourceFind( CScript &s, LPCTSTR pszFilename, bool bCritical )
{
ADDTOCALLSTACK("CResourceBase::OpenResourceFind");
// Open a single resource script file.
// Look in the specified path.
if ( pszFilename == NULL )
{
pszFilename = s.GetFilePath();
}
// search the local dir or full path first.
if ( s.Open(pszFilename, OF_READ | OF_NONCRIT ))
return( true );
if ( !bCritical ) return false;
// next, check the script file path
CGString sPathName = CGFile::GetMergedFileName( m_sSCPBaseDir, pszFilename );
if ( s.Open(sPathName, OF_READ | OF_NONCRIT ))
return( true );
// finally, strip the directory and re-check script file path
LPCTSTR pszTitle = CGFile::GetFilesTitle(pszFilename);
sPathName = CGFile::GetMergedFileName( m_sSCPBaseDir, pszTitle );
return( s.Open( sPathName, OF_READ ));
}
bool CResourceBase::LoadResourceSection( CScript * pScript )
{
ADDTOCALLSTACK("CResourceBase::LoadResourceSection");
UNREFERENCED_PARAMETER(pScript);
// Just stub this out for others for now.
return( false );
}
//*********************************************************
// Resource Block Definitions
LPCTSTR CResourceBase::ResourceGetName( RESOURCE_ID_BASE rid ) const
{
ADDTOCALLSTACK("CResourceBase::ResourceGetName");
// Get a portable name for the resource id type.
CResourceDef * pResourceDef = dynamic_cast <CResourceDef *>( ResourceGetDef( rid ));
if ( pResourceDef )
return( pResourceDef->GetResourceName());
TCHAR * pszTmp = Str_GetTemp();
ASSERT(pszTmp);
if ( ! rid.IsValidUID())
{
sprintf( pszTmp, "%ld", static_cast<long>(rid.GetPrivateUID()) );
}
else
{
sprintf( pszTmp, "0%x", rid.GetResIndex() );
}
return( pszTmp );
}
RESOURCE_ID CResourceBase::ResourceGetID( RES_TYPE restype, LPCTSTR & pszName )
{
ADDTOCALLSTACK("CResourceBase::ResourceGetID");
// Find the Resource ID given this name.
// We are NOT creating a new resource. just looking up an existing one
// NOTE: Do not enforce the restype.
// Just fill it in if we are not sure what the type is.
// NOTE:
// Some restype's have private name spaces. (ie. RES_AREA)
// RETURN:
// pszName is now set to be after the expression.
// We are NOT creating.
RESOURCE_ID rid;
// Try to handle private name spaces.
switch ( restype )
{
case RES_ACCOUNT:
case RES_AREA:
case RES_GMPAGE:
case RES_ROOM:
case RES_SECTOR:
break;
default:
break;
}
rid.SetPrivateUID( Exp_GetVal(pszName)); // May be some complex expression {}
if ( restype != RES_UNKNOWN && rid.GetResType() == RES_UNKNOWN )
{
// Label it with the type we want.
return RESOURCE_ID( restype, rid.GetResIndex());
}
return( rid );
}
int CResourceBase::ResourceGetIndexType( RES_TYPE restype, LPCTSTR pszName )
{
ADDTOCALLSTACK("CResourceBase::ResourceGetIndexType");
// Get a resource of just this index type.
RESOURCE_ID rid = ResourceGetID( restype, pszName );
if ( rid.GetResType() != restype )
return( -1 );
return( rid.GetResIndex());
}
CResourceDef * CResourceBase::ResourceGetDef( RESOURCE_ID_BASE rid ) const
{
ADDTOCALLSTACK("CResourceBase::ResourceGetDef");
if ( ! rid.IsValidUID())
return( NULL );
size_t index = m_ResHash.FindKey( rid );
if ( index == m_ResHash.BadIndex() )
return( NULL );
return( m_ResHash.GetAt( rid, index ));
}
//*******************************************************
// Open resource blocks.
bool CResourceBase::ResourceLock( CResourceLock & s, RESOURCE_ID_BASE rid )
{
ADDTOCALLSTACK("CResourceBase::ResourceLock");
// Lock a referenced resource object.
if ( ! rid.IsValidUID())
return( false );
CResourceLink * pResourceLink = dynamic_cast <CResourceLink *>( ResourceGetDef( rid ));
if ( pResourceLink )
{
return( pResourceLink->ResourceLock(s));
}
return( false );
}
/////////////////////////////////////////////////
// -CResourceDef
bool CResourceDef::SetResourceName( LPCTSTR pszName )
{
ADDTOCALLSTACK("CResourceDef::SetResourceName");
ASSERT(pszName);
// This is the global def for this item.
for ( size_t i = 0; pszName[i]; i++ )
{
if ( i >= EXPRESSION_MAX_KEY_LEN )
{
DEBUG_ERR(( "Too long DEFNAME=%s\n", pszName ));
return( false );
}
if ( ! _ISCSYM(pszName[i]))
{
DEBUG_ERR(( "Bad chars in DEFNAME=%s\n", pszName ));
return( false );
}
}
int iVarNum;
CVarDefCont * pVarKey = g_Exp.m_VarDefs.GetKey( pszName );
if ( pVarKey )
{
if ( (DWORD)pVarKey->GetValNum() == GetResourceID().GetPrivateUID() )
{
return( true );
}
if ( RES_GET_INDEX(pVarKey->GetValNum()) == GetResourceID().GetResIndex())
{
DEBUG_WARN(( "The DEFNAME=%s has a strange type mismatch? 0%llx!=0%lx\n", pszName, pVarKey->GetValNum(), GetResourceID().GetPrivateUID() ));
}
else
{
DEBUG_WARN(( "The DEFNAME=%s already exists! 0%llx!=0%x\n", pszName, RES_GET_INDEX(pVarKey->GetValNum()), GetResourceID().GetResIndex() ));
}
iVarNum = g_Exp.m_VarDefs.SetNum( pszName, GetResourceID().GetPrivateUID() );
}
else
{
iVarNum = g_Exp.m_VarDefs.SetNumNew( pszName, GetResourceID().GetPrivateUID() );
}
if ( iVarNum < 0 )
return( false );
SetResourceVar( dynamic_cast <const CVarDefContNum*>( g_Exp.m_VarDefs.GetAt( iVarNum )));
return( true );
}
LPCTSTR CResourceDef::GetResourceName() const
{
ADDTOCALLSTACK("CResourceDef::GetResourceName");
if ( m_pDefName )
return m_pDefName->GetKey();
TemporaryString pszTmp;
sprintf(pszTmp, "0%x", GetResourceID().GetResIndex());
return pszTmp;
}
bool CResourceDef::HasResourceName()
{
ADDTOCALLSTACK("CResourceDef::HasResourceName");
if ( m_pDefName )
return true;
return false;
}
bool CResourceDef::MakeResourceName()
{
ADDTOCALLSTACK("CResourceDef::MakeResourceName");
if ( m_pDefName )
return true;
LPCTSTR pszName = GetName();
GETNONWHITESPACE( pszName );
TCHAR * pbuf = Str_GetTemp();
TCHAR ch;
TCHAR * pszDef;
strcpy(pbuf, "a_");
LPCTSTR pszKey = NULL; // auxiliary, the key of a similar CVarDef, if any found
pszDef = pbuf + 2;
for ( ; *pszName; pszName++ )
{
ch = *pszName;
if ( ch == ' ' || ch == '\t' || ch == '-' )
ch = '_';
else if ( !isalnum( ch ) )
continue;
// collapse multiple spaces together
if ( ch == '_' && *(pszDef-1) == '_' )
continue;
*pszDef = ch;
pszDef++;
}
*pszDef = '_';
*(++pszDef) = '\0';
size_t iMax = g_Exp.m_VarDefs.GetCount();
int iVar = 1;
size_t iLen = strlen( pbuf );
for ( size_t i = 0; i < iMax; i++ )
{
// Is this a similar key?
pszKey = g_Exp.m_VarDefs.GetAt(i)->GetKey();
if ( strnicmp( pbuf, pszKey, iLen ) != 0 )
continue;
// skip underscores
pszKey = pszKey + iLen;
while ( *pszKey == '_' )
pszKey++;
// Is this is subsequent key with a number? Get the highest (plus one)
if ( IsStrNumericDec( pszKey ) )
{
int iVarThis = ATOI( pszKey );
if ( iVarThis >= iVar )
iVar = iVarThis + 1;
}
else
iVar++;
}
// add an extra _, hopefully won't conflict with named areas
sprintf( pszDef, "_%i", iVar );
SetResourceName( pbuf );
// Assign name
return true;
}
bool CRegionBase::MakeRegionName()
{
ADDTOCALLSTACK("CRegionBase::MakeRegionName");
if ( m_pDefName )
return true;
TCHAR ch;
LPCTSTR pszKey = NULL; // auxiliary, the key of a similar CVarDef, if any found
TCHAR * pbuf = Str_GetTemp();
TCHAR * pszDef = pbuf + 2;
strcpy(pbuf, "a_");
LPCTSTR pszName = GetName();
GETNONWHITESPACE( pszName );
if ( !strnicmp( "the ", pszName, 4 ) )
pszName += 4;
else if ( !strnicmp( "a ", pszName, 2 ) )
pszName += 2;
else if ( !strnicmp( "an ", pszName, 3 ) )
pszName += 3;
else if ( !strnicmp( "ye ", pszName, 3 ) )
pszName += 3;
for ( ; *pszName; pszName++ )
{
if ( !strnicmp( " of ", pszName, 4 ) || !strnicmp( " in ", pszName, 4 ) )
{ pszName += 4; continue; }
if ( !strnicmp( " the ", pszName, 5 ) )
{ pszName += 5; continue; }
ch = *pszName;
if ( ch == ' ' || ch == '\t' || ch == '-' )
ch = '_';
else if ( !isalnum( ch ) )
continue;
// collapse multiple spaces together
if ( ch == '_' && *(pszDef-1) == '_' )
continue;
*pszDef = static_cast<TCHAR>(tolower(ch));
pszDef++;
}
*pszDef = '_';
*(++pszDef) = '\0';
size_t iMax = g_Cfg.m_RegionDefs.GetCount();
int iVar = 1;
size_t iLen = strlen( pbuf );
for ( size_t i = 0; i < iMax; i++ )
{
CRegionBase * pRegion = dynamic_cast <CRegionBase*> (g_Cfg.m_RegionDefs.GetAt(i));
if ( !pRegion )
continue;
pszKey = pRegion->GetResourceName();
if ( !pszKey )
continue;
// Is this a similar key?
if ( strnicmp( pbuf, pszKey, iLen ) != 0 )
continue;
// skip underscores
pszKey = pszKey + iLen;
while ( *pszKey == '_' )
pszKey++;
// Is this is subsequent key with a number? Get the highest (plus one)
if ( IsStrNumericDec( pszKey ) )
{
int iVarThis = ATOI( pszKey );
if ( iVarThis >= iVar )
iVar = iVarThis + 1;
}
else
iVar++;
}
// Only one, no need for the extra "_"
sprintf( pszDef, "%i", iVar );
SetResourceName( pbuf );
// Assign name
return true;
}
//***************************************************************************
// -CResourceScript
bool CResourceScript::CheckForChange()
{
ADDTOCALLSTACK("CResourceScript::CheckForChange");
// Get Size/Date info on the file to see if it has changed.
time_t dateChange;
DWORD dwSize;
if ( ! CFileList::ReadFileInfo( GetFilePath(), dateChange, dwSize ))
{
DEBUG_ERR(( "Can't get stats info for file '%s'\n", static_cast<LPCTSTR>(GetFilePath()) ));
return false;
}
bool fChange = false;
// See If the script has changed
if ( ! IsFirstCheck())
{
if ( m_dwSize != dwSize || m_dateChange != dateChange )
{
g_Log.Event(LOGL_WARN, "Resource '%s' changed, resync.\n", static_cast<LPCTSTR>(GetFilePath()));
fChange = true;
}
}
m_dwSize = dwSize; // Compare to see if this has changed.
m_dateChange = dateChange; // real world time/date of last change.
return( fChange );
}
void CResourceScript::ReSync()
{
ADDTOCALLSTACK("CResourceScript::ReSync");
if ( ! IsFirstCheck())
{
if ( ! CheckForChange())
return;
}
if ( ! Open())
return;
g_Cfg.LoadResourcesOpen( this );
Close();
}
bool CResourceScript::Open( LPCTSTR pszFilename, UINT wFlags )
{
ADDTOCALLSTACK("CResourceScript::Open");
// Open the file if it is not already open for use.
if ( !IsFileOpen() )
{
UINT mode = 0;
mode |= OF_SHARE_DENY_WRITE;
if ( ! CScript::Open( pszFilename, wFlags|mode)) // OF_READ
return( false );
if ( ! ( wFlags & OF_READWRITE ) && CheckForChange())
{
// what should we do about it ? reload it of course !
g_Cfg.LoadResourcesOpen( this );
}
}
m_iOpenCount++;
ASSERT( IsFileOpen());
return( true );
}
void CResourceScript::CloseForce()
{
ADDTOCALLSTACK("CResourceScript::CloseForce");
m_iOpenCount = 0;
CScript::CloseForce();
}
void CResourceScript::Close()
{
ADDTOCALLSTACK("CResourceScript::Close");
// Don't close the file yet.
// Close it later when we know it has not been used for a bit.
if ( ! IsFileOpen())
return;
m_iOpenCount--;
if ( ! m_iOpenCount )
{
m_timeLastAccess = CServTime::GetCurrentTime();
// Just leave it open for caching purposes
//CloseForce();
}
}
//***************************************************************************
// CResourceLock
//
bool CResourceLock::OpenBase( void * pExtra )
{
ADDTOCALLSTACK("CResourceLock::OpenBase");
UNREFERENCED_PARAMETER(pExtra);
ASSERT(m_pLock);
if ( m_pLock->IsFileOpen())
m_PrvLockContext = m_pLock->GetContext();
if ( ! m_pLock->Open()) // make sure the original is open.
return( false );
// Open a seperate copy of an already opend file.
m_pStream = m_pLock->m_pStream;
m_hFile = m_pLock->m_hFile;
#ifndef _NOSCRIPTCACHE
PhysicalScriptFile::dupeFrom(m_pLock);
//#else
// dupeFrom(m_pLock);
#endif
// Assume this is the new error context !
m_PrvScriptContext.OpenScript( this );
return( true );
}
void CResourceLock::CloseBase()
{
ADDTOCALLSTACK("CResourceLock::CloseBase");
ASSERT(m_pLock);
m_pStream = NULL;
// Assume this is not the context anymore.
m_PrvScriptContext.Close();
if ( m_PrvLockContext.IsValid())
{
m_pLock->SeekContext(m_PrvLockContext); // no need to set the line number as it should not have changed.
}
// Restore old position in the file (if there was one)
m_pLock->Close(); // decrement open count on the orig.
if( IsWriteMode() || ( GetFullMode() & OF_DEFAULTMODE )) {
Init();
}
}
bool CResourceLock::ReadTextLine( bool fRemoveBlanks ) // Read a line from the opened script file
{
ADDTOCALLSTACK("CResourceLock::ReadTextLine");
// ARGS:
// fRemoveBlanks = Don't report any blank lines, (just keep reading)
//
ASSERT(m_pLock);
ASSERT( ! IsBinaryMode());
while ( PhysicalScriptFile::ReadString( GetKeyBufferRaw(SCRIPT_MAX_LINE_LEN), SCRIPT_MAX_LINE_LEN ))
{
m_pLock->m_iLineNum = ++m_iLineNum; // share this with original open.
if ( fRemoveBlanks )
{
if ( ParseKeyEnd() <= 0 )
continue;
}
return( true );
}
m_pszKey[0] = '\0';
return( false );
}
int CResourceLock::OpenLock( CResourceScript * pLock, CScriptLineContext context )
{
ADDTOCALLSTACK("CResourceLock::OpenLock");
// ONLY called from CResourceLink
ASSERT(pLock);
Close();
m_pLock = pLock;
if ( ! Open( m_pLock->GetFilePath(), m_pLock->GetMode() )) // open my copy.
return( -2 );
if ( ! SeekContext( context ))
{
return( -3 );
}
return( 0 );
}
/////////////////////////////////////////////////
// -CResourceLink
CResourceLink::CResourceLink( RESOURCE_ID rid, const CVarDefContNum * pDef ) :
CResourceDef( rid, pDef )
{
m_pScript = NULL;
m_Context.Init(); // not yet tested.
m_lRefInstances = 0;
ClearTriggers();
}
CResourceLink::~CResourceLink()
{
}
void CResourceLink::ScanSection( RES_TYPE restype )
{
ADDTOCALLSTACK("CResourceLink::ScanSection");
// Scan the section we are linking to for useful stuff.
ASSERT(m_pScript);
LPCTSTR const * ppTable = NULL;
int iQty = 0;
switch (restype)
{
case RES_TYPEDEF:
case RES_ITEMDEF:
ppTable = CItem::sm_szTrigName;
iQty = ITRIG_QTY;
break;
case RES_CHARDEF:
case RES_EVENTS:
case RES_SKILLCLASS:
ppTable = CChar::sm_szTrigName;
iQty = CTRIG_QTY;
break;
case RES_SKILL:
ppTable = CSkillDef::sm_szTrigName;
iQty = SKTRIG_QTY;
break;
case RES_SPELL:
ppTable = CSpellDef::sm_szTrigName;
iQty = SPTRIG_QTY;
break;
case RES_AREA:
case RES_ROOM:
case RES_REGIONTYPE:
ppTable = CRegionWorld::sm_szTrigName;
iQty = RTRIG_QTY;
break;
case RES_WEBPAGE:
ppTable = CWebPageDef::sm_szTrigName;
iQty = WTRIG_QTY;
break;
case RES_REGIONRESOURCE:
ppTable = CRegionResourceDef::sm_szTrigName;
iQty = RRTRIG_QTY;
break;
default:
break;
}
ClearTriggers();
while ( m_pScript->ReadKey(false))
{
if ( m_pScript->IsKeyHead( "DEFNAME", 7 ))
{
m_pScript->ParseKeyLate();
SetResourceName( m_pScript->GetArgRaw());
}
if ( m_pScript->IsKeyHead( "ON", 2 ))
{
int iTrigger;
if ( iQty )
{
m_pScript->ParseKeyLate();
iTrigger = FindTableSorted( m_pScript->GetArgRaw(), ppTable, iQty );
if ( iTrigger < 0 ) // unknown triggers ?
iTrigger = XTRIG_UNKNOWN;
else
{
TriglistAdd(m_pScript->GetArgRaw());
if ( HasTrigger(iTrigger))
{
DEBUG_ERR(( "Duplicate trigger '%s' in '%s'\n", ppTable[iTrigger], GetResourceName()));
continue;
}
}
}
else
iTrigger = XTRIG_UNKNOWN;
SetTrigger(iTrigger);
}
}
}
bool CResourceLink::IsLinked() const
{
ADDTOCALLSTACK("CResourceLink::IsLinked");
if ( !m_pScript )
return false;
return m_Context.IsValid();
}
CResourceScript *CResourceLink::GetLinkFile() const
{
ADDTOCALLSTACK("CResourceLink::GetLinkFile");
return m_pScript;
}
long CResourceLink::GetLinkOffset() const
{
ADDTOCALLSTACK("CResourceLink::GetLinkOffset");
return m_Context.m_lOffset;
}
void CResourceLink::SetLink(CResourceScript *pScript)
{
ADDTOCALLSTACK("CResourceLink::SetLink");
m_pScript = pScript;
m_Context = pScript->GetContext();
}
void CResourceLink::CopyTransfer(CResourceLink *pLink)
{
ADDTOCALLSTACK("CResourceLink::CopyTransfer");
ASSERT(pLink);
CResourceDef::CopyDef( pLink );
m_pScript = pLink->m_pScript;
m_Context = pLink->m_Context;
memcpy(m_dwOnTriggers, pLink->m_dwOnTriggers, sizeof(m_dwOnTriggers));
m_lRefInstances = pLink->m_lRefInstances;
pLink->m_lRefInstances = 0; // instance has been transfered.
}
void CResourceLink::ClearTriggers()
{
ADDTOCALLSTACK("CResourceLink::ClearTriggers");
memset(m_dwOnTriggers, 0, sizeof(m_dwOnTriggers));
}
void CResourceLink::SetTrigger(int i)
{
ADDTOCALLSTACK("CResourceLink::SetTrigger");
if ( i >= 0 )
{
for ( int j = 0; j < MAX_TRIGGERS_ARRAY; j++ )
{
if ( i < 32 )
{
DWORD flag = 1 << i;
m_dwOnTriggers[j] |= flag;
return;
}
i -= 32;
}
}
}
bool CResourceLink::HasTrigger(int i) const
{
ADDTOCALLSTACK("CResourceLink::HasTrigger");
// Specific to the RES_TYPE; CTRIG_QTY, ITRIG_QTY or RTRIG_QTY
if ( i < XTRIG_UNKNOWN )
i = XTRIG_UNKNOWN;
for ( int j = 0; j < MAX_TRIGGERS_ARRAY; j++ )
{
if ( i < 32 )
{
DWORD flag = 1 << i;
return ((m_dwOnTriggers[j] & flag) != 0);
}
i -= 32;
}
return false;
}
bool CResourceLink::ResourceLock( CResourceLock &s )
{
ADDTOCALLSTACK("CResourceLink::ResourceLock");
// Find the definition of this item in the scripts.
// Open a locked copy of this script
// NOTE: How can we tell the file has changed since last open ?
// RETURN: true = found it.
if ( !IsLinked() ) // has already failed previously.
return false;
ASSERT(m_pScript);
// Give several tryes to lock the script while multithreading
int iRet = s.OpenLock( m_pScript, m_Context );
if ( ! iRet ) return true;
s.AttachObj( this );
// ret = -2 or -3
LPCTSTR pszName = GetResourceName();
DEBUG_ERR(("ResourceLock '%s':%ld id=%s FAILED\n", static_cast<LPCTSTR>(s.GetFilePath()), m_Context.m_lOffset, pszName));
return false;
}