Skip to content

Commit

Permalink
use path/pathbuf
Browse files Browse the repository at this point in the history
  • Loading branch information
Sequal32 committed Feb 14, 2021
1 parent 35f751e commit 166cd98
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 54 deletions.
6 changes: 3 additions & 3 deletions src-tauri/src/finder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::BufRead, env, fs::File, io::BufReader};
use std::{env, fs::File, io::BufRead, io::BufReader, path::PathBuf};

use crate::util::Error;

Expand Down Expand Up @@ -29,7 +29,7 @@ impl FlightSimFinder {
)
}

pub fn get_package_location() -> Result<String, Error> {
pub fn get_package_location() -> Result<PathBuf, Error> {
let file = Self::get_config_file()?;
let reader = BufReader::new(file);

Expand All @@ -49,7 +49,7 @@ impl FlightSimFinder {
None => return Err(Error::MissingQuote)
};

return Ok(line[first_quote + 1..closing_quote + 1].to_string() + "\\Community")
return Ok([&line[first_quote + 1..closing_quote + 1], "Community"].iter().collect())

}

Expand Down
67 changes: 34 additions & 33 deletions src-tauri/src/installer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::{ffi::OsStr, path::Path};
#[cfg(windows)]
use std::{collections::HashSet, fs, io::{self, Cursor}, path::{PathBuf}};
use log::{error, info, warn};
Expand Down Expand Up @@ -30,39 +31,39 @@ fn add_to_generator(generator: &mut SizeGenerator, relative_path: &str, file: &Z
}

pub struct Installer {
package_dir: String,
program_dir: String
package_dir: PathBuf,
program_dir: PathBuf
}

impl Installer {
pub fn new() -> Self {
Self {
package_dir: String::new(),
program_dir: String::new()
package_dir: PathBuf::new(),
program_dir: PathBuf::new()
}
}

pub fn store_program_path(&self, path: &String) -> Result<(), io::Error> {
pub fn store_program_path(&self, path: &str) -> Result<(), io::Error> {
let hklm = winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER);
let (subkey, _) = hklm.create_subkey("Software\\YourControls")?;

subkey.set_value("path", path)?;
subkey.set_value("path", &path)?;

Ok(())
}

pub fn get_program_path_from_registry(&self) -> Result<String, io::Error> {
pub fn get_program_path_from_registry(&self) -> Result<PathBuf, io::Error> {
let hklm = winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER);
let subkey = hklm.open_subkey("Software\\YourControls")?;

subkey.get_value("path")
subkey.get_value("path").map(|x: String| PathBuf::from(x))
}

pub fn set_package_dir(&mut self, package_dir: String) {
pub fn set_package_dir(&mut self, package_dir: PathBuf) {
self.package_dir = package_dir;
}

pub fn set_program_dir(&mut self, program_dir: String) {
pub fn set_program_dir(&mut self, program_dir: PathBuf) {
self.program_dir = program_dir;
}

Expand Down Expand Up @@ -91,9 +92,11 @@ impl Installer {
}

pub fn remove_package(&self) -> Result<(), io::Error> {
match fs::remove_dir_all(format!("{}\\YourControls", self.package_dir)) {
let package_dir = self.package_dir.join("YourControls");

match fs::remove_dir_all(package_dir) {
Ok(_) => {
info!("Removed existing package installation {}", self.package_dir);
info!("Removed existing package installation {:?}", self.package_dir);
Ok(())
},
Err(e) => {
Expand All @@ -107,9 +110,9 @@ impl Installer {
let path = self.get_exe_path();

if path.exists() {
return match fs::remove_dir_all(self.program_dir.clone()) {
return match fs::remove_dir_all(&self.program_dir) {
Ok(_) => {
info!("Removed exe folder contents at {}", self.program_dir);
info!("Removed exe folder contents at {:?}", self.program_dir);
Ok(())
},
Err(e) => {
Expand All @@ -128,14 +131,14 @@ impl Installer {
}

pub fn get_exe_path(&self) -> PathBuf {
format!("{}\\{}", self.program_dir, EXE_NAME).into()
self.program_dir.join(EXE_NAME)
}

pub fn get_program_dir(&self) -> &String {
pub fn get_program_dir(&self) -> &PathBuf {
&self.program_dir
}

pub fn get_package_dir(&self) -> &String {
pub fn get_package_dir(&self) -> &PathBuf {
&self.package_dir
}

Expand All @@ -159,53 +162,51 @@ impl Installer {

// Overwrite installation
for i in 0..contents.len() {
let mut file = contents.by_index(i).unwrap();
let mut content = contents.by_index(i).unwrap();

// Determine whether community or program files
let (relative_path, install_location) = match self.get_relative_path_for_file(file.name(), &feature_paths) {
let (relative_path, install_location) = match self.get_relative_path_for_file(content.name(), &feature_paths) {
Some(d) => d,
None => continue
};

let full_path = match install_location {
InstallLocation::Package => {
if file.is_file() {
add_to_generator(&mut generator, &relative_path, &file)?;
if content.is_file() {
add_to_generator(&mut generator, &relative_path, &content)?;
}

format!("{}\\{}", self.package_dir, relative_path)
self.package_dir.join(relative_path)
}
InstallLocation::Program => {
format!("{}\\{}", self.program_dir, relative_path)
self.program_dir.join(relative_path)
}
};

// Write dir
info!("Writing {}", full_path);
if file.is_dir() {

fs::create_dir(full_path).ok();

} else {
info!("Writing {:?}", full_path);
if content.is_file() {
info!("Creating dir(s) {:?}", full_path.parent());
fs::create_dir_all(full_path.parent().unwrap()).ok();
// Write file
let mut file_handle = match fs::File::create(full_path) {
Ok(file) => file,
Err(e) => return Err(Error::IOError(e))
};

io::copy(&mut file, &mut file_handle).ok();
io::copy(&mut content, &mut file_handle).ok();
}
}

match self.store_program_path(&self.program_dir.to_string()) {
Ok(_) => info!("Wrote {} to registry.", self.program_dir),
match self.store_program_path(&self.program_dir.to_str().unwrap_or("")) {
Ok(_) => info!("Wrote {:?} to registry.", self.program_dir),
Err(e) => {
warn!("Could not write to registry, Reason: {}", e);
}
}

// Write layout.json
match generator.write_to_file(&format!("{}\\YourControls\\layout.json", self.package_dir)) {
match generator.write_to_file(self.package_dir.join("YourControls/layout.json")) {
Ok(_) => {}
Err(e) => {
error!("Could not write layout.json!");
Expand Down
32 changes: 16 additions & 16 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod finder;
mod sizegenerator;
mod util;

use std::{env, fs::File, thread::{sleep, spawn}, time::Duration};
use std::{env, fs::File, path::PathBuf, thread::{sleep, spawn}, time::Duration};
use std::fmt::Display;
use crossbeam_channel::unbounded;
use downloader::{Downloader, ReleaseData};
Expand Down Expand Up @@ -72,27 +72,26 @@ fn main() {
Err(_) => match env::var("APPDATA") {
Ok(path) => {
info!("Using default installation path.");
path + "\\YourControls"
PathBuf::from(path)
},
Err(e) => {
error!("Could not use any installation path. Reason: {}", e);
"YourControls".to_string()
PathBuf::from("YourControls")
}
}
};

info!("Installation path: {}", default_install_path);
info!("Installation path: {:?}", default_install_path);
installer.set_program_dir(default_install_path.clone());
//
let default_package_path = match finder::FlightSimFinder::get_package_location() {
Ok(path) => {
info!("Found package location: {}", path);

info!("Found package location: {:?}", path);
path
},
Err(e) => {
error!("Could not find any installation path. Reason: {}", e);
"Community\\YourControls".to_string()
error!("Could not find any installation path. Reason: {:?}", e);
PathBuf::from("Community/YourControls")
}
};

Expand Down Expand Up @@ -137,8 +136,8 @@ fn main() {

Ok(StartupResponse {
feature_list: feature_list.clone(),
package_directory: default_package_path,
program_directory: default_install_path,
package_directory: default_package_path.to_string_lossy().into_owned(),
program_directory: default_install_path.to_string_lossy().into_owned(),
release_data
})

Expand All @@ -156,20 +155,21 @@ fn main() {

println!("{:?}", installer.get_package_dir());

to_main_tx.send(AppMessage::Browse(open_path.clone())).ok();
to_main_tx.send(AppMessage::Browse(open_path.to_string_lossy().into_owned())).ok();

let location = match to_app_rx.recv() {
Ok(AppMessage::BrowseResult(Ok(dialog::Response::Okay(mut location)))) => {
Ok(AppMessage::BrowseResult(Ok(dialog::Response::Okay(location)))) => {
let mut location_path = PathBuf::from(&location);

match browse_for {
cmd::BrowseFor::Program => {
if !location.ends_with("YourControls") {
location += "\\YourControls";
if !location_path.ends_with("YourControls") {
location_path.push("YourControls");
}
installer.set_program_dir(location.clone());
installer.set_program_dir(location_path);
}
cmd::BrowseFor::Package => {
installer.set_package_dir(location.clone());
installer.set_package_dir(location_path);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/sizegenerator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::File};
use std::{fs::File, path::Path};
use chrono::offset::Utc;
use serde_json::{json};
use serde::{Serialize};
Expand Down Expand Up @@ -39,7 +39,7 @@ impl SizeGenerator {
Ok(())
}

pub fn write_to_file(&self, path: &str) -> Result<(), Error> {
pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let data = json!({
"content": self.file_data
});
Expand Down

0 comments on commit 166cd98

Please sign in to comment.