Skip to main content

roblox_studio_utils/paths/
mod.rs

1use std::{
2    path::{Path, PathBuf},
3    sync::Arc,
4};
5
6use crate::RobloxStudioResult;
7
8#[cfg(target_os = "linux")]
9mod linux;
10#[cfg(target_os = "macos")]
11mod macos;
12#[cfg(target_os = "windows")]
13mod windows;
14
15/**
16    References to discovered, validated paths to the current
17    Roblox Studio executable, content, and plugins directories.
18
19    Can be cheaply cloned and shared between threads.
20*/
21#[derive(Debug, Clone)]
22pub struct RobloxStudioPaths {
23    inner: Arc<RobloxStudioPathsInner>,
24}
25
26impl RobloxStudioPaths {
27    /**
28        Tries to locate the current Roblox Studio installation and directories.
29
30        # Errors
31
32        - If Roblox Studio is not installed.
33    */
34    pub fn new() -> RobloxStudioResult<Self> {
35        RobloxStudioPathsInner::new().map(Self::from)
36    }
37
38    /**
39        Returns the path to the Roblox Studio executable.
40    */
41    #[must_use]
42    pub fn exe(&self) -> &Path {
43        self.inner.exe.as_path()
44    }
45
46    /**
47        Returns the path to the Roblox Studio content directory.
48
49        This directory contains Roblox bundled assets, in sub-directories such as:
50
51        - `fonts` - bundled font files, typically in OpenType or TrueType format
52        - `sounds` - bundled basic sounds, such as the character reset sound
53        - `textures` - bundled texture files, typically used for `CoreGui`
54    */
55    #[must_use]
56    pub fn content(&self) -> &Path {
57        self.inner.content.as_path()
58    }
59
60    /**
61        Returns the path to the Roblox Studio **user plugins** directory.
62
63        For the path to built-in plugins, see [`RobloxStudioPaths::built_in_plugins`].
64
65        # Warning
66
67        This directory may or may not exist as it is created on demand,
68        either when a user opens it through the Roblox Studio settings,
69        or when they install their first plugin.
70    */
71    #[must_use]
72    pub fn user_plugins(&self) -> &Path {
73        self.inner.plugins_user.as_path()
74    }
75
76    /**
77        Returns the path to the Roblox Studio **built-in plugins** directory.
78
79        These plugins are bundled with Roblox Studio itself, and the directory is guaranteed
80        to exist unlike the user plugins directory ([`RobloxStudioPaths::user_plugins`]).
81    */
82    #[must_use]
83    pub fn built_in_plugins(&self) -> &Path {
84        self.inner.plugins_builtin.as_path()
85    }
86}
87
88// Private inner struct to make RobloxStudioPaths cheaper to clone
89#[derive(Debug, Clone)]
90struct RobloxStudioPathsInner {
91    exe: PathBuf,
92    content: PathBuf,
93    plugins_user: PathBuf,
94    plugins_builtin: PathBuf,
95}
96
97impl From<RobloxStudioPathsInner> for RobloxStudioPaths {
98    fn from(inner: RobloxStudioPathsInner) -> Self {
99        Self {
100            inner: Arc::new(inner),
101        }
102    }
103}