[GUI] Add File System Watching

This commit is contained in:
Ben C 2023-04-10 23:18:42 -04:00
parent d8d372cf2a
commit 625006a2fe
12 changed files with 354 additions and 106 deletions

View File

@ -24,5 +24,6 @@
],
"yaml.schemas": {
"https://json.schemastore.org/github-issue-config.json": "file:///home/bean/Documents/GitHub/ow-mod-man/.github/ISSUE_TEMPLATE/config.yml"
}
},
"rust-analyzer.showUnlinkedFileNotification": false
}

244
Cargo.lock generated
View File

@ -89,9 +89,9 @@ checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
[[package]]
name = "async-stream"
version = "0.3.4"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad445822218ce64be7a341abfb0b1ea43b5c23aa83902542a4542e78309d8e5e"
checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51"
dependencies = [
"async-stream-impl",
"futures-core",
@ -100,13 +100,13 @@ dependencies = [
[[package]]
name = "async-stream-impl"
version = "0.3.4"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4655ae1a7b0cdf149156f780c5bf3f1352bc53cbd9e0a361a7ef7b22947e965"
checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.13",
]
[[package]]
@ -195,9 +195,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.0.2"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1"
checksum = "c70beb79cbb5ce9c4f8e20849978f34225931f665bb49efa6982875a4d5facb3"
[[package]]
name = "block"
@ -573,9 +573,9 @@ dependencies = [
[[package]]
name = "crossbeam-channel"
version = "0.5.7"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf2b3e8478797446514c91ef04bafcb59faba183e621ad488df88983cc14128c"
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
dependencies = [
"cfg-if",
"crossbeam-utils",
@ -748,6 +748,15 @@ dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs"
version = "5.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dece029acd3353e3a58ac2e3eb3c8d6c35827a892edc6cc4138ef9c33df46ecd"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
@ -830,13 +839,13 @@ dependencies = [
[[package]]
name = "errno"
version = "0.3.0"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0"
checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys 0.45.0",
"windows-sys 0.48.0",
]
[[package]]
@ -858,6 +867,15 @@ dependencies = [
"instant",
]
[[package]]
name = "fdeflate"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d329bdeac514ee06249dabc27877490f17f5d371ec693360768b838e19f3ae10"
dependencies = [
"simd-adler32",
]
[[package]]
name = "field-offset"
version = "0.3.5"
@ -870,14 +888,14 @@ dependencies = [
[[package]]
name = "filetime"
version = "0.2.20"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a3de6e8d11b22ff9edc6d916f890800597d60f8b2da1caf2955c274638d6412"
checksum = "5cbc844cecaee9d4443931972e1289c8ff485cb4cc2767cb03ca139ed6885153"
dependencies = [
"cfg-if",
"libc",
"redox_syscall 0.2.16",
"windows-sys 0.45.0",
"windows-sys 0.48.0",
]
[[package]]
@ -887,7 +905,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841"
dependencies = [
"crc32fast",
"miniz_oxide",
"miniz_oxide 0.6.2",
]
[[package]]
@ -920,6 +938,15 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
dependencies = [
"libc",
]
[[package]]
name = "futf"
version = "0.1.5"
@ -1136,9 +1163,9 @@ dependencies = [
[[package]]
name = "getrandom"
version = "0.2.8"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4"
dependencies = [
"cfg-if",
"libc",
@ -1573,6 +1600,26 @@ dependencies = [
"cfb",
]
[[package]]
name = "inotify"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff"
dependencies = [
"bitflags 1.3.2",
"inotify-sys",
"libc",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"libc",
]
[[package]]
name = "instant"
version = "0.1.12"
@ -1597,13 +1644,13 @@ dependencies = [
[[package]]
name = "io-lifetimes"
version = "1.0.9"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb"
checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220"
dependencies = [
"hermit-abi 0.3.1",
"libc",
"windows-sys 0.45.0",
"windows-sys 0.48.0",
]
[[package]]
@ -1614,14 +1661,14 @@ checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f"
[[package]]
name = "is-terminal"
version = "0.4.6"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "256017f749ab3117e93acb91063009e1f1bb56d03965b14c2c8df4eb02c524d8"
checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f"
dependencies = [
"hermit-abi 0.3.1",
"io-lifetimes",
"rustix",
"windows-sys 0.45.0",
"windows-sys 0.48.0",
]
[[package]]
@ -1708,6 +1755,26 @@ dependencies = [
"treediff",
]
[[package]]
name = "kqueue"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c8fc60ba15bf51257aa9807a48a61013db043fcf3a78cb0d916e8e396dcad98"
dependencies = [
"kqueue-sys",
"libc",
]
[[package]]
name = "kqueue-sys"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8367585489f01bc55dd27404dcf56b95e6da061a256a666ab23be9ba96a2e587"
dependencies = [
"bitflags 1.3.2",
"libc",
]
[[package]]
name = "kuchiki"
version = "0.8.1"
@ -1728,9 +1795,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.140"
version = "0.2.141"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
[[package]]
name = "line-wrap"
@ -1871,6 +1938,16 @@ dependencies = [
"adler",
]
[[package]]
name = "miniz_oxide"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
dependencies = [
"adler",
"simd-adler32",
]
[[package]]
name = "mio"
version = "0.8.6"
@ -1950,6 +2027,36 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "notify"
version = "5.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58ea850aa68a06e48fdb069c0ec44d0d64c8dbffa49bf3b6f7f0a901fdea1ba9"
dependencies = [
"bitflags 1.3.2",
"crossbeam-channel",
"filetime",
"fsevent-sys",
"inotify",
"kqueue",
"libc",
"mio",
"serde",
"walkdir",
"windows-sys 0.42.0",
]
[[package]]
name = "notify-debouncer-mini"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e23e9fa24f094b143c1eb61f90ac6457de87be6987bc70746e0179f7dbc9007b"
dependencies = [
"crossbeam-channel",
"notify",
"serde",
]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
@ -2117,9 +2224,9 @@ dependencies = [
[[package]]
name = "openssl"
version = "0.10.49"
version = "0.10.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d2f106ab837a24e03672c59b1239669a0596406ff657c3c0835b6b7f0f35a33"
checksum = "7e30d8bc91859781f0a943411186324d580f2bbeb71b452fe91ae344806af3f1"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
@ -2149,9 +2256,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.84"
version = "0.9.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a20eace9dc2d82904039cb76dcf50fb1a0bba071cfd1629720b5d6f1ddba0fa"
checksum = "0d3d193fb1488ad46ffe3aaabc912cc931d02ee8518fe2959aea8ef52718b0c0"
dependencies = [
"cc",
"libc",
@ -2208,7 +2315,7 @@ dependencies = [
"tokio",
"tokio-test",
"typeshare",
"uuid 1.3.0",
"uuid 1.3.1",
"version-compare 0.1.1",
"zip",
]
@ -2227,6 +2334,7 @@ dependencies = [
"tauri",
"tauri-build",
"tauri-plugin-deep-link",
"tauri-plugin-fs-watch",
"tauri-plugin-window-state",
"time",
"tokio",
@ -2431,14 +2539,15 @@ dependencies = [
[[package]]
name = "png"
version = "0.17.7"
version = "0.17.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d708eaf860a19b19ce538740d2b4bdeeb8337fa53f7738455e706623ad5c638"
checksum = "aaeebc51f9e7d2c150d3f3bfeb667f2aa985db5ef1e3d212847bdedb488beeaa"
dependencies = [
"bitflags 1.3.2",
"crc32fast",
"fdeflate",
"flate2",
"miniz_oxide",
"miniz_oxide 0.7.1",
]
[[package]]
@ -2586,7 +2695,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom 0.2.8",
"getrandom 0.2.9",
]
[[package]]
@ -2637,7 +2746,7 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom 0.2.8",
"getrandom 0.2.9",
"redox_syscall 0.2.16",
"thiserror",
]
@ -2746,16 +2855,16 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.37.7"
version = "0.37.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2aae838e49b3d63e9274e1c01833cc8139d3fec468c3b84688c628f44b1ae11d"
checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77"
dependencies = [
"bitflags 1.3.2",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys 0.45.0",
"windows-sys 0.48.0",
]
[[package]]
@ -3010,6 +3119,12 @@ dependencies = [
"libc",
]
[[package]]
name = "simd-adler32"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f"
[[package]]
name = "siphasher"
version = "0.3.10"
@ -3204,7 +3319,7 @@ dependencies = [
"scopeguard",
"serde",
"unicode-segmentation",
"uuid 1.3.0",
"uuid 1.3.1",
"windows 0.39.0",
"windows-implement",
"x11-dl",
@ -3268,7 +3383,7 @@ dependencies = [
"time",
"tokio",
"url",
"uuid 1.3.0",
"uuid 1.3.1",
"webkit2gtk",
"webview2-com",
"windows 0.39.0",
@ -3313,7 +3428,7 @@ dependencies = [
"tauri-utils",
"thiserror",
"time",
"uuid 1.3.0",
"uuid 1.3.1",
"walkdir",
]
@ -3333,17 +3448,32 @@ dependencies = [
[[package]]
name = "tauri-plugin-deep-link"
version = "0.1.0"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d8e1a461d5bea85fa0b1d171efb25b0439ffcee08ae95ebf7f53007dfd8aab"
checksum = "33a3ae55bcfe692e5361edc4708bd9f415270cc02e1cdba8ab7768566208b4e2"
dependencies = [
"dirs-next",
"dirs",
"interprocess",
"log",
"objc2",
"once_cell",
"tauri-utils",
"winreg 0.11.0",
"windows-sys 0.48.0",
"winreg 0.50.0",
]
[[package]]
name = "tauri-plugin-fs-watch"
version = "0.1.0"
source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#cea1d4f7b54b94354e8100daf804b810c680efd7"
dependencies = [
"log",
"notify",
"notify-debouncer-mini",
"serde",
"serde_json",
"tauri",
"thiserror",
]
[[package]]
@ -3352,7 +3482,7 @@ version = "0.1.0"
source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#cea1d4f7b54b94354e8100daf804b810c680efd7"
dependencies = [
"bincode",
"bitflags 2.0.2",
"bitflags 2.1.0",
"log",
"serde",
"serde_json",
@ -3375,7 +3505,7 @@ dependencies = [
"serde_json",
"tauri-utils",
"thiserror",
"uuid 1.3.0",
"uuid 1.3.1",
"webview2-com",
"windows 0.39.0",
]
@ -3393,7 +3523,7 @@ dependencies = [
"raw-window-handle",
"tauri-runtime",
"tauri-utils",
"uuid 1.3.0",
"uuid 1.3.1",
"webkit2gtk",
"webview2-com",
"windows 0.39.0",
@ -3841,11 +3971,11 @@ checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
[[package]]
name = "uuid"
version = "1.3.0"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79"
checksum = "5b55a3fef2a1e3b3a00ce878640918820d3c51081576ac657d23af9fc7928fdb"
dependencies = [
"getrandom 0.2.8",
"getrandom 0.2.9",
"rand 0.8.5",
]
@ -4407,12 +4537,12 @@ dependencies = [
[[package]]
name = "winreg"
version = "0.11.0"
version = "0.50.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76a1a57ff50e9b408431e8f97d5456f2807f8eb2a2cd79b06068fc87f8ecf189"
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
dependencies = [
"cfg-if",
"winapi",
"windows-sys 0.48.0",
]
[[package]]
@ -4526,9 +4656,9 @@ dependencies = [
[[package]]
name = "zstd-sys"
version = "2.0.7+zstd.1.5.4"
version = "2.0.8+zstd.1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94509c3ba2fe55294d752b79842c530ccfab760192521df74a081a78d2b3c7f5"
checksum = "5556e6ee25d32df2586c098bbfa278803692a20d0ab9565e049480d52707ec8c"
dependencies = [
"cc",
"libc",

View File

@ -76,10 +76,12 @@ pub fn fix_json(txt: &str) -> String {
/// Removes the BOM on a JSON file
pub fn fix_json_file(path: &Path) -> Result<()> {
let txt = read_to_string(path)?;
let txt = fix_json(&txt);
let mut file = File::create(path)?;
write!(file, "{}", txt)?;
let txt_old = read_to_string(path)?;
let txt = fix_json(&txt_old);
if txt != txt_old {
let mut file = File::create(path)?;
write!(file, "{}", txt)?;
}
Ok(())
}

View File

@ -22,6 +22,7 @@ regex = "1.7.1"
time = { version = "0.3.20", features = ["macros"] }
tauri-plugin-deep-link = "0.1.0"
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
[features]
default = ["custom-protocol"]

View File

@ -20,7 +20,7 @@ use owmods_core::updates::check_mod_needs_update;
use owmods_core::validate::fix_deps;
use rust_fuzzy_search::fuzzy_compare;
use tauri::api::dialog;
use tauri::Manager;
use tauri::{AppHandle, Manager};
use time::macros::format_description;
use time::OffsetDateTime;
use tokio::try_join;
@ -60,6 +60,10 @@ fn search<'a, T>(
scores.iter().map(|(m, _)| *m).collect()
}
fn toggle_fs_watch(handle: &AppHandle, enabled: bool) {
handle.emit_all("TOGGLE_FS_WATCH", enabled).ok();
}
#[tauri::command]
pub async fn initial_setup(
handle: tauri::AppHandle,
@ -79,6 +83,7 @@ pub async fn refresh_local_db(
handle: tauri::AppHandle,
state: tauri::State<'_, State>,
) -> Result<(), String> {
toggle_fs_watch(&handle, false);
let conf = state.config.read().await;
{
let mut db = state.local_db.write().await;
@ -86,6 +91,7 @@ pub async fn refresh_local_db(
*db = local_db.unwrap_or_else(|_| LocalDatabase::default());
}
handle.emit_all("LOCAL-REFRESH", "").ok();
toggle_fs_watch(&handle, true);
Ok(())
}
@ -143,6 +149,7 @@ pub async fn refresh_remote_db(
handle: tauri::AppHandle,
state: tauri::State<'_, State>,
) -> Result<(), String> {
toggle_fs_watch(&handle, false);
let conf = state.config.read().await;
{
let mut db = state.remote_db.write().await;
@ -150,6 +157,7 @@ pub async fn refresh_remote_db(
*db = remote_db.unwrap_or_else(|_| RemoteDatabase::default());
}
handle.emit_all("REMOTE-REFRESH", "").ok();
toggle_fs_watch(&handle, true);
Ok(())
}
@ -657,3 +665,17 @@ pub async fn get_alert(state: tauri::State<'_, State>) -> Result<Alert, String>
let config = state.config.read().await;
fetch_alert(&config.alert_url).await.map_err(e_to_str)
}
#[tauri::command]
pub async fn get_watcher_paths(state: tauri::State<'_, State>) -> Result<Vec<String>, String> {
let config = state.config.read().await;
Ok(vec![
config.owml_path.clone(),
GuiConfig::path().unwrap().to_str().unwrap().to_string(),
Config::default_path()
.unwrap()
.to_str()
.unwrap()
.to_string(),
])
}

View File

@ -49,7 +49,7 @@ impl Default for GuiConfig {
}
impl GuiConfig {
fn path() -> Result<PathBuf, anyhow::Error> {
pub fn path() -> Result<PathBuf, anyhow::Error> {
let path = get_app_path()?.join("gui_settings.json");
Ok(path)
}

View File

@ -29,6 +29,10 @@ type StatePart<T> = Arc<TokioLock<T>>;
type LogPort = u16;
type LogMessages = HashMap<LogPort, (Vec<GameMessage>, BufWriter<File>)>;
fn manage<T>(obj: T) -> StatePart<T> {
Arc::new(TokioLock::new(obj))
}
pub struct State {
local_db: StatePart<LocalDatabase>,
remote_db: StatePart<RemoteDatabase>,
@ -39,20 +43,19 @@ pub struct State {
fn main() -> Result<(), Box<dyn Error>> {
let config = Config::get(None).unwrap_or(Config::default(None)?);
let gui_config = GuiConfig::get().unwrap_or(GuiConfig::default());
let local_db = LocalDatabase::fetch(&config.owml_path).unwrap_or(LocalDatabase::default());
let remote_db =
RemoteDatabase::fetch_blocking(&config.database_url).unwrap_or(RemoteDatabase::default());
let gui_config = GuiConfig::get().unwrap_or_default();
let local_db = LocalDatabase::fetch(&config.owml_path).unwrap_or_default();
let remote_db = RemoteDatabase::fetch_blocking(&config.database_url).unwrap_or_default();
tauri_plugin_deep_link::prepare("com.bwc9876.owmods-gui");
tauri::Builder::default()
.manage(State {
local_db: Arc::new(TokioLock::new(local_db)),
remote_db: Arc::new(TokioLock::new(remote_db)),
config: Arc::new(TokioLock::new(config)),
gui_config: Arc::new(TokioLock::new(gui_config)),
game_log: Arc::new(TokioLock::new(HashMap::new())),
local_db: manage(local_db),
remote_db: manage(remote_db),
config: manage(config),
gui_config: manage(gui_config),
game_log: manage(HashMap::new()),
})
.setup(move |app| {
let logger = Logger::new(app.handle());
@ -128,9 +131,11 @@ fn main() -> Result<(), Box<dyn Error>> {
import_mods,
fix_mod_deps,
db_has_issues,
get_alert
get_alert,
get_watcher_paths
])
.plugin(tauri_plugin_window_state::Builder::default().build())
.plugin(tauri_plugin_fs_watch::init())
.run(tauri::generate_context!())
.expect("Error while running tauri application.");
Ok(())

View File

@ -22,7 +22,8 @@
"react-dom": "^18.2.0",
"react-icons": "^4.8.0",
"react-virtualized-auto-sizer": "^1.0.11",
"react-window": "^1.8.8"
"react-window": "^1.8.8",
"tauri-plugin-fs-watch-api": "github:tauri-apps/tauri-plugin-fs-watch"
},
"devDependencies": {
"@types/react": "^18.0.33",

View File

@ -22,6 +22,9 @@ dependencies:
react-window:
specifier: ^1.8.8
version: 1.8.8(react-dom@18.2.0)(react@18.2.0)
tauri-plugin-fs-watch-api:
specifier: github:tauri-apps/tauri-plugin-fs-watch
version: github.com/tauri-apps/tauri-plugin-fs-watch/3bb757ff970248efaa654084a339761bd607e2aa
devDependencies:
"@types/react":
@ -3854,3 +3857,14 @@ packages:
}
engines: { node: ">=10" }
dev: true
github.com/tauri-apps/tauri-plugin-fs-watch/3bb757ff970248efaa654084a339761bd607e2aa:
resolution:
{
tarball: https://codeload.github.com/tauri-apps/tauri-plugin-fs-watch/tar.gz/3bb757ff970248efaa654084a339761bd607e2aa
}
name: tauri-plugin-fs-watch-api
version: 0.0.0
dependencies:
"@tauri-apps/api": 1.2.0
dev: false

View File

@ -70,7 +70,8 @@ const commandInfo = {
importMods: $<ActionCommand<{ path: string }>>("import_mods"),
fixDeps: $<ActionCommand<{ uniqueName: string }>>("fix_mod_deps"),
checkDBForIssues: $<GetCommand<boolean>>("db_has_issues"),
getAlert: $<GetCommand<Alert>>("get_alert")
getAlert: $<GetCommand<Alert>>("get_alert"),
getWatcherPaths: $<GetCommand<string[]>>("get_watcher_paths")
};
type Command = keyof typeof commandInfo;

View File

@ -3,7 +3,6 @@ import {
BsQuestion,
BsGearFill,
BsInfoCircleFill,
BsArrowRepeat,
BsBoxArrowInDown,
BsBoxArrowUpRight
} from "react-icons/bs";
@ -21,6 +20,7 @@ import { useTranslations } from "@hooks";
import { commands } from "@commands";
import { dialog } from "@tauri-apps/api";
import CenteredSpinner from "@components/common/CenteredSpinner";
import NavRefreshButton from "./NavRefresh";
const Nav = () => {
const openSettings = useRef<() => void>(() => null);
@ -29,32 +29,17 @@ const Nav = () => {
const [areLogsStarting, setLogsStarting] = useState<boolean>(false);
const [
refresh,
runGame,
help,
settings,
installFrom,
about,
exportLabel,
confirm,
launchAnyway
] = useTranslations([
"REFRESH",
"RUN_GAME",
"HELP",
"SETTINGS",
"INSTALL_FROM",
"ABOUT",
"EXPORT_MODS",
"CONFIRM",
"LAUNCH_ANYWAY"
]);
const onRefresh = useCallback(() => {
commands.refreshLocalDb().catch(console.warn);
commands.refreshRemoteDb().catch(console.warn);
}, []);
const [runGame, help, settings, installFrom, about, exportLabel, confirm, launchAnyway] =
useTranslations([
"RUN_GAME",
"HELP",
"SETTINGS",
"INSTALL_FROM",
"ABOUT",
"EXPORT_MODS",
"CONFIRM",
"LAUNCH_ANYWAY"
]);
const onPlay = useCallback(() => {
const start = () =>
@ -112,9 +97,7 @@ const Nav = () => {
<nav>
<ul>
<Downloads />
<NavButton onClick={onRefresh} labelPlacement="bottom" ariaLabel={refresh}>
<Icon iconType={BsArrowRepeat} />
</NavButton>
<NavRefreshButton />
</ul>
<ul>
{areLogsStarting ? (

View File

@ -0,0 +1,88 @@
import Icon from "@components/common/Icon";
import NavButton from "./NavButton";
import { BsArrowRepeat } from "react-icons/bs";
import { useTranslation } from "@hooks";
import { useCallback, useEffect, useRef, useState } from "react";
import { commands, hooks } from "@commands";
import { watchImmediate } from "tauri-plugin-fs-watch-api";
import { listen } from "@tauri-apps/api/event";
const checkPaths = (paths: string[]) => {
for (const path of paths) {
if (
path.endsWith("config.json") ||
path.endsWith("manifest.json") ||
path.endsWith(".dll") ||
path.endsWith("OWML.Config.json") ||
path.endsWith("settings.json") ||
path.endsWith("gui_settings.json")
) {
return true;
}
}
return false;
};
const NavRefreshButton = () => {
const [watchingFileSystem, setWatchFS] = useState(false);
const [status, config, err] = hooks.getConfig("CONFIG_RELOAD");
const guiConfig = hooks.getGuiConfig("GUI_CONFIG_RELOAD")[1];
const refreshLabel = useTranslation("REFRESH");
const currentTimeout = useRef<number | null>(null);
const onRefresh = useCallback(() => {
const task = async () => {
setWatchFS(false);
await commands.refreshLocalDb();
await commands.refreshRemoteDb();
await commands.initialSetup();
setWatchFS(true);
};
task();
}, []);
useEffect(() => {
let cancel = false;
listen("TOGGLE_FS_WATCH", (e) => {
if (cancel) return;
const enabled = e.payload as boolean;
if (!enabled && currentTimeout.current) {
clearTimeout(currentTimeout.current);
currentTimeout.current = null;
}
setWatchFS(e.payload as boolean);
});
return () => {
cancel = true;
};
}, []);
useEffect(() => {
let cancel = false;
if (status === "Done" && (guiConfig?.watchFs ?? false)) {
commands.getWatcherPaths().then((paths) => {
watchImmediate(paths, { recursive: true }, (e) => {
if (cancel || !watchingFileSystem || !checkPaths(e.paths)) return;
if (currentTimeout.current) {
clearTimeout(currentTimeout.current);
currentTimeout.current = null;
}
currentTimeout.current = setTimeout(onRefresh, 500);
});
});
} else if (status === "Error") {
console.error(err);
}
return () => {
cancel = true;
};
}, [status, config, guiConfig?.watchFs ?? false]);
return (
<NavButton onClick={onRefresh} labelPlacement="bottom" ariaLabel={refreshLabel}>
<Icon iconType={BsArrowRepeat} />
</NavButton>
);
};
export default NavRefreshButton;