libobs_wrapper\utils/
path.rs1use std::{
2 env,
3 path::{Path, PathBuf},
4};
5
6use super::ObsString;
7
8#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
15pub struct ObsPath {
16 path: PathBuf,
17}
18
19impl ObsPath {
20 pub fn new(path_str: &str) -> Self {
26 Self {
27 path: Path::new(path_str).into(),
28 }
29 }
30
31 pub fn from_relative(path_str: &str) -> Self {
42 let obs_path = Self {
43 path: runtime_base_dir(),
44 };
45
46 let path_str = path_str.trim_matches('/');
47
48 obs_path.push(path_str)
49 }
50
51 pub fn push(mut self, value: &str) -> Self {
55 let split = value.split(['/', '\\'].as_ref());
56
57 for item in split {
58 if !item.is_empty() {
59 self.path.push(item);
60 }
61 }
62
63 self
64 }
65
66 pub fn pop(mut self) -> Self {
69 self.path.pop();
70 self
71 }
72
73 pub fn build(self) -> ObsString {
84 let mut bytes = self.path.display().to_string().replace("\\", "/");
85
86 if self.path.is_dir() {
87 bytes += "/";
88 }
89 let obs_string = ObsString::from(bytes.as_str());
90
91 drop(self);
92 obs_string
93 }
94}
95
96impl From<ObsPath> for ObsString {
97 fn from(val: ObsPath) -> Self {
98 val.build()
99 }
100}
101
102impl From<ObsPath> for PathBuf {
103 fn from(val: ObsPath) -> Self {
104 val.path
105 }
106}
107
108pub(crate) fn executable_dir() -> PathBuf {
110 match env::current_exe() {
111 Ok(mut path) => {
112 path.pop();
113 path
114 }
115 Err(err) => {
116 log::warn!("Could not resolve current executable for relative OBS path: {err}");
117 env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
118 }
119 }
120}
121
122pub(crate) fn cargo_profile_dir(exe_dir: &Path) -> Option<PathBuf> {
128 let name = exe_dir.file_name()?;
129 if name == "deps" {
130 return exe_dir.parent().map(Path::to_path_buf);
131 }
132
133 if name != "out" {
134 return None;
135 }
136
137 let build_dir = exe_dir.parent()?.parent()?.parent()?;
138 if build_dir.file_name()? != "build" {
139 return None;
140 }
141
142 build_dir.parent().map(Path::to_path_buf)
143}
144
145pub(crate) fn runtime_base_dir() -> PathBuf {
147 resolve_runtime_base_dir(&executable_dir())
148}
149
150const RUNTIME_MARKER: &str = "data/libobs";
151
152pub(crate) fn resolve_runtime_base_dir(exe_dir: &Path) -> PathBuf {
153 if exe_dir.join(RUNTIME_MARKER).is_dir() {
154 return exe_dir.to_path_buf();
155 }
156
157 match cargo_profile_dir(exe_dir) {
158 Some(profile_dir) if profile_dir.join(RUNTIME_MARKER).is_dir() => profile_dir,
159 _ => exe_dir.to_path_buf(),
160 }
161}
162
163#[cfg(any(windows, target_os = "macos"))]
167pub(crate) fn provide_runtime_helpers_next_to_executable() {
168 #[cfg(windows)]
169 const HELPERS: &[&str] = &["obs-ffmpeg-mux.exe", "obs-nvenc-test.exe"];
170 #[cfg(target_os = "macos")]
171 const HELPERS: &[&str] = &["obs-ffmpeg-mux"];
172
173 let exe_dir = executable_dir();
174 let Some(profile_dir) = cargo_profile_dir(&exe_dir) else {
175 return;
176 };
177
178 for helper in HELPERS {
179 let destination = exe_dir.join(helper);
180 let source = profile_dir.join(helper);
181 if destination.exists() || !source.is_file() {
182 continue;
183 }
184
185 #[cfg(windows)]
186 let result = std::fs::hard_link(&source, &destination)
187 .or_else(|_| std::fs::copy(&source, &destination).map(|_| ()));
188 #[cfg(target_os = "macos")]
189 let result = std::os::unix::fs::symlink(&source, &destination);
190
191 match result {
192 Ok(()) => log::debug!(
193 "[libobs-wrapper]: Linked OBS helper '{}' next to the executable",
194 source.display()
195 ),
196 Err(error) => log::warn!(
197 "[libobs-wrapper]: Failed to provide OBS helper '{}' at '{}': {error}",
198 source.display(),
199 destination.display()
200 ),
201 }
202 }
203}