~starkingdoms/starkingdoms

ref: 386a970b53fcd24c4701300b2a8a02a5b5844ce6 starkingdoms/crates/unified/src/main.rs -rw-r--r-- 8.7 KiB
386a970b — core recursive disconnect 26 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#![warn(clippy::pedantic)] // Be annoying, and disable specific irritating lints if needed
#![deny(
    clippy::allow_attributes_without_reason,
    clippy::assertions_on_result_states
)]
#![warn(clippy::if_then_some_else_none)]
#![allow(clippy::type_complexity, reason = "Bevy makes this a nightmare")]
#![allow(clippy::needless_pass_by_value, reason = "Bevy makes this a nightmare")]
#![allow(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    reason = "We cast ints to floats a lot"
)]
#![allow(clippy::missing_panics_doc, reason = "Gamedev! We panic a lot")]
#![allow(clippy::too_many_arguments, reason = "Le Bevy:tm:")]
#![allow(
    clippy::too_many_lines,
    reason = "With the three of us, this is impossible"
)]

pub mod attachment;
pub mod client;
pub mod client_plugins;
pub mod config;
pub mod ecs;
#[cfg(all(not(target_arch = "wasm32"), feature = "particle_editor"))]
pub mod particle_editor;
pub mod particles;
#[cfg(all(not(target_arch = "wasm32"), feature = "native"))]
pub mod server;
#[cfg(all(not(target_arch = "wasm32"), feature = "native"))]
pub mod server_plugins;
pub mod shared_plugins;

pub mod physics;
pub mod prelude;

#[cfg(target_arch = "wasm32")]
pub mod wasm_entrypoint;
#[cfg(target_arch = "wasm32")]
pub use wasm_entrypoint::*;

use bevy::log::{tracing_subscriber, LogPlugin};
use crate::prelude::*;
use clap::Parser;
use crate::client_plugins::ClientPluginGroup;
#[cfg(not(target_arch = "wasm32"))]
use crate::server_plugins::ServerPluginGroup;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::process::exit;
use std::str::FromStr;
use std::time::Duration;
use bevy::app::ScheduleRunnerPlugin;
use bevy::diagnostic::FrameCountPlugin;
use bevy::state::app::StatesPlugin;
use bevy::time::TimePlugin;
use bevy::ui::UiPlugin;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::filter::Directive;
use tracing_subscriber::util::SubscriberInitExt;
use crate::shared_plugins::SharedPluginGroup;

#[derive(Parser, Debug, Clone)]
#[command(version, about)]
enum Cli {
    Client {
        #[arg(short, long)]
        server: String,
        #[arg(long, action)]
        hotpatching_enabled: bool,
    },
    #[cfg(not(target_arch = "wasm32"))]
    Server {
        #[arg(short = 'b', long)]
        bind: SocketAddr,
        #[arg(short = 'r', long)]
        tick_rate: f32,
        #[arg(short = 'C', long)]
        max_clients: usize,
        #[arg(long, action)]
        with_client: bool,
        #[arg(long, action)]
        hotpatching_enabled: bool
    },
    #[cfg(all(not(target_arch = "wasm32"), feature = "particle_editor"))]
    ParticleEditor {},
}

