From f064e3eb9a28948b9c7d3217fa4e2f72825b20a5 Mon Sep 17 00:00:00 2001 From: poslop Date: Thu, 6 Jun 2024 00:11:57 -0500 Subject: [PATCH] end of using REFS --- Cargo.toml | 9 +- config.toml | 3 - index.html | 17 +++- public/glue.js | 13 +++ src-tauri/.gitignore | 1 + src-tauri/.taurignore | 1 + src-tauri/Cargo.toml | 5 +- src-tauri/config.toml | 3 + src-tauri/src/config_settings.rs | 45 +++++++++ src-tauri/src/main.rs | 6 +- src-tauri/tauri.conf.json | 8 +- src/app.rs | 5 +- src/config_settings.rs | 41 ++++---- src/main.rs | 2 +- src/views/settings.rs | 164 +++++++++++++------------------ 15 files changed, 192 insertions(+), 131 deletions(-) delete mode 100644 config.toml create mode 100644 public/glue.js create mode 100644 src-tauri/.taurignore create mode 100644 src-tauri/config.toml create mode 100644 src-tauri/src/config_settings.rs diff --git a/Cargo.toml b/Cargo.toml index 00a8a77..7ec3c8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ yew-router = "0.18" walkdir = "2.3.2" wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" -web-sys = "0.3" toml = "0.5" js-sys = "0.3" serde = { version = "1", features = ["derive"] } @@ -19,3 +18,11 @@ console_error_panic_hook = "0.1.7" [workspace] members = ["src-tauri"] + + +[dependencies.web-sys] +version = "0.3" +features = [ + "Window", + "console" +] \ No newline at end of file diff --git a/config.toml b/config.toml deleted file mode 100644 index 1752b44..0000000 --- a/config.toml +++ /dev/null @@ -1,3 +0,0 @@ -cslol_dir = "C:\\Users\\poslop\\Games\\lol\\cslol-manager" -wad_dir = "C:\\Riot Games\\League of Legends\\Game\\DATA\\FINAL\\Champions" -extract_dir = "" diff --git a/index.html b/index.html index 2666354..65d2d8f 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,19 @@ - - d + + + diff --git a/public/glue.js b/public/glue.js new file mode 100644 index 0000000..e3bd0b5 --- /dev/null +++ b/public/glue.js @@ -0,0 +1,13 @@ +const invoke = window.__TAURI__.invoke + +export async function init_config_js() { + return await invoke("init_config"); +} + +export async function write_config_js(config) { + return await invoke("write_config", { config: config }); +} + +export async function load_config_js() { + return await invoke("load_config"); +} \ No newline at end of file diff --git a/src-tauri/.gitignore b/src-tauri/.gitignore index b21bd68..8358599 100644 --- a/src-tauri/.gitignore +++ b/src-tauri/.gitignore @@ -5,3 +5,4 @@ # Generated by Tauri # will have schema files for capabilities auto-completion /gen/schemas + diff --git a/src-tauri/.taurignore b/src-tauri/.taurignore new file mode 100644 index 0000000..ab8b69c --- /dev/null +++ b/src-tauri/.taurignore @@ -0,0 +1 @@ +config.toml \ No newline at end of file diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a7cb50e..9c69acb 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -11,9 +11,12 @@ edition = "2021" tauri-build = { version = "1", features = [] } [dependencies] -tauri = { version = "1", features = ["shell-open"] } +tauri = { version = "1", features = [ "api-all"] } serde = { version = "1", features = ["derive"] } serde_json = "1" +toml = "0.5" +tokio = "1.38.0" +serde-wasm-bindgen = "0.4" [features] # This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!! diff --git a/src-tauri/config.toml b/src-tauri/config.toml new file mode 100644 index 0000000..b7ce5f5 --- /dev/null +++ b/src-tauri/config.toml @@ -0,0 +1,3 @@ +cslol_dir = "WHY U FUCK NO WORK" +wad_dir = "" +extract_dir = "" diff --git a/src-tauri/src/config_settings.rs b/src-tauri/src/config_settings.rs new file mode 100644 index 0000000..e7c5cc6 --- /dev/null +++ b/src-tauri/src/config_settings.rs @@ -0,0 +1,45 @@ +use tokio::fs; + +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize)] +pub struct Config { + pub cslol_dir: String, + pub wad_dir: String, + pub extract_dir: String, +} + +#[tauri::command] +pub async fn init_config() { + // Check if the config file exists asynchronously + if fs::metadata("config.toml").await.is_err() { + let config = Config { + cslol_dir: "".to_string(), + wad_dir: "C:\\Riot Games\\League of Legends\\Game\\DATA\\FINAL\\Champions".to_string(), + extract_dir: "".to_string(), + }; + write_config(config).await; // Call the async version of write_config + } +} + +#[tauri::command] +pub async fn load_config() -> Config { + // Read the config file asynchronously + let config = fs::read_to_string("config.toml").await + .expect("Failed to read config.toml file"); + + // Parse the config synchronously as before (no async TOML parser by default) + toml::from_str(&config) + .expect("Failed to parse config.toml file") +} + +#[tauri::command] +pub async fn write_config(config: Config) { + // Serialize the config synchronously (no async TOML serializer by default) + let config_content = toml::to_string(&config) + .expect("Failed to serialize config"); + + // Write the config file asynchronously + fs::write("config.toml", config_content).await + .expect("Failed to write to config.toml file"); +} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 523550d..452f30f 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,7 +1,9 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command +mod config_settings; +use config_settings::*; + #[tauri::command] fn greet(name: &str) -> String { format!("Hello, {}! You've been greeted from Rust!", name) @@ -9,7 +11,7 @@ fn greet(name: &str) -> String { fn main() { tauri::Builder::default() - .invoke_handler(tauri::generate_handler![greet]) + .invoke_handler(tauri::generate_handler![greet, init_config, load_config, write_config]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 5283c8b..44f9408 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -12,10 +12,16 @@ }, "tauri": { "allowlist": { - "all": false, + "all": true, "shell": { "all": false, "open": true + }, + "fs": { + "all": true + }, + "window": { + "all": true } }, "windows": [ diff --git a/src/app.rs b/src/app.rs index 731dec4..a808485 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,8 +1,9 @@ use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::spawn_local; use yew::prelude::*; use yew_router::prelude::*; -use crate::views::*; +use crate::{config_settings::init_config, views::*}; #[wasm_bindgen] extern "C" { @@ -25,7 +26,7 @@ pub enum Route { #[function_component(App)] -pub fn app() -> Html { +pub fn app() -> Html { html! { diff --git a/src/config_settings.rs b/src/config_settings.rs index 60c27df..db1e7b9 100644 --- a/src/config_settings.rs +++ b/src/config_settings.rs @@ -1,37 +1,32 @@ -use std::fs; use serde::{Deserialize, Serialize}; +use serde_wasm_bindgen::to_value; +use wasm_bindgen::prelude::*; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Default, Clone, Serialize, Deserialize)] pub struct Config { pub cslol_dir: String, pub wad_dir: String, pub extract_dir: String, } -pub fn init_config() { - if fs::metadata("config.toml").is_err() { - let config = Config { - cslol_dir: "".to_string(), - wad_dir: "C:\\Riot Games\\League of Legends\\Game\\DATA\\FINAL\\Champions".to_string(), - extract_dir: "".to_string(), - }; - write_config(&config); - } +#[wasm_bindgen(module = "/public/glue.js")] +extern "C" { + #[wasm_bindgen(js_name = init_config_js, catch)] + pub async fn init_config() -> Result; + + #[wasm_bindgen(js_name = write_config_js, catch)] + pub async fn write_config_js(config: JsValue) -> Result; + + #[wasm_bindgen(js_name = load_config_js, catch)] + pub async fn load_config_js() -> Result; } -pub fn load_config() -> Config { - let config = fs::read_to_string("config.toml") - .expect("Failed to read config.toml file"); - - toml::from_str(&config) - .expect("Failed to parse config.toml file") +pub async fn write_config(config: Config) -> Result { + write_config_js(to_value(&config).unwrap()).await } -pub fn write_config(config: &Config) { - let config_content = toml::to_string(config) - .expect("Failed to serialize config"); - - fs::write("config.toml", config_content) - .expect("Failed to write to config.toml file"); +pub async fn load_config() -> Result { + Ok(serde_wasm_bindgen::from_value(load_config_js().await.unwrap()).unwrap()) } + diff --git a/src/main.rs b/src/main.rs index f0a7826..0b1d961 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod app; mod views; -mod extractor; +// mod extractor; mod config_settings; use app::App; diff --git a/src/views/settings.rs b/src/views/settings.rs index 9a0732e..7a1be92 100644 --- a/src/views/settings.rs +++ b/src/views/settings.rs @@ -1,116 +1,90 @@ -use web_sys::HtmlInputElement; -use yew::prelude::*; +use std::{cell::RefCell, rc::Rc}; + use crate::config_settings::*; -pub enum Msg { - UpdateCSLoLDir(String), - UpdateWadDir(String), - UpdateExtractDir(String), - Submit, -} +use wasm_bindgen_futures::spawn_local; +use web_sys::{console, HtmlInputElement}; +use yew::prelude::*; -pub type Settings = Config; +#[function_component(Settings)] +pub fn settings() -> Html { + let config = Rc::new(RefCell::new(Config::default())); + { + let config = config.clone(); -impl Component for Settings { - type Message = Msg; - type Properties = (); + use_effect(move || { + spawn_local(async move { + init_config().await.unwrap(); + *config.borrow_mut() = load_config().await.unwrap(); + console::log_1(&config.borrow().cslol_dir.clone().into()); + }) + }) - fn create(_ctx: &Context) -> Self { - init_config(); - load_config() + // spawn_local(async move { + // init_config().await.unwrap(); + // *config.borrow_mut() = load_config().await.unwrap(); + // console::log_1(&config.borrow().cslol_dir.clone().into()); + // }) } - fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { - match msg { - Msg::UpdateCSLoLDir(value) => { - self.cslol_dir = value; - true - }, - Msg::UpdateWadDir(value) => { - self.wad_dir = value; - true - }, - Msg::UpdateExtractDir(value) => { - self.extract_dir = value; - true - }, - Msg::Submit => { - write_config(&self); - false - } - } - } + let cslol_dir_ref = use_node_ref(); + let wad_dir_ref = use_node_ref(); + let extract_dir_ref = use_node_ref(); - fn view(&self, ctx: &Context) -> Html { - html! { -
-

