banner
AcoFork

AcoFork

LOVETOLOVE

Phira Multiplayer Server Setup Tutorial

Introduction#

  1. If you are using the Windows system, you can directly download the pre-built files (default ports are 12345 or 12346): Dmocken's Phira Download Station, Binary Tree's Cloud Disk

Installing Rust#

Phira multiplayer server is written in Rust, so you need the Rust environment to set it up.

Windows#

  1. Go to the Rust download page and download Rust.
    image
  2. After opening, a CMD window will pop up. Enter 1 (Quick Install) and press Enter, wait for Visual Studio to install (if the Visual Studio download is slow, you can also manually download it).
    Snipaste_2024-08-20_00-24-56
  3. In Visual Studio, check Desktop development with C++ and install it.
    image
    Then skip the Linux tutorial and read Building phira-mp directly.

Linux#

  1. Execute: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  2. Choose 1 and press Enter.
  3. Execute: source $HOME/.cargo/env

Building phira-mp#

  1. Clone the repository: git clone https://github.com/TeamFlos/phira-mp.git
  2. Replace the following code in /phira-mp/phira-mp-server/src/main.rs
mod l10n;

mod room;
pub use room::*;

mod server;
pub use server::*;

mod session;
pub use session::*;

use anyhow::Result;
use clap::Parser;
use std::{
    collections::{
        hash_map::{Entry, VacantEntry},
        HashMap,
    },
    net::{Ipv6Addr, SocketAddr},
    path::Path,
};
use tokio::{net::TcpListener, sync::RwLock};
use tracing::warn;
use tracing_appender::non_blocking::WorkerGuard;
use uuid::Uuid;

pub type SafeMap<K, V> = RwLock<HashMap<K, V>>;
pub type IdMap<V> = SafeMap<Uuid, V>;

fn vacant_entry<V>(map: &mut HashMap<Uuid, V>) -> VacantEntry<'_, Uuid, V> {
    let mut id = Uuid::new_v4();
    while map.contains_key(&id) {
        id = Uuid::new_v4();
    }
    match map.entry(id) {
        Entry::Vacant(entry) => entry,
        _ => unreachable!(),
    }
}

pub fn init_log(file: &str) -> Result<WorkerGuard> {
    use tracing::{metadata::LevelFilter, Level};
    use tracing_log::LogTracer;
    use tracing_subscriber::{filter, fmt, prelude::*, EnvFilter};

    let log_dir = Path::new("log");
    if log_dir.exists() {
        if !log_dir.is_dir() {
            panic!("log exists and is not a folder");
        }
    } else {
        std::fs::create_dir(log_dir).expect("failed to create log folder");
    }

    LogTracer::init()?;

    let (non_blocking, guard) =
        tracing_appender::non_blocking(tracing_appender::rolling::hourly(log_dir, file));

    let subscriber = tracing_subscriber::registry()
        .with(
            fmt::layer()
                .with_writer(non_blocking)
                .with_filter(LevelFilter::DEBUG),
        )
        .with(
            fmt::layer()
                .with_writer(std::io::stdout)
                .with_filter(EnvFilter::from_default_env()),
        )
        .with(
            filter::Targets::new()
                .with_target("hyper", Level::INFO)
                .with_target("rustls", Level::INFO)
                .with_target("isahc", Level::INFO)
                .with_default(Level::TRACE),
        );

    tracing::subscriber::set_global_default(subscriber).expect("unable to set global subscriber");
    Ok(guard)
}

/// Command line arguments
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
    #[clap(
        short,
        long,
        default_value_t = 12346,
        help = "Specify the port number to use for the server"
    )]
    port: u16,
}

#[tokio::main]
async fn main() -> Result<()> {
    let _guard = init_log("phira-mp")?;

    let args = Args::parse();
    let port = args.port;
    let addrs: &[SocketAddr] = &[
        SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), port),
    ];

    // Print local address and port
    for addr in addrs {
        println!("Local Address: {}", addr);
    }

    let listener: Server = TcpListener::bind(addrs).await?.into();

    loop {
        if let Err(err) = listener.accept().await {
            warn!("failed to accept: {err:?}");
        }
    }
}
  1. cd phira-mp
  2. Update dependencies: cargo update
  3. Build: cargo build --release -p phira-mp-server
  4. Run the program and print the log to the terminal, it will display the port you are listening on: RUST_LOG=info target/release/phira-mp-server
    (If you need to specify the port number: RUST_LOG=info target/release/phira-mp-server --port 8080)
    Snipaste_2024-08-20_01-33-32
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.