From 44b148f2a458d3c130095169c8502d4aaa88a238 Mon Sep 17 00:00:00 2001 From: ffauzan <32434534+ffauzan@users.noreply.github.com> Date: Mon, 18 Jul 2022 22:03:01 +0700 Subject: [PATCH 1/5] Fix toggle encryption Open the config file in write mode so the toggle encryption button is actually working --- src-tauri/src/file_helpers.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/file_helpers.rs b/src-tauri/src/file_helpers.rs index bcfeaee..68c4878 100644 --- a/src-tauri/src/file_helpers.rs +++ b/src-tauri/src/file_helpers.rs @@ -138,7 +138,7 @@ pub fn write_file(path: String, contents: String) { let path_buf = std::path::PathBuf::from(&path); // Create file if it exists, otherwise just open and rewrite - let mut file = match fs::File::open(&path_buf) { + let mut file = match fs::File::options().write(true).truncate(true).open(&path_buf) { Ok(file) => file, Err(e) => { println!("Failed to open file: {}", e); @@ -162,4 +162,4 @@ pub fn write_file(path: String, contents: String) { return; } } -} \ No newline at end of file +} From c7954d2294688303b0770711faef02c1064a5433 Mon Sep 17 00:00:00 2001 From: ffauzan <32434534+ffauzan@users.noreply.github.com> Date: Tue, 19 Jul 2022 06:39:29 +0700 Subject: [PATCH 2/5] Update file_helpers.rs --- src-tauri/src/file_helpers.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/file_helpers.rs b/src-tauri/src/file_helpers.rs index 68c4878..db06794 100644 --- a/src-tauri/src/file_helpers.rs +++ b/src-tauri/src/file_helpers.rs @@ -138,19 +138,11 @@ pub fn write_file(path: String, contents: String) { let path_buf = std::path::PathBuf::from(&path); // Create file if it exists, otherwise just open and rewrite - let mut file = match fs::File::options().write(true).truncate(true).open(&path_buf) { + let mut file = match fs::File::create(&path_buf) { Ok(file) => file, Err(e) => { println!("Failed to open file: {}", e); - - // attempt to create file. otherwise return - match fs::File::create(&path_buf) { - Ok(file) => file, - Err(e) => { - println!("Failed to create file: {}", e); - return; - } - } + return; } }; From 656fa2cfe32301aa869890947ebb1dd3499803b6 Mon Sep 17 00:00:00 2001 From: ffauzan <32434534+ffauzan@users.noreply.github.com> Date: Tue, 19 Jul 2022 11:47:38 +0700 Subject: [PATCH 3/5] Revert "fix conflict" This reverts commit 1588bee5a3c8be20153259229dbe91ac7a906f9d, reversing changes made to 75b79d02027adddd8dea52b5f6f82d6f5d583d52. --- src-tauri/src/file_helpers.rs | 320 +++++++++++++++++----------------- 1 file changed, 164 insertions(+), 156 deletions(-) diff --git a/src-tauri/src/file_helpers.rs b/src-tauri/src/file_helpers.rs index 66ea649..514ceb6 100644 --- a/src-tauri/src/file_helpers.rs +++ b/src-tauri/src/file_helpers.rs @@ -1,156 +1,164 @@ -use file_diff::diff; -use std::fs; -use std::io::{Read, Write}; - -#[tauri::command] -pub fn rename(path: String, new_name: String) { - let mut new_path = path.clone(); - - // Check if file/folder to replace exists - if fs::metadata(&path).is_err() { - return; - } - - // Check if path uses forward or back slashes - if new_path.contains('\\') { - new_path = path.replace('\\', "/"); - } - - let path_replaced = &path.replace(&new_path.split('/').last().unwrap(), &new_name); - - fs::rename(path, &path_replaced).unwrap(); -} - -#[tauri::command] -pub fn dir_create(path: String) { - fs::create_dir_all(path).unwrap(); -} - -#[tauri::command] -pub fn dir_exists(path: &str) -> bool { - let path_buf = std::path::PathBuf::from(path); - fs::metadata(path_buf).is_ok() -} - -#[tauri::command] -pub fn dir_is_empty(path: &str) -> bool { - let path_buf = std::path::PathBuf::from(path); - fs::read_dir(path_buf).unwrap().count() == 0 -} - -#[tauri::command] -pub fn dir_delete(path: &str) { - let path_buf = std::path::PathBuf::from(path); - fs::remove_dir_all(path_buf).unwrap(); -} - -#[tauri::command] -pub fn are_files_identical(path1: &str, path2: &str) -> bool { - diff(path1, path2) -} - -#[tauri::command] -pub fn copy_file(path: String, new_path: String) -> bool { - let filename = &path.split('/').last().unwrap(); - let mut new_path_buf = std::path::PathBuf::from(&new_path); - let path_buf = std::path::PathBuf::from(&path); - - // If the new path doesn't exist, create it. - if !dir_exists(new_path_buf.pop().to_string().as_str()) { - std::fs::create_dir_all(&new_path).unwrap(); - } - - // Copy old to new - match std::fs::copy(&path_buf, format!("{}/{}", new_path, filename)) { - Ok(_) => true, - Err(e) => { - println!("Failed to copy file: {}", e); - println!("Path: {}", path); - println!("New Path: {}", new_path); - false - } - } -} - -#[tauri::command] -pub fn copy_file_with_new_name(path: String, new_path: String, new_name: String) -> bool { - let mut new_path_buf = std::path::PathBuf::from(&new_path); - let path_buf = std::path::PathBuf::from(&path); - - // If the new path doesn't exist, create it. - if !dir_exists(new_path_buf.pop().to_string().as_str()) { - match std::fs::create_dir_all(&new_path) { - Ok(_) => {} - Err(e) => { - println!("Failed to create directory: {}", e); - return false; - } - }; - } - - // Copy old to new - match std::fs::copy(&path_buf, format!("{}/{}", new_path, new_name)) { - Ok(_) => true, - Err(e) => { - println!("Failed to copy file: {}", e); - println!("Path: {}", path); - println!("New Path: {}", new_path); - false - } - } -} - -#[tauri::command] -pub fn delete_file(path: String) -> bool { - let path_buf = std::path::PathBuf::from(&path); - - match std::fs::remove_file(path_buf) { - Ok(_) => true, - Err(e) => { - println!("Failed to delete file: {}", e); - false - } - }; - - false -} - -#[tauri::command] -pub fn read_file(path: String) -> String { - let path_buf = std::path::PathBuf::from(&path); - - let mut file = match fs::File::open(path_buf) { - Ok(file) => file, - Err(e) => { - println!("Failed to open file: {}", e); - return String::new(); - } - }; - - let mut contents = String::new(); - file.read_to_string(&mut contents).unwrap(); - - contents -} - -#[tauri::command] -pub fn write_file(path: String, contents: String) { - let path_buf = std::path::PathBuf::from(&path); - - // Create file if it exists, otherwise just open and rewrite - let mut file = match fs::File::create(&path_buf) { - Ok(file) => file, - Err(e) => { - println!("Failed to open file: {}", e); - return; - } - }; - - // Write contents to file - match file.write_all(contents.as_bytes()) { - Ok(_) => (), - Err(e) => { - println!("Failed to write to file: {}", e); - } - } -} +use file_diff::diff; +use std::fs; +use std::io::{Read, Write}; + +#[tauri::command] +pub fn rename(path: String, new_name: String) { + let mut new_path = path.clone(); + + // Check if file/folder to replace exists + if fs::metadata(&path).is_err() { + return; + } + + // Check if path uses forward or back slashes + if new_path.contains('\\') { + new_path = path.replace('\\', "/"); + } + + let path_replaced = &path.replace(&new_path.split('/').last().unwrap(), &new_name); + + fs::rename(path, &path_replaced).unwrap(); +} + +#[tauri::command] +pub fn dir_create(path: String) { + fs::create_dir_all(path).unwrap(); +} + +#[tauri::command] +pub fn dir_exists(path: &str) -> bool { + let path_buf = std::path::PathBuf::from(path); + fs::metadata(path_buf).is_ok() +} + +#[tauri::command] +pub fn dir_is_empty(path: &str) -> bool { + let path_buf = std::path::PathBuf::from(path); + fs::read_dir(path_buf).unwrap().count() == 0 +} + +#[tauri::command] +pub fn dir_delete(path: &str) { + let path_buf = std::path::PathBuf::from(path); + fs::remove_dir_all(path_buf).unwrap(); +} + +#[tauri::command] +pub fn are_files_identical(path1: &str, path2: &str) -> bool { + diff(path1, path2) +} + +#[tauri::command] +pub fn copy_file(path: String, new_path: String) -> bool { + let filename = &path.split('/').last().unwrap(); + let mut new_path_buf = std::path::PathBuf::from(&new_path); + let path_buf = std::path::PathBuf::from(&path); + + // If the new path doesn't exist, create it. + if !dir_exists(new_path_buf.pop().to_string().as_str()) { + std::fs::create_dir_all(&new_path).unwrap(); + } + + // Copy old to new + match std::fs::copy(&path_buf, format!("{}/{}", new_path, filename)) { + Ok(_) => true, + Err(e) => { + println!("Failed to copy file: {}", e); + println!("Path: {}", path); + println!("New Path: {}", new_path); + false + } + } +} + +#[tauri::command] +pub fn copy_file_with_new_name(path: String, new_path: String, new_name: String) -> bool { + let mut new_path_buf = std::path::PathBuf::from(&new_path); + let path_buf = std::path::PathBuf::from(&path); + + // If the new path doesn't exist, create it. + if !dir_exists(new_path_buf.pop().to_string().as_str()) { + match std::fs::create_dir_all(&new_path) { + Ok(_) => {} + Err(e) => { + println!("Failed to create directory: {}", e); + return false; + } + }; + } + + // Copy old to new + match std::fs::copy(&path_buf, format!("{}/{}", new_path, new_name)) { + Ok(_) => true, + Err(e) => { + println!("Failed to copy file: {}", e); + println!("Path: {}", path); + println!("New Path: {}", new_path); + false + } + } +} + +#[tauri::command] +pub fn delete_file(path: String) -> bool { + let path_buf = std::path::PathBuf::from(&path); + + match std::fs::remove_file(path_buf) { + Ok(_) => true, + Err(e) => { + println!("Failed to delete file: {}", e); + false + } + }; + + false +} + +#[tauri::command] +pub fn read_file(path: String) -> String { + let path_buf = std::path::PathBuf::from(&path); + + let mut file = match fs::File::open(path_buf) { + Ok(file) => file, + Err(e) => { + println!("Failed to open file: {}", e); + return String::new(); + } + }; + + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap(); + + contents +} + +#[tauri::command] +pub fn write_file(path: String, contents: String) { + let path_buf = std::path::PathBuf::from(&path); + + // Create file if it exists, otherwise just open and rewrite + let mut file = match fs::File::options().write(true).truncate(true).open(&path_buf) { + Ok(file) => file, + Err(e) => { + println!("Failed to open file: {}", e); + + // attempt to create file. otherwise return + match fs::File::create(&path_buf) { + Ok(file) => file, + Err(e) => { + println!("Failed to create file: {}", e); + return; + } + } + } + }; + + // Write contents to file + match file.write_all(contents.as_bytes()) { + Ok(_) => (), + Err(e) => { + println!("Failed to write to file: {}", e); + } + } +} From 69201bc8b1af666e1cd13f1e7b023b194088ad96 Mon Sep 17 00:00:00 2001 From: ffauzan <32434534+ffauzan@users.noreply.github.com> Date: Tue, 19 Jul 2022 11:52:12 +0700 Subject: [PATCH 4/5] Update file_helpers.rs --- src-tauri/src/file_helpers.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/file_helpers.rs b/src-tauri/src/file_helpers.rs index 514ceb6..c752492 100644 --- a/src-tauri/src/file_helpers.rs +++ b/src-tauri/src/file_helpers.rs @@ -138,19 +138,11 @@ pub fn write_file(path: String, contents: String) { let path_buf = std::path::PathBuf::from(&path); // Create file if it exists, otherwise just open and rewrite - let mut file = match fs::File::options().write(true).truncate(true).open(&path_buf) { + let mut file = match fs::File::create(&path_buf) { Ok(file) => file, Err(e) => { println!("Failed to open file: {}", e); - - // attempt to create file. otherwise return - match fs::File::create(&path_buf) { - Ok(file) => file, - Err(e) => { - println!("Failed to create file: {}", e); - return; - } - } + return; } }; From 109f98db663e1ffa69e3c04034866bade3f6d036 Mon Sep 17 00:00:00 2001 From: ffauzan <32434534+ffauzan@users.noreply.github.com> Date: Tue, 19 Jul 2022 16:41:25 +0700 Subject: [PATCH 5/5] Fix patched metadata checking Issue: The function checked the wrong directory for global-metadata-patched.dat --- src/utils/metadata.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/metadata.ts b/src/utils/metadata.ts index cc933f1..0f971f9 100644 --- a/src/utils/metadata.ts +++ b/src/utils/metadata.ts @@ -72,7 +72,7 @@ export async function patchGame() { // Do we have a patch already? const patchedExists = await invoke('dir_exists', { - path: await getGameMetadataPath() + '\\global-metadata-patched.dat' + path: await getBackupMetadataPath() + '\\global-metadata-patched.dat' }) if (!patchedExists) { @@ -225,4 +225,4 @@ export async function restoreMetadata(manager: DownloadHandler) { await unpatchGame() return true -} \ No newline at end of file +}