-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.rs
154 lines (132 loc) · 4.44 KB
/
lib.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::{
fmt, io,
path::{Path, PathBuf},
};
#[cfg(target_os = "windows")]
use winreg::RegKey;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
DocumentsDirectoryNotFound,
MalformedRegistry,
PlatformNotSupported,
PluginsDirectoryNotFound,
RegistryError(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::DocumentsDirectoryNotFound => write!(formatter, "Couldn't find Documents directory"),
Error::PluginsDirectoryNotFound => write!(formatter, "Couldn't find Plugins directory"),
Error::MalformedRegistry => write!(formatter, "The values of the registry keys used to find Roblox are malformed, maybe your Roblox installation is corrupt?"),
Error::PlatformNotSupported => write!(formatter, "Your platform is not currently supported"),
Error::RegistryError(error) => write!(formatter, "Couldn't find registry keys, Roblox might not be installed. ({})", error),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let Error::RegistryError(error) = self {
Some(error)
} else {
None
}
}
}
#[derive(Debug)]
#[must_use]
pub struct RobloxStudio {
content: PathBuf,
application: PathBuf,
built_in_plugins: PathBuf,
plugins: PathBuf,
root: PathBuf,
}
impl RobloxStudio {
#[cfg(target_os = "windows")]
pub fn locate() -> Result<RobloxStudio> {
let hkcu = RegKey::predef(winreg::enums::HKEY_CURRENT_USER);
let roblox_studio_reg = hkcu
.open_subkey(r"Software\Roblox\RobloxStudio")
.map_err(Error::RegistryError)?;
let content_folder_value: String = roblox_studio_reg
.get_value("ContentFolder")
.map_err(Error::RegistryError)?;
let content_folder_path = PathBuf::from(content_folder_value);
let root = content_folder_path
.parent()
.ok_or(Error::MalformedRegistry)?
.to_path_buf();
let user_dir = dirs::home_dir().ok_or(Error::PluginsDirectoryNotFound)?;
let plugin_dir = user_dir
.join("AppData")
.join("Local")
.join("Roblox")
.join("Plugins");
Ok(RobloxStudio {
content: content_folder_path,
application: root.join("RobloxStudioBeta.exe"),
built_in_plugins: root.join("BuiltInPlugins"),
plugins: plugin_dir,
root,
})
}
#[cfg(target_os = "macos")]
pub fn locate() -> Result<RobloxStudio> {
let root = PathBuf::from("/Applications").join("RobloxStudio.app");
let contents = root.join("Contents");
let application = contents.join("MacOS").join("RobloxStudio");
let built_in_plugins = contents.join("Resources").join("BuiltInPlugins");
let documents = dirs::document_dir().ok_or(Error::DocumentsDirectoryNotFound)?;
let plugins = documents.join("Roblox").join("Plugins");
let content = contents.join("Resources").join("content");
Ok(RobloxStudio {
content,
application,
built_in_plugins,
plugins,
root,
})
}
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
#[inline]
pub fn locate() -> Result<RobloxStudio> {
Err(Error::PlatformNotSupported)
}
#[deprecated(
since = "0.2.0",
note = "The contents of the studio directory are inconsistent across platforms. \
Please use a dedicated method (like application_path) or file a feature request if one does not exist."
)]
#[must_use]
#[inline]
pub fn root_path(&self) -> &Path {
&self.root
}
#[must_use]
#[inline]
pub fn application_path(&self) -> &Path {
&self.application
}
#[must_use]
#[inline]
pub fn content_path(&self) -> &Path {
&self.content
}
#[deprecated(since = "0.2.0", note = "Please use application_path instead.")]
#[must_use]
#[inline]
pub fn exe_path(&self) -> PathBuf {
self.application_path().to_owned()
}
#[must_use]
#[inline]
pub fn built_in_plugins_path(&self) -> &Path {
&self.built_in_plugins
}
#[must_use]
#[inline]
pub fn plugins_path(&self) -> &Path {
&self.plugins
}
}