Skip to main content

veloren_server/
lib.rs

1#![deny(unsafe_code)]
2#![expect(
3    clippy::option_map_unit_fn,
4    clippy::needless_pass_by_ref_mut // until we find a better way for specs
5)]
6#![deny(clippy::clone_on_ref_ptr)]
7#![feature(box_patterns, option_zip, const_type_name, slice_partition_dedup)]
8
9pub mod automod;
10mod character_creator;
11pub mod chat;
12pub mod chunk_generator;
13mod chunk_serialize;
14pub mod client;
15pub mod cmd;
16pub mod connection_handler;
17mod data_dir;
18pub mod error;
19pub mod events;
20pub mod input;
21pub mod location;
22pub mod lod;
23pub mod login_provider;
24pub mod metrics;
25pub mod persistence;
26mod pet;
27pub mod presence;
28pub mod rtsim;
29pub mod settings;
30pub mod state_ext;
31pub mod sys;
32#[cfg(feature = "persistent_world")]
33pub mod terrain_persistence;
34#[cfg(not(feature = "worldgen"))] mod test_world;
35
36#[cfg(feature = "worldgen")] mod weather;
37
38pub mod wiring;
39
40// Reexports
41pub use crate::{
42    data_dir::DEFAULT_DATA_DIR_NAME,
43    error::Error,
44    events::Event,
45    input::Input,
46    settings::{CalendarMode, EditableSettings, Settings},
47};
48
49#[cfg(feature = "persistent_world")]
50use crate::terrain_persistence::TerrainPersistence;
51use crate::{
52    automod::AutoMod,
53    chunk_generator::ChunkGenerator,
54    client::Client,
55    cmd::ChatCommandExt,
56    connection_handler::ConnectionHandler,
57    data_dir::DataDir,
58    location::Locations,
59    login_provider::LoginProvider,
60    persistence::PersistedComponents,
61    presence::{RegionSubscription, RepositionToFreeSpace},
62    state_ext::StateExt,
63    sys::sentinel::DeletedEntities,
64};
65use authc::Uuid;
66use censor::Censor;
67#[cfg(not(feature = "worldgen"))]
68use common::grid::Grid;
69#[cfg(feature = "worldgen")]
70use common::terrain::CoordinateConversions;
71#[cfg(feature = "worldgen")]
72use common::terrain::TerrainChunkSize;
73use common::{
74    assets::AssetExt,
75    calendar::Calendar,
76    character::{CharacterId, CharacterItem},
77    cmd::ServerChatCommand,
78    comp::{self, ChatType, Content},
79    event::{
80        ClientDisconnectEvent, ClientDisconnectWithoutPersistenceEvent, EventBus, ExitIngameEvent,
81        UpdateCharacterDataEvent,
82    },
83    link::Is,
84    mounting::{Volume, VolumeRider},
85    region::RegionMap,
86    resources::{BattleMode, GameMode, Time, TimeOfDay},
87    rtsim::RtSimEntity,
88    shared_server_config::ServerConstants,
89    slowjob::SlowJobPool,
90    terrain::TerrainChunk,
91    uid::Uid,
92    vol::RectRasterableVol,
93};
94use common_base::prof_span;
95use common_ecs::run_now;
96use common_net::{
97    msg::{ClientType, DisconnectReason, PlayerListUpdate, ServerGeneral, ServerInfo, ServerMsg},
98    sync::WorldSyncExt,
99};
100use common_state::{AreasContainer, BlockDiff, BuildArea, State};
101use common_systems::add_local_systems;
102use metrics::{EcsSystemMetrics, GameplayMetrics, PhysicsMetrics, TickMetrics};
103use network::{ListenAddr, Network, Pid};
104use persistence::{
105    character_loader::{CharacterLoader, CharacterUpdaterMessage},
106    character_updater::CharacterUpdater,
107};
108use prometheus::Registry;
109use rustls::pki_types::{CertificateDer, PrivateKeyDer};
110use settings::banlist::NormalizedIpAddr;
111use specs::{
112    Builder, Entity as EcsEntity, Entity, Join, LendJoin, WorldExt, shred::SendDispatcher,
113};
114use std::{
115    ops::{Deref, DerefMut},
116    sync::{Arc, Mutex},
117    time::{Duration, Instant},
118};
119#[cfg(not(feature = "worldgen"))]
120use test_world::{IndexOwned, World};
121use tokio::runtime::Runtime;
122use tracing::{debug, error, info, trace, warn};
123use vek::*;
124use veloren_query_server::server::QueryServer;
125pub use world::{WorldGenerateStage, civ::WorldCivStage, sim::WorldSimStage};
126
127use crate::{
128    persistence::{DatabaseSettings, SqlLogMode},
129    sys::terrain,
130};
131use hashbrown::HashMap;
132use std::sync::RwLock;
133
134use crate::settings::Protocol;
135
136#[cfg(feature = "plugins")]
137use {
138    common::uid::IdMaps,
139    common_state::plugin::{PluginMgr, memory_manager::EcsWorld},
140};
141
142use crate::{chat::ChatCache, persistence::character_loader::CharacterScreenResponseKind};
143use common::comp::Anchor;
144#[cfg(feature = "worldgen")]
145pub use world::{
146    IndexOwned, World,
147    sim::{DEFAULT_WORLD_MAP, DEFAULT_WORLD_SEED, FileOpts, GenOpts, WorldOpts},
148};
149
150/// Number of seconds a player must wait before they can change their battle
151/// mode after each change.
152///
153/// TODO: Discuss time
154const BATTLE_MODE_COOLDOWN: f64 = 60.0 * 5.0;
155
156/// SpawnPoint corresponds to the default location that players are positioned
157/// at if they have no waypoint. Players *should* always have a waypoint, so
158/// this should basically never be used in practice.
159#[derive(Copy, Clone)]
160pub struct SpawnPoint(pub Vec3<f32>);
161
162impl Default for SpawnPoint {
163    fn default() -> Self { Self(Vec3::new(0.0, 0.0, 256.0)) }
164}
165
166// This is the minimum chunk range that is kept loaded around each player
167// server-side. This is independent of the client's view distance and exists to
168// avoid exploits such as small view distance chunk reloading and also to keep
169// various mechanics working fluidly (i.e: not unloading nearby entities).
170pub const MIN_VD: u32 = 6;
171
172// Tick count used for throttling network updates
173// Note this doesn't account for dt (so update rate changes with tick rate)
174#[derive(Copy, Clone, Default)]
175pub struct Tick(u64);
176
177#[derive(Clone)]
178pub struct HwStats {
179    hardware_threads: u32,
180    rayon_threads: u32,
181}
182
183#[derive(Clone, Copy, PartialEq)]
184enum DisconnectType {
185    WithPersistence,
186    WithoutPersistence,
187}
188
189// Start of Tick, used for metrics
190#[derive(Copy, Clone)]
191pub struct TickStart(Instant);
192
193/// Store of BattleMode cooldowns for players while they go offline
194#[derive(Clone, Default, Debug)]
195pub struct BattleModeBuffer {
196    map: HashMap<CharacterId, (BattleMode, Time)>,
197}
198
199impl BattleModeBuffer {
200    pub fn push(&mut self, char_id: CharacterId, save: (BattleMode, Time)) {
201        self.map.insert(char_id, save);
202    }
203
204    pub fn get(&self, char_id: &CharacterId) -> Option<&(BattleMode, Time)> {
205        self.map.get(char_id)
206    }
207
208    pub fn pop(&mut self, char_id: &CharacterId) -> Option<(BattleMode, Time)> {
209        self.map.remove(char_id)
210    }
211}
212
213/// Keeps the IPs of recently logged off clients in memory, only used
214/// for IP bans if the target is no longer online.
215pub struct RecentClientIPs {
216    pub last_addrs: schnellru::LruMap<Uuid, NormalizedIpAddr>,
217}
218
219impl Default for RecentClientIPs {
220    fn default() -> Self {
221        Self {
222            last_addrs: schnellru::LruMap::new(schnellru::ByLength::new(1000)),
223        }
224    }
225}
226
227pub struct ChunkRequest {
228    entity: EcsEntity,
229    key: Vec2<i32>,
230}
231
232#[derive(Debug)]
233pub enum ServerInitStage {
234    DbMigrations,
235    DbVacuum,
236    WorldGen(WorldGenerateStage),
237    StartingSystems,
238}
239
240pub struct Server {
241    state: State,
242    world: Arc<World>,
243    index: IndexOwned,
244
245    connection_handler: ConnectionHandler,
246
247    runtime: Arc<Runtime>,
248
249    metrics_registry: Arc<Registry>,
250    chat_cache: ChatCache,
251    database_settings: Arc<RwLock<DatabaseSettings>>,
252    disconnect_all_clients_requested: bool,
253
254    event_dispatcher: SendDispatcher<'static>,
255}
256
257impl Server {
258    /// Create a new `Server`
259    pub fn new(
260        settings: Settings,
261        editable_settings: EditableSettings,
262        database_settings: DatabaseSettings,
263        data_dir: &std::path::Path,
264        report_stage: &(dyn Fn(ServerInitStage) + Send + Sync),
265        runtime: Arc<Runtime>,
266    ) -> Result<Self, Error> {
267        prof_span!("Server::new");
268        info!("Server data dir is: {}", data_dir.display());
269        if settings.auth_server_address.is_none() {
270            info!("Authentication is disabled");
271        }
272
273        report_stage(ServerInitStage::DbMigrations);
274        // Run pending DB migrations (if any)
275        debug!("Running DB migrations...");
276        persistence::run_migrations(&database_settings);
277
278        report_stage(ServerInitStage::DbVacuum);
279        // Vacuum database
280        debug!("Vacuuming database...");
281        persistence::vacuum_database(&database_settings);
282
283        let database_settings = Arc::new(RwLock::new(database_settings));
284
285        let registry = Arc::new(Registry::new());
286        let chunk_gen_metrics = metrics::ChunkGenMetrics::new(&registry).unwrap();
287        let job_metrics = metrics::JobMetrics::new(&registry).unwrap();
288        let network_request_metrics = metrics::NetworkRequestMetrics::new(&registry).unwrap();
289        let player_metrics = metrics::PlayerMetrics::new(&registry).unwrap();
290        let ecs_system_metrics = EcsSystemMetrics::new(&registry).unwrap();
291        let tick_metrics = TickMetrics::new(&registry).unwrap();
292        let physics_metrics = PhysicsMetrics::new(&registry).unwrap();
293        let server_event_metrics = metrics::ServerEventMetrics::new(&registry).unwrap();
294        let gameplay_metrics = GameplayMetrics::new(&registry).unwrap();
295        let query_server_metrics = metrics::QueryServerMetrics::new(&registry).unwrap();
296
297        let battlemode_buffer = BattleModeBuffer::default();
298
299        let pools = State::pools(GameMode::Server);
300
301        // Load plugins before generating the world.
302        #[cfg(feature = "plugins")]
303        let plugin_mgr = PluginMgr::from_asset_or_default();
304
305        debug!("Generating world, seed: {}", settings.world_seed);
306        #[cfg(feature = "worldgen")]
307        let (world, index) = World::generate(
308            settings.world_seed,
309            WorldOpts {
310                seed_elements: true,
311                world_file: if let Some(ref opts) = settings.map_file {
312                    opts.clone()
313                } else {
314                    // Load default map from assets.
315                    FileOpts::LoadAsset(DEFAULT_WORLD_MAP.into())
316                },
317                calendar: Some(settings.calendar_mode.calendar_now()),
318            },
319            &pools,
320            &|stage| {
321                report_stage(ServerInitStage::WorldGen(stage));
322            },
323        );
324        #[cfg(not(feature = "worldgen"))]
325        let (world, index) = World::generate(settings.world_seed);
326
327        #[cfg(feature = "worldgen")]
328        let map = world.get_map_data(index.as_index_ref(), &pools);
329        #[cfg(not(feature = "worldgen"))]
330        let map = common_net::msg::WorldMapMsg {
331            dimensions_lg: Vec2::zero(),
332            max_height: 1.0,
333            rgba: Grid::new(Vec2::new(1, 1), 1),
334            horizons: [(vec![0], vec![0]), (vec![0], vec![0])],
335            alt: Grid::new(Vec2::new(1, 1), 1),
336            sites: Vec::new(),
337            possible_starting_sites: Vec::new(),
338            pois: Vec::new(),
339            default_chunk: Arc::new(world.generate_oob_chunk()),
340        };
341
342        #[cfg(feature = "worldgen")]
343        let map_size_lg = world.sim().map_size_lg();
344        #[cfg(not(feature = "worldgen"))]
345        let map_size_lg = world.map_size_lg();
346
347        let lod = lod::Lod::from_world(&world, index.as_index_ref(), &pools);
348
349        report_stage(ServerInitStage::StartingSystems);
350
351        let mut state = State::server(
352            Arc::clone(&pools),
353            map_size_lg,
354            Arc::clone(&map.default_chunk),
355            |dispatcher_builder| {
356                add_local_systems(dispatcher_builder);
357                sys::msg::add_server_systems(dispatcher_builder);
358                sys::add_server_systems(dispatcher_builder);
359                #[cfg(feature = "worldgen")]
360                {
361                    rtsim::add_server_systems(dispatcher_builder);
362                    weather::add_server_systems(dispatcher_builder);
363                }
364            },
365            #[cfg(feature = "plugins")]
366            plugin_mgr,
367        );
368        events::register_event_busses(state.ecs_mut());
369        state.ecs_mut().insert(battlemode_buffer);
370        state.ecs_mut().insert(RecentClientIPs::default());
371        state.ecs_mut().insert(settings.clone());
372        state.ecs_mut().insert(editable_settings);
373        state.ecs_mut().insert(DataDir {
374            path: data_dir.to_owned(),
375        });
376
377        state.ecs_mut().insert(Vec::<ChunkRequest>::new());
378        state
379            .ecs_mut()
380            .insert(EventBus::<chunk_serialize::ChunkSendEntry>::default());
381        state.ecs_mut().insert(Locations::default());
382        state.ecs_mut().insert(LoginProvider::new(
383            settings.auth_server_address.clone(),
384            Arc::clone(&runtime),
385        ));
386        state.ecs_mut().insert(HwStats {
387            hardware_threads: num_cpus::get() as u32,
388            rayon_threads: num_cpus::get() as u32,
389        });
390        state.ecs_mut().insert(ServerConstants {
391            day_cycle_coefficient: settings.day_cycle_coefficient(),
392        });
393        state.ecs_mut().insert(Tick(0));
394        state.ecs_mut().insert(TickStart(Instant::now()));
395        state.ecs_mut().insert(job_metrics);
396        state.ecs_mut().insert(network_request_metrics);
397        state.ecs_mut().insert(player_metrics);
398        state.ecs_mut().insert(ecs_system_metrics);
399        state.ecs_mut().insert(tick_metrics);
400        state.ecs_mut().insert(physics_metrics);
401        state.ecs_mut().insert(server_event_metrics);
402        state.ecs_mut().insert(gameplay_metrics);
403        state.ecs_mut().insert(query_server_metrics);
404        if settings.experimental_terrain_persistence {
405            #[cfg(feature = "persistent_world")]
406            {
407                warn!(
408                    "Experimental terrain persistence support is enabled. This feature may break, \
409                     be disabled, or otherwise change under your feet at *any time*. \
410                     Additionally, it is expected to be replaced in the future *without* \
411                     migration or warning. You have been warned."
412                );
413                state
414                    .ecs_mut()
415                    .insert(TerrainPersistence::new(data_dir.to_owned()));
416            }
417            #[cfg(not(feature = "persistent_world"))]
418            error!(
419                "Experimental terrain persistence support was requested, but the server was not \
420                 compiled with the feature. Terrain modifications will *not* be persisted."
421            );
422        }
423        {
424            let pool = state.ecs_mut().write_resource::<SlowJobPool>();
425            pool.configure("CHUNK_DROP", |_n| 1);
426            pool.configure("CHUNK_GENERATOR", |n| n / 2 + n / 4);
427            pool.configure("CHUNK_SERIALIZER", |n| n / 2);
428            pool.configure("RTSIM_SAVE", |_| 1);
429            pool.configure("WEATHER", |_| 1);
430        }
431        state
432            .ecs_mut()
433            .insert(ChunkGenerator::new(chunk_gen_metrics));
434        {
435            let (sender, receiver) =
436                crossbeam_channel::bounded::<chunk_serialize::SerializedChunk>(10_000);
437            state.ecs_mut().insert(sender);
438            state.ecs_mut().insert(receiver);
439        }
440
441        state.ecs_mut().insert(CharacterUpdater::new(
442            Arc::<RwLock<DatabaseSettings>>::clone(&database_settings),
443        )?);
444
445        let ability_map = comp::item::tool::AbilityMap::<comp::AbilityItem>::load_expect_cloned(
446            "common.abilities.ability_set_manifest",
447        );
448        state.ecs_mut().insert(ability_map);
449
450        let msm = comp::inventory::item::MaterialStatManifest::load().cloned();
451        state.ecs_mut().insert(msm);
452
453        let rbm = common::recipe::RecipeBookManifest::load().cloned();
454        state.ecs_mut().insert(rbm);
455
456        state.ecs_mut().insert(CharacterLoader::new(
457            Arc::<RwLock<DatabaseSettings>>::clone(&database_settings),
458        )?);
459
460        // System schedulers to control execution of systems
461        state
462            .ecs_mut()
463            .insert(sys::PersistenceScheduler::every(Duration::from_secs(10)));
464
465        // Region map (spatial structure for entity synchronization)
466        state.ecs_mut().insert(RegionMap::new());
467
468        // Server-only components
469        state.ecs_mut().register::<RegionSubscription>();
470        state.ecs_mut().register::<Client>();
471        state.ecs_mut().register::<comp::Presence>();
472        state.ecs_mut().register::<wiring::WiringElement>();
473        state.ecs_mut().register::<wiring::Circuit>();
474        state.ecs_mut().register::<Anchor>();
475        state.ecs_mut().register::<comp::Pet>();
476        state.ecs_mut().register::<login_provider::PendingLogin>();
477        state.ecs_mut().register::<RepositionToFreeSpace>();
478        state.ecs_mut().register::<RtSimEntity>();
479
480        // Load banned words list
481        let banned_words = settings.moderation.load_banned_words(data_dir);
482        let censor = Arc::new(Censor::Custom(banned_words.into_iter().collect()));
483        state.ecs_mut().insert(Arc::clone(&censor));
484
485        // Init automod
486        state
487            .ecs_mut()
488            .insert(AutoMod::new(&settings.moderation, censor));
489
490        state.ecs_mut().insert(map);
491
492        #[cfg(feature = "worldgen")]
493        let spawn_point = SpawnPoint({
494            let index = index.as_index_ref();
495            // NOTE: all of these `.map(|e| e as [type])` calls should compile into no-ops,
496            // but are needed to be explicit about casting (and to make the compiler stop
497            // complaining)
498
499            // Search for town defined by spawn_town server setting. If this fails, or is
500            // None, set spawn to the nearest town to the centre of the world
501            let center_chunk = world.sim().map_size_lg().chunks().map(i32::from) / 2;
502            let spawn_chunk = world
503                .civs()
504                .sites()
505                .filter(|site| site.is_settlement())
506                .map(|site| site.center)
507                .min_by_key(|site_pos| site_pos.distance_squared(center_chunk))
508                .unwrap_or(center_chunk);
509
510            world.find_accessible_pos(index, TerrainChunkSize::center_wpos(spawn_chunk), false)
511        });
512        #[cfg(not(feature = "worldgen"))]
513        let spawn_point = SpawnPoint::default();
514
515        // Set the spawn point we calculated above
516        state.ecs_mut().insert(spawn_point);
517
518        // Insert a default AABB for the world
519        // TODO: prevent this from being deleted
520        {
521            #[cfg(feature = "worldgen")]
522            let size = world.sim().get_size();
523            #[cfg(not(feature = "worldgen"))]
524            let size = world.map_size_lg().chunks().map(u32::from);
525
526            let world_size = size.map(|e| e as i32) * TerrainChunk::RECT_SIZE.map(|e| e as i32);
527            let world_aabb = Aabb {
528                min: Vec3::new(0, 0, -32768),
529                max: Vec3::new(world_size.x, world_size.y, 32767),
530            }
531            .made_valid();
532
533            state
534                .ecs()
535                .write_resource::<AreasContainer<BuildArea>>()
536                .insert("world".to_string(), world_aabb)
537                .expect("The initial insert should always work.");
538        }
539
540        // Insert the world into the ECS (todo: Maybe not an Arc?)
541        let world = Arc::new(world);
542        state.ecs_mut().insert(Arc::clone(&world));
543        state.ecs_mut().insert(lod);
544        state.ecs_mut().insert(index.clone());
545
546        // Set starting time for the server.
547        state.ecs_mut().write_resource::<TimeOfDay>().0 = settings.world.start_time;
548
549        // Register trackers
550        sys::sentinel::UpdateTrackers::register(state.ecs_mut());
551
552        state.ecs_mut().insert(DeletedEntities::default());
553
554        let network = Network::new_with_registry(Pid::new(), &runtime, &registry);
555        let (chat_cache, chat_tracker) = ChatCache::new(Duration::from_secs(60), &runtime);
556        state.ecs_mut().insert(chat_tracker);
557
558        let mut printed_quic_warning = false;
559        for protocol in &settings.gameserver_protocols {
560            match protocol {
561                Protocol::Tcp { address } => {
562                    runtime.block_on(network.listen(ListenAddr::Tcp(*address)))?;
563                },
564                Protocol::Quic {
565                    address,
566                    cert_file_path,
567                    key_file_path,
568                } => {
569                    use rustls_pemfile::Item;
570                    use std::fs;
571
572                    match || -> Result<_, Box<dyn std::error::Error>> {
573                        let key = fs::read(key_file_path)?;
574                        let key = if key_file_path.extension().is_some_and(|x| x == "der") {
575                            PrivateKeyDer::try_from(key).map_err(|_| "No valid pem key in file")?
576                        } else {
577                            debug!("convert pem key to der");
578                            rustls_pemfile::read_all(&mut key.as_slice())
579                                .find_map(|item| match item {
580                                    Ok(Item::Pkcs1Key(v)) => Some(PrivateKeyDer::Pkcs1(v)),
581                                    Ok(Item::Pkcs8Key(v)) => Some(PrivateKeyDer::Pkcs8(v)),
582                                    Ok(Item::Sec1Key(v)) => Some(PrivateKeyDer::Sec1(v)),
583                                    Ok(Item::Crl(_)) => None,
584                                    Ok(Item::Csr(_)) => None,
585                                    Ok(Item::X509Certificate(_)) => None,
586                                    Ok(_) => None,
587                                    Err(e) => {
588                                        tracing::warn!(?e, "error while reading key_file");
589                                        None
590                                    },
591                                })
592                                .ok_or("No valid pem key in file")?
593                        };
594                        let cert_chain = fs::read(cert_file_path)?;
595                        let cert_chain = if cert_file_path.extension().is_some_and(|x| x == "der") {
596                            vec![CertificateDer::from(cert_chain)]
597                        } else {
598                            debug!("convert pem cert to der");
599                            rustls_pemfile::certs(&mut cert_chain.as_slice())
600                                .filter_map(|item| match item {
601                                    Ok(cert) => Some(cert),
602                                    Err(e) => {
603                                        tracing::warn!(?e, "error while reading cert_file");
604                                        None
605                                    },
606                                })
607                                .collect()
608                        };
609                        let server_config = quinn::ServerConfig::with_single_cert(cert_chain, key)?;
610                        Ok(server_config)
611                    }() {
612                        Ok(server_config) => {
613                            runtime.block_on(
614                                network.listen(ListenAddr::Quic(*address, server_config.clone())),
615                            )?;
616
617                            if !printed_quic_warning {
618                                warn!(
619                                    "QUIC is enabled. This is experimental and not recommended in \
620                                     production"
621                                );
622                                printed_quic_warning = true;
623                            }
624                        },
625                        Err(e) => {
626                            error!(
627                                ?e,
628                                "Failed to load the TLS certificate, running without QUIC {}",
629                                *address
630                            );
631                        },
632                    }
633                },
634            }
635        }
636
637        if let Some(addr) = settings.query_address {
638            use veloren_query_server::proto::ServerInfo;
639
640            const QUERY_SERVER_RATELIMIT: u16 = 120;
641
642            let (query_server_info_tx, query_server_info_rx) =
643                tokio::sync::watch::channel(ServerInfo {
644                    git_hash: *common::util::GIT_HASH,
645                    git_timestamp: *common::util::GIT_TIMESTAMP,
646                    players_count: 0,
647                    player_cap: settings.max_players,
648                    battlemode: settings.gameplay.battle_mode.into(),
649                });
650            let mut query_server =
651                QueryServer::new(addr, query_server_info_rx, QUERY_SERVER_RATELIMIT);
652            let query_server_metrics =
653                Arc::new(Mutex::new(veloren_query_server::server::Metrics::default()));
654            let query_server_metrics2 = Arc::clone(&query_server_metrics);
655            runtime.spawn(async move {
656                let err = query_server.run(query_server_metrics2).await.err();
657                error!(?err, "Query server stopped unexpectedly");
658            });
659            state.ecs_mut().insert(query_server_info_tx);
660            state.ecs_mut().insert(query_server_metrics);
661        }
662
663        runtime.block_on(network.listen(ListenAddr::Mpsc(14004)))?;
664
665        let connection_handler = ConnectionHandler::new(network, &runtime);
666
667        // Init rtsim, loading it from disk if possible
668        #[cfg(feature = "worldgen")]
669        {
670            match rtsim::RtSim::new(
671                &settings.world,
672                index.as_index_ref(),
673                &world,
674                data_dir.to_owned(),
675            ) {
676                Ok(rtsim) => {
677                    state.ecs_mut().insert(rtsim.state().data().time_of_day);
678                    state.ecs_mut().insert(rtsim);
679                },
680                Err(err) => {
681                    error!("Failed to load rtsim: {}", err);
682                    return Err(Error::RtsimError(err));
683                },
684            }
685            weather::init(&mut state);
686        }
687
688        let this = Self {
689            state,
690            world,
691            index,
692            connection_handler,
693            runtime,
694
695            metrics_registry: registry,
696            chat_cache,
697            database_settings,
698            disconnect_all_clients_requested: false,
699
700            event_dispatcher: Self::create_event_dispatcher(pools),
701        };
702
703        debug!(?settings, "created veloren server with");
704
705        info!("Server version: {}", *common::util::DISPLAY_VERSION);
706
707        Ok(this)
708    }
709
710    pub fn get_server_info(&self) -> ServerInfo {
711        let settings = self.state.ecs().fetch::<Settings>();
712
713        ServerInfo {
714            name: settings.server_name.clone(),
715            git_hash: *common::util::GIT_HASH,
716            git_timestamp: *common::util::GIT_TIMESTAMP,
717            auth_provider: settings.auth_server_address.clone(),
718        }
719    }
720
721    /// Get a reference to the server's settings
722    pub fn settings(&self) -> impl Deref<Target = Settings> + '_ {
723        self.state.ecs().fetch::<Settings>()
724    }
725
726    /// Get a mutable reference to the server's settings
727    pub fn settings_mut(&self) -> impl DerefMut<Target = Settings> + '_ {
728        self.state.ecs().fetch_mut::<Settings>()
729    }
730
731    /// Get a mutable reference to the server's editable settings
732    pub fn editable_settings_mut(&self) -> impl DerefMut<Target = EditableSettings> + '_ {
733        self.state.ecs().fetch_mut::<EditableSettings>()
734    }
735
736    /// Get a reference to the server's editable settings
737    pub fn editable_settings(&self) -> impl Deref<Target = EditableSettings> + '_ {
738        self.state.ecs().fetch::<EditableSettings>()
739    }
740
741    /// Get path to the directory that the server info into
742    pub fn data_dir(&self) -> impl Deref<Target = DataDir> + '_ {
743        self.state.ecs().fetch::<DataDir>()
744    }
745
746    /// Get a reference to the server's game state.
747    pub fn state(&self) -> &State { &self.state }
748
749    /// Get a mutable reference to the server's game state.
750    pub fn state_mut(&mut self) -> &mut State { &mut self.state }
751
752    /// Get a reference to the server's world.
753    pub fn world(&self) -> &World { &self.world }
754
755    /// Get a reference to the Metrics Registry
756    pub fn metrics_registry(&self) -> &Arc<Registry> { &self.metrics_registry }
757
758    /// Get a reference to the Chat Cache
759    pub fn chat_cache(&self) -> &ChatCache { &self.chat_cache }
760
761    fn parse_locations(&self, character_list_data: &mut [CharacterItem]) {
762        character_list_data.iter_mut().for_each(|c| {
763            let name = c
764                .location
765                .as_ref()
766                .and_then(|s| {
767                    persistence::parse_waypoint(s)
768                        .ok()
769                        .and_then(|(waypoint, _)| waypoint.map(|w| w.get_pos()))
770                })
771                .and_then(|wpos| {
772                    self.world
773                        .get_location_name(self.index.as_index_ref(), wpos.xy().as_::<i32>())
774                });
775            c.location = name;
776        });
777    }
778
779    /// Execute a single server tick, handle input and update the game state by
780    /// the given duration.
781    pub fn tick(&mut self, _input: Input, dt: Duration) -> Result<Vec<Event>, Error> {
782        self.state.ecs().write_resource::<Tick>().0 += 1;
783        self.state.ecs().write_resource::<TickStart>().0 = Instant::now();
784
785        // Update calendar events as time changes
786        // TODO: If a lot of calendar events get added, this might become expensive.
787        // Maybe don't do this every tick?
788        let new_calendar = self
789            .state
790            .ecs()
791            .read_resource::<Settings>()
792            .calendar_mode
793            .calendar_now();
794        *self.state.ecs_mut().write_resource::<Calendar>() = new_calendar;
795
796        // This tick function is the centre of the Veloren universe. Most server-side
797        // things are managed from here, and as such it's important that it
798        // stays organised. Please consult the core developers before making
799        // significant changes to this code. Here is the approximate order of
800        // things. Please update it as this code changes.
801        //
802        // 1) Collect input from the frontend, apply input effects to the state of the
803        //    game
804        // 2) Go through any events (timer-driven or otherwise) that need handling and
805        //    apply them to the state of the game
806        // 3) Go through all incoming client network communications, apply them to the
807        //    game state
808        // 4) Perform a single LocalState tick (i.e: update the world and entities in
809        //    the world)
810        // 5) Go through the terrain update queue and apply all changes to the terrain
811        // 6) Send relevant state updates to all clients
812        // 7) Check for persistence updates related to character data, and message the
813        //    relevant entities
814        // 8) Update Metrics with current data
815        // 9) Finish the tick, passing control of the main thread back to the frontend
816
817        // 1) Build up a list of events for this frame, to be passed to the frontend.
818        let mut frontend_events = Vec::new();
819
820        // 2)
821
822        let before_new_connections = Instant::now();
823
824        // 3) Handle inputs from clients
825        self.handle_new_connections(&mut frontend_events);
826
827        let before_state_tick = Instant::now();
828
829        fn on_block_update(ecs: &specs::World, changes: Vec<BlockDiff>) {
830            // When a resource block updates, inform rtsim
831            if changes
832                .iter()
833                .any(|c| c.old.get_rtsim_resource() != c.new.get_rtsim_resource())
834            {
835                ecs.write_resource::<rtsim::RtSim>().hook_block_update(
836                    &ecs.read_resource::<Arc<world::World>>(),
837                    ecs.read_resource::<world::IndexOwned>().as_index_ref(),
838                    changes,
839                );
840            }
841        }
842
843        // 4) Tick the server's LocalState.
844        // 5) Fetch any generated `TerrainChunk`s and insert them into the terrain.
845        // in sys/terrain.rs
846        let mut state_tick_metrics = Default::default();
847        let server_constants = (*self.state.ecs().read_resource::<ServerConstants>()).clone();
848        self.state.tick(
849            dt,
850            false,
851            Some(&mut state_tick_metrics),
852            &server_constants,
853            on_block_update,
854        );
855
856        let before_handle_events = Instant::now();
857
858        // Process any pending request to disconnect all clients, the disconnections
859        // will be processed once handle_events() is called below
860        let disconnect_type = self.disconnect_all_clients_if_requested();
861
862        // Handle entity links (such as mounting)
863        self.state.maintain_links();
864
865        // Handle game events
866        frontend_events.append(&mut self.handle_events());
867
868        let before_update_terrain_and_regions = Instant::now();
869
870        // Apply terrain changes and update the region map after processing server
871        // events so that changes made by server events will be immediately
872        // visible to client synchronization systems, minimizing the latency of
873        // `ServerEvent` mediated effects
874        self.update_region_map();
875        // NOTE: apply_terrain_changes sends the *new* value since it is not being
876        // synchronized during the tick.
877        self.state.apply_terrain_changes(on_block_update);
878
879        let before_sync = Instant::now();
880
881        // 6) Synchronise clients with the new state of the world.
882        sys::run_sync_systems(self.state.ecs_mut());
883
884        let before_world_tick = Instant::now();
885
886        // Tick the world
887        self.world.tick(dt);
888
889        let before_entity_cleanup = Instant::now();
890
891        // In the event of a request to disconnect all players without persistence, we
892        // must run the terrain system a second time after the messages to
893        // perform client disconnections have been processed. This ensures that any
894        // items on the ground are deleted.
895        if let Some(DisconnectType::WithoutPersistence) = disconnect_type {
896            run_now::<terrain::Sys>(self.state.ecs_mut());
897        }
898
899        // Hook rtsim chunk unloads
900        #[cfg(feature = "worldgen")]
901        {
902            let mut rtsim = self.state.ecs().write_resource::<rtsim::RtSim>();
903            let world = self.state.ecs().read_resource::<Arc<World>>();
904            for chunk in &self.state.terrain_changes().removed_chunks {
905                rtsim.hook_unload_chunk(*chunk, &world);
906            }
907        }
908
909        // Prevent anchor entity chains which are not currently supported due to:
910        // * potential cycles?
911        // * unloading a chain could occur across an unbounded number of ticks with the
912        //   current implementation.
913        // * in particular, we want to be able to unload all entities in a
914        //   limited number of ticks when a database error occurs and kicks all
915        //   players (not quiet sure on exact time frame, since it already
916        //   takes a tick after unloading all chunks for entities to despawn?),
917        //   see this thread and the discussion linked from there:
918        //   https://gitlab.com/veloren/veloren/-/merge_requests/2668#note_634913847
919        let anchors = self.state.ecs().read_storage::<Anchor>();
920        let anchored_anchor_entities: Vec<Entity> = (
921            &self.state.ecs().entities(),
922            &self.state.ecs().read_storage::<Anchor>(),
923        )
924            .join()
925            .filter_map(|(_, anchor)| match anchor {
926                Anchor::Entity(anchor_entity) => Some(*anchor_entity),
927                _ => None,
928            })
929            // We allow Anchor::Entity(_) -> Anchor::Chunk(_) connections, since they can't chain further.
930            //
931            // NOTE: The entity with `Anchor::Entity` will unload one tick after the entity with `Anchor::Chunk`.
932            .filter(|anchor_entity| match anchors.get(*anchor_entity) {
933                Some(Anchor::Entity(_)) => true,
934                Some(Anchor::Chunk(_)) | None => false
935            })
936            .collect();
937        drop(anchors);
938
939        for entity in anchored_anchor_entities {
940            if cfg!(debug_assertions) {
941                panic!("Entity anchor chain detected");
942            }
943            error!(
944                "Detected an anchor entity that itself has an anchor entity - anchor chains are \
945                 not currently supported. The entity's Anchor component has been deleted"
946            );
947            self.state.delete_component::<Anchor>(entity);
948        }
949
950        // Remove NPCs that are outside the view distances of all players
951        // This is done by removing NPCs in unloaded chunks
952        let to_delete = {
953            let terrain = self.state.terrain();
954            (
955                &self.state.ecs().entities(),
956                &self.state.ecs().read_storage::<comp::Pos>(),
957                !&self.state.ecs().read_storage::<comp::Presence>(),
958                self.state.ecs().read_storage::<Anchor>().maybe(),
959                self.state.ecs().read_storage::<Is<VolumeRider>>().maybe(),
960            )
961                .join()
962                .filter(|(_, pos, _, anchor, is_volume_rider)| {
963                    let pos = is_volume_rider
964                        .and_then(|is_volume_rider| match is_volume_rider.pos.kind {
965                            Volume::Terrain => None,
966                            Volume::Entity(e) => {
967                                let e = self.state.ecs().entity_from_uid(e)?;
968                                let pos = self
969                                    .state
970                                    .ecs()
971                                    .read_storage::<comp::Pos>()
972                                    .get(e)
973                                    .copied()?;
974
975                                Some(pos.0)
976                            },
977                        })
978                        .unwrap_or(pos.0);
979                    let chunk_key = terrain.pos_key(pos.map(|e| e.floor() as i32));
980                    match anchor {
981                        Some(Anchor::Chunk(hc)) => {
982                            // Check if both this chunk and the NPCs `home_chunk` is unloaded. If
983                            // so, we delete them. We check for
984                            // `home_chunk` in order to avoid duplicating
985                            // the entity under some circumstances.
986                            terrain.get_key_real(chunk_key).is_none()
987                                && terrain.get_key_real(*hc).is_none()
988                        },
989                        Some(Anchor::Entity(entity)) => !self.state.ecs().is_alive(*entity),
990                        None => terrain.get_key_real(chunk_key).is_none(),
991                    }
992                })
993                .map(|(entity, _, _, _, _)| entity)
994                .collect::<Vec<_>>()
995        };
996
997        #[cfg(feature = "worldgen")]
998        {
999            let mut rtsim = self.state.ecs().write_resource::<rtsim::RtSim>();
1000            let rtsim_entities = self.state.ecs().read_storage();
1001            for entity in &to_delete {
1002                if let Some(rtsim_entity) = rtsim_entities.get(*entity) {
1003                    rtsim.hook_rtsim_entity_unload(*rtsim_entity);
1004                }
1005            }
1006        }
1007
1008        // Actually perform entity deletion
1009        for entity in to_delete {
1010            if let Err(e) = self.state.delete_entity_recorded(entity) {
1011                error!(?e, "Failed to delete agent outside the terrain");
1012            }
1013        }
1014
1015        if let Some(DisconnectType::WithoutPersistence) = disconnect_type {
1016            info!(
1017                "Disconnection of all players without persistence complete, signalling to \
1018                 persistence thread that character updates may continue to be processed"
1019            );
1020            self.state
1021                .ecs()
1022                .fetch_mut::<CharacterUpdater>()
1023                .disconnected_success();
1024        }
1025
1026        // 7 Persistence updates
1027        let before_persistence_updates = Instant::now();
1028
1029        let character_loader = self.state.ecs().read_resource::<CharacterLoader>();
1030
1031        let mut character_updater = self.state.ecs().write_resource::<CharacterUpdater>();
1032        let updater_messages: Vec<CharacterUpdaterMessage> = character_updater.messages().collect();
1033
1034        // Get character-related database responses and notify the requesting client
1035        character_loader
1036            .messages()
1037            .chain(updater_messages)
1038            .for_each(|message| match message {
1039                CharacterUpdaterMessage::DatabaseBatchCompletion(batch_id) => {
1040                    character_updater.process_batch_completion(batch_id);
1041                },
1042                CharacterUpdaterMessage::CharacterScreenResponse(response) => {
1043                    match response.response_kind {
1044                        CharacterScreenResponseKind::CharacterList(result) => match result {
1045                            Ok(mut character_list_data) => {
1046                                self.parse_locations(&mut character_list_data);
1047                                self.notify_client(
1048                                    response.target_entity,
1049                                    ServerGeneral::CharacterListUpdate(character_list_data),
1050                                )
1051                            },
1052                            Err(error) => self.notify_client(
1053                                response.target_entity,
1054                                ServerGeneral::CharacterActionError(error.to_string()),
1055                            ),
1056                        },
1057                        CharacterScreenResponseKind::CharacterCreation(result) => match result {
1058                            Ok((character_id, mut list)) => {
1059                                self.parse_locations(&mut list);
1060                                self.notify_client(
1061                                    response.target_entity,
1062                                    ServerGeneral::CharacterListUpdate(list),
1063                                );
1064                                self.notify_client(
1065                                    response.target_entity,
1066                                    ServerGeneral::CharacterCreated(character_id),
1067                                );
1068                            },
1069                            Err(error) => self.notify_client(
1070                                response.target_entity,
1071                                ServerGeneral::CharacterActionError(error.to_string()),
1072                            ),
1073                        },
1074                        CharacterScreenResponseKind::CharacterEdit(result) => match result {
1075                            Ok((character_id, mut list)) => {
1076                                self.parse_locations(&mut list);
1077                                self.notify_client(
1078                                    response.target_entity,
1079                                    ServerGeneral::CharacterListUpdate(list),
1080                                );
1081                                self.notify_client(
1082                                    response.target_entity,
1083                                    ServerGeneral::CharacterEdited(character_id),
1084                                );
1085                            },
1086                            Err(error) => self.notify_client(
1087                                response.target_entity,
1088                                ServerGeneral::CharacterActionError(error.to_string()),
1089                            ),
1090                        },
1091                        CharacterScreenResponseKind::CharacterData(result) => {
1092                            match *result {
1093                                Ok((character_data, skill_set_persistence_load_error)) => {
1094                                    let PersistedComponents {
1095                                        body,
1096                                        hardcore,
1097                                        stats,
1098                                        skill_set,
1099                                        inventory,
1100                                        waypoint,
1101                                        pets,
1102                                        active_abilities,
1103                                        map_marker,
1104                                    } = character_data;
1105                                    let character_data = (
1106                                        body,
1107                                        hardcore,
1108                                        stats,
1109                                        skill_set,
1110                                        inventory,
1111                                        waypoint,
1112                                        pets,
1113                                        active_abilities,
1114                                        map_marker,
1115                                    );
1116                                    // TODO: Does this need to be a server event? E.g. we could
1117                                    // just handle it here.
1118                                    self.state.emit_event_now(UpdateCharacterDataEvent {
1119                                        entity: response.target_entity,
1120                                        components: character_data,
1121                                        metadata: skill_set_persistence_load_error,
1122                                    })
1123                                },
1124                                Err(error) => {
1125                                    // We failed to load data for the character from the DB. Notify
1126                                    // the client to push the state back to character selection,
1127                                    // with the error to display
1128                                    self.notify_client(
1129                                        response.target_entity,
1130                                        ServerGeneral::CharacterDataLoadResult(Err(
1131                                            error.to_string()
1132                                        )),
1133                                    );
1134
1135                                    // Clean up the entity data on the server
1136                                    self.state.emit_event_now(ExitIngameEvent {
1137                                        entity: response.target_entity,
1138                                    })
1139                                },
1140                            }
1141                        },
1142                    }
1143                },
1144            });
1145
1146        drop(character_loader);
1147        drop(character_updater);
1148
1149        {
1150            // Check for new chunks; cancel and regenerate all chunks if the asset has been
1151            // reloaded. Note that all of these assignments are no-ops, so the
1152            // only work we do here on the fast path is perform a relaxed read on an atomic.
1153            // boolean.
1154            let index = &mut self.index;
1155            let world = &mut self.world;
1156            let ecs = self.state.ecs_mut();
1157            let slow_jobs = ecs.write_resource::<SlowJobPool>();
1158
1159            index.reload_if_changed(|index| {
1160                let mut chunk_generator = ecs.write_resource::<ChunkGenerator>();
1161                let client = ecs.read_storage::<Client>();
1162                let mut terrain = ecs.write_resource::<common::terrain::TerrainGrid>();
1163                #[cfg(feature = "worldgen")]
1164                let rtsim = ecs.read_resource::<rtsim::RtSim>();
1165                #[cfg(not(feature = "worldgen"))]
1166                let rtsim = ();
1167
1168                // Cancel all pending chunks.
1169                chunk_generator.cancel_all();
1170
1171                if client.is_empty() {
1172                    // No clients, so just clear all terrain.
1173                    terrain.clear();
1174                } else {
1175                    // There's at least one client, so regenerate all chunks.
1176                    terrain.iter().for_each(|(pos, _)| {
1177                        chunk_generator.generate_chunk(
1178                            None,
1179                            pos,
1180                            &slow_jobs,
1181                            Arc::clone(world),
1182                            &rtsim,
1183                            index.clone(),
1184                            (
1185                                *ecs.read_resource::<TimeOfDay>(),
1186                                (*ecs.read_resource::<Calendar>()).clone(),
1187                            ),
1188                        );
1189                    });
1190                }
1191            });
1192        }
1193
1194        let end_of_server_tick = Instant::now();
1195
1196        // 8) Update Metrics
1197        run_now::<sys::metrics::Sys>(self.state.ecs());
1198
1199        {
1200            // Report timing info
1201            let tick_metrics = self.state.ecs().read_resource::<TickMetrics>();
1202
1203            let tt = &tick_metrics.tick_time;
1204            tt.with_label_values(&["new connections"])
1205                .set((before_state_tick - before_new_connections).as_nanos() as i64);
1206            tt.with_label_values(&["handle server events"])
1207                .set((before_update_terrain_and_regions - before_handle_events).as_nanos() as i64);
1208            tt.with_label_values(&["update terrain and region map"])
1209                .set((before_sync - before_update_terrain_and_regions).as_nanos() as i64);
1210            tt.with_label_values(&["state"])
1211                .set((before_handle_events - before_state_tick).as_nanos() as i64);
1212            tt.with_label_values(&["world tick"])
1213                .set((before_entity_cleanup - before_world_tick).as_nanos() as i64);
1214            tt.with_label_values(&["entity cleanup"])
1215                .set((before_persistence_updates - before_entity_cleanup).as_nanos() as i64);
1216            tt.with_label_values(&["persistence_updates"])
1217                .set((end_of_server_tick - before_persistence_updates).as_nanos() as i64);
1218            for (label, duration) in state_tick_metrics.timings {
1219                tick_metrics
1220                    .state_tick_time
1221                    .with_label_values(&[label])
1222                    .set(duration.as_nanos() as i64);
1223            }
1224            tick_metrics.tick_time_hist.observe(
1225                end_of_server_tick
1226                    .duration_since(before_state_tick)
1227                    .as_secs_f64(),
1228            );
1229        }
1230
1231        // 9) Finish the tick, pass control back to the frontend.
1232
1233        Ok(frontend_events)
1234    }
1235
1236    /// Clean up the server after a tick.
1237    pub fn cleanup(&mut self) {
1238        // Cleanup the local state
1239        self.state.cleanup();
1240
1241        // Maintain persisted terrain
1242        #[cfg(feature = "persistent_world")]
1243        self.state
1244            .ecs()
1245            .try_fetch_mut::<TerrainPersistence>()
1246            .map(|mut t| t.maintain());
1247    }
1248
1249    // Run RegionMap tick to update entity region occupancy
1250    fn update_region_map(&mut self) {
1251        prof_span!("Server::update_region_map");
1252        let ecs = self.state().ecs();
1253        ecs.write_resource::<RegionMap>().tick(
1254            ecs.read_storage::<comp::Pos>(),
1255            ecs.read_storage::<comp::Vel>(),
1256            ecs.read_storage::<comp::Presence>(),
1257            ecs.entities(),
1258        );
1259    }
1260
1261    fn initialize_client(&mut self, client: connection_handler::IncomingClient) -> Entity {
1262        let entity = self
1263            .state
1264            .ecs_mut()
1265            .create_entity_synced()
1266            .with(client)
1267            .build();
1268        self.state
1269            .ecs()
1270            .read_resource::<metrics::PlayerMetrics>()
1271            .clients_connected
1272            .inc();
1273        entity
1274    }
1275
1276    /// Disconnects all clients if requested by either an admin command or
1277    /// due to a persistence transaction failure and returns the processed
1278    /// DisconnectionType
1279    fn disconnect_all_clients_if_requested(&mut self) -> Option<DisconnectType> {
1280        let mut character_updater = self.state.ecs().fetch_mut::<CharacterUpdater>();
1281
1282        let disconnect_type = self.get_disconnect_all_clients_requested(&mut character_updater);
1283        if let Some(disconnect_type) = disconnect_type {
1284            let with_persistence = disconnect_type == DisconnectType::WithPersistence;
1285            let clients = self.state.ecs().read_storage::<Client>();
1286            let entities = self.state.ecs().entities();
1287
1288            info!(
1289                "Disconnecting all clients ({} persistence) as requested",
1290                if with_persistence { "with" } else { "without" }
1291            );
1292            for (_, entity) in (&clients, &entities).join() {
1293                info!("Emitting client disconnect event for entity: {:?}", entity);
1294                if with_persistence {
1295                    self.state.emit_event_now(ClientDisconnectEvent(
1296                        entity,
1297                        comp::DisconnectReason::Kicked,
1298                    ))
1299                } else {
1300                    self.state
1301                        .emit_event_now(ClientDisconnectWithoutPersistenceEvent(entity))
1302                };
1303            }
1304
1305            self.disconnect_all_clients_requested = false;
1306        }
1307
1308        disconnect_type
1309    }
1310
1311    fn get_disconnect_all_clients_requested(
1312        &self,
1313        character_updater: &mut CharacterUpdater,
1314    ) -> Option<DisconnectType> {
1315        let without_persistence_requested = character_updater.disconnect_all_clients_requested();
1316        let with_persistence_requested = self.disconnect_all_clients_requested;
1317
1318        if without_persistence_requested {
1319            return Some(DisconnectType::WithoutPersistence);
1320        };
1321        if with_persistence_requested {
1322            return Some(DisconnectType::WithPersistence);
1323        };
1324        None
1325    }
1326
1327    /// Handle new client connections.
1328    fn handle_new_connections(&mut self, frontend_events: &mut Vec<Event>) {
1329        while let Ok(sender) = self.connection_handler.info_requester_receiver.try_recv() {
1330            // can fail, e.g. due to timeout or network prob.
1331            trace!("sending info to connection_handler");
1332            let _ = sender.send(connection_handler::ServerInfoPacket {
1333                info: self.get_server_info(),
1334                time: self.state.get_time(),
1335            });
1336        }
1337
1338        while let Ok(incoming) = self.connection_handler.client_receiver.try_recv() {
1339            let entity = self.initialize_client(incoming);
1340            frontend_events.push(Event::ClientConnected { entity });
1341        }
1342    }
1343
1344    pub fn notify_client<S>(&self, entity: EcsEntity, msg: S)
1345    where
1346        S: Into<ServerMsg>,
1347    {
1348        if let Some(client) = self.state.ecs().read_storage::<Client>().get(entity) {
1349            client.send_fallible(msg);
1350        }
1351    }
1352
1353    pub fn notify_players(&mut self, msg: ServerGeneral) { self.state.notify_players(msg); }
1354
1355    fn process_command(&mut self, entity: EcsEntity, name: String, args: Vec<String>) {
1356        // Find the command object and run its handler.
1357        if let Ok(command) = name.parse::<ServerChatCommand>() {
1358            command.execute(self, entity, args);
1359        } else {
1360            #[cfg(feature = "plugins")]
1361            {
1362                let mut plugin_manager = self.state.ecs().write_resource::<PluginMgr>();
1363                let ecs_world = EcsWorld {
1364                    entities: &self.state.ecs().entities(),
1365                    health: self.state.ecs().read_component().into(),
1366                    uid: self.state.ecs().read_component().into(),
1367                    id_maps: &self.state.ecs().read_resource::<IdMaps>().into(),
1368                    player: self.state.ecs().read_component().into(),
1369                };
1370                let uid = if let Some(uid) = ecs_world.uid.get(entity).copied() {
1371                    uid
1372                } else {
1373                    self.notify_client(
1374                        entity,
1375                        ServerGeneral::server_msg(
1376                            comp::ChatType::CommandError,
1377                            common::comp::Content::Plain(
1378                                "Can't get player UUID (player may be disconnected?)".to_string(),
1379                            ),
1380                        ),
1381                    );
1382                    return;
1383                };
1384                match plugin_manager.command_event(&ecs_world, &name, args.as_slice(), uid) {
1385                    Err(common_state::plugin::CommandResults::UnknownCommand) => self
1386                        .notify_client(
1387                            entity,
1388                            ServerGeneral::server_msg(
1389                                comp::ChatType::CommandError,
1390                                common::comp::Content::Plain(format!(
1391                                    "Unknown command '/{name}'.\nType '/help' for available \
1392                                     commands",
1393                                )),
1394                            ),
1395                        ),
1396                    Ok(value) => {
1397                        self.notify_client(
1398                            entity,
1399                            ServerGeneral::server_msg(
1400                                comp::ChatType::CommandInfo,
1401                                common::comp::Content::Plain(value.join("\n")),
1402                            ),
1403                        );
1404                    },
1405                    Err(common_state::plugin::CommandResults::PluginError(err)) => {
1406                        self.notify_client(
1407                            entity,
1408                            ServerGeneral::server_msg(
1409                                comp::ChatType::CommandError,
1410                                common::comp::Content::Plain(format!(
1411                                    "Error occurred while executing command '/{name}'.\n{err}"
1412                                )),
1413                            ),
1414                        );
1415                    },
1416                    Err(common_state::plugin::CommandResults::HostError(err)) => {
1417                        error!(?err, ?name, ?args, "Can't execute command");
1418                        self.notify_client(
1419                            entity,
1420                            ServerGeneral::server_msg(
1421                                comp::ChatType::CommandError,
1422                                common::comp::Content::Plain(format!(
1423                                    "Internal error {err:?} while executing '/{name}'.\nContact \
1424                                     the server administrator",
1425                                )),
1426                            ),
1427                        );
1428                    },
1429                }
1430            }
1431        }
1432    }
1433
1434    fn entity_admin_role(&self, entity: EcsEntity) -> Option<comp::AdminRole> {
1435        self.state
1436            .read_component_copied::<comp::Admin>(entity)
1437            .map(|admin| admin.0)
1438    }
1439
1440    pub fn number_of_players(&self) -> i64 {
1441        self.state.ecs().read_storage::<Client>().join().count() as i64
1442    }
1443
1444    /// NOTE: Do *not* allow this to be called from any command that doesn't go
1445    /// through the CLI!
1446    pub fn add_admin(&mut self, username: &str, role: comp::AdminRole) {
1447        let mut editable_settings = self.editable_settings_mut();
1448        let login_provider = self.state.ecs().fetch::<LoginProvider>();
1449        let data_dir = self.data_dir();
1450        if let Some(entity) = add_admin(
1451            username,
1452            role,
1453            &login_provider,
1454            &mut editable_settings,
1455            &data_dir.path,
1456        )
1457        .and_then(|uuid| {
1458            let state = &self.state;
1459            (
1460                &state.ecs().entities(),
1461                &state.read_storage::<comp::Player>(),
1462            )
1463                .join()
1464                .find(|(_, player)| player.uuid() == uuid)
1465                .map(|(e, _)| e)
1466        }) {
1467            drop((data_dir, login_provider, editable_settings));
1468            // Add admin component if the player is ingame; if they are not, we can ignore
1469            // the write failure.
1470            self.state
1471                .write_component_ignore_entity_dead(entity, comp::Admin(role));
1472        };
1473    }
1474
1475    /// NOTE: Do *not* allow this to be called from any command that doesn't go
1476    /// through the CLI!
1477    pub fn remove_admin(&self, username: &str) {
1478        let mut editable_settings = self.editable_settings_mut();
1479        let login_provider = self.state.ecs().fetch::<LoginProvider>();
1480        let data_dir = self.data_dir();
1481        if let Some(entity) = remove_admin(
1482            username,
1483            &login_provider,
1484            &mut editable_settings,
1485            &data_dir.path,
1486        )
1487        .and_then(|uuid| {
1488            let state = &self.state;
1489            (
1490                &state.ecs().entities(),
1491                &state.read_storage::<comp::Player>(),
1492            )
1493                .join()
1494                .find(|(_, player)| player.uuid() == uuid)
1495                .map(|(e, _)| e)
1496        }) {
1497            // Remove admin component if the player is ingame
1498            self.state
1499                .ecs()
1500                .write_storage::<comp::Admin>()
1501                .remove(entity);
1502        };
1503    }
1504
1505    /// Useful for testing without a client
1506    /// view_distance: distance in chunks that are persisted, this acts like the
1507    /// player view distance so it is actually a bit farther due to a buffer
1508    /// zone
1509    #[cfg(feature = "worldgen")]
1510    pub fn create_centered_persister(&mut self, view_distance: u32) {
1511        let world_dims_chunks = self.world.sim().get_size();
1512        let world_dims_blocks = TerrainChunkSize::blocks(world_dims_chunks);
1513        // NOTE: origin is in the corner of the map
1514        // TODO: extend this function to have picking a random position or specifying a
1515        // position as options
1516        //let mut rng = rand::rng();
1517        // // Pick a random position but not to close to the edge
1518        // let rand_pos = world_dims_blocks.map(|e| e as i32).map(|e| e / 2 +
1519        // rng.random_range(-e/2..e/2 + 1));
1520        let pos = comp::Pos(Vec3::from(world_dims_blocks.map(|e| e as f32 / 2.0)));
1521        self.state
1522            .create_persister(pos, view_distance, &self.world, &self.index)
1523            .build();
1524    }
1525
1526    /// Used by benchmarking code.
1527    pub fn chunks_pending(&mut self) -> bool {
1528        self.state_mut()
1529            .mut_resource::<ChunkGenerator>()
1530            .pending_chunks()
1531            .next()
1532            .is_some()
1533    }
1534
1535    /// Sets the SQL log mode at runtime
1536    pub fn set_sql_log_mode(&mut self, sql_log_mode: SqlLogMode) {
1537        // Unwrap is safe here because we only perform a variable assignment with the
1538        // RwLock taken meaning that no panic can occur that would cause the
1539        // RwLock to become poisoned. This justification also means that calling
1540        // unwrap() on the associated read() calls for this RwLock is also safe
1541        // as long as no code that can panic is introduced here.
1542        let mut database_settings = self.database_settings.write().unwrap();
1543        database_settings.sql_log_mode = sql_log_mode;
1544        // Drop the RwLockWriteGuard to avoid performing unnecessary actions (logging)
1545        // with the lock taken.
1546        drop(database_settings);
1547        info!("SQL log mode changed to {:?}", sql_log_mode);
1548    }
1549
1550    pub fn disconnect_all_clients(&mut self) {
1551        info!("Disconnecting all clients due to local console command");
1552        self.disconnect_all_clients_requested = true;
1553    }
1554
1555    /// Sends the given client a message with their current battle mode and
1556    /// whether they can change it.
1557    ///
1558    /// This function expects the `EcsEntity` to represent a player, otherwise
1559    /// it will log an error.
1560    pub fn get_battle_mode_for(&mut self, client: EcsEntity) {
1561        let ecs = self.state.ecs();
1562        let time = ecs.read_resource::<Time>();
1563        let settings = ecs.read_resource::<Settings>();
1564        let players = ecs.read_storage::<comp::Player>();
1565        let get_player_result = players.get(client).ok_or_else(|| {
1566            error!("Can't get player component for client.");
1567
1568            Content::Plain("Can't get player component for client.".to_string())
1569        });
1570        let player = match get_player_result {
1571            Ok(player) => player,
1572            Err(content) => {
1573                self.notify_client(
1574                    client,
1575                    ServerGeneral::server_msg(ChatType::CommandError, content),
1576                );
1577                return;
1578            },
1579        };
1580
1581        let mut msg = format!("Current battle mode: {:?}.", player.battle_mode);
1582
1583        if settings.gameplay.battle_mode.allow_choosing() {
1584            msg.push_str(" Possible to change.");
1585        } else {
1586            msg.push_str(" Global.");
1587        }
1588
1589        if let Some(change) = player.last_battlemode_change {
1590            let Time(time) = *time;
1591            let Time(change) = change;
1592            let elapsed = time - change;
1593            let next = BATTLE_MODE_COOLDOWN - elapsed;
1594
1595            if next > 0.0 {
1596                let notice = format!(" Next change will be available in: {:.0} seconds", next);
1597                msg.push_str(&notice);
1598            }
1599        }
1600
1601        self.notify_client(
1602            client,
1603            ServerGeneral::server_msg(ChatType::CommandInfo, Content::Plain(msg)),
1604        );
1605    }
1606
1607    /// Sets the battle mode for the given client or informs them if they are
1608    /// not allowed to change it.
1609    ///
1610    /// This function expects the `EcsEntity` to represent a player, otherwise
1611    /// it will log an error.
1612    pub fn set_battle_mode_for(&mut self, client: EcsEntity, battle_mode: BattleMode) {
1613        let ecs = self.state.ecs();
1614        let time = ecs.read_resource::<Time>();
1615        let settings = ecs.read_resource::<Settings>();
1616
1617        if !settings.gameplay.battle_mode.allow_choosing() {
1618            self.notify_client(
1619                client,
1620                ServerGeneral::server_msg(
1621                    ChatType::CommandInfo,
1622                    Content::localized("command-disabled-by-settings"),
1623                ),
1624            );
1625
1626            return;
1627        }
1628
1629        #[cfg(feature = "worldgen")]
1630        let in_town = {
1631            let pos = if let Some(pos) = self
1632                .state
1633                .ecs()
1634                .read_storage::<comp::Pos>()
1635                .get(client)
1636                .copied()
1637            {
1638                pos
1639            } else {
1640                self.notify_client(
1641                    client,
1642                    ServerGeneral::server_msg(
1643                        ChatType::CommandInfo,
1644                        Content::localized_with_args("command-position-unavailable", [(
1645                            "target", "target",
1646                        )]),
1647                    ),
1648                );
1649
1650                return;
1651            };
1652
1653            let wpos = pos.0.xy().map(|x| x as i32);
1654            let chunk_pos = wpos.wpos_to_cpos();
1655            self.world.civs().sites().any(|site| {
1656                // empirical
1657                const RADIUS: f32 = 9.0;
1658                let delta = site
1659                    .center
1660                    .map(|x| x as f32)
1661                    .distance(chunk_pos.map(|x| x as f32));
1662                delta < RADIUS
1663            })
1664        };
1665
1666        #[cfg(not(feature = "worldgen"))]
1667        let in_town = true;
1668
1669        if !in_town {
1670            self.notify_client(
1671                client,
1672                ServerGeneral::server_msg(
1673                    ChatType::CommandInfo,
1674                    Content::localized("command-battlemode-intown"),
1675                ),
1676            );
1677
1678            return;
1679        }
1680
1681        let mut players = ecs.write_storage::<comp::Player>();
1682        let mut player = if let Some(info) = players.get_mut(client) {
1683            info
1684        } else {
1685            error!("Failed to get info for player.");
1686
1687            return;
1688        };
1689
1690        if let Some(Time(last_change)) = player.last_battlemode_change {
1691            let Time(time) = *time;
1692            let elapsed = time - last_change;
1693            if elapsed < BATTLE_MODE_COOLDOWN {
1694                let next = BATTLE_MODE_COOLDOWN - elapsed;
1695
1696                self.notify_client(
1697                    client,
1698                    ServerGeneral::server_msg(
1699                        ChatType::CommandInfo,
1700                        Content::Plain(format!(
1701                            "Next change will be available in {next:.0} seconds."
1702                        )),
1703                    ),
1704                );
1705
1706                return;
1707            }
1708        }
1709
1710        if player.battle_mode == battle_mode {
1711            self.notify_client(
1712                client,
1713                ServerGeneral::server_msg(
1714                    ChatType::CommandInfo,
1715                    Content::localized("command-battlemode-same"),
1716                ),
1717            );
1718
1719            return;
1720        }
1721
1722        player.battle_mode = battle_mode;
1723        player.last_battlemode_change = Some(*time);
1724
1725        self.notify_client(
1726            client,
1727            ServerGeneral::server_msg(
1728                ChatType::CommandInfo,
1729                Content::localized_with_args("command-battlemode-updated", [(
1730                    "battlemode",
1731                    format!("{battle_mode:?}"),
1732                )]),
1733            ),
1734        );
1735
1736        drop(players);
1737
1738        let uid = ecs.read_storage::<Uid>().get(client).copied().unwrap();
1739
1740        self.state().notify_players(ServerGeneral::PlayerListUpdate(
1741            PlayerListUpdate::UpdateBattleMode(uid, battle_mode),
1742        ));
1743    }
1744}
1745
1746impl Drop for Server {
1747    fn drop(&mut self) {
1748        self.state
1749            .notify_players(ServerGeneral::Disconnect(DisconnectReason::Shutdown));
1750
1751        #[cfg(feature = "persistent_world")]
1752        self.state
1753            .ecs()
1754            .try_fetch_mut::<TerrainPersistence>()
1755            .map(|mut terrain_persistence| {
1756                info!("Unloading terrain persistence...");
1757                terrain_persistence.unload_all()
1758            });
1759
1760        #[cfg(feature = "worldgen")]
1761        {
1762            debug!("Saving rtsim state...");
1763            self.state.ecs().write_resource::<rtsim::RtSim>().save(true);
1764        }
1765    }
1766}
1767
1768#[must_use]
1769pub fn handle_edit<T, S: settings::EditableSetting>(
1770    data: T,
1771    result: Option<(String, Result<(), settings::SettingError<S>>)>,
1772) -> Option<T> {
1773    use crate::settings::SettingError;
1774    let (info, result) = result?;
1775    match result {
1776        Ok(()) => {
1777            info!("{}", info);
1778            Some(data)
1779        },
1780        Err(SettingError::Io(err)) => {
1781            warn!(
1782                ?err,
1783                "Failed to write settings file to disk, but succeeded in memory (success message: \
1784                 {})",
1785                info,
1786            );
1787            Some(data)
1788        },
1789        Err(SettingError::Integrity(err)) => {
1790            error!(?err, "Encountered an error while validating the request",);
1791            None
1792        },
1793    }
1794}
1795
1796/// If successful returns the Some(uuid) of the added admin
1797///
1798/// NOTE: Do *not* allow this to be called from any command that doesn't go
1799/// through the CLI!
1800#[must_use]
1801pub fn add_admin(
1802    username: &str,
1803    role: comp::AdminRole,
1804    login_provider: &LoginProvider,
1805    editable_settings: &mut EditableSettings,
1806    data_dir: &std::path::Path,
1807) -> Option<common::uuid::Uuid> {
1808    use crate::settings::EditableSetting;
1809    let role_ = role.into();
1810    match login_provider.username_to_uuid(username) {
1811        Ok(uuid) => handle_edit(
1812            uuid,
1813            editable_settings.admins.edit(data_dir, |admins| {
1814                match admins.insert(uuid, settings::AdminRecord {
1815                    username_when_admined: Some(username.into()),
1816                    date: chrono::Utc::now(),
1817                    role: role_,
1818                }) {
1819                    None => Some(format!(
1820                        "Successfully added {} ({}) as {:?}!",
1821                        username, uuid, role
1822                    )),
1823                    Some(old_admin) if old_admin.role == role_ => {
1824                        info!("{} ({}) already has role: {:?}!", username, uuid, role);
1825                        None
1826                    },
1827                    Some(old_admin) => Some(format!(
1828                        "{} ({}) role changed from {:?} to {:?}!",
1829                        username, uuid, old_admin.role, role
1830                    )),
1831                }
1832            }),
1833        ),
1834        Err(err) => {
1835            error!(
1836                ?err,
1837                "Could not find uuid for this name; either the user does not exist or there was \
1838                 an error communicating with the auth server."
1839            );
1840            None
1841        },
1842    }
1843}
1844
1845/// If successful returns the Some(uuid) of the removed admin
1846///
1847/// NOTE: Do *not* allow this to be called from any command that doesn't go
1848/// through the CLI!
1849#[must_use]
1850pub fn remove_admin(
1851    username: &str,
1852    login_provider: &LoginProvider,
1853    editable_settings: &mut EditableSettings,
1854    data_dir: &std::path::Path,
1855) -> Option<common::uuid::Uuid> {
1856    use crate::settings::EditableSetting;
1857    match login_provider.username_to_uuid(username) {
1858        Ok(uuid) => handle_edit(
1859            uuid,
1860            editable_settings.admins.edit(data_dir, |admins| {
1861                if let Some(admin) = admins.remove(&uuid) {
1862                    Some(format!(
1863                        "Successfully removed {} ({}) with role {:?} from the admins list",
1864                        username, uuid, admin.role,
1865                    ))
1866                } else {
1867                    info!("{} ({}) is not an admin!", username, uuid);
1868                    None
1869                }
1870            }),
1871        ),
1872        Err(err) => {
1873            error!(
1874                ?err,
1875                "Could not find uuid for this name; either the user does not exist or there was \
1876                 an error communicating with the auth server."
1877            );
1878            None
1879        },
1880    }
1881}