fn run(cli: Cli) -> AppExit {
    let mut app = App::new();

    match cli {
        Cli::Client { server, hotpatching_enabled } => {
            if hotpatching_enabled {
                warn!("-+-+-+-+-+-+- Starting with hotpatching enabled -+-+-+-+-+-+-");
                warn!("This can result in segfaults and inconsistent behavior! If there is weirdness, try disabling it.");
                warn!("-+-+-+-+-+-+- Starting with hotpatching enabled -+-+-+-+-+-+-");
            }
            app.add_plugins(
                DefaultPlugins.build()
                    .disable::<LogPlugin>()
                    .disable::<UiPlugin>()
            );
            app.add_plugins(ClientPluginGroup { server: Some(server) });
            app.add_plugins(SharedPluginGroup);
        }
        #[cfg(not(target_arch = "wasm32"))]
        Cli::Server {
            bind,
            tick_rate,
            max_clients,
            hotpatching_enabled,
            with_client
        } => {
            if hotpatching_enabled {
                warn!("-+-+-+-+-+-+- Starting with hotpatching enabled -+-+-+-+-+-+-");
                warn!("This can result in segfaults and inconsistent behavior! If there is weirdness, try disabling it.");
                warn!("-+-+-+-+-+-+- Starting with hotpatching enabled -+-+-+-+-+-+-");
            }
            if cfg!(target_family = "wasm") {
                eprintln!("the server cannot run on webassembly");
                exit(1);
            }


            if with_client {
                app.add_plugins(
                    DefaultPlugins.build()
                        .disable::<LogPlugin>()
                        .disable::<UiPlugin>()
                );
                app.add_plugins(|app: &mut App| {
                    app.add_systems(Startup, crate::server::player::join::ls_magically_invent_player);
                });
            } else {
               app
                   .add_plugins(AssetPlugin::default())
                   .add_plugins(StatesPlugin)
                    .add_plugins(TaskPoolPlugin::default())
                    .add_plugins(FrameCountPlugin)
                    .add_plugins(TimePlugin)
                    .add_plugins(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f32(
                        1.0 / tick_rate,
                    )));
            }

            app.add_plugins(SharedPluginGroup);

            let mut pg = ServerPluginGroup {
                bind,
                tick_rate,
                max_clients,
            }.build();
            if with_client {
                pg = pg.add_group(ClientPluginGroup {
                    server: None
                });
            }
            app.add_plugins(pg);
        }
        #[cfg(all(not(target_arch = "wasm32"), feature = "particle_editor"))]
        Cli::ParticleEditor {} => {
            app.add_plugins(crate::particle_editor::particle_editor_plugin);
        }
    }

    app.run()
}

fn main() -> AppExit {
    let cli = Cli::parse();

    tracing_subscriber::fmt()
        .with_env_filter(
            EnvFilter::from_default_env().add_directive(Directive::from_str("naga=error").unwrap()),
        )
        .finish()
        .init();

    ctrlc::set_handler(|| {
        info!("caught ^C, ciao!");
        exit(0);
    }).unwrap();

    match cli {
        Cli::Client { .. } => { run(cli) },
        Cli::ParticleEditor { .. } => { run(cli) },
        Cli::Server { with_client, bind, hotpatching_enabled, .. } => {
            run(cli)
            /*
            if !with_client {
                run(cli)
            } else {
                warn!("-----------------------------------------");
                warn!("RUNNING IN EXPERIMENTAL LISTENSERVER MODE");
                warn!("-----------------------------------------");
                warn!("This mode is HIGHLY EXPERIMENTAL, relies on janky threading, and may or may not work at all.");
                warn!("Use at your own risk. If weird things happen, try running separately.");
                warn!("-----------------------------------------");
                warn!("RUNNING IN EXPERIMENTAL LISTENSERVER MODE");
                warn!("-----------------------------------------");

                let scli_clone = cli.clone();
                let server_thread = std::thread::Builder::new()
                    .name("server".to_string())
                    .spawn(move || {
                        info!("starting server thread...");
                        run(scli_clone)
                    })
                    .unwrap();

                let clt_exit;

                    info!("starting client thread...");

                    let is_multicast = bind.ip().is_unspecified();

                    let target_ip = if is_multicast {
                        if bind.ip().is_ipv4() { IpAddr::V4(Ipv4Addr::LOCALHOST) } else { IpAddr::V6(Ipv6Addr::LOCALHOST) }
                    } else {
                        bind.ip()
                    };
                    let target_port = bind.port();
                    let target_ip_str = match target_ip {
                        IpAddr::V4(a) => a.to_string(),
                        IpAddr::V6(a) => format!("[{a}]"),
                    };
                    let target_url = format!("ws://{target_ip_str}:{target_port}");
                    info!("starting the client with autocalculated target server url {target_url}");

                    let cli2 = Cli::Client { server: target_url, hotpatching_enabled };

                    clt_exit = run(cli2);

                    if let AppExit::Error(c) = clt_exit {
                        error!("client exited with error {c}");
                    }
                    info!("-------- CLIENT HAS EXITED --------");
                    info!("  ^C  to stop the server");

                let srv_exit = server_thread.join().unwrap();

                match srv_exit {
                    AppExit::Error(c) => AppExit::Error(c),
                    _ => match clt_exit {
                        AppExit::Error(c) => AppExit::Error(c),
                        _ => AppExit::Success
                    }
                }*/
        }
    }
}