{"Settings"}

-
-
+ let on_submit = { + let cslol_dir_ref = cslol_dir_ref.clone(); + let wad_dir_ref = wad_dir_ref.clone(); + let extract_dir_ref = extract_dir_ref.clone(); + + Callback::from(move |event: SubmitEvent| { + event.prevent_default(); + + let cslol_dir = cslol_dir_ref.cast::().unwrap().value(); + let wad_dir = wad_dir_ref.cast::().unwrap().value(); + let extract_dir = extract_dir_ref.cast::().unwrap().value(); + + let config = Config { + cslol_dir, + wad_dir, + extract_dir, + }; + spawn_local(async move { + write_config(config).await.unwrap(); + }); + }) + }; + + let mut tconfig = config.try_borrow().unwrap().clone(); + console::log_1(&tconfig.cslol_dir.clone().into()); + console::log_1(&"IM MAD".into()); + + html! { +
+

{"Settings"}

+
+ +

- ().value()))} - value={self.cslol_dir.clone()} /> +
-
+ +

- ().value()))} - value={self.wad_dir.clone()} /> +
-
+ +

- ().value()))} - value={self.extract_dir.clone()} /> -
-
- +
+ + +
- } +
} } - - -// #[function_component(Settings)] -// pub fn settings() -> Html { -// html! { -//
-//

{"Settings"}

-//
-//
-// -//
-// -//
- -//
-// -//
-// -//
- -//
-// -//
-// -//
- -//
-// -//
-//
-//
-// } -// }