• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polserver / polserver / 21541532363

31 Jan 2026 08:14AM UTC coverage: 60.532% (+0.03%) from 60.507%
21541532363

push

github

web-flow
Tidy modernize for loops (#862)

* trigger loop convert

* Automated clang-tidy change: modernize-loop-convert

* fixed refactor

* Automated clang-tidy change: modernize-loop-convert

* compile

* first look through

* fixes and start to use a few ranges

* revert autogenerated file

* compilation fix

* second pass

* renamed loop variable

---------

Co-authored-by: Clang Tidy <clang-tidy@users.noreply.github.com>

164 of 447 new or added lines in 61 files covered. (36.69%)

6 existing lines in 5 files now uncovered.

44377 of 73312 relevant lines covered (60.53%)

499857.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.48
/pol-core/pol/pol.cpp
1
/** @file
2
 *
3
 * @par History
4
 * - 2005/02/23 Shinigami: ServSpecOpt DecayItems to enable/disable item decay
5
 * - 2005/04/03 Shinigami: send_feature_enable() call moved from start_client_char()
6
 *                         to send_start() to send before char selection
7
 * - 2005/04/04 Shinigami: added can_delete_character( chr, delete_by )
8
 * - 2005/06/15 Shinigami: ServSpecOpt UseWinLFH to enable/disable Windows XP/2003 low-fragmentation
9
 * Heap
10
 *                         added Check_for_WinXP_or_Win2003() and low_fragmentation_Heap()
11
 * - 2005/06/20 Shinigami: added llog (needs defined MEMORYLEAK)
12
 * - 2005/07/01 Shinigami: removed Check_for_WinXP_or_Win2003() and transformed call of
13
 *                         Use_low_fragmentation_Heap() into Run-Time Dynamic Linking
14
 * - 2005/10/13 Shinigami: added Check_libc_version() and printing a Warning if libc is to old
15
 * - 2005/11/25 Shinigami: PKTBI_BF::TYPE_SESPAM will not block Inactivity-Check
16
 * - 2005/11/28 MuadDib:   Created check_inactivity() bool function to handle checking packets
17
 *                         for ones to be considered "ignored" for inactivity. Returns true if
18
 *                         the packet was one to be ignored.
19
 * - 2005/11/28 MuadDib:   Implemented check_inactivity() function in appropriate place.
20
 * - 2006/03/01 MuadDib:   Added connect = true to start_client_char so char creation can use.
21
 * - 2006/03/03 MuadDib:   Moved all instances of connected = true to start_client_char.
22
 * - 2006/06/03 Shinigami: added little bit more logging @ pthread_create
23
 * - 2006/06/05 Shinigami: added little bit more logging @ Client disconnects by Core
24
 * - 2006/07/05 Shinigami: moved MakeDirectory("log") a little bit up
25
 * - 2006/10/07 Shinigami: FreeBSD fix - changed some __linux__ to __unix__
26
 * - 2007/03/08 Shinigami: added pthread_exit and _endhreadex to close threads
27
 * - 2007/05/06 Shinigami: smaller bugfix in Check_libc_version()
28
 * - 2007/06/17 Shinigami: Pergon-Linux-Release generates file "pol.pid"
29
 * - 2007/07/08 Shinigami: added UO:KR login process
30
 * - 2008/07/08 Turley:    removed Checkpoint "initializing random number generator"
31
 * - 2008/12/17 MuadDub:   Added check when loading Realms for no realms existing.
32
 * - 2009/01/19 Nando:     added unload_aux_services() and unload_packages() to the shutdown cleanup
33
 * - 2009/1/24  MuadDib:   Added read_bannedips_config() and checkpoint for it after loading of
34
 * pol.cfg
35
 * - 2009/07/23 MuadDib:   Updates for MSGOUT naming.
36
 * - 2009/07/31 MuadDib:   xmain_inner(): Force Client Disconnect to initiate cleanup of clients and
37
 * chars, after shutdown,
38
 *                         before other cleanups.
39
 * - 2009/08/01 MuadDib:   Removed send_tech_stuff(), send_betaclient_BF(), just_ignore_message(),
40
 * and ignore_69() due to not used or obsolete.
41
 * - 2009/08/03 MuadDib:   Renaming of MSG_HANDLER_6017 and related, to MSG_HANDLER_V2 for better
42
 * description
43
 *                         Renamed secondary handler class to *_V2 for naming convention
44
 * - 2009/08/14 Turley:    fixed definition of PKTIN_5D
45
 * - 2009/08/19 Turley:    PKTIN_5D clientflag saved in client->UOExpansionFlagClient
46
 * - 2009/09/03 MuadDib:   Relocation of account related cpp/h
47
 *                         Changes for multi related source file relocation
48
 * - 2009/09/15 MuadDib:   Multi registration/unregistration support added.
49
 * - 2009/09/06 Turley:    Changed Version checks to bitfield client->ClientType
50
 * - 2009/09/22 MuadDib:   Fix for lightlevel resets in client during login.
51
 * - 2009/11/19 Turley:    ssopt.core_sends_season & .core_handled_tags - Tomi
52
 * - 2009/12/04 Turley:    Crypto cleanup - Tomi
53
 * - 2010/01/22 Turley:    Speedhack Prevention System
54
 * - 2010/03/28 Shinigami: Transmit Pointer as Pointer and not Int as Pointer within
55
 * decay_thread_shadow
56
 * - 2011/11/12 Tomi:    Added extobj.cfg
57
 */
58

59
#include "pol.h"
60

61
#include <errno.h>
62

63
#include "pol_global_config.h"
64

65
#include "../bscript/bobject.h"
66
#include "../bscript/escriptv.h"
67
#include "../clib/Debugging/ExceptionParser.h"
68
#include "../clib/Program/ProgramConfig.h"
69
#include "../clib/clib_endian.h"
70
#include "../clib/esignal.h"
71
#include "../clib/fileutil.h"
72
#include "../clib/kbhit.h"
73
#include "../clib/logfacility.h"
74
#include "../clib/network/sockets.h"
75
#include "../clib/passert.h"
76
#include "../clib/rawtypes.h"
77
#include "../clib/refptr.h"
78
#include "../clib/stlutil.h"
79
#include "../clib/streamsaver.h"
80
#include "../clib/threadhelp.h"
81
#include "../clib/timer.h"
82
#include "../clib/tracebuf.h"
83
#include "../plib/pkg.h"
84
#include "../plib/systemstate.h"
85
#include "accounts/account.h"
86
#include "base/position.h"
87
#include "checkpnt.h"
88
#include "console.h"
89
#include "core.h"
90
#include "decay.h"
91
#include "extobj.h"
92
#include "fnsearch.h"
93
#include "gameclck.h"
94
#include "globals/network.h"
95
#include "globals/object_storage.h"
96
#include "globals/state.h"
97
#include "globals/uvars.h"
98
#include "item/armor.h"
99
#include "item/equipmnt.h"
100
#include "item/itemdesc.h"
101
#include "loadunld.h"
102
#include "menu.h"
103
#include "mobile/charactr.h"
104
#include "multi/house.h"
105
#include "multi/multi.h"
106
#include "network/cgdata.h"
107
#include "network/client.h"
108
#include "network/clientthread.h"
109
#include "network/clienttransmit.h"
110
#include "network/cliface.h"
111
#include "network/packethelper.h"
112
#include "network/packethooks.h"
113
#include "network/packets.h"
114
#include "network/pktboth.h"
115
#include "network/pktdef.h"
116
#include "network/pktin.h"
117
#include "network/sockio.h"
118
#include "party.h"
119
#include "polclock.h"
120
#include "poldbg.h"
121
#include "polsem.h"
122
#include "polsig.h"
123
#include "polwww.h"
124
#include "profile.h"
125
#include "realms/WorldChangeReasons.h"
126
#include "realms/realm.h"
127
#include "regions/guardrgn.h"
128
#include "regions/miscrgn.h"
129
#include "regions/musicrgn.h"
130
#include "savedata.h"
131
#include "scrdef.h"
132
#include "scrsched.h"
133
#include "sqlscrobj.h"
134
#include "ssopt.h"
135
#include "testing/poltest.h"
136
#include "ufunc.h"
137
#include "uimport.h"
138
#include "uoclient.h"
139
#include "uoscrobj.h"
140
#include "uworld.h"
141

142

143
#ifndef NDEBUG
144
#include "containr.h"
145
#include "mobile/npc.h"
146
#endif
147

148
#ifdef _WIN32
149
#include <process.h>
150

151
#include "../clib/mdump.h"
152
#endif
153

154

155
#include <cstdio>
156
#include <cstring>
157
#include <exception>
158
#include <iosfwd>
159
#include <string>
160

161
#ifdef _MSC_VER
162
#pragma warning( disable : 4127 )  // conditional expression is constant (needed because of FD_SET)
163
#endif
164

165
namespace Pol
166
{
167
namespace Bscript
168
{
169
void display_executor_instances();
170
void display_bobjectimp_instances();
171
}  // namespace Bscript
172
namespace Items
173
{
174
void load_intrinsic_weapons();
175
void allocate_intrinsic_weapon_serials();
176
}  // namespace Items
177
namespace Network
178
{
179
void load_aux_services();
180
void start_aux_services();
181
void read_bannedips_config( bool initial_load );
182
}  // namespace Network
183
namespace Core
184
{
185
void cancel_all_trades();
186
void load_system_hooks();
187
bool load_realms();
188
void InitializeSystemTrayHandling();
189
void ShutdownSystemTrayHandling();
190
void start_uo_client_listeners();
191
void start_tasks();
192

193

194
using namespace threadhelp;
195

196
void send_startup( Network::Client* client )
2✔
197
{
198
  Mobile::Character* chr = client->chr;
2✔
199
  Network::PktHelper::PacketOut<Network::PktOut_1B> msg;
2✔
200
  msg->Write<u32>( chr->serial_ext );
2✔
201
  msg->offset += 4;  // u8 unk5, unk6, unk7, unk8
2✔
202
  msg->WriteFlipped<u16>( chr->graphic );
2✔
203
  msg->WriteFlipped<u16>( chr->x() );
2✔
204
  msg->WriteFlipped<u16>( chr->y() );
2✔
205
  msg->offset++;  // u8 unk_15
2✔
206
  msg->Write<s8>( chr->z() );
2✔
207
  msg->Write<u8>( chr->facing );
2✔
208
  msg->offset += 3;  // u8 unk18,unk19,unk20
2✔
209
  msg->Write<u8>( 0x7Fu );
2✔
210
  msg->offset++;     // u8 unk22
2✔
211
  msg->offset += 4;  // u16 map_startx, map_starty
2✔
212
  msg->WriteFlipped<u16>( client->chr->realm()->width() );
2✔
213
  msg->WriteFlipped<u16>( client->chr->realm()->height() );
2✔
214
  msg->offset += 6;  // u8 unk31, unk32, unk33, unk34, unk35, unk36
2✔
215
  msg.Send( client );
2✔
216
}
2✔
217

218
void send_inrange_items( Network::Client* client )
×
219
{
220
  WorldIterator<ItemFilter>::InMaxVisualRange( client->chr,
×
221
                                               [&]( Items::Item* item )
×
222
                                               {
223
                                                 if ( client->chr->in_visual_range( item ) )
×
224
                                                   send_item( client, item );
×
225
                                               } );
×
226
}
×
227

228
void send_inrange_multis( Network::Client* client )
×
229
{
230
  WorldIterator<MultiFilter>::InMaxVisualRange( client->chr,
×
231
                                                [&]( Multi::UMulti* multi )
×
232
                                                {
233
                                                  if ( client->chr->in_visual_range( multi ) )
×
234
                                                    send_multi( client, multi );
×
235
                                                } );
×
236
}
×
237

238
void textcmd_startlog( Network::Client* client );
239
void textcmd_stoplog( Network::Client* client );
240
void start_client_char( Network::Client* client )
2✔
241
{
242
  client->ready = true;
2✔
243
  client->chr->connected( true );
2✔
244

245
  // even if this stuff just gets queued, we still want the client to start
246
  // getting data now.
247
  client->pause();
2✔
248

249
  Multi::UMulti* supporting_multi;
250
  Items::Item* walkon;
251
  short newz;
252
  if ( client->chr->realm()->walkheight( client->chr, client->chr->pos2d(), client->chr->z(), &newz,
2✔
253
                                         &supporting_multi, &walkon ) )
254
  {
255
    client->chr->setposition( Pos4d( client->chr->pos() ).z( static_cast<s8>( newz ) ) );
×
256
    if ( supporting_multi != nullptr )
×
257
    {
258
      supporting_multi->register_object( client->chr );
×
259
      if ( client->chr->registered_multi == 0 )
×
260
      {
261
        client->chr->registered_multi = supporting_multi->serial;
×
262
        supporting_multi->walk_on( client->chr );
×
263
      }
264
    }
265
    else
266
    {
267
      if ( client->chr->registered_multi > 0 )
×
268
      {
269
        Multi::UMulti* multi = system_find_multi( client->chr->registered_multi );
×
270
        if ( multi != nullptr )
×
271
        {
272
          multi->unregister_object( client->chr );
×
273
        }
274
        client->chr->registered_multi = 0;
×
275
      }
276
    }
277
    client->chr->position_changed();
×
278
  }
279

280
  send_startup( client );
2✔
281

282
  send_realm_change( client, client->chr->realm() );
2✔
283
  send_map_difs( client );
2✔
284

285
  if ( settingsManager.ssopt.core_sends_season )
2✔
286
    send_season_info( client );
2✔
287

288
  client->chr->lastpos = Pos4d( 0, 0, 0, nullptr );
2✔
289

290
  client->gd->music_region =
4✔
291
      gamestate.musicdef->getregion( Pos4d( 0, 0, 0, client->chr->realm() ) );
2✔
292
  client->gd->justice_region =
4✔
293
      gamestate.justicedef->getregion( Pos4d( 0, 0, 0, client->chr->realm() ) );
2✔
294
  client->gd->weather_region =
4✔
295
      gamestate.weatherdef->getregion( Pos4d( 0, 0, 0, client->chr->realm() ) );
2✔
296

297
  send_goxyz( client, client->chr );
2✔
298
  client->chr->check_region_changes();
2✔
299

300
  client->chr->send_warmode();
2✔
301
  login_complete( client );
2✔
302
  client->chr->tellmove();
2✔
303

304
  client->chr->check_weather_region_change( true );
2✔
305

306
  if ( settingsManager.ssopt.core_sends_season )
2✔
307
    send_season_info( client );
2✔
308

309
  send_objects_newly_inrange( client );
2✔
310

311
  client->chr->send_highlight();
2✔
312
  send_owncreate( client, client->chr );
2✔
313

314
  send_goxyz( client, client->chr );
2✔
315

316
  client->restart();
2✔
317

318
  client->chr->clear_gotten_item();
2✔
319
  on_loggon_party( client->chr );
2✔
320
  client->chr->send_buffs();
2✔
321

322

323
  //  Moved login_complete higher to prevent weather regions from messing up client
324
  //  spell icon packets(made it look like it was raining spell icons from spellbook if logged
325
  //  into a weather region with rain.
326
  //  login_complete(client);
327
}
2✔
328

329

330
void call_chr_scripts( Mobile::Character* chr, const std::string& root_script_ecl,
7✔
331
                       const std::string& pkg_script_ecl, bool offline = false )
332
{
333
  ScriptDef sd;
7✔
334
  sd.quickconfig( root_script_ecl );
7✔
335

336
  if ( sd.exists() )
7✔
337
  {
338
    call_script( sd, offline ? new Module::EOfflineCharacterRefObjImp( chr )
×
339
                             : new Module::ECharacterRefObjImp( chr ) );
×
340
  }
341

342
  for ( auto pkg : Plib::systemstate.packages )
140✔
343
  {
344
    sd.quickconfig( pkg, pkg_script_ecl );
133✔
345
    if ( sd.exists() )
133✔
346
    {
347
      call_script( sd, offline ? new Module::EOfflineCharacterRefObjImp( chr )
×
348
                               : new Module::ECharacterRefObjImp( chr ) );
×
349
    }
350
  }
351
}
7✔
352

353
void run_logon_script( Mobile::Character* chr )
2✔
354
{
355
  call_chr_scripts( chr, "scripts/misc/logon.ecl", "logon.ecl" );
2✔
356
}
2✔
357
void run_reconnect_script( Mobile::Character* chr )
×
358
{
359
  call_chr_scripts( chr, "scripts/misc/reconnect.ecl", "reconnect.ecl" );
×
360
}
×
361
bool can_delete_character( Mobile::Character* chr, int delete_by )
3✔
362
{
363
  ScriptDef sd;
3✔
364
  sd.quickconfig( "scripts/misc/candelete.ecl" );
3✔
365

366
  if ( sd.exists() )
3✔
367
  {
368
    return call_script( sd, new Module::EOfflineCharacterRefObjImp( chr ),
×
369
                        new Bscript::BLong( delete_by ) );
×
370
  }
371

372
  return true;
3✔
373
}
3✔
374
void call_ondelete_scripts( Mobile::Character* chr )
3✔
375
{
376
  call_chr_scripts( chr, "scripts/misc/ondelete.ecl", "ondelete.ecl", true );
3✔
377
}
3✔
378

379
// FIXME: Consider moving most of this into a function, so character
380
// creation can use the same code.
381
void char_select( Network::Client* client, PKTIN_5D* msg )
2✔
382
{
383
  bool reconnecting = false;
2✔
384
  int charidx = cfBEu32( msg->charidx );
2✔
385
  if ( ( charidx >= Plib::systemstate.config.character_slots ) || ( client->acct == nullptr ) ||
4✔
386
       ( client->acct->get_character( charidx ) == nullptr ) )
2✔
387
  {
388
    send_login_error( client, LOGIN_ERROR_MISC );
×
389
    client->Disconnect();
×
390
    return;
×
391
  }
392

393
  Mobile::Character* chosen_char = client->acct->get_character( charidx );
2✔
394

395
  POLLOGLN( "Account {} selecting character {}", client->acct->name(), chosen_char->name() );
2✔
396

397
  if ( Plib::systemstate.config.min_cmdlevel_to_login > chosen_char->cmdlevel() )
2✔
398
  {
399
    POLLOGLN(
×
400
        "Account {} with character {} doesn't fit MinCmdlevelToLogin from pol.cfg. Client "
401
        "disconnected by Core.",
402
        client->acct->name(), chosen_char->name() );
×
403

404
    send_login_error( client, LOGIN_ERROR_MISC );
×
405
    client->Disconnect();
×
406
    return;
×
407
  }
408

409
  // Dave moved this from login.cpp so client cmdlevel can be checked before denying login
410
  if ( ( ( std::count_if( networkManager.clients.begin(), networkManager.clients.end(),
2✔
411
                          clientHasCharacter ) ) >= Plib::systemstate.config.max_clients ) &&
2✔
412
       ( chosen_char->cmdlevel() < Plib::systemstate.config.max_clients_bypass_cmdlevel ) )
×
413
  {
414
    POLLOGLN(
×
415
        "To much clients connected. Check MaximumClients and/or MaximumClientsBypassCmdLevel in "
416
        "pol.cfg.\n"
417
        "Account {} with character {} Client disconnected by Core.",
418
        client->acct->name(), chosen_char->name() );
×
419

420
    send_login_error( client, LOGIN_ERROR_MISC );
×
421
    client->Disconnect();
×
422
    return;
×
423
  }
424

425
  if ( chosen_char->client )
2✔
426
  {
427
    // we're reattaching to a character that is in-game.  If there is still
428
    // a client attached, disconnect it.
429

430
    chosen_char->client->gd->clear();
×
431
    chosen_char->client->forceDisconnect();
×
432
    chosen_char->client->ready = false;
×
433
    chosen_char->client->msgtype_filter = networkManager.disconnected_filter.get();
×
434

435
    // disassociate the objects from each other.
436
    chosen_char->client->acct = nullptr;
×
437
    chosen_char->client->chr = nullptr;
×
438

439
    chosen_char->client = nullptr;
×
440
    reconnecting = true;
×
441
  }
442
  else if ( !Plib::systemstate.config.allow_multi_clients_per_account &&
4✔
443
            client->acct->has_active_characters() )
2✔
444
  {
445
    // We are trying to attach a new character, but AllowMultiCharacters is not set
446
    send_login_error( client, LOGIN_ERROR_OTHER_CHAR_INUSE );
×
447
    client->Disconnect();
×
448
    return;
×
449
  }
450
  else
451
  {
452
    // logging in a character that's offline.
453
    SetCharacterWorldPosition( chosen_char, Realms::WorldChangeReason::PlayerEnter );
2✔
454
    chosen_char->logged_in( true );
2✔
455
  }
456

457
  client->chr = chosen_char;
2✔
458
  chosen_char->client = client;
2✔
459
  chosen_char->acct.set( client->acct );
2✔
460

461
  client->UOExpansionFlagClient = cfBEu32( msg->clientflags );
2✔
462

463
  client->msgtype_filter = networkManager.game_filter.get();
2✔
464
  start_client_char( client );
2✔
465

466
  if ( !chosen_char->lastpos.realm() )
2✔
467
    chosen_char->lastpos = chosen_char->pos();
2✔
468

469
  if ( !reconnecting )
2✔
470
    run_logon_script( chosen_char );
2✔
471
  else
472
    run_reconnect_script( chosen_char );
×
473
}
474

475
void send_client_char_data( Mobile::Character* chr, Network::Client* client );
476
void handle_resync_request( Network::Client* client, PKTBI_22_SYNC* /*msg*/ )
×
477
{
478
  send_goxyz( client, client->chr );
×
479

480
  client->send_pause();  // dave removed force=true 5/10/3, let uoclient.cfg option determine xflow
×
481
                         // packets (else this hangs 4.0.0e clients)
482

483
  Core::WorldIterator<Core::MobileFilter>::InRange( client->chr->pos(), client->chr->los_size(),
×
484
                                                    [&]( Mobile::Character* zonechr )
×
485
                                                    { send_client_char_data( zonechr, client ); } );
×
486

487
  send_inrange_items( client );
×
488
  send_inrange_multis( client );
×
489

490
  client->send_restart();  // dave removed force=true 5/10/3
×
491
}
×
492

493
void restart_all_clients()
34,425,888✔
494
{
495
  if ( !networkManager.uoclient_protocol.EnableFlowControlPackets )
34,425,888✔
496
    return;
34,425,888✔
NEW
497
  for ( auto client : networkManager.clients )
×
498
  {
499
    if ( client->pause_count )
×
500
    {
501
      client->restart();
×
502
    }
503
  }
504
}
505

506
void polclock_checkin()
34,425,789✔
507
{
508
  stateManager.checkin_clock_times_out_at = polclock() + 30 * POLCLOCKS_PER_SEC;
34,425,789✔
509
}
34,425,789✔
510

511
#define clock_t_to_ms( x ) ( x )
512

513
void tasks_thread()
2✔
514
{
515
  polclock_t sleeptime;
516
  bool activity;
517
  try
518
  {
519
    while ( !Clib::exit_signalled )
127✔
520
    {
521
      THREAD_CHECKPOINT( tasks, 1 );
125✔
522
      {
523
        PolLock lck;
125✔
524
        polclock_checkin();
125✔
525
        THREAD_CHECKPOINT( tasks, 2 );
125✔
526
        INC_PROFILEVAR( task_passes );
125✔
527
        check_scheduled_tasks( &sleeptime, &activity );
125✔
528
        THREAD_CHECKPOINT( tasks, 3 );
125✔
529
        restart_all_clients();
125✔
530
        THREAD_CHECKPOINT( tasks, 5 );
125✔
531
      }
125✔
532

533
      THREAD_CHECKPOINT( tasks, 6 );
125✔
534
      if ( activity )
125✔
535
        send_pulse();
116✔
536
      else
537
        INC_PROFILEVAR( noactivity_task_passes );
9✔
538
      THREAD_CHECKPOINT( tasks, 7 );
125✔
539

540
      passert( sleeptime > 0 );
125✔
541

542
      TRACEBUF_ADDELEM( "tasks wait_for_pulse now", static_cast<u32>( polclock() ) );
543
      TRACEBUF_ADDELEM( "tasks wait_for_pulse sleeptime", static_cast<u32>( sleeptime ) );
544

545
      THREAD_CHECKPOINT( tasks, 8 );
125✔
546
      tasks_thread_sleep( static_cast<u32>( polclock_t_to_ms( sleeptime ) ) );
125✔
547
      THREAD_CHECKPOINT( tasks, 9 );
125✔
548
    }
549
  }
550
  catch ( const char* msg )
×
551
  {
552
    POLLOGLN( "Tasks Thread exits due to exception: {}", msg );
×
553
    throw;
×
554
  }
×
555
  catch ( std::string& str )
×
556
  {
557
    POLLOGLN( "Tasks Thread exits due to exception: {}", str );
×
558
    throw;
×
559
  }
×
560
  catch ( std::exception& ex )
×
561
  {
562
    POLLOGLN( "Tasks Thread exits due to exception: {}", ex.what() );
×
563
    throw;
×
564
  }
×
565
}
2✔
566

567
void scripts_thread()
2✔
568
{
569
  using namespace std::chrono_literals;
570
  polclock_t sleeptime;
571
  bool activity;
572
  while ( !Clib::exit_signalled )
34,425,395✔
573
  {
574
    THREAD_CHECKPOINT( scripts, 0 );
34,425,391✔
575
    {
576
      PolLock lck;
34,425,391✔
577
      polclock_checkin();
34,425,391✔
578
      TRACEBUF_ADDELEM( "scripts thread now", static_cast<u32>( polclock() ) );
579
      INC_PROFILEVAR( script_passes );
34,425,391✔
580
      THREAD_CHECKPOINT( scripts, 1 );
34,425,391✔
581

582
      Tools::HighPerfTimer duration_timer{};
34,425,391✔
583
      step_scripts( &sleeptime, &activity );
34,425,391✔
584
      stateManager.profilevars.script_passes_duration.update( duration_timer.ellapsed() / 1.0us );
34,425,391✔
585

586
      THREAD_CHECKPOINT( scripts, 50 );
34,425,391✔
587

588
      restart_all_clients();
34,425,391✔
589

590
      THREAD_CHECKPOINT( scripts, 52 );
34,425,391✔
591

592
      if ( TaskScheduler::is_dirty() )
34,425,391✔
593
      {
594
        THREAD_CHECKPOINT( scripts, 53 );
573✔
595

596
        wake_tasks_thread();
573✔
597
      }
598
    }
34,425,391✔
599

600
    if ( activity )
34,425,391✔
601
    {
602
      ++stateManager.profilevars.script_passes_activity;
34,425,390✔
603
    }
604
    else
605
    {
606
      ++stateManager.profilevars.script_passes_noactivity;
1✔
607
    }
608

609
    if ( sleeptime )
34,425,391✔
610
    {
611
      THREAD_CHECKPOINT( scripts, 54 );
1✔
612

613
      wait_for_pulse( static_cast<u32>( polclock_t_to_ms( sleeptime ) ) );
1✔
614

615
      THREAD_CHECKPOINT( scripts, 55 );
1✔
616
    }
617
  }
618
}
2✔
619

620
template <class T>
621
inline void Delete( T* p )
622
{
623
  delete p;
624
}
625

626
template <class T>
627
class delete_ob
628
{
629
public:
630
  void operator()( T* p ) { delete p; }
631
};
632

633
void reap_thread()
2✔
634
{
635
  while ( !Clib::exit_signalled )
57✔
636
  {
637
    {
638
      PolLock lck;
55✔
639
      polclock_checkin();
55✔
640
      objStorageManager.objecthash.Reap();
55✔
641
      for ( auto& item : gamestate.dynamic_item_descriptors )
58✔
642
      {
643
        delete item;
3✔
644
      }
645
      gamestate.dynamic_item_descriptors.clear();
55✔
646
    }
55✔
647

648
    threadhelp::thread_sleep_ms( 2000 );
55✔
649
  }
650
}
2✔
651

652

653
void threadstatus_thread()
2✔
654
{
655
  int timeouts_remaining = 1;
2✔
656
  bool sent_wakeups = false;
2✔
657
  // we want this thread to be the last out, so that it can report stuff at shutdown.
658
  while ( !Clib::exit_signalled || threadhelp::child_threads > 1 )
119✔
659
  {
660
    if ( is_polclock_paused_at_zero() )
117✔
661
    {
662
      polclock_t now = polclock();
117✔
663
      if ( now >= stateManager.checkin_clock_times_out_at )
117✔
664
      {
665
        ERROR_PRINTLN(
×
666
            "########################################################\n"
667
            "No clock movement in 30 seconds.  Dumping thread status." );
668
        stateManager.polsig.report_status_signalled = true;
×
669
        stateManager.checkin_clock_times_out_at = now + 30 * POLCLOCKS_PER_SEC;
×
670
      }
671
    }
672

673
    if ( stateManager.polsig.report_status_signalled )
117✔
674
    {
675
      std::string tmp = fmt::format(
676
          "*Thread Info*\n"
677
          "Semaphore TID: {}\n",
678
          locker );
×
679

680
      if ( Plib::systemstate.config.log_traces_when_stuck )
×
681
        Pol::Clib::ExceptionParser::logAllStackTraces();
×
682

683
      fmt::format_to( std::back_inserter( tmp ),
×
684
                      "Scripts Thread Checkpoint: {}\n"
685
                      "Last Script: {} PC: {}\n"
686
                      "Escript Instruction Cycles: {}\n"
687
                      "Tasks Thread Checkpoint: {}\n"
688
                      "Active Client Thread Checkpoint: {}\n"
689
                      "Number of clients: {}\n",
690
                      stateManager.polsig.scripts_thread_checkpoint, Clib::scripts_thread_script,
691
                      Clib::scripts_thread_scriptPC, Bscript::escript_instr_cycles,
692
                      stateManager.polsig.tasks_thread_checkpoint,
693
                      stateManager.polsig.active_client_thread_checkpoint,
694
                      Core::networkManager.clients.size() );
×
695
      for ( const auto& client : Core::networkManager.clients )
×
696
        fmt::format_to( std::back_inserter( tmp ), " {} {} {}\n", client->ipaddrAsString(),
×
697
                        client->acct == nullptr ? "prelogin " : client->acct->name(),
×
698
                        client->session()->checkpoint );
×
699
      if ( stateManager.polsig.check_attack_after_move_function_checkpoint )
×
700
        fmt::format_to( std::back_inserter( tmp ), "check_attack_after_move() Checkpoint: {}\n",
×
701
                        stateManager.polsig.check_attack_after_move_function_checkpoint );
702
      tmp += "Current Threads:\n";
×
703
      ThreadMap::Contents contents;
×
704
      threadmap.CopyContents( contents );
×
705
      for ( ThreadMap::Contents::const_iterator citr = contents.begin(); citr != contents.end();
×
706
            ++citr )
×
707
      {
708
        fmt::format_to( std::back_inserter( tmp ), "{} - {}\n", ( *citr ).first, ( *citr ).second );
×
709
      }
710
      fmt::format_to( std::back_inserter( tmp ),
×
711
                      "Child threads (child_threads): {}\n"
712
                      "Registered threads (ThreadMap): {}",
713
                      threadhelp::child_threads, contents.size() );
×
714
      stateManager.polsig.report_status_signalled = false;
×
715
      ERROR_PRINTLN( tmp );
×
716
    }
×
717
    if ( Clib::exit_signalled )
117✔
718
    {
719
      if ( !sent_wakeups )
8✔
720
      {
721
        send_pulse();
2✔
722
        wake_tasks_thread();
2✔
723
        networkManager.clientTransmit->Cancel();
2✔
724
#ifdef HAVE_MYSQL
725
        networkManager.sql_service->stop();
2✔
726
#endif
727
        sent_wakeups = true;
2✔
728
      }
729

730
      --timeouts_remaining;
8✔
731
      if ( timeouts_remaining == 0 )
8✔
732
      {
733
        INFO_PRINTLN( "Waiting for {} child threads to exit", threadhelp::child_threads );
2✔
734
        timeouts_remaining = 5;
2✔
735
      }
736
    }
737
    pol_sleep_ms( 1000 );
117✔
738
  }
739
  // cerr << "threadstatus thread exits." << endl;
740
  signal_catch_thread();
2✔
741
}
2✔
742

743
void catch_signals_thread();
744
void reload_configuration();
745

746
void console_thread()
2✔
747
{
748
#ifndef _WIN32
749
  Clib::KeyboardHook kb;  // local to have a defined deconstruction to uninstall the hook
2✔
750
#endif
751
  while ( !Clib::exit_signalled )
13✔
752
  {
753
    pol_sleep_ms( 1000 );
9✔
754
#ifdef _WIN32
755
    ConsoleCommand::check_console_commands();
756
#else
757
    ConsoleCommand::check_console_commands( &kb );
9✔
758
    if ( stateManager.polsig.reload_configuration_signalled )
9✔
759
    {
760
      PolLock lck;
×
761
      INFO_PRINT( "Reloading configuration..." );
×
762
      stateManager.polsig.reload_configuration_signalled = false;
×
763
      reload_configuration();
×
764
      INFO_PRINTLN( "Done." );
×
765
    }
×
766
#endif
767
  }
768
}
2✔
769

770
void start_threads()
2✔
771
{
772
  threadmap.Register( thread_pid(), "Main" );
2✔
773

774
  if ( Plib::systemstate.config.web_server )
2✔
775
    start_http_server();
2✔
776

777
  checkpoint( "start tasks thread" );
2✔
778
  threadhelp::start_thread( tasks_thread, "Tasks" );
2✔
779
  checkpoint( "start scripts thread" );
2✔
780
  threadhelp::start_thread( scripts_thread, "Scripts" );
2✔
781

782
  if ( settingsManager.ssopt.decay_items )
2✔
783
  {
784
    checkpoint( "start decay thread" );
2✔
785
    threadhelp::start_thread( Decay::decay_thread, "Decay", nullptr );
2✔
786
  }
787
  else
788
  {
789
    checkpoint( "don't start decay thread" );
×
790
  }
791

792
  checkpoint( "start reap thread" );
2✔
793
  threadhelp::start_thread( reap_thread, "Reap" );
2✔
794

795
  checkpoint( "start dbglisten thread" );
2✔
796
  threadhelp::start_thread( debug_listen_thread, "DbgListn" );
2✔
797

798
  checkpoint( "start threadstatus thread" );
2✔
799
  start_thread( threadstatus_thread, "ThreadStatus" );
2✔
800

801
  checkpoint( "start clienttransmit thread" );
2✔
802
  start_thread( Network::ClientTransmitThread, "ClientTransmit" );
2✔
803

804
#ifdef HAVE_MYSQL
805
  checkpoint( "start sql service thread" );
2✔
806
  start_sql_service();
2✔
807
#endif
808
}
2✔
809

810
#if REFPTR_DEBUG
811
unsigned int ref_counted::_ctor_calls;
812
#endif
813

814
void display_unreaped_orphan_instances();
815
void display_reftypes();
816

817
void display_leftover_objects()
3✔
818
{
819
  Bscript::display_executor_instances();
3✔
820
  display_unreaped_orphan_instances();
3✔
821
#if BOBJECTIMP_DEBUG
822
  Bscript::display_bobjectimp_instances();
823
#endif
824
  display_reftypes();
3✔
825
  {
826
    std::ofstream ofs;
3✔
827
    ofs.open( "leftovers.txt", std::ios::out | std::ios::trunc );
3✔
828
    objStorageManager.objecthash.PrintContents( &ofs );
3✔
829
  }
3✔
830
  std::string tmp;
3✔
831
  if ( stateManager.uobjcount.uobject_count != 0 )
3✔
832
    tmp += fmt::format( "Remaining UObjects: {}\n", stateManager.uobjcount.uobject_count );
×
833
  if ( stateManager.uobjcount.ucharacter_count != 0 )
3✔
834
    tmp += fmt::format( "Remaining Mobiles: {}\n", stateManager.uobjcount.ucharacter_count );
×
835
  if ( stateManager.uobjcount.npc_count != 0 )
3✔
836
    tmp += fmt::format( "Remaining NPCs: {}\n", stateManager.uobjcount.npc_count );
×
837
  if ( stateManager.uobjcount.uitem_count != 0 )
3✔
838
    tmp += fmt::format( "Remaining Items: {}\n", stateManager.uobjcount.uitem_count );
×
839
  if ( stateManager.uobjcount.umulti_count != 0 )
3✔
840
    tmp += fmt::format( "Remaining Multis: {}\n", stateManager.uobjcount.umulti_count );
×
841
  if ( stateManager.uobjcount.unreaped_orphans != 0 )
3✔
842
    tmp += fmt::format( "Unreaped orphans: {}\n", stateManager.uobjcount.unreaped_orphans );
×
843
  if ( stateManager.uobjcount.uobj_count_echrref != 0 )
3✔
844
    tmp +=
845
        fmt::format( "Remaining EChrRef objects: {}\n", stateManager.uobjcount.uobj_count_echrref );
×
846
  if ( Bscript::executor_count )
3✔
847
    tmp += fmt::format( "Remaining Executors: {}\n", Bscript::executor_count );
×
848
  if ( Bscript::eobject_imp_count )
3✔
849
    tmp += fmt::format( "Remaining script objectimps: {}\n", Bscript::eobject_imp_count );
6✔
850
  INFO_PRINT( tmp );
3✔
851
}
3✔
852

853
void run_start_scripts()
2✔
854
{
855
  INFO_PRINTLN( "Running startup script." );
2✔
856
  run_script_to_completion( "start" );
2✔
857
  for ( const auto& pkg : Plib::systemstate.packages )
40✔
858
  {
859
    std::string scriptname = pkg->dir() + "start.ecl";
38✔
860

861
    if ( Clib::FileExists( scriptname.c_str() ) )
38✔
862
    {
863
      ScriptDef script( "start", pkg, "" );
×
864
      Bscript::BObject obj( run_script_to_completion( script ) );
×
865
    }
×
866
  }
38✔
867
  INFO_PRINTLN( "Startup script complete." );
2✔
868
}
2✔
869

870
#ifdef _WIN32
871
typedef BOOL( WINAPI* DynHeapSetInformation )( PVOID HeapHandle,
872
                                               HEAP_INFORMATION_CLASS HeapInformationClass,
873
                                               PVOID HeapInformation,
874
                                               SIZE_T HeapInformationLength );
875

876
const char* Use_low_fragmentation_Heap()
877
{
878
  if ( settingsManager.ssopt.use_win_lfh )
879
  {
880
    HINSTANCE hKernel32;
881

882
    hKernel32 = LoadLibrary( "Kernel32" );
883
    if ( hKernel32 != nullptr )
884
    {
885
      DynHeapSetInformation ProcAdd;
886
      ProcAdd = (DynHeapSetInformation)GetProcAddress( hKernel32, "HeapSetInformation" );
887
      if ( ProcAdd != nullptr )
888
      {
889
        ULONG HeapFragValue = 2;
890

891
        if ( (ProcAdd)( GetProcessHeap(), HeapCompatibilityInformation, &HeapFragValue,
892
                        sizeof( HeapFragValue ) ) )
893
        {
894
          FreeLibrary( hKernel32 );
895
          return "low-fragmentation Heap ...activated";
896
        }
897
        else
898
        {
899
          FreeLibrary( hKernel32 );
900
          return "low-fragmentation Heap ...not activated";
901
        }
902
      }
903
      else
904
      {
905
        FreeLibrary( hKernel32 );
906
        return "low-fragmentation Heap ...not available on your Windows";
907
      }
908
    }
909
    else
910
      return "low-fragmentation Heap ...not available on your Windows";
911
  }
912
  else
913
    return "low-fragmentation Heap ...disabled via ServSpecOpt";
914
}
915
#endif
916

917
}  // namespace Core
918

919
int xmain_inner( bool testing )
3✔
920
{
921
#ifdef _WIN32
922
  Clib::MiniDumper::Initialize();
923
  // Aug. 15, 2006 Austin
924
  // Added atexit() call to remove the tray icon.
925
  atexit( Core::ShutdownSystemTrayHandling );
926
#else
927
#ifdef __linux__
928
  std::ofstream polpid;
3✔
929

930
  polpid.open( ( Plib::systemstate.config.pidfile_path + "pol.pid" ).c_str(),
3✔
931
               std::ios::out | std::ios::trunc );
932

933
  if ( polpid.is_open() )
3✔
934
    polpid << Clib::tostring( getpid() );
3✔
935
  else
936
    INFO_PRINTLN( "Cannot create pid file in {}", Plib::systemstate.config.pidfile_path );
×
937

938
  polpid.close();
3✔
939
#endif
940
#endif
941

942
  // problem with order of global construction, threads cannot be registered in the constructor of
943
  // gamestate :(
944
  Core::gamestate.task_thread_pool.init_pool(
9✔
945
      std::max( 2u, std::thread::hardware_concurrency() / 2 ), "generic_task_thread" );
6✔
946

947
  int res;
948

949
  // for profiling:
950
  // chdir( "d:\\pol" );
951
  // PrintAllocationData();
952

953
  Clib::MakeDirectory( "log" );
3✔
954

955
  POLLOG_INFOLN( "POL {} - {}\nCompiled on {}\n{}", POL_VERSION_ID,
3✔
956
                 Clib::ProgramConfig::build_target(), Clib::ProgramConfig::build_datetime(),
6✔
957
                 POL_COPYRIGHT );
958
  if ( testing )
3✔
959
    POLLOG_INFOLN( "TESTING MODE" );
1✔
960

961
#ifndef NDEBUG
962
  POLLOG_INFOLN(
963
      "Sizes: \n"
964
      "   UObject:    {}\n"
965
      "   Item:       {}\n"
966
      "   UContainer: {}\n"
967
      "   Character:  {}\n"
968
      "   Client:     {}\n"
969
      "   NPC:        {}",
970
      sizeof( Core::UObject ), sizeof( Items::Item ), sizeof( Core::UContainer ),
971
      sizeof( Mobile::Character ), sizeof( Network::Client ), sizeof( Mobile::NPC ) );
972

973
#ifdef __unix__
974
#ifdef PTHREAD_THREADS_MAX
975
  POLLOG_INFOLN( "   Max Threads: {}", PTHREAD_THREADS_MAX );
976
#endif
977
#endif
978
  POLLOG_INFOLN( "" );
979
#endif
980
  POLLOG_INFOLN( "Using {} out of {} worldsave threads", Core::gamestate.task_thread_pool.size(),
3✔
981
                 std::thread::hardware_concurrency() );
3✔
982

983
  Core::checkpoint( "installing signal handlers" );
3✔
984
  Core::install_signal_handlers();
3✔
985

986
  Core::checkpoint( "starting POL clocks" );
3✔
987
  Core::start_pol_clocks();
3✔
988
  Core::pause_pol_clocks();
3✔
989

990
  POLLOG_INFOLN( "Reading Configuration." );
3✔
991

992
  Core::stateManager.gflag_in_system_startup = true;
3✔
993

994
  Core::checkpoint( "reading pol.cfg" );
3✔
995
  Plib::systemstate.config.read( true );
3✔
996
  Core::apply_polcfg( true );
3✔
997

998
  Core::checkpoint( "reading config/bannedips.cfg" );
3✔
999
  Network::read_bannedips_config( true );
3✔
1000

1001
  Core::checkpoint( "reading servspecopt.cfg" );
3✔
1002
  Core::ServSpecOpt::read_servspecopt();
3✔
1003

1004
  Core::checkpoint( "reading extobj.cfg" );
3✔
1005
  Core::read_extobj();
3✔
1006

1007
#ifdef _WIN32
1008
  Core::checkpoint( Core::Use_low_fragmentation_Heap() );
1009
#endif
1010

1011
  Core::checkpoint( "init default itemdesc defaults" );
3✔
1012
  Core::gamestate.empty_itemdesc->doubleclick_range =
6✔
1013
      Core::settingsManager.ssopt.default_doubleclick_range;
3✔
1014
  Core::gamestate.empty_itemdesc->decay_time = Core::settingsManager.ssopt.default_decay_time;
3✔
1015

1016
  Core::checkpoint( "loading POL map file" );
3✔
1017
  if ( !Core::load_realms() )
3✔
1018
  {
1019
    POLLOG_ERRORLN(
×
1020
        "Unable to load Realms. Please make sure your Realms have been generated by "
1021
        "UOConvert and your RealmDataPath is set correctly in Pol.cfg." );
1022
    return 1;
×
1023
  }
1024

1025
  // PrintAllocationData();
1026

1027
  Core::checkpoint( "initializing IPC structures" );
3✔
1028
  Core::init_ipc_vars();
3✔
1029
  threadhelp::init_threadhelp();
3✔
1030

1031
#ifdef _WIN32
1032
  Core::InitializeSystemTrayHandling();
1033
#endif
1034

1035
  Core::checkpoint( "initializing sockets library" );
3✔
1036
  res = Network::init_sockets_library();
3✔
1037
  if ( res < 0 )
3✔
1038
  {
1039
    POLLOG_ERRORLN( "Unable to initialize sockets library." );
×
1040
    return 1;
×
1041
  }
1042

1043
  Core::checkpoint( "loading configuration" );
3✔
1044
  Core::load_data();
3✔
1045

1046
  Core::checkpoint( "loading system hooks" );
3✔
1047
  Core::load_system_hooks();
3✔
1048

1049
  Core::checkpoint( "loading packet hooks" );
3✔
1050
  Network::load_packet_hooks();
3✔
1051

1052
  Core::checkpoint( "loading auxservice configuration" );
3✔
1053
  Network::load_aux_services();
3✔
1054

1055
  Core::checkpoint( "reading menus" );
3✔
1056
  Core::Menu::read_menus();
3✔
1057

1058
  Core::checkpoint( "loading intrinsic weapons" );
3✔
1059
  Items::load_intrinsic_weapons();
3✔
1060
  Core::checkpoint( "validating intrinsic shield template" );
3✔
1061
  Items::validate_intrinsic_shield_template();
3✔
1062
  Core::checkpoint( "reading gameservers" );
3✔
1063
  Core::read_gameservers();
3✔
1064
  Core::checkpoint( "reading starting locations" );
3✔
1065
  Core::read_starting_locations();
3✔
1066

1067
  if ( testing )
3✔
1068
  {
1069
    Items::allocate_intrinsic_equipment_serials();
1✔
1070
    Core::stateManager.gflag_in_system_startup = false;
1✔
1071
    POLLOG_INFOLN( "Running POL test suite." );
1✔
1072
    bool res_test = Testing::run_pol_tests();
1✔
1073
    Core::cancel_all_trades();
1✔
1074
    Core::stop_gameclock();
1✔
1075
    Core::gamestate.deinitialize();
1✔
1076
    return !res_test;
1✔
1077
  }
1078

1079
  // PrintAllocationData();
1080
  POLLOG_INFOLN( "Reading data files:" );
2✔
1081
  {
1082
    Tools::Timer<> timer;
2✔
1083
    Core::checkpoint( "reading account data" );
2✔
1084
    Accounts::read_account_data();
2✔
1085

1086
    Core::checkpoint( "reading data" );
2✔
1087
    Core::read_data();
2✔
1088
    POLLOG_INFOLN( "Done! {} milliseconds.", timer.ellapsed() );
2✔
1089
  }
2✔
1090

1091

1092
  Items::allocate_intrinsic_equipment_serials();
2✔
1093
  Core::stateManager.gflag_in_system_startup = false;
2✔
1094

1095
  // PrintAllocationData();
1096

1097
  Core::checkpoint( "running start scripts" );
2✔
1098
  Core::run_start_scripts();
2✔
1099

1100
  Core::checkpoint( "starting client listeners" );
2✔
1101
  Core::start_uo_client_listeners();
2✔
1102

1103
  POLLOG_INFO( "Initialization complete.  POL is active.  Ctrl-C to stop.\n\n" );
2✔
1104

1105
  DEINIT_STARTLOG();
2✔
1106
  POLLOGLN( "{0:s} ({1:s}) compiled on {2:s} running.", "POL ", POL_VERSION_ID,
2✔
1107
            Clib::ProgramConfig::build_target(), Clib::ProgramConfig::build_datetime() );
4✔
1108

1109
  POLLOG_INFOLN( "Game is active." );
2✔
1110

1111
  Core::CoreSetSysTrayToolTip( "Running", Core::ToolTipPrioritySystem );
2✔
1112

1113
  Core::restart_pol_clocks();
2✔
1114
  Core::polclock_checkin();
2✔
1115

1116
  // this is done right after reading globals from pol.txt:
1117
  // checkpoint( "starting game clock" );
1118
  // start_gameclock();
1119

1120
  Core::checkpoint( "starting periodic tasks" );
2✔
1121
  Core::start_tasks();
2✔
1122

1123
  Core::checkpoint( "starting threads" );
2✔
1124
  Core::start_threads();
2✔
1125
  Network::start_aux_services();
2✔
1126
  Core::networkManager.initialize();
2✔
1127

1128
#ifdef _WIN32
1129
  Core::console_thread();
1130
  Core::checkpoint( "exit signal detected" );
1131
  Core::CoreSetSysTrayToolTip( "Shutting down", Core::ToolTipPriorityShutdown );
1132
#else
1133
  // On Linux, signals are directed to a particular thread, if we use pthread_sigmask like we're
1134
  // supposed to.
1135
  // therefore, we have to do this signal checking in this thread.
1136
  threadhelp::start_thread( Core::console_thread, "Console" );
2✔
1137

1138
  Core::catch_signals_thread();
2✔
1139
#endif
1140
  Core::checkpoint( "waiting for child threads to exit" );
2✔
1141
  // NOTE that it's possible that the thread_status thread not have exited yet..
1142
  // it signals the catch_signals_thread (this one) just before it exits.
1143
  // and on windows, we get here right after the console thread exits.
1144
  while ( threadhelp::child_threads )
2✔
1145
  {
1146
    Core::pol_sleep_ms( 1000 );
×
1147
  }
1148
  Core::checkpoint( "child threads have shut down" );
2✔
1149
  Core::cancel_all_trades();
2✔
1150
  Core::stop_gameclock();
2✔
1151
  POLLOG_INFOLN( "Shutting down..." );
2✔
1152

1153
  Core::checkpoint( "writing data" );
2✔
1154
  if ( Core::should_write_data() )
2✔
1155
  {
1156
    Core::CoreSetSysTrayToolTip( "Writing data files", Core::ToolTipPriorityShutdown );
2✔
1157
    POLLOG_INFO( "Writing data files..." );
2✔
1158

1159
    Core::PolLock lck;
2✔
1160
    s64 elapsed_ms;
1161
    Tools::Timer<> timer;
2✔
1162
    auto res_save = Core::write_data( {}, nullptr, nullptr, &elapsed_ms );
2✔
1163
    Core::SaveContext::ready();
2✔
1164
    if ( !res_save || !( *res_save ) )
2✔
1165
      POLLOG_INFOLN( "Data save failed!" );
×
1166
    else
1167
      POLLOG_INFOLN( "Data save completed in {} ms. {} total.", elapsed_ms, timer.ellapsed() );
2✔
1168
  }
2✔
1169
  else
1170
  {
1171
    if ( Clib::passert_shutdown_due_to_assertion && Clib::passert_nosave )
×
1172
      POLLOG_INFOLN( "Not writing data due to assertion failure." );
×
1173
    else if ( Plib::systemstate.config.inhibit_saves )
×
1174
      POLLOG_INFOLN( "Not writing data due to pol.cfg InhibitSaves=1 setting." );
×
1175
  }
1176
  Core::gamestate.deinitialize();
2✔
1177
  return Clib::exit_code;
2✔
1178
}
3✔
1179

1180
int xmain_outer( bool testing )
3✔
1181
{
1182
  try
1183
  {
1184
    return xmain_inner( testing );
3✔
1185
  }
1186
  catch ( std::exception& )
×
1187
  {
1188
    if ( Core::stateManager.last_checkpoint != nullptr )
×
1189
    {
1190
      POLLOG_INFOLN( "Server Shutdown: {}", Core::stateManager.last_checkpoint );
×
1191
      // pol_sleep_ms( 10000 );
1192
    }
1193
    Core::gamestate.deinitialize();
×
1194

1195
    throw;
×
1196
  }
×
1197
}
1198
}  // namespace Pol
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc