Skip to content

Commit

Permalink
[Mac] Use Resources Dir
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Jan 24, 2021
1 parent 0200682 commit 6d0cfa6
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 4 deletions.
Binary file not shown.
4 changes: 4 additions & 0 deletions .github/app/Neothesia.app/Contents/Resources/settings.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(
speed_multiplier: 1,
playback_offset: 0,
)
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
**/*.rs.bk
settings.ron
/settings.ron
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ serde = { version = "1.0.118", features = ["serde_derive"] }
# console_error_panic_hook = "0.1.6"
# console_log = "0.2.0"

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2.7"

[build-dependencies]
glsl-to-spirv = "0.1.7"
6 changes: 4 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub struct Config {

impl Config {
pub fn new() -> Self {
let config: Option<Config> = if let Ok(file) = std::fs::read_to_string("./settings.ron") {
let path = crate::resources::settings_ron();
let config: Option<Config> = if let Ok(file) = std::fs::read_to_string(path) {
match ron::from_str(&file) {
Ok(config) => Some(config),
Err(err) => {
Expand All @@ -30,7 +31,8 @@ impl Config {
impl Drop for Config {
fn drop(&mut self) {
if let Ok(s) = ron::ser::to_string_pretty(self, Default::default()) {
std::fs::write("./settings.ron", &s).ok();
let path = crate::resources::settings_ron();
std::fs::write(path, &s).ok();
}
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use winit::{

mod rectangle_pipeline;

mod resources;

pub struct MainState {
pub midi_file: Option<lib_midi::Midi>,
pub output_manager: OutputManager,
Expand Down
2 changes: 1 addition & 1 deletion src/output_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl OutputManager {
(desc, Box::new(synth.new_output_connection(&font)));
self.selected_font_path = Some(font);
} else {
let path = std::path::Path::new("./default.sf2");
let path = crate::resources::default_sf2();
if path.exists() {
let path = path.into();
self.output_connection =
Expand Down
50 changes: 50 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pub fn default_sf2() -> PathBuff {
#[cfg(not(target_os = "macos"))]
return std::path::Path::new("./default.sf2");
#[cfg(target_os = "macos")]
return bundled_resource_path("default", "sf2")
.unwrap_or(std::path::Path::new("./default.sf2"));
}

pub fn settings_ron() -> PathBuff {
#[cfg(not(target_os = "macos"))]
return std::path::Path::new("./settings.ron");
#[cfg(target_os = "macos")]
return bundled_resource_path("settings", "ron")
.unwrap_or(std::path::Path::new("./settings.ron"));
}

#[cfg(target_os = "macos")]
fn bundled_resource_path(name: &str, extension: &str) -> Option<String> {
use objc::runtime::{Class, Object};
use objc::{msg_send, sel, sel_impl};

unsafe {
let cls = Class::get("NSBundle").unwrap();
let bundle: *mut Object = msg_send![cls, mainBundle];
let cls = Class::get("NSString").unwrap();
let objc_str: *mut Object = msg_send![cls, alloc];
let objc_name: *mut Object = msg_send![objc_str,
initWithBytes:name.as_ptr()
length:name.len()
encoding: 4]; // UTF8_ENCODING
let objc_str: *mut Object = msg_send![cls, alloc];
let objc_ext: *mut Object = msg_send![objc_str,
initWithBytes:extension.as_ptr()
length:extension.len()
encoding: 4]; // UTF8_ENCODING
let ini: *mut Object = msg_send![bundle,
pathForResource:objc_name
ofType:objc_ext];
let _: () = msg_send![objc_name, release];
let _: () = msg_send![objc_ext, release];
let cstr: *const i8 = msg_send![ini, UTF8String];
if cstr != std::ptr::null() {
let rstr = std::ffi::CStr::from_ptr(cstr)
.to_string_lossy()
.into_owned();
return Some(rstr);
}
None
}
}

0 comments on commit 6d0cfa6

Please sign in to comment.