diff options
author | Daniel Schadt <kingdread@gmx.de> | 2021-11-13 11:18:31 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2021-11-13 11:18:31 +0100 |
commit | 786d5e2ba1544f29445651f2d2aa52ba0fba9fbd (patch) | |
tree | 579bdd7d7cbfa3f16d0464e7a9b8618593a4379e | |
parent | 2ee0f5c5df9c9a02dc81f9597c28894ee26dddb0 (diff) | |
download | modderbaas-786d5e2ba1544f29445651f2d2aa52ba0fba9fbd.tar.gz modderbaas-786d5e2ba1544f29445651f2d2aa52ba0fba9fbd.tar.bz2 modderbaas-786d5e2ba1544f29445651f2d2aa52ba0fba9fbd.zip |
Properly extract mod name from path for legacy mod
We introduced the change to make MineMod compatible with mods that do
not have a mod.conf, but we forgot that read_conf()? will just make the
function return early in that case - and the fallback value is never
used.
This change makes it so that read_conf() is only called when a mod.conf
exists, which has the benefit that we get the legacy name resolution for
mods without mod.conf, but if there are I/O errors while reading the
config, we will still propagate them properly (same as dependencies()).
-rw-r--r-- | modderbaas/src/minemod.rs | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/modderbaas/src/minemod.rs b/modderbaas/src/minemod.rs index 6be0ab4..00acc3a 100644 --- a/modderbaas/src/minemod.rs +++ b/modderbaas/src/minemod.rs @@ -43,6 +43,9 @@ pub struct MineMod { path: PathBuf, } +/// The name of the mod configuration file. +const MOD_CONF: &str = "mod.conf"; + impl MineMod { /// Opens the given directory as a mod. pub fn open<P: AsRef<Path>>(path: P) -> Result<MineMod> { @@ -50,7 +53,7 @@ impl MineMod { } fn open_path(path: &Path) -> Result<MineMod> { - let conf = path.join("mod.conf"); + let conf = path.join(MOD_CONF); let depends = path.join("depends.txt"); if !conf.is_file() && !depends.is_file() { return Err(Error::InvalidModDir(path.into())); @@ -65,7 +68,7 @@ impl MineMod { } fn read_conf(&self) -> Result<HashMap<String, String>> { - let conf = self.path.join("mod.conf"); + let conf = self.path.join(MOD_CONF); kvstore::read(&conf) } @@ -76,12 +79,19 @@ impl MineMod { .file_name() .map(|s| s.to_str().expect("Non-UTF8 directory encountered")); - let conf = self.read_conf()?; - conf.get("name") - .map(String::as_str) - .or(fallback) - .map(Into::into) - .ok_or_else(|| Error::InvalidModDir(self.path.clone())) + // Same reasoning as dependencies() + if self.path.join(MOD_CONF).is_file() { + let conf = self.read_conf()?; + conf.get("name") + .map(String::as_str) + .or(fallback) + .map(Into::into) + .ok_or_else(|| Error::InvalidModDir(self.path.clone())) + } else { + fallback + .map(Into::into) + .ok_or_else(|| Error::InvalidModDir(self.path.clone())) + } } /// Returns all dependencies of this mod. @@ -89,7 +99,7 @@ impl MineMod { // We could do this with Result combinators and default values, but we also don't want to // end up swallowing errors (like filesystem errors). Therefore, we do some basic checks // first and propagate the errors. - if self.path.join("mod.conf").is_file() { + if self.path.join(MOD_CONF).is_file() { // First try to extract dependencies from mod.conf (the new way) let conf = self.read_conf()?; if let Some(depstr) = conf.get("depends") { |