-
Notifications
You must be signed in to change notification settings - Fork 60
/
uimport.cpp
1498 lines (1346 loc) · 43.6 KB
/
uimport.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
/** @file
*
* @par History
* - 2005/01/23 Shinigami: for_all_mobiles, write_items & write_multis - Tokuno MapDimension doesn't
* fit blocks of 64x64 (WGRID_SIZE)
* - 2007/06/17 Shinigami: added config.world_data_path
* - 2009/09/03 MuadDib: Relocation of account related cpp/h
* Relocation of multi related cpp/h
* - 2009/09/14 MuadDib: All UOX3 Import Code commented out. You can script this.
* - 2009/12/02 Turley: added config.max_tile_id - Tomi
* - 2011/11/28 MuadDib: Removed last of uox referencing code.
*/
#include "uimport.h"
#include <atomic>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <exception>
#include <future>
#include <string>
#include <time.h>
#include "../clib/Program/ProgramConfig.h"
#include "../clib/cfgelem.h"
#include "../clib/cfgfile.h"
#include "../clib/clib.h"
#include "../clib/clib_endian.h"
#include "../clib/esignal.h"
#include "../clib/fileutil.h"
#include "../clib/logfacility.h"
#include "../clib/passert.h"
#include "../clib/rawtypes.h"
#include "../clib/threadhelp.h"
#include "../clib/timer.h"
#include "../plib/poltype.h"
#include "../plib/systemstate.h"
#include "accounts/accounts.h"
#include "containr.h"
#include "fnsearch.h"
#include "gameclck.h"
#include "globals/network.h"
#include "globals/object_storage.h"
#include "globals/settings.h"
#include "globals/state.h"
#include "globals/uvars.h"
#include "item/item.h"
#include "item/itemdesc.h"
#include "loaddata.h"
#include "mobile/charactr.h"
#include "mobile/npc.h"
#include "multi/house.h"
#include "multi/multi.h"
#include "objecthash.h"
#include "polvar.h"
#include "regions/resource.h"
#include "savedata.h"
#include "servdesc.h"
#include "startloc.h"
#include "storage.h"
#include "ufunc.h"
#include "uobject.h"
#include "uworld.h"
namespace Pol
{
namespace Module
{
void commit_datastore();
void read_datastore_dat();
void write_datastore( Clib::StreamWriter& sw );
} // namespace Module
namespace Plib
{
bool version_greater_or_equal( const std::string& version_have, const std::string& version_need );
bool version_equal( const std::string& version_have, const std::string& version_need );
} // namespace Plib
namespace Core
{
void read_party_dat();
void write_party( Clib::StreamWriter& sw );
void read_guilds_dat();
void write_guilds( Clib::StreamWriter& sw );
std::shared_future<bool> SaveContext::finished;
/****************** POL Native Files *******************/
// Dave changed 3/8/3 to use objecthash
void read_character( Clib::ConfigElem& elem )
{
// if this object is modified in a subsequent incremental save,
// don't load it now.
pol_serial_t serial = 0;
elem.get_prop( "SERIAL", &serial );
if ( get_save_index( serial ) > objStorageManager.current_incremental_save )
return;
CharacterRef chr( new Mobile::Character( elem.remove_ushort( "OBJTYPE" ) ) );
try
{
// note chr->logged_in is true..
chr->readProperties( elem );
// Allows the realm to recognize this char as offline
chr->realm()->add_mobile( *chr, Realms::WorldChangeReason::PlayerLoad );
chr->clear_dirty();
// readProperties gets the serial, so we can't add to the objecthash until now.
objStorageManager.objecthash.Insert( chr.get() );
}
catch ( std::exception& )
{
if ( chr.get() != nullptr )
chr->destroy();
throw;
}
}
// Dave changed 3/8/3 to use objecthash
void read_npc( Clib::ConfigElem& elem )
{
// if this object is modified in a subsequent incremental save,
// don't load it now.
pol_serial_t serial = 0;
elem.get_prop( "SERIAL", &serial );
if ( get_save_index( serial ) > objStorageManager.current_incremental_save )
return;
NpcRef npc( new Mobile::NPC( elem.remove_ushort( "OBJTYPE" ), elem ) );
try
{
npc->readProperties( elem );
SetCharacterWorldPosition( npc.get(), Realms::WorldChangeReason::NpcLoad );
npc->clear_dirty();
////HASH
objStorageManager.objecthash.Insert( npc.get() );
////
}
catch ( std::exception& )
{
if ( npc.get() != nullptr )
npc->destroy();
throw;
}
}
// hmm, an interesting idea, what if there was an
// Item::create( ConfigElem& elem ) member,
// which would do this?
// Item would need a constructor (ConfigElem&) also.
// Polymorphism would then take
// care of it, and move the logic into the derived
// classes.
Items::Item* read_item( Clib::ConfigElem& elem )
{
u32 serial;
u32 objtype;
if ( elem.remove_prop( "SERIAL", &serial ) == false )
{
ERROR_PRINTLN( "Item element has no SERIAL property, omitting." );
return nullptr;
}
if ( !IsItem( serial ) )
{
elem.warn_with_line(
"Item element does not have an item serial\n(should be larger than 0x40000000)" );
if ( Plib::systemstate.config.check_integrity )
{
throw std::runtime_error( "Data integrity error" );
}
return nullptr;
}
if ( Plib::systemstate.config.check_integrity )
{
if ( system_find_item( serial ) )
{
ERROR_PRINTLN( "Duplicate item read from datafiles (Serial={:#X})", serial );
throw std::runtime_error( "Data integrity error" );
}
}
if ( elem.remove_prop( "OBJTYPE", &objtype ) == false )
{
ERROR_PRINTLN( "Item (Serial {:#X}) has no OBJTYPE property, omitting.", serial );
return nullptr;
}
if ( gamestate.old_objtype_conversions.count( objtype ) )
objtype = gamestate.old_objtype_conversions[objtype];
Items::Item* item = Items::Item::create( objtype, serial );
if ( item == nullptr )
{
ERROR_PRINTLN( "Unable to create item: objtype={:#X}, serial={:#X}", objtype, serial );
if ( !Plib::systemstate.config.ignore_load_errors )
throw std::runtime_error( "Item::create failed!" );
else
return nullptr;
}
item->setposition( Pos4d( item->pos().xyz(), find_realm( "britannia" ) ) );
item->readProperties( elem );
item->clear_dirty();
return item;
}
#define USE_PARENT_CONTS 1
typedef std::stack<UContainer*> ContStack;
static ContStack parent_conts;
void read_global_item( Clib::ConfigElem& elem, int /*sysfind_flags*/ )
{
// if this object is modified in a subsequent incremental save,
// don't load it now.
pol_serial_t serial = 0;
elem.get_prop( "SERIAL", &serial );
if ( get_save_index( serial ) > objStorageManager.current_incremental_save )
return;
u32 container_serial = 0; // defaults to item in the world's top-level
(void)elem.remove_prop( "CONTAINER",
&container_serial ); // therefore we don't need to check the return value
Items::Item* item = read_item( elem );
// dave added 1/15/3, protect against further crash if item is null. Should throw instead?
if ( item == nullptr )
{
elem.warn_with_line( "Error reading item SERIAL or OBJTYPE." );
return;
}
ItemRef itemref( item ); // dave 1/28/3 prevent item from being destroyed before function ends
if ( container_serial == 0 )
{
add_item_to_world( item );
if ( item->isa( UOBJ_CLASS::CLASS_CONTAINER ) )
parent_conts.push( static_cast<UContainer*>( item ) );
}
else
{
if ( IsCharacter( container_serial ) ) // it's equipped on a character
{
Mobile::Character* chr = system_find_mobile( container_serial );
if ( chr != nullptr )
{
equip_loaded_item( chr, item );
}
else
{
defer_item_insertion( item, container_serial );
}
return;
}
Items::Item* cont_item = nullptr;
// bool new_parent_cont = false;
while ( !parent_conts.empty() )
{
UContainer* cont = parent_conts.top();
if ( cont->serial == container_serial )
{
cont_item = cont;
break;
}
else
{
parent_conts.pop();
}
}
if ( cont_item == nullptr )
{
cont_item = system_find_item( container_serial );
// new_parent_cont = true;
}
if ( cont_item )
{
add_loaded_item( cont_item, item );
}
else
{
defer_item_insertion( item, container_serial );
}
}
}
void read_system_vars( Clib::ConfigElem& elem )
{
settingsManager.polvar.DataWrittenBy = elem.remove_string( "CoreVersion" );
settingsManager.polvar.DataWrittenBy99OrLater =
Plib::version_greater_or_equal( settingsManager.polvar.DataWrittenBy, "99.0.0" );
settingsManager.polvar.DataWrittenBy93 =
Plib::version_equal( settingsManager.polvar.DataWrittenBy, "93.0.0" );
stateManager.stored_last_item_serial =
elem.remove_ulong( "LastItemSerialNumber", UINT_MAX ); // dave 3/9/3
stateManager.stored_last_char_serial =
elem.remove_ulong( "LastCharSerialNumber", UINT_MAX ); // dave 3/9/3
}
void read_shadow_realms( Clib::ConfigElem& elem )
{
std::string name = elem.remove_string( "Name" );
Realms::Realm* baserealm = find_realm( elem.remove_string( "BaseRealm" ) );
if ( !baserealm )
elem.warn_with_line( "BaseRealm not found." );
else if ( defined_realm( name ) )
elem.warn_with_line( "Realmname already defined" );
else
{
add_realm( name, baserealm );
INFO_PRINTLN( "\nShadowrealm {}", name );
}
}
void read_multi( Clib::ConfigElem& elem )
{
// if this object is modified in a subsequent incremental save,
// don't load it now.
pol_serial_t serial = 0;
elem.get_prop( "SERIAL", &serial );
if ( get_save_index( serial ) > objStorageManager.current_incremental_save )
return;
u32 objtype;
if ( elem.remove_prop( "SERIAL", &serial ) == false )
{
ERROR_PRINTLN( "A Multi has no SERIAL property." );
throw std::runtime_error( "Config File error." );
}
if ( system_find_multi( serial ) || system_find_item( serial ) )
{
ERROR_PRINTLN( "Duplicate item read from datafiles (Serial={:#X})", serial );
throw std::runtime_error( "Data integrity error" );
}
if ( elem.remove_prop( "OBJTYPE", &objtype ) == false )
{
ERROR_PRINTLN( "Multi (Serial {:#X}) has no OBJTYPE property, omitting.", serial );
return;
}
if ( gamestate.old_objtype_conversions.count( objtype ) )
objtype = gamestate.old_objtype_conversions[objtype];
Multi::UMulti* multi = Multi::UMulti::create( Items::find_itemdesc( objtype ), serial );
if ( multi == nullptr )
{
ERROR_PRINTLN( "Unable to create multi: objtype={:#X}, serial={:#X}", objtype, serial );
throw std::runtime_error( "Multi::create failed!" );
}
multi->readProperties( elem );
add_multi_to_world( multi );
}
std::string elapsed( clock_t start, clock_t end )
{
size_t ms = static_cast<size_t>( ( end - start ) * 1000.0 / CLOCKS_PER_SEC );
return Clib::tostring( ms ) + " ms";
}
void slurp( const char* filename, const char* tags, int sysfind_flags )
{
static int num_until_dot = 1000;
if ( Clib::FileExists( filename ) )
{
INFO_PRINT( " {}:", filename );
Clib::ConfigFile cf( filename, tags );
Clib::ConfigElem elem;
Tools::Timer<> timer;
unsigned int nobjects = 0;
while ( cf.read( elem ) )
{
if ( --num_until_dot == 0 )
{
INFO_PRINT( "." );
num_until_dot = 1000;
}
try
{
if ( stricmp( elem.type(), "CHARACTER" ) == 0 )
read_character( elem );
else if ( stricmp( elem.type(), "NPC" ) == 0 )
read_npc( elem );
else if ( stricmp( elem.type(), "ITEM" ) == 0 )
read_global_item( elem, sysfind_flags );
else if ( stricmp( elem.type(), "GLOBALPROPERTIES" ) == 0 )
gamestate.global_properties->readProperties( elem );
else if ( elem.type_is( "SYSTEM" ) )
read_system_vars( elem );
else if ( elem.type_is( "MULTI" ) )
read_multi( elem );
else if ( elem.type_is( "STORAGEAREA" ) )
{
StorageArea* storage_area = gamestate.storage.create_area( elem );
// this will be followed by an item
if ( !cf.read( elem ) )
throw std::runtime_error( "Expected an item to exist after the storagearea." );
storage_area->load_item( elem );
}
else if ( elem.type_is( "REALM" ) )
read_shadow_realms( elem );
}
catch ( std::exception& )
{
if ( !Plib::systemstate.config.ignore_load_errors )
throw;
}
++nobjects;
}
timer.stop();
INFO_PRINTLN( " {} elements in {} ms.", nobjects, timer.ellapsed() );
}
}
void read_pol_dat()
{
std::string polfile = Plib::systemstate.config.world_data_path + "pol.txt";
slurp( polfile.c_str(), "GLOBALPROPERTIES SYSTEM REALM" );
if ( settingsManager.polvar.DataWrittenBy.empty() )
{
ERROR_PRINTLN(
"CoreVersion not found in {}\n"
"\n"
"{} must contain a section similar to: \n"
"System\n"
"{\n"
"\tCoreVersion 99\n"
"}\n"
"\n"
"Ensure that the CoreVersion matches the version that created your data files!",
polfile, polfile );
throw std::runtime_error( "Data file error" );
}
}
void read_objects_dat()
{
slurp( ( Plib::systemstate.config.world_data_path + "objects.txt" ).c_str(),
"CHARACTER NPC ITEM GLOBALPROPERTIES" );
}
void read_pcs_dat()
{
slurp( ( Plib::systemstate.config.world_data_path + "pcs.txt" ).c_str(), "CHARACTER ITEM",
SYSFIND_SKIP_WORLD );
}
void read_pcequip_dat()
{
slurp( ( Plib::systemstate.config.world_data_path + "pcequip.txt" ).c_str(), "ITEM",
SYSFIND_SKIP_WORLD );
}
void read_npcs_dat()
{
slurp( ( Plib::systemstate.config.world_data_path + "npcs.txt" ).c_str(), "NPC ITEM",
SYSFIND_SKIP_WORLD );
}
void read_npcequip_dat()
{
slurp( ( Plib::systemstate.config.world_data_path + "npcequip.txt" ).c_str(), "ITEM",
SYSFIND_SKIP_WORLD );
}
void read_items_dat()
{
slurp( ( Plib::systemstate.config.world_data_path + "items.txt" ).c_str(), "ITEM" );
}
void read_multis_dat()
{
slurp( ( Plib::systemstate.config.world_data_path + "multis.txt" ).c_str(), "MULTI" );
// string multisfile = config.world_data_path + "multis.txt";
// if (FileExists( multisfile ))
// {
// cout << multisfile << ":";
// ConfigFile cf( multisfile, "MULTI" );
// ConfigElem elem;
// while( cf.read( elem ))
// {
// UMulti* multi = read_multi( elem );
// if (multi == nullptr) throw runtime_error( "multi creation returned nullptr!" );
//
// add_multi_to_world( multi );
// }
// }
}
void read_storage_dat()
{
std::string storagefile = Plib::systemstate.config.world_data_path + "storage.txt";
if ( Clib::FileExists( storagefile ) )
{
INFO_PRINT( " {}:", storagefile );
Clib::ConfigFile cf2( storagefile );
gamestate.storage.read( cf2 );
}
}
Items::Item* find_existing_item( u32 objtype, const Pos4d& pos )
{
Pos2d gridp = zone_convert( pos );
for ( auto& item : pos.realm()->getzone_grid( gridp ).items )
{
// FIXME won't find doors which have been perturbed
if ( item->objtype_ == objtype && item->pos() == pos )
{
return item;
}
}
return nullptr;
}
int import_count;
int dupe_count;
void import( Clib::ConfigElem& elem )
{
u32 objtype;
objtype = elem.remove_unsigned( "OBJTYPE" );
if ( objtype > Plib::systemstate.config.max_tile_id )
{
ERROR_PRINTLN( "Importing file: {:#X} is out of range.", objtype );
throw std::runtime_error( "Error while importing file." );
}
Items::Item* item = Items::Item::create( objtype, 0x40000000 ); // dummy serial
if ( item == nullptr )
{
ERROR_PRINTLN( "Unable to import item: objtype={:#X}", objtype );
throw std::runtime_error( "Item::create failed!" );
}
item->readProperties( elem );
if ( find_existing_item( item->objtype_, item->pos() ) )
{
item->destroy();
++dupe_count;
}
else
{
item->serial = GetNewItemSerialNumber();
item->serial_ext = ctBEu32( item->serial );
add_item_to_world( item );
register_with_supporting_multi( item );
++import_count;
}
}
void import_new_data()
{
std::string importfile = Plib::systemstate.config.world_data_path + "import.txt";
if ( Clib::FileExists( importfile ) )
{
Clib::ConfigFile cf( importfile, "ITEM" );
Clib::ConfigElem elem;
while ( cf.read( elem ) )
{
import( elem );
}
unlink( importfile.c_str() );
INFO_PRINTLN( "Import Results: {} imported, {} duplicates.", import_count, dupe_count );
}
}
void rndat( const std::string& basename )
{
std::string datname = Plib::systemstate.config.world_data_path + basename + ".dat";
std::string txtname = Plib::systemstate.config.world_data_path + basename + ".txt";
if ( Clib::FileExists( datname.c_str() ) )
{
rename( datname.c_str(), txtname.c_str() );
}
}
void rename_dat_files()
{
rndat( "pol" );
rndat( "objects" );
rndat( "pcs" );
rndat( "pcequip" );
rndat( "npcs" );
rndat( "npcequip" );
rndat( "items" );
rndat( "multis" );
rndat( "storage" );
rndat( "resource" );
rndat( "guilds" );
rndat( "parties" );
}
int read_data()
{
std::string objectsndtfile = Plib::systemstate.config.world_data_path + "objects.ndt";
std::string storagendtfile = Plib::systemstate.config.world_data_path + "storage.ndt";
stateManager.gflag_in_system_load = true;
if ( Clib::FileExists( objectsndtfile ) )
{
// Display reads "Reading data files..."
ERROR_PRINTLN(
"Error!\n"
"'{} exists. This probably means the system\n"
"exited while writing its state. To avoid loss of data,\n"
"forcing human intervention.",
objectsndtfile );
throw std::runtime_error( "Human intervention required." );
}
if ( Clib::FileExists( storagendtfile ) )
{
ERROR_PRINTLN(
"Error!\n"
"'{} exists. This probably means the system\n"
"exited while writing its state. To avoid loss of data,\n"
"forcing human intervention.",
storagendtfile );
throw std::runtime_error( "Human intervention required." );
}
rename_dat_files();
load_incremental_indexes();
read_pol_dat();
// POL clock should be paused at this point.
start_gameclock();
read_objects_dat();
read_pcs_dat();
read_pcequip_dat();
read_npcs_dat();
read_npcequip_dat();
read_items_dat();
read_multis_dat();
read_storage_dat();
read_resources_dat();
read_guilds_dat();
Module::read_datastore_dat();
read_party_dat();
read_incremental_saves();
insert_deferred_items();
register_deleted_serials();
clear_save_index();
import_new_data();
// import_wsc();
// dave 3/9/3
if ( stateManager.stored_last_item_serial < GetCurrentItemSerialNumber() )
SetCurrentItemSerialNumber( stateManager.stored_last_item_serial );
if ( stateManager.stored_last_char_serial < GetCurrentCharSerialNumber() )
SetCurrentCharSerialNumber( stateManager.stored_last_char_serial );
while ( !parent_conts.empty() )
parent_conts.pop();
for ( ObjectHash::hs::const_iterator citr = objStorageManager.objecthash.begin(),
citrend = objStorageManager.objecthash.end();
citr != citrend; ++citr )
{
UObject* obj = ( *citr ).second.get();
if ( obj->ismobile() )
{
Mobile::Character* chr = static_cast<Mobile::Character*>( obj );
if ( chr->acct != nullptr )
chr->logged_in( false );
}
}
stateManager.gflag_in_system_load = false;
return 0;
}
SaveContext::SaveContext()
: _pol(),
_objects(),
_pcs(),
_pcequip(),
_npcs(),
_npcequip(),
_items(),
_multis(),
_storage(),
_resource(),
_guilds(),
_datastore(),
_party(),
pol( &_pol ),
objects( &_objects ),
pcs( &_pcs ),
pcequip( &_pcequip ),
npcs( &_npcs ),
npcequip( &_npcequip ),
items( &_items ),
multis( &_multis ),
storage( &_storage ),
resource( &_resource ),
guilds( &_guilds ),
datastore( &_datastore ),
party( &_party )
{
pol.init( Plib::systemstate.config.world_data_path + "pol.ndt" );
objects.init( Plib::systemstate.config.world_data_path + "objects.ndt" );
pcs.init( Plib::systemstate.config.world_data_path + "pcs.ndt" );
pcequip.init( Plib::systemstate.config.world_data_path + "pcequip.ndt" );
npcs.init( Plib::systemstate.config.world_data_path + "npcs.ndt" );
npcequip.init( Plib::systemstate.config.world_data_path + "npcequip.ndt" );
items.init( Plib::systemstate.config.world_data_path + "items.ndt" );
multis.init( Plib::systemstate.config.world_data_path + "multis.ndt" );
storage.init( Plib::systemstate.config.world_data_path + "storage.ndt" );
resource.init( Plib::systemstate.config.world_data_path + "resource.ndt" );
guilds.init( Plib::systemstate.config.world_data_path + "guilds.ndt" );
datastore.init( Plib::systemstate.config.world_data_path + "datastore.ndt" );
party.init( Plib::systemstate.config.world_data_path + "parties.ndt" );
pcs.comment( "" );
pcs.comment( " PCS.TXT: Player-Character Data" );
pcs.comment( "" );
pcs.comment( " In addition to PC data, this also contains hair, beards, death shrouds," );
pcs.comment( " and backpacks, but not the contents of each backpack." );
pcs.comment( "\n" );
pcequip.comment( "" );
pcequip.comment( " PCEQUIP.TXT: Player-Character Equipment Data" );
pcequip.comment( "" );
pcequip.comment( " This file can be deleted to wipe all items held/equipped by characters" );
pcequip.comment( " Note that hair, beards, empty backpacks, and death shrouds are in PCS.TXT." );
pcequip.comment( "\n" );
npcs.comment( "" );
npcs.comment( " NPCS.TXT: Nonplayer-Character Data" );
npcs.comment( "" );
npcs.comment( " If you delete this file to perform an NPC wipe," );
npcs.comment( " be sure to also delete NPCEQUIP.TXT" );
npcs.comment( "\n" );
npcequip.comment( "" );
npcequip.comment( " NPCEQUIP.TXT: Nonplayer-Character Equipment Data" );
npcequip.comment( "" );
npcequip.comment( " Delete this file along with NPCS.TXT to perform an NPC wipe" );
npcequip.comment( "\n" );
items.comment( "" );
items.comment( " ITEMS.TXT: Item data" );
items.comment( "" );
items.comment( " This file also contains ship and house components (doors, planks etc)" );
items.comment( "\n" );
multis.comment( "" );
multis.comment( " MULTIS.TXT: Ship and House data" );
multis.comment( "" );
multis.comment( " Deleting this file will not properly wipe houses and ships," );
multis.comment( " because doors, planks, and tillermen will be left in the world." );
multis.comment( "\n" );
storage.comment( "" );
storage.comment( " STORAGE.TXT: Contains bank boxes, vendor inventories, and other data." );
storage.comment( "" );
storage.comment( " This file can safely be deleted to wipe bank boxes and vendor inventories." );
storage.comment( " Note that scripts may use this for other types of storage as well" );
storage.comment( "\n" );
resource.comment( "" );
resource.comment( " RESOURCE.TXT: Resource System Data" );
resource.comment( "\n" );
guilds.comment( "" );
guilds.comment( " GUILDS.TXT: Guild Data" );
guilds.comment( "\n" );
datastore.comment( "" );
datastore.comment( " DATASTORE.TXT: DataStore Data" );
datastore.comment( "\n" );
party.comment( "" );
party.comment( " PARTIES.TXT: Party Data" );
party.comment( "\n" );
}
SaveContext::~SaveContext()
{
pol.flush_file();
objects.flush_file();
pcs.flush_file();
pcequip.flush_file();
npcs.flush_file();
npcequip.flush_file();
items.flush_file();
multis.flush_file();
storage.flush_file();
resource.flush_file();
guilds.flush_file();
datastore.flush_file();
party.flush_file();
}
/// blocks till possible last commit finishes
void SaveContext::ready()
{
if ( SaveContext::finished.valid() )
{
// Tools::Timer<Tools::DebugT> t("future");
SaveContext::finished.wait();
}
}
void write_global_properties( Clib::StreamWriter& sw )
{
sw.begin( "GlobalProperties" );
gamestate.global_properties->printProperties( sw );
sw.end();
}
void write_system_data( Clib::StreamWriter& sw )
{
sw.begin( "System" );
sw.add( "CoreVersion", POL_VERSION_STR );
sw.add( "CoreVersionString", POL_VERSION_STR );
sw.add( "CompileDateTime", Clib::ProgramConfig::build_datetime() );
sw.add( "LastItemSerialNumber", GetCurrentItemSerialNumber() );
sw.add( "LastCharSerialNumber", GetCurrentCharSerialNumber() );
sw.end();
}
void write_shadow_realms( Clib::StreamWriter& sw )
{
for ( const auto& realm : gamestate.Realms )
{
if ( realm->is_shadowrealm )
{
sw.begin( "Realm" );
sw.add( "Name", realm->shadowname );
sw.add( "BaseRealm", realm->baserealm->name() );
sw.end();
}
}
}
// Austin (Oct. 17, 2006)
// Added to handle gotten item saving.
inline void WriteGottenItem( Mobile::Character* chr, Items::Item* item, Clib::StreamWriter& sw )
{
if ( item == nullptr || item->orphan() )
return;
// For now, it just saves the item in items.txt
item->setposition( chr->pos() );
item->printOn( sw );
item->setposition(
Pos4d( 0, 0, 0,
item->realm() ) ); // TODO POS position should have no meaning remove this completely
}
void write_characters( Core::SaveContext& sc )
{
for ( const auto& objitr : objStorageManager.objecthash )
{
UObject* obj = objitr.second.get();
if ( obj->ismobile() && !obj->orphan() )
{
Mobile::Character* chr = static_cast<Mobile::Character*>( obj );
if ( !chr->isa( UOBJ_CLASS::CLASS_NPC ) )
{
chr->printOn( sc.pcs );
chr->clear_dirty();
chr->printWornItems( sc.pcs, sc.pcequip );
}
}
}
}
void write_npcs( Core::SaveContext& sc )
{
for ( const auto& objitr : objStorageManager.objecthash )
{
UObject* obj = objitr.second.get();
if ( obj->ismobile() && !obj->orphan() )
{
Mobile::Character* chr = static_cast<Mobile::Character*>( obj );
if ( chr->isa( UOBJ_CLASS::CLASS_NPC ) )
{
if ( chr->saveonexit() )
{
chr->printOn( sc.npcs );
chr->clear_dirty();
chr->printWornItems( sc.npcs, sc.npcequip );
}
}
}
}
}
void write_items( Clib::StreamWriter& sw_items )
{
for ( const auto& realm : gamestate.Realms )
{
for ( const auto& p : realm->gridarea() )
{
for ( const auto& item : realm->getzone_grid( p ).items )
{
if ( item->itemdesc().save_on_exit && item->saveonexit() )
{
item->printOn( sw_items );
item->clear_dirty();
}
}
}
}
for ( const auto& objitr : objStorageManager.objecthash )
{
UObject* obj = objitr.second.get();
if ( obj->ismobile() && !obj->orphan() )
{
Mobile::Character* chr = static_cast<Mobile::Character*>( obj );
if ( !chr->isa( UOBJ_CLASS::CLASS_NPC ) )
{
// Figure out where to save the 'gotten item' - Austin (Oct. 17, 2006)
if ( chr->has_gotten_item() )
WriteGottenItem( chr, chr->gotten_item().item(), sw_items );
}
}
}
}
void write_multis( Clib::StreamWriter& ofs )
{
for ( const auto& realm : gamestate.Realms )
{
for ( const auto& p : realm->gridarea() )
{
for ( auto& multi : realm->getzone_grid( p ).multis )
{
if ( Clib::exit_signalled ) // drop waiting commit on shutdown
{
Multi::UHouse* house = multi->as_house();
if ( house != nullptr )
{
if ( house->IsCustom() )
{
if ( house->IsWaitingForAccept() )
house->AcceptHouseCommit( nullptr, false );
}
}
}
multi->printOn( ofs );
multi->clear_dirty();
}
}
}
}
bool commit( const std::string& basename )
{
std::string bakfile = Plib::systemstate.config.world_data_path + basename + ".bak";
std::string datfile = Plib::systemstate.config.world_data_path + basename + ".txt";
std::string ndtfile = Plib::systemstate.config.world_data_path + basename + ".ndt";
const char* bakfile_c = bakfile.c_str();
const char* datfile_c = datfile.c_str();
const char* ndtfile_c = ndtfile.c_str();
bool any = false;
if ( Clib::FileExists( bakfile_c ) )
{
any = true;