OneLauncher-Cracked/packages/entity/src/domain/loader.rs
2026-03-31 21:13:18 -04:00

114 lines
2.8 KiB
Rust

use std::fmt::Display;
use std::str::FromStr;
use sea_orm::{DeriveActiveEnum, EnumIter};
use serde::{Deserialize, Serialize};
#[onelauncher_macro::specta]
#[derive(
Default,
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
Serialize,
Deserialize,
EnumIter,
DeriveActiveEnum,
)]
#[serde(rename_all = "lowercase")]
#[sea_orm(rs_type = "u8", db_type = "Integer")]
pub enum GameLoader {
#[default]
#[serde(alias = "minecraft")]
Vanilla = 0,
Forge = 1,
NeoForge = 2,
Quilt = 3,
Fabric = 4,
LegacyFabric = 5,
}
impl GameLoader {
#[must_use]
pub const fn is_modded(&self) -> bool {
!matches!(self, Self::Vanilla)
}
#[must_use]
pub const fn get_format_version(&self) -> usize {
match self {
Self::Vanilla => interfrost::api::minecraft::CURRENT_FORMAT_VERSION,
Self::Forge => interfrost::api::modded::CURRENT_FORGE_FORMAT_VERSION,
Self::NeoForge => interfrost::api::modded::CURRENT_NEOFORGE_FORMAT_VERSION,
Self::Quilt => interfrost::api::modded::CURRENT_QUILT_FORMAT_VERSION,
Self::Fabric => interfrost::api::modded::CURRENT_FABRIC_FORMAT_VERSION,
Self::LegacyFabric => interfrost::api::modded::CURRENT_LEGACY_FABRIC_FORMAT_VERSION,
}
}
#[must_use]
pub fn get_format_name(&self) -> String {
match self {
Self::Vanilla => String::from("minecraft"),
Self::NeoForge => String::from("neo"), // TODO(metadata): change to neoforge
_ => self.to_string().to_lowercase().replace(['_', '.', ' '], ""),
}
}
#[must_use]
pub const fn compatible_with(&self, other: &Self) -> bool {
match self {
Self::Vanilla => matches!(other, Self::Vanilla),
Self::Forge => matches!(other, Self::Forge),
Self::NeoForge => matches!(other, Self::NeoForge),
Self::Quilt => matches!(other, Self::Quilt | Self::Fabric),
Self::Fabric => matches!(other, Self::Fabric),
Self::LegacyFabric => matches!(other, Self::LegacyFabric),
}
}
#[must_use]
pub const fn modded_loaders() -> &'static [Self] {
&[
Self::Forge,
Self::NeoForge,
Self::Quilt,
Self::Fabric,
Self::LegacyFabric,
]
}
}
impl FromStr for GameLoader {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(
match s.to_lowercase().replace(['_', '.', ' '], "").as_str() {
"vanilla" | "minecraft" => Self::Vanilla,
"forge" => Self::Forge,
"neoforge" | "neo" => Self::NeoForge,
"quilt" | "quilt-loader" => Self::Quilt,
"fabric" | "fabric-loader" => Self::Fabric,
"legacyfabric" => Self::LegacyFabric,
_ => return Err(format!("'{s}' is not a valid game loader")),
},
)
}
}
impl Display for GameLoader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Vanilla => "Vanilla",
Self::Forge => "Forge",
Self::NeoForge => "NeoForge",
Self::Quilt => "Quilt",
Self::Fabric => "Fabric",
Self::LegacyFabric => "LegacyFabric",
})
}
}