use std::path::Path;
pub mod mods;
pub mod structs;
use inner_future::stream::StreamExt;
use self::structs::VersionInfo;
use crate::prelude::*;
pub struct Version {
pub name: String,
pub version_type: VersionType,
pub created_date: std::time::SystemTime,
pub access_date: std::time::SystemTime,
}
impl Default for Version {
fn default() -> Self {
Self {
name: String::new(),
version_type: VersionType::Unknown,
created_date: std::time::SystemTime::UNIX_EPOCH,
access_date: std::time::SystemTime::UNIX_EPOCH,
}
}
}
pub async fn get_avaliable_versions(
version_directory_path: impl AsRef<Path>,
) -> DynResult<Vec<Version>> {
let mut version_info = VersionInfo {
version_base: version_directory_path
.as_ref()
.to_string_lossy()
.to_string(),
..Default::default()
};
if version_directory_path.as_ref().is_dir() {
let mut entries = inner_future::fs::read_dir(version_directory_path.as_ref()).await?;
let mut result = Vec::with_capacity(32);
while let Some(entry) = entries.try_next().await? {
let (created_date, access_date) = if let Ok(metadata) = entry.metadata().await {
(
metadata
.created()
.unwrap_or(std::time::SystemTime::UNIX_EPOCH),
metadata
.accessed()
.unwrap_or(std::time::SystemTime::UNIX_EPOCH),
)
} else {
(
std::time::SystemTime::UNIX_EPOCH,
std::time::SystemTime::UNIX_EPOCH,
)
};
if entry.file_type().await?.is_dir() {
let entry = entry
.path()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
version_info.version = entry.to_owned();
if version_info.load().await.is_ok() {
result.push(Version {
name: entry,
version_type: version_info.guess_version_type(),
created_date,
access_date,
});
} else {
result.push(Version {
name: entry,
version_type: VersionType::Unknown,
created_date,
access_date,
});
}
}
}
Ok(result)
} else {
Ok(vec![])
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VersionType {
Vanilla,
Forge,
Fabric,
QuiltMC,
Optifine,
Unknown,
}
impl Default for VersionType {
fn default() -> Self {
Self::Unknown
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoadVersionInfoError {
VersionBaseMissing,
MainJarFileMissing,
VersionFileMissing,
VersionFileOpenFail,
SCLConfigFileOpenFail,
ParseError(String),
}