-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworld.rs
117 lines (98 loc) · 3.5 KB
/
world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::{
env, fs, io,
path::{Path, PathBuf},
};
use crate::error::Error;
/// Exports the given world to the `minecraftWorlds` directory.
pub fn export(name: &str, worlds: &[String], target: &str, overwrite: bool) -> Result<(), Error> {
let from = local_worlds_dir(worlds, name)?;
let to = mojang_worlds_dir(name, target)?;
if to.exists() && !overwrite {
return Err(Error::CannotOverwriteWorld(name.to_string()));
}
copy_dir(&from, &to).map_err(|e| Error::CannotCopyWorld(e.kind(), name.to_string()))?;
Ok(())
}
/// Imports the given world from the `minecraftWorlds` directory.
pub fn import(name: &str, worlds: &[String], target: &str) -> Result<(), Error> {
let from = mojang_worlds_dir(name, target)?;
let to: PathBuf = local_worlds_dir(worlds, name)?;
copy_dir(&from, &to).map_err(|e| Error::CannotCopyWorld(e.kind(), name.to_string()))?;
Ok(())
}
/// Returns the list of worlds from the given glob patterns.
pub fn all_worlds(globs: &[String]) -> Result<Vec<String>, Error> {
let worlds = globs
.iter()
.map(|pattern| {
glob::glob(pattern).map_err(|e| Error::InvalidWorldsGlobPattern(e, pattern.to_string()))
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.filter_map(Result::ok)
.filter(|p| p.is_dir())
.map(|p| p.file_name().unwrap().to_string_lossy().to_string())
.collect::<Vec<_>>();
if worlds.is_empty() {
return Err(Error::EmptyWorldsProperty);
}
Ok(worlds)
}
/// Returns local worlds directory from the given glob patterns.
fn local_worlds_dir(globs: &[String], name: &str) -> Result<PathBuf, Error> {
let paths = globs
.iter()
.map(|pattern| {
glob::glob(pattern).map_err(|e| Error::InvalidWorldsGlobPattern(e, pattern.to_string()))
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.filter_map(Result::ok)
.filter(|p| p.is_dir())
.collect::<Vec<_>>();
if paths.is_empty() {
return Err(Error::EmptyWorldsProperty);
}
match paths.iter().find(|p| p.ends_with(name)) {
Some(path) => Ok(path.clone()),
None => Err(Error::WorldNotFound(paths, name.to_string())),
}
}
/// Returns the path to the mojang worlds directory.
fn mojang_worlds_dir(name: &str, target: &str) -> Result<PathBuf, Error> {
let target = match target {
"stable" => "Microsoft.MinecraftUWP_8wekyb3d8bbwe",
"preview" => "Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe",
"education" => "Microsoft.MinecraftEducationEdition_8wekyb3d8bbwe",
path => return Ok(PathBuf::from(path)),
};
env::var("LOCALAPPDATA")
.map(|base| {
PathBuf::from(&base)
.join("Packages")
.join(target)
.join("LocalState")
.join("games")
.join("com.mojang")
.join("minecraftWorlds")
.join(name)
})
.ok()
.ok_or_else(Error::CannotFindLocalAppData)
}
/// Copies a directory recursively.
fn copy_dir(from: &Path, to: &Path) -> Result<(), io::Error> {
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let file_type = entry.file_type()?;
if file_type.is_dir() {
copy_dir(&entry.path(), &to.join(entry.file_name()))?;
} else {
fs::copy(&entry.path(), &to.join(entry.file_name()))?;
}
}
Ok(())
}