@@ -286,6 +286,7 @@ class Container
286
286
map_type Items;
287
287
288
288
base_type* SavingObject;
289
+ extension_type_ptr SavingExtPointer;
289
290
IStream* SavingStream;
290
291
const char * Name;
291
292
@@ -361,10 +362,6 @@ class Container
361
362
362
363
extension_type_ptr TryAllocate (base_type_ptr key, bool bCond, const std::string_view& nMessage)
363
364
{
364
- // Do not allow allocation when loading save games.
365
- if (Phobos::IsLoadingSaveGame)
366
- return nullptr ;
367
-
368
365
if (!key || (!bCond && !nMessage.empty ()))
369
366
{
370
367
Debug::Log (" %s \n " , nMessage.data ());
@@ -376,10 +373,6 @@ class Container
376
373
377
374
extension_type_ptr TryAllocate (base_type_ptr key)
378
375
{
379
- // Do not allow allocation when loading save games.
380
- if (Phobos::IsLoadingSaveGame)
381
- return nullptr ;
382
-
383
376
if (!key)
384
377
{
385
378
Debug::Log (" Attempted to allocate %s from nullptr!\n " , typeid (extension_type).name ());
@@ -400,6 +393,22 @@ class Container
400
393
return this ->Items .find (key);
401
394
}
402
395
396
+ // Only used on loading, does not check if key is nullptr.
397
+ extension_type_ptr FindOrAllocate (base_type_ptr key)
398
+ {
399
+ extension_type_ptr value = nullptr ;
400
+
401
+ if constexpr (HasOffset<T>)
402
+ value = GetExtensionPointer (key);
403
+ else
404
+ value = this ->Items .find (key);
405
+
406
+ if (!value)
407
+ value = Allocate (key);
408
+
409
+ return value;
410
+ }
411
+
403
412
void Remove (base_type_ptr key)
404
413
{
405
414
if (auto Item = Find (key))
@@ -442,6 +451,10 @@ class Container
442
451
443
452
this ->SavingObject = key;
444
453
this ->SavingStream = pStm;
454
+
455
+ // Loading the base type data might override the ext pointer stored on it so it needs to be saved.
456
+ if constexpr (HasOffset<T>)
457
+ this ->SavingExtPointer = GetExtensionPointer (key);
445
458
}
446
459
447
460
void SaveStatic ()
@@ -466,6 +479,10 @@ class Container
466
479
{
467
480
if (this ->SavingObject && this ->SavingStream )
468
481
{
482
+ // Restore stored ext pointer data.
483
+ if constexpr (HasOffset<T>)
484
+ SetExtensionPointer (this ->SavingObject , this ->SavingExtPointer );
485
+
469
486
// Debug::Log("[LoadStatic] Loading object %p as '%s'\n", this->SavingObject, this->Name);
470
487
if (!this ->Load (this ->SavingObject , this ->SavingStream ))
471
488
Debug::FatalErrorAndExit (" LoadStatic - Loading object %p as '%s' failed!\n " , this ->SavingObject , this ->Name );
@@ -550,8 +567,8 @@ class Container
550
567
return nullptr ;
551
568
}
552
569
553
- extension_type_ptr buffer = this -> Allocate (key);
554
-
570
+ // get or allocate the value data
571
+ extension_type_ptr buffer = this -> FindOrAllocate (key);
555
572
if (!buffer)
556
573
{
557
574
Debug::Log (" LoadKey - Could not find or allocate value.\n " );
0 commit comments