Skip to main content

nitro_core/
config.rs

1use nitro_auth::mc::ClientId;
2
3use crate::util::secrets::get_ms_client_id;
4
5macro_rules! builder_method {
6	($name:ident, $ty:ty, $doc:literal) => {
7		#[doc = $doc]
8		pub fn $name(mut self, $name: $ty) -> Self {
9			self.config.$name = $name;
10			self
11		}
12	};
13}
14
15/// Configuration for different parts of the core library
16pub struct Configuration {
17	/// The Microsoft client ID to use for Microsoft authentication
18	pub(crate) ms_client_id: ClientId,
19	/// Whether to censor account credentials in output messages and logs
20	pub(crate) censor_secrets: bool,
21	/// Whether to use file copies instead of hardlinks. Useful if you
22	/// are on a filesystem that doesn't like hardlinks
23	pub(crate) disable_hardlinks: bool,
24	/// Launcher branding
25	pub(crate) branding: BrandingProperties,
26}
27
28impl Default for Configuration {
29	fn default() -> Self {
30		Self::new()
31	}
32}
33
34impl Configuration {
35	/// Construct the default configuration
36	pub fn new() -> Self {
37		Self {
38			ms_client_id: get_ms_client_id(),
39			censor_secrets: true,
40			disable_hardlinks: false,
41			branding: BrandingProperties::default(),
42		}
43	}
44
45	/// Get a builder for the configuration
46	pub fn builder() -> ConfigBuilder {
47		ConfigBuilder::new()
48	}
49}
50
51/// Simple builder for the configuration
52pub struct ConfigBuilder {
53	config: Configuration,
54}
55
56impl ConfigBuilder {
57	/// Start a new ConfigBuilder with default configuration
58	pub fn new() -> Self {
59		Self {
60			config: Configuration::new(),
61		}
62	}
63
64	/// Finish building and get the configuration
65	pub fn build(self) -> Configuration {
66		self.config
67	}
68
69	builder_method!(
70		ms_client_id,
71		ClientId,
72		"Set the Microsoft client ID to use for Microsoft / Xbox Live authentication"
73	);
74
75	builder_method!(
76		censor_secrets,
77		bool,
78		"Set whether to censor account credentials in output messages and logs"
79	);
80
81	builder_method!(
82		disable_hardlinks,
83		bool,
84		"Set whether to disable the use of hardlinks"
85	);
86
87	builder_method!(branding, BrandingProperties, "Set the branding properties");
88}
89
90impl Default for ConfigBuilder {
91	fn default() -> Self {
92		Self::new()
93	}
94}
95
96/// Branding properties for the launcher to send to the client
97#[derive(Clone)]
98pub struct BrandingProperties {
99	/// The desired launcher name to send to the client
100	pub(crate) launcher_name: String,
101	/// The desired launcher version to send to the client
102	pub(crate) launcher_version: String,
103}
104
105impl Default for BrandingProperties {
106	fn default() -> Self {
107		Self {
108			launcher_name: "nitro_core".into(),
109			launcher_version: env!("CARGO_PKG_VERSION").into(),
110		}
111	}
112}
113
114impl BrandingProperties {
115	/// Create new BrandingProperties
116	pub fn new(name: String, version: String) -> Self {
117		Self {
118			launcher_name: name,
119			launcher_version: version,
120		}
121	}
122}