~starkingdoms/starkingdoms

ref: 9b91629f33034dab27fe10f953bd39b6477fcac2 starkingdoms/crates/unified/src/main.rs -rw-r--r-- 2.4 KiB
9b91629f — core chore(lint): lint/format 5 months 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
pub mod client;
pub mod client_plugins;
pub mod config;
pub mod ecs;
pub mod server;
pub mod server_plugins;
pub mod shared_plugins;

use crate::client_plugins::ClientPluginGroup;
use crate::server_plugins::ServerPluginGroup;
use crate::shared_plugins::SharedPluginGroup;
use bevy::log::{Level, tracing_subscriber};
use bevy::prelude::*;
use clap::Parser;
use std::net::SocketAddr;
use std::process::exit;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::util::SubscriberInitExt;

#[derive(Parser, Debug)]
#[command(version, about)]
enum Cli {
    Client {
        #[cfg(target_arch = "wasm32")]
        #[arg(short, long)]
        server: url::Url,
        #[cfg(not(target_arch = "wasm32"))]
        #[arg(short, long)]
        server: SocketAddr,
    },
    Server {
        #[arg(short = 'w', long)]
        bind_ws: SocketAddr,
        #[arg(short = 'u', long)]
        bind_native: SocketAddr,
        #[arg(short = 'r', long)]
        tick_rate: f64,
        #[arg(short = 'C', long)]
        max_clients: usize,
    },
}

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

    tracing_subscriber::fmt()
        .with_env_filter(
            EnvFilter::builder()
                .with_default_directive(Level::INFO.into())
                .from_env_lossy()
                .add_directive("bevy_app::plugin_group=error".parse().unwrap())
                .add_directive("bevy_app::app=error".parse().unwrap())
                .add_directive("bevy_replicon=warn".parse().unwrap())
                .add_directive("bevy_replicon_renet2=warn".parse().unwrap())
                .add_directive("naga=warn".parse().unwrap())
                .add_directive("wgpu_hal::vulkan=error".parse().unwrap()),
        )
        .finish()
        .init();

    match cli {
        Cli::Client { server } => {
            app.add_plugins(ClientPluginGroup { server });
        }
        Cli::Server {
            bind_ws,
            bind_native,
            tick_rate,
            max_clients,
        } => {
            if cfg!(target_family = "wasm") {
                eprintln!("the server cannot run on webassembly");
                exit(1);
            }

            app.add_plugins(ServerPluginGroup {
                bind_ws,
                bind_native,
                tick_rate,
                max_clients,
            });
        }
    }

    app.add_plugins(SharedPluginGroup);

    app.run()
}