-
Notifications
You must be signed in to change notification settings - Fork 53
Change rbx_reflection_database to support loading from FS #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dekkonot
wants to merge
23
commits into
rojo-rbx:master
Choose a base branch
from
Dekkonot:dynamic-reflection-pog
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7145f07
Add local support to rbx_reflection_database
Dekkonot a4409b7
Add some logging
Dekkonot c5bfda2
Update changelog
Dekkonot ca2cbf0
Update README and module documentation
Dekkonot f8c833f
remove easily
Dekkonot 69d6a70
Specify that env var points to file, not dir
Dekkonot 6bd1b0b
Correctly refer to environment variables as such
Dekkonot f61eab3
Update crate documentation to reflect 69d6a70
Dekkonot d066132
Make `get_local` and `get` use Result
Dekkonot a9f3beb
Update usage of `get` in other crates
Dekkonot 2413092
Correctly handle local path not existing
Dekkonot 99171cb
Merge branch 'master' into dynamic-reflection-pog
Dekkonot d1eeac4
Merge branch 'master' into dynamic-reflection-pog
Dekkonot e5f8c05
Add PR number and link to changelog
Dekkonot 17128fe
Fix database tests
Dekkonot d95bc80
Undo formatting I did on accident
Dekkonot d525f03
Use LazyStatic + drop lazy_static as dependency
Dekkonot 7d639f3
Add test to confirm that the location returned by `get_local_location…
Dekkonot a1d2d37
Allow the `unnecessary_operation` clippy lint
Dekkonot 230c4ae
Handle env vars better
Dekkonot ec8a046
...Disable the clippy lint correctly?
Dekkonot 7328ded
Accept defeat, make a function that does what we're testing
Dekkonot bc78a6f
Merge branch 'master' into dynamic-reflection-pog
Dekkonot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,148 @@ | ||
//! Contains an API to get a Roblox reflection database using the types | ||
//! from [`rbx_reflection`]. This crate embeds a database for this purpose, | ||
//! but also provides an API for dependents to get a reflection database | ||
//! from a consistent location. | ||
//! | ||
//! The general way this crate should be used is via [`get`]. This method will | ||
//! search for a locally stored reflection database and return it if it's | ||
//! found. If it isn't, it will instead return the bundled one. | ||
//! | ||
//! Additionally, this crate exposes [`get_local`] and [`get_bundled`] for | ||
//! only loading the locally stored database or only the bundled one | ||
//! respectively. | ||
//! | ||
//! ## Local Details | ||
//! | ||
//! This crate will load a reflection database from the file system if one | ||
//! exists in the default location. This location varies upon the OS and is | ||
//! specified here: | ||
//! | ||
//! | OS | Location | | ||
//! |:--------|:--------------------------------------------------------------------| | ||
//! | Windows | `%localappdata%/.rbxreflection/database.msgpack` | | ||
//! | MacOS | `$HOME/Library/Application Support/.rbxreflection/database.msgpack` | | ||
//! | Linux | `$HOME/.rbxreflection/database.msgpack` | | ||
//! | ||
//! Additionally, a location override may be specified via the `RBX_DATABASE` | ||
//! environmental variable. | ||
//! | ||
//! Both the default `database.msgpack` files and any files pointed to by | ||
//! `RBX_DATABASE` must be valid MessagePack serializations of a | ||
//! [`ReflectionDatabase`] if they're present. | ||
use rbx_reflection::ReflectionDatabase; | ||
|
||
use std::{env, fs, path::PathBuf}; | ||
|
||
static ENCODED_DATABASE: &[u8] = include_bytes!("../database.msgpack"); | ||
|
||
/// The name of an environmental variable that may be used to specify | ||
/// the location of a reflection database to use. The expected format of | ||
/// a file at this point is MessagePack. | ||
pub const OVERRIDE_PATH_VAR: &str = "RBX_DATABASE"; | ||
|
||
/// The name of the directory used for the local location for a reflection | ||
/// database. The directory will be placed inside the current user's | ||
/// local data folder on MacOS and Windows and inside | ||
/// the home directory on Linux. | ||
pub const LOCAL_DIR_NAME: &str = ".rbxreflection"; | ||
|
||
lazy_static::lazy_static! { | ||
static ref DATABASE: ReflectionDatabase<'static> = { | ||
static ref BUNDLED_DATABASE: ReflectionDatabase<'static> = { | ||
log::debug!("Loading bundled reflection database"); | ||
rmp_serde::decode::from_slice(ENCODED_DATABASE).unwrap_or_else(|e| panic!("could not decode reflection database because: {}", e)) | ||
}; | ||
|
||
static ref LOCAL_DATABASE: Option<ReflectionDatabase<'static>> = { | ||
Dekkonot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let location = get_local_location()?; | ||
if let Ok(file) = fs::read(&location) { | ||
log::debug!("Loading local reflection database from {}", location.display()); | ||
Some( | ||
rmp_serde::decode::from_slice(&file).unwrap_or_else(|e| { | ||
panic!("could not decode reflection database because: {}", e) | ||
Dekkonot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
) | ||
} else { | ||
None | ||
} | ||
}; | ||
} | ||
|
||
/// Returns a populated [`ReflectionDatabase`]. This will attempt to load one locally and | ||
/// if one can't be found, it will return one that is bundled with this crate. | ||
/// | ||
/// ## Panics | ||
/// | ||
/// Panics if a locally stored [`ReflectionDatabase`] is not valid MessagePack. | ||
pub fn get() -> &'static ReflectionDatabase<'static> { | ||
&DATABASE | ||
get_local().unwrap_or(&BUNDLED_DATABASE) | ||
} | ||
|
||
/// Returns a reflection database from the file system, if one can be found. | ||
/// This is loaded from a location set by the `RBX_DATABASE` environmental | ||
/// variable if it's set. Otherwise, the default location is checked. | ||
/// | ||
/// The default location varies depending upon OS: | ||
/// | ||
/// | OS | Location | | ||
/// |:--------|:--------------------------------------------------------------------| | ||
/// | Windows | `%localappdata%/.rbxreflection/database.msgpack` | | ||
/// | MacOS | `$HOME/Library/Application Support/.rbxreflection/database.msgpack` | | ||
/// | Linux | `$HOME/.rbxreflection/database.msgpack` | | ||
/// | ||
/// The file at the above location (or the one pointed to by `RBX_DATABASE`) | ||
/// must be valid MessagePack. | ||
/// | ||
/// ## Panics | ||
/// | ||
/// Panics if the file specified by `RBX_DATABASE` or in the default location | ||
/// exists but is invalid MessagePack. | ||
pub fn get_local() -> Option<&'static ReflectionDatabase<'static>> { | ||
Dekkonot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LOCAL_DATABASE.as_ref() | ||
} | ||
|
||
/// Returns the locally bundled [`ReflectionDatabase`]. This database may or may | ||
/// not be up to date, but it will always exist. | ||
pub fn get_bundled() -> &'static ReflectionDatabase<'static> { | ||
&BUNDLED_DATABASE | ||
} | ||
|
||
/// Fetches the location a [`ReflectionDatabase`] is expected to be loaded from. | ||
/// This may return [`None`] if the local data directory cannot be found. | ||
pub fn get_local_location() -> Option<PathBuf> { | ||
if let Ok(location) = env::var(OVERRIDE_PATH_VAR) { | ||
log::debug!("Using enviromental variable {OVERRIDE_PATH_VAR} to fetch reflection database"); | ||
Dekkonot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Some(PathBuf::from(location)) | ||
} else { | ||
// Due to concerns about the local data directory existing | ||
// on Linux, we use the home directory instead. | ||
#[cfg(target_os = "linux")] | ||
let mut home = dirs::home_dir()?; | ||
#[cfg(not(target_os = "linux"))] | ||
let mut home = dirs::data_local_dir()?; | ||
|
||
home.push(LOCAL_DIR_NAME); | ||
home.push("database.msgpack"); | ||
Some(home) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn smoke_test() { | ||
let _database = get(); | ||
fn bundled() { | ||
let _database = get_bundled(); | ||
} | ||
|
||
#[test] | ||
fn env_var() { | ||
let mut test_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
test_path.push("empty.msgpack"); | ||
|
||
env::set_var(OVERRIDE_PATH_VAR, &test_path); | ||
let empty_db = get(); | ||
println!("{:?}", empty_db.version); | ||
assert!(empty_db.version == [0, 0, 0, 0]); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.