use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use crate::prelude::*;
use crate::wad::elem::GMElement;
use crate::wad::load::reader::DataReader;
use crate::wad::save::writer::DataWriter;
#[derive(Clone, Copy, PartialEq)]
pub struct IdeVersion {
pub major: u32,
pub minor: u32,
pub release: u32,
pub build: u32,
}
impl IdeVersion {
pub const GMS2: Self = Self::new(2, 0, 0, 0);
#[must_use]
pub const fn new(major: u32, minor: u32, release: u32, build: u32) -> Self {
Self { major, minor, release, build }
}
#[must_use]
pub const fn is_beta(self) -> bool {
self.major == 1 && self.build < 1000
}
}
impl GMElement for IdeVersion {
fn deserialize(reader: &mut DataReader) -> Result<Self> {
let major = reader.read_u32()?;
let minor = reader.read_u32()?;
let release = reader.read_u32()?;
let build = reader.read_u32()?;
Ok(Self { major, minor, release, build })
}
fn serialize(&self, writer: &mut DataWriter) -> Result<()> {
writer.write_u32(self.major);
writer.write_u32(self.minor);
writer.write_u32(self.release);
writer.write_u32(self.build);
Ok(())
}
}
impl fmt::Debug for IdeVersion {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}.{}.{}.{}", self.major, self.minor, self.release, self.build)
}
}
impl Display for IdeVersion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let major = self.major;
let minor = self.minor;
let release = self.release;
let build = self.build;
if major == 1 {
return if let Some(minor) = gms1_minor_by_build(build) {
write!(f, "1.{minor}.{build}")
} else {
write!(f, "1.X.{build}")
};
}
write!(f, "{major}.{minor}")?;
if release != 0 || build != 0 {
write!(f, ".{release}")?;
if build != 0 {
write!(f, ".{build}")?;
}
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum GMVersion {
Wad12,
Wad13,
Wad14,
Wad14Beta,
Wad15,
Wad16,
Wad16Pad,
GMS2,
GMS2_0_6,
GMS2_2_1,
GMS2_2_2_302,
GMS2_3,
GMS2_3_1,
GMS2_3_2,
GMS2_3_6,
GM2022_1,
GM2022_2,
GM2022_3,
GM2022_5,
GM2022_6,
GM2022_8,
GM2022_9,
Lts2022_0_3,
GM2023_1,
GM2023_2,
GM2023_4,
GM2023_6,
GM2023_8,
GM2023_11,
GM2024_2,
GM2024_4,
GM2024_6,
GM2024_8,
GM2024_11,
GM2024_13,
GM2024_14,
GM2024_14_1,
GM2026_1,
}
impl GMVersion {
#[must_use]
pub const fn wad_version(self) -> u8 {
match self {
Self::Wad12 => 12,
Self::Wad13 => 13,
Self::Wad14 | Self::Wad14Beta => 14,
Self::Wad15 => 15,
Self::Wad16 | Self::Wad16Pad | Self::GMS2 | Self::GMS2_0_6 => 16,
_ => 17,
}
}
}
impl Display for GMVersion {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let s = match self {
Self::Wad12 => "WAD 12",
Self::Wad13 => "WAD 13",
Self::Wad14 => "WAD 14",
Self::Wad14Beta => "WAD 14 (Beta)",
Self::Wad15 => "WAD 15",
Self::Wad16 => "WAD 16 (pre-GMS 2)",
Self::Wad16Pad => "WAD 16 (GMS 1.4.9999+)",
Self::GMS2 => "GMS 2",
Self::GMS2_0_6 => "GMS 2.0.6",
Self::GMS2_2_1 => "GMS 2.2.1",
Self::GMS2_2_2_302 => "GMS 2.2.2.302",
Self::GMS2_3 => "GMS 2.3",
Self::GMS2_3_1 => "GMS 2.3.1",
Self::GMS2_3_2 => "GMS 2.3.2",
Self::GMS2_3_6 => "GMS 2.3.6",
Self::GM2022_1 => "2022.1",
Self::GM2022_2 => "2022.2",
Self::GM2022_3 => "2022.3",
Self::GM2022_5 => "2022.5",
Self::GM2022_6 => "2022.6",
Self::GM2022_8 => "2022.8",
Self::GM2022_9 => "2022.9", Self::Lts2022_0_3 => "2022.0.3 LTS",
Self::GM2023_1 => "2023.1",
Self::GM2023_2 => "2023.2",
Self::GM2023_4 => "2023.4",
Self::GM2023_6 => "2022.6",
Self::GM2023_8 => "2023.8",
Self::GM2023_11 => "2023.11",
Self::GM2024_2 => "2024.2",
Self::GM2024_4 => "2024.4",
Self::GM2024_6 => "2024.6",
Self::GM2024_8 => "2024.8",
Self::GM2024_11 => "2024.11",
Self::GM2024_13 => "2024.13",
Self::GM2024_14 => "2024.14",
Self::GM2024_14_1 => "2024.14.1",
Self::GM2026_1 => "2026.1 LTS",
};
f.write_str(s)
}
}
#[must_use]
#[rustfmt::skip]
pub const fn gms1_minor_by_build(build_number: u32) -> Option<u8> {
match build_number {
23 | 31 | 52 | 83 | 95 | 97 | 98 | 114 | 125 | 128 | 129 | 131 | 142 | 144 | 153 | 183 | 187 | 191 | 198 | 202 | 210 | 217 | 230 | 231 | 240 | 249 | 253 | 263 | 282 | 292 | 298 | 299 | 303 | 305 | 316 | 327 | 342 => Some(0),
412 | 416 | 450 | 469 | 513 | 522 | 533 | 560 | 609 | 615 | 622 | 666 | 676 | 690 | 694 | 711 | 734 | 750 | 754 | 867 | 872 | 913 | 917 | 929 | 964 | 1013 | 1044 | 1058 | 1076 | 1089 => Some(1),
1113 | 1130 | 1135 | 1146 | 1150 | 1161 | 1214 | 1220 | 1224 | 1262 | 1264 | 1279 => Some(2),
1307 | 1312 | 1336 | 1344 | 1347 | 1354 | 1373 | 1379 | 1386 => Some(3),
1398 | 1451 | 1474 | 1499 | 1514 | 1522 | 1542 | 1556 | 1567 | 1598 | 1629 | 1657 | 1675 | 1683 | 1690 | 1711 | 1749 | 1757 | 1760 | 1763 | 1767 | 1772 | 1773 | 1788 | 1804 | 9999 => Some(4),
_ => None,
}
}