Skip to content

Commit

Permalink
✨ Improve error/warning system; add com_mojang folder detection
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixr-codes committed Apr 6, 2024
1 parent e8a7e3f commit b8a2a6b
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 48 deletions.
54 changes: 23 additions & 31 deletions src/cli/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,33 @@ use std::process::ExitCode;
pub fn cmd() -> Command {
Command::new("explain")
.about("Explain an error or a warning")
.arg(Arg::new("id").help("The ID of the diagnostic (e.g. `W0001` or `E0001`)"))
.arg(Arg::new("id").help("The ID of the diagnostic without the prefix (e.g. `0001`)"))
}

pub fn run(matches: &ArgMatches) -> ExitCode {
let id: &String = matches.get_one("id").unwrap();
if id.starts_with(diagnostic::ERROR_PREFIX) {
// NOTE: there are no error codes yet
ExitCode::FAILURE
} else if id.starts_with(diagnostic::WARNING_PREFIX) {
let code: u8 = id
.strip_prefix(diagnostic::WARNING_PREFIX)
.unwrap()
.parse()
.expect("Code is invalid (must be 0-255)");
let w = diagnostic::Warning::from_code(code);
match w.extensive_description() {
Some(desc) => {
// TODO: open in pager
// if open::with(desc, std::env::var("PAGER").unwrap_or(String::from("less"))).is_err()
// {
println!("{}", desc);
// };
ExitCode::SUCCESS
}
None => {
log::error!("Warning exists but does not conatin an extensive description");
ExitCode::FAILURE
}
let code: u8 = id
.parse()
.expect(format!("Code is invalid (must be 0-255)",).as_str());
let notif = match diagnostic::Notification::from_code(code) {
Some(n) => n,
None => {
log::error!("Error or warning with code {} does not exist", code);
return ExitCode::FAILURE;
}
};
match notif.extensive_description() {
Some(desc) => {
// TODO: open in pager
// if open::with(desc, std::env::var("PAGER").unwrap_or(String::from("less"))).is_err()
// {
println!("{}", desc);
// };
ExitCode::SUCCESS
}
None => {
log::error!("Warning exists but does not conatin an extensive description");
ExitCode::FAILURE
}
} else {
log::error!(
"Invalid ID: it must either start with `{}` or `{}`",
diagnostic::ERROR_PREFIX,
diagnostic::WARNING_PREFIX
);
ExitCode::FAILURE
}
}
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod prelude;
mod schema;
#[cfg(feature = "share")]
mod share;
mod sync;
mod uuid;
#[cfg(feature = "watch")]
mod watch;
Expand Down Expand Up @@ -87,6 +88,7 @@ pub fn cmd() -> Command {
schema::cmd(),
#[cfg(feature = "share")]
share::cmd(),
sync::cmd(),
uuid::cmd(),
#[cfg(feature = "watch")]
watch::cmd(),
Expand Down Expand Up @@ -170,6 +172,7 @@ pub fn run(matches: &ArgMatches) -> ExitCode {
Some(("schema", m)) => schema::run(m),
#[cfg(feature = "share")]
Some(("share", m)) => share::run(m),
Some(("sync", m)) => sync::run(m),
Some(("uuid", m)) => uuid::run(m),
#[cfg(feature = "watch")]
Some(("watch", m)) => watch::run(m),
Expand Down
60 changes: 60 additions & 0 deletions src/cli/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use clap::{ArgMatches, Command};
use std::{env, path::PathBuf, process::ExitCode};

mod location {
use crate::diagnostic;
use std::{env, path::PathBuf};

fn android_termux() -> Option<PathBuf> {
let path = PathBuf::from(env::var_os("HOME").expect("HOME environment variable not set"))
.join("storage/shared/Android/data/com.mojang.minecraftpe/files/games/com.mojang");
if path.exists() {
Some(path)
} else {
log::error!("{}", diagnostic::Notification::ComMojangNotFoundAndroid);
None
}
}

fn windows() -> Option<PathBuf> {
let path = PathBuf::from(
env::var_os("LOCALAPPDATA").expect("LOCALAPPDATA environment variable not set"),
)
.join("Packages/Microsoft.MinecraftUWP_8wekyb3d8bbwe/LocalState/games/com.mojang");
if path.exists() {
Some(path)
} else {
log::error!("{}", diagnostic::Notification::ComMojangWindows);
None
}
}

pub fn get() -> Option<PathBuf> {
if cfg!(android) {
android_termux()
} else if cfg!(ios) {
log::error!("iOS devices are not supported for syncing yet");
None
} else if cfg!(windows) {
windows()
} else {
log::error!("Your OS is not supported for syncing");
None
}
}
}

pub fn cmd() -> Command {
Command::new("sync").about("Update the associated packs in the Minecraft directories")
}

pub fn run(matches: &ArgMatches) -> ExitCode {
let com_mojang: PathBuf = match env::var_os("COM_MOJANG") {
Some(var) => PathBuf::from(var),
None => match location::get() {
Some(loc) => loc,
None => return ExitCode::FAILURE,
},
};
todo!("find packs with same id as current project and update them")
}
14 changes: 14 additions & 0 deletions src/diagnostic/com_mojang_not_found_android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# `com.mojang` folder not found on Android

Make sure you

- are using [Termux][]
- have used the [`termux-setup-storage` command][termux-setup-storage]
- have set the file storage location of Minecraft to "External"
(Minecraft > Settings > Storage > File Storage Location)

If the steps above do not resolve your issues, find the `com.mojang` folder on your system and set the
environment variable `COM_MOJANG` to that path.

[Termux]: https://termux.dev/en/
[termux-setup-storage]: https://wiki.termux.com/wiki/Termux-setup-storage
71 changes: 57 additions & 14 deletions src/diagnostic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO: impl errors

use std::fmt::Display;

pub const ERROR_PREFIX: char = 'E';
Expand All @@ -10,18 +8,28 @@ pub trait Diagnostic {
fn extensive_description(&self) -> Option<&str>;
fn code(self) -> u8;
fn id(self) -> String;
fn from_code(code: u8) -> Self;
fn from_code(code: u8) -> Option<Self>
where
Self: Sized;
fn kind(self) -> Kind;
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Error,
Warning,
}

#[derive(Copy, Clone)]
#[repr(u8)]
pub enum Warning {
RedundantManifest = 1,
RedundantPackIcon = 2,
EmptyAddOn = 3,
pub enum Notification {
RedundantManifest,
RedundantPackIcon,
EmptyAddOn,
ComMojangNotFoundAndroid,
ComMojangWindows,
}

impl Diagnostic for Warning {
impl Diagnostic for Notification {
fn brief_description(&self) -> &str {
match self {
Self::RedundantManifest => {
Expand All @@ -31,6 +39,9 @@ impl Diagnostic for Warning {
"Found `pack_icon.png` but ignoring it as `custom-pack-icon` is not set to `true`"
}
Self::EmptyAddOn => "Add-On contains no packs",
Self::ComMojangNotFoundAndroid | Self::ComMojangWindows => {
"The `com.mojang` folder cannot be found"
}
}
}

Expand All @@ -39,23 +50,55 @@ impl Diagnostic for Warning {
Self::RedundantManifest => Some(include_str!("redundant_manifest.md")),
Self::RedundantPackIcon => Some(include_str!("redundant_pack_icon.md")),
Self::EmptyAddOn => None,
Self::ComMojangNotFoundAndroid => Some(include_str!("com_mojang_not_found_android.md")),
Self::ComMojangWindows => None,
}
}

fn code(self) -> u8 {
self as u8
match self {
Self::RedundantManifest => 1,
Self::RedundantPackIcon => 2,
Self::EmptyAddOn => 3,
Self::ComMojangNotFoundAndroid => 4,
Self::ComMojangWindows => 5,
}
}

fn id(self) -> String {
format!("W{:03}", self.code())
format!(
"{}{:03}",
match self.kind() {
Kind::Error => ERROR_PREFIX,
Kind::Warning => WARNING_PREFIX,
},
self.code()
)
}

fn from_code(code: u8) -> Option<Self> {
match code {
1 => Some(Self::RedundantManifest),
2 => Some(Self::RedundantPackIcon),
3 => Some(Self::EmptyAddOn),
4 => Some(Self::ComMojangNotFoundAndroid),
5 => Some(Self::ComMojangWindows),
_ => None,
}
}

fn from_code(code: u8) -> Self {
unsafe { std::mem::transmute::<u8, Self>(code) }
fn kind(self) -> Kind {
match self {
Self::RedundantManifest => Kind::Warning,
Self::RedundantPackIcon => Kind::Warning,
Self::EmptyAddOn => Kind::Warning,
Self::ComMojangNotFoundAndroid => Kind::Error,
Self::ComMojangWindows => Kind::Error,
}
}
}

impl Display for Warning {
impl Display for Notification {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand Down
6 changes: 3 additions & 3 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Project {
};
let p = dest.join("manifest.json");
if p.try_exists().unwrap_or(false) && p.is_file() {
log::warn!("{}", diagnostic::Warning::RedundantManifest);
log::warn!("{}", diagnostic::Notification::RedundantManifest);
};
fs::write(&p, json)?;
}
Expand All @@ -162,7 +162,7 @@ impl Project {
log::debug!("Copying pack icon");
let p = dest.join("pack_icon.png");
if p.try_exists().unwrap_or(false) && p.is_file() {
log::warn!("{}", diagnostic::Warning::RedundantPackIcon);
log::warn!("{}", diagnostic::Notification::RedundantPackIcon);
};
fs::copy(paths::pack_icon(), dest.join("pack_icon.png"))?;
}
Expand Down Expand Up @@ -233,7 +233,7 @@ impl Project {
}

if packs.is_empty() {
log::warn!("{}", diagnostic::Warning::EmptyAddOn);
log::warn!("{}", diagnostic::Notification::EmptyAddOn);
return Ok(());
}

Expand Down

0 comments on commit b8a2a6b

Please sign in to comment.