donet_core/lib.rs
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 254 255 256 257 258 259 260 261 262
/*
This file is part of Donet.
Copyright © 2024 Max Rodriguez <[email protected]>
Donet is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License,
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
Donet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with Donet. If not, see <https://www.gnu.org/licenses/>.
*/
//! # donet-core
//! Provides the necessary utilities and definitions for using the Donet networking protocol.
//!
//! These utilities include a lexer, parser, and high-level representation of the parsed DC
//! file, as well as creating datagrams, iterating through datagrams, and the definition of
//! every message type in the Donet protocol.
//!
//! ### Getting Started
//! The recommended way to get started is to enable all features.
//! Do this by enabling the `full` feature flag:
//! ```toml
//! donet-core = { version = "0.1.0", features = ["full"] }
//! ```
//!
//! ### Feature Flags
//! The crate provides a set of feature flags to reduce the amount of compiled code.
//! It is possible to just enable certain features over others.
//! Below is a list of the available feature flags.
//!
//! - **`full`**: Enables all feature flags available for donet-core.
//! - **`datagram`**: Includes Datagram / Datagram Iterator source for writing network packets.
//! - **`dcfile`**: Includes the DC file lexer, parser, and DC element structures.
//!
//! You can return to the Donet manual at [`docs.donet-server.org`].
//!
//! [`docs.donet-server.org`]: https://docs.donet-server.org/
#![doc(
html_logo_url = "https://gitlab.com/donet-server/donet/-/raw/master/logo/donet_logo_v3.png?ref_type=heads"
)]
#![allow(clippy::module_inception)]
//#![warn(missing_docs)]
#![deny(unused_extern_crates)]
pub mod globals;
pub mod protocol;
// Re-export of protocol module
pub use protocol::*;
#[macro_use]
extern crate cfg_if;
#[cfg(feature = "datagram")]
pub mod datagram;
cfg_if! {
if #[cfg(feature = "dcfile")] {
mod parser;
pub mod dcarray;
pub mod dcatomic;
pub mod dcfield;
pub mod dcfile;
pub mod dckeyword;
pub mod dclass;
pub mod dcmolecular;
pub mod dcnumeric;
pub mod dconfig;
pub mod dcparameter;
pub mod dcstruct;
pub mod dcswitch;
pub mod dctype;
mod hashgen;
use anyhow::Result;
use dcfile::DCFile;
use parser::error::DCReadError;
}
}
/// Returns false if a [`log`] logger is not initialized.
///
/// [`log`]: https://docs.rs/log/latest/log/
///
#[cfg(feature = "dcfile")]
fn logger_initialized() -> bool {
use log::Level::*;
let levels: &[log::Level] = &[Error, Warn, Info, Debug, Trace];
for level in levels {
if log::log_enabled!(*level) {
return true;
}
}
false
}
/// Creates a [`pretty_env_logger`] logger if no [`log`]
/// logger is found to be initialized in this process.
///
/// [`pretty_env_logger`]: https://docs.rs/pretty_env_logger/latest/pretty_env_logger/
/// [`log`]: https://docs.rs/log/latest/log/
///
#[cfg(feature = "dcfile")]
fn init_logger() {
if logger_initialized() {
return;
}
pretty_env_logger::init();
}
/// Easy to use interface for the DC file parser. Handles reading
/// the DC files, instantiating the DC parsing pipeline, and either
/// returns the DCFile object or a Parse/File error.
#[cfg(feature = "dcfile")]
pub fn read_dc_files<'a>(
config: dconfig::DCFileConfig,
file_paths: Vec<String>,
) -> Result<DCFile<'a>, DCReadError> {
use log::{info, warn};
use parser::InputFile;
use std::fs::File;
use std::io::{Error, ErrorKind, Read};
use std::path::Path;
init_logger();
info!("DC read of {:?}", file_paths);
let mut filenames: Vec<String> = vec![];
let mut file_results: Vec<Result<File, std::io::Error>> = vec![];
let mut pipeline_input: Vec<parser::InputFile> = vec![];
if file_paths.is_empty() {
warn!("No DC files given! Using empty DC file.");
return read_dc(config, String::default());
}
for file_path in &file_paths {
// Get filename from given path
match Path::new(file_path).file_name() {
Some(filename_osstr) => {
// Convert OsStr to String and store filename
filenames.push(filename_osstr.to_string_lossy().into_owned());
}
None => {
// std::path::Path.file_name() **only** returns `None`
// if the path terminates in '..'.
let filename_err: Error = Error::new(
ErrorKind::InvalidInput,
"Failed to get filename from path because\
path terminates in '..'.",
);
return Err(DCReadError::IO(filename_err));
}
}
// Open file using path and store result
file_results.push(File::open(file_path));
}
for (index, io_result) in file_results.into_iter().enumerate() {
if let Ok(mut dcf) = io_result {
// Prepare `InputFile` tuple for the pipeline function.
let filename: String = filenames.get(index).unwrap().to_owned();
let mut in_file: InputFile = (filename, String::default());
let res: std::io::Result<usize> = dcf.read_to_string(&mut in_file.1);
if let Err(res_err) = res {
// DC file content may not be in proper UTF-8 encoding.
return Err(DCReadError::IO(res_err));
}
pipeline_input.push(in_file);
} else {
// Failed to open one of the DC files. (most likely permission error)
return Err(DCReadError::IO(io_result.unwrap_err()));
}
}
parser::dcparse_pipeline(config, pipeline_input)
}
/// Front end to the donet-core DC parser pipeline.
///
/// ## Example Usage
/// The following is an example of parsing a simple DC file string,
/// printing its DC hash in hexadecimal notation, and accessing
/// the elements of a defined Distributed Class:
/// ```rust
/// use donet_core::dcfile::DCFile;
/// use donet_core::dclass::DClass;
/// use donet_core::dconfig::*;
/// use donet_core::read_dc;
///
/// let dc_file = "
///
/// from game.ai import AnonymousContact/UD
/// from game.ai import LoginManager/AI
/// from game.world import DistributedWorld/AI
/// from game.avatar import DistributedAvatar/AI/OV
///
/// typedef uint32 doId;
/// typedef uint32 zoneId;
/// typedef uint64 channel;
///
/// dclass AnonymousContact {
/// login(string username, string password) clsend airecv;
/// };
///
/// dclass LoginManager {
/// login(channel client, string username, string password) airecv;
/// };
///
/// dclass DistributedWorld {
/// create_avatar(channel client) airecv;
/// };
///
/// dclass DistributedAvatar {
/// set_xyzh(int16 x, int16 y, int16 z, int16 h) broadcast required;
/// indicate_intent(int16 / 10, int16 / 10) ownsend airecv;
/// };
///
/// ";
///
/// let dc_conf = DCFileConfig::default();
/// let dc_read = read_dc(dc_conf, dc_file.into());
///
/// if let Ok(dc_file) = dc_read {
/// // Print the DC File's 32-bit hash in hexadecimal format.
/// println!("{}", dc_file.get_pretty_hash());
///
/// // TODO: Retrieve the `DistributedAvatar` dclass by ID.
/// //let class: &DClass = dc_file.get_dclass_by_id(3);
///
/// // TODO: Print the identifier of the dclass.
/// //println!("{}", class.get_name());
/// }
/// ```
///
/// The output of the program would be the following:
/// ```txt
/// 0x9c737148
/// DistributedAvatar
/// ```
/// <br><img src="https://c.tenor.com/myQHgyWQQ9sAAAAd/tenor.gif">
///
#[cfg(feature = "dcfile")]
pub fn read_dc<'a>(config: dconfig::DCFileConfig, input: String) -> Result<DCFile<'a>, DCReadError> {
let dcparse_input: Vec<parser::InputFile> = vec![("input.dc".to_string(), input)];
parser::dcparse_pipeline(config, dcparse_input)
}