end of using REFS

This commit is contained in:
poslop
2024-06-06 00:11:57 -05:00
parent c60c8fee4b
commit f064e3eb9a
15 changed files with 192 additions and 131 deletions

View File

@@ -10,7 +10,6 @@ yew-router = "0.18"
walkdir = "2.3.2" walkdir = "2.3.2"
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"
web-sys = "0.3"
toml = "0.5" toml = "0.5"
js-sys = "0.3" js-sys = "0.3"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
@@ -19,3 +18,11 @@ console_error_panic_hook = "0.1.7"
[workspace] [workspace]
members = ["src-tauri"] members = ["src-tauri"]
[dependencies.web-sys]
version = "0.3"
features = [
"Window",
"console"
]

View File

@@ -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 = ""

View File

@@ -6,6 +6,19 @@
<link data-trunk rel="css" href="styles.css" /> <link data-trunk rel="css" href="styles.css" />
<link data-trunk rel="copy-dir" href="public" /> <link data-trunk rel="copy-dir" href="public" />
</head> </head>
<body></body> <body>
d <script>
// access the pre-bundled global API functions
const { invoke } = window.__TAURI__.tauri
// now we can call our Command!
// You will see "Welcome from Tauri" replaced
// by "Hello, World!"!
invoke('init_config', { name: 'World' })
// `invoke` returns a Promise
.then((response) => {
window.header.innerHTML = response
})
</script>
</body>
</html> </html>

13
public/glue.js Normal file
View File

@@ -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");
}

View File

@@ -5,3 +5,4 @@
# Generated by Tauri # Generated by Tauri
# will have schema files for capabilities auto-completion # will have schema files for capabilities auto-completion
/gen/schemas /gen/schemas

1
src-tauri/.taurignore Normal file
View File

@@ -0,0 +1 @@
config.toml

View File

@@ -11,9 +11,12 @@ edition = "2021"
tauri-build = { version = "1", features = [] } tauri-build = { version = "1", features = [] }
[dependencies] [dependencies]
tauri = { version = "1", features = ["shell-open"] } tauri = { version = "1", features = [ "api-all"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
toml = "0.5"
tokio = "1.38.0"
serde-wasm-bindgen = "0.4"
[features] [features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!! # This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!

3
src-tauri/config.toml Normal file
View File

@@ -0,0 +1,3 @@
cslol_dir = "WHY U FUCK NO WORK"
wad_dir = ""
extract_dir = ""

View File

@@ -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");
}

View File

@@ -1,7 +1,9 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!! // Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![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] #[tauri::command]
fn greet(name: &str) -> String { fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name) format!("Hello, {}! You've been greeted from Rust!", name)
@@ -9,7 +11,7 @@ fn greet(name: &str) -> String {
fn main() { fn main() {
tauri::Builder::default() 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!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@@ -12,10 +12,16 @@
}, },
"tauri": { "tauri": {
"allowlist": { "allowlist": {
"all": false, "all": true,
"shell": { "shell": {
"all": false, "all": false,
"open": true "open": true
},
"fs": {
"all": true
},
"window": {
"all": true
} }
}, },
"windows": [ "windows": [

View File

@@ -1,8 +1,9 @@
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use yew::prelude::*; use yew::prelude::*;
use yew_router::prelude::*; use yew_router::prelude::*;
use crate::views::*; use crate::{config_settings::init_config, views::*};
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {

View File

@@ -1,37 +1,32 @@
use std::fs;
use serde::{Deserialize, Serialize}; 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 struct Config {
pub cslol_dir: String, pub cslol_dir: String,
pub wad_dir: String, pub wad_dir: String,
pub extract_dir: String, pub extract_dir: String,
} }
pub fn init_config() { #[wasm_bindgen(module = "/public/glue.js")]
if fs::metadata("config.toml").is_err() { extern "C" {
let config = Config { #[wasm_bindgen(js_name = init_config_js, catch)]
cslol_dir: "".to_string(), pub async fn init_config() -> Result<JsValue, JsValue>;
wad_dir: "C:\\Riot Games\\League of Legends\\Game\\DATA\\FINAL\\Champions".to_string(),
extract_dir: "".to_string(), #[wasm_bindgen(js_name = write_config_js, catch)]
}; pub async fn write_config_js(config: JsValue) -> Result<JsValue, JsValue>;
write_config(&config);
} #[wasm_bindgen(js_name = load_config_js, catch)]
pub async fn load_config_js() -> Result<JsValue, JsValue>;
} }
pub fn load_config() -> Config { pub async fn write_config(config: Config) -> Result<JsValue, JsValue> {
let config = fs::read_to_string("config.toml") write_config_js(to_value(&config).unwrap()).await
.expect("Failed to read config.toml file");
toml::from_str(&config)
.expect("Failed to parse config.toml file")
} }
pub fn write_config(config: &Config) { pub async fn load_config() -> Result<Config, JsValue> {
let config_content = toml::to_string(config) Ok(serde_wasm_bindgen::from_value(load_config_js().await.unwrap()).unwrap())
.expect("Failed to serialize config");
fs::write("config.toml", config_content)
.expect("Failed to write to config.toml file");
} }

View File

@@ -1,6 +1,6 @@
mod app; mod app;
mod views; mod views;
mod extractor; // mod extractor;
mod config_settings; mod config_settings;
use app::App; use app::App;

View File

@@ -1,116 +1,90 @@
use web_sys::HtmlInputElement; use std::{cell::RefCell, rc::Rc};
use yew::prelude::*;
use crate::config_settings::*; use crate::config_settings::*;
pub enum Msg { use wasm_bindgen_futures::spawn_local;
UpdateCSLoLDir(String), use web_sys::{console, HtmlInputElement};
UpdateWadDir(String), use yew::prelude::*;
UpdateExtractDir(String),
Submit,
}
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 { use_effect(move || {
type Message = Msg; spawn_local(async move {
type Properties = (); 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>) -> Self { // spawn_local(async move {
init_config(); // init_config().await.unwrap();
load_config() // *config.borrow_mut() = load_config().await.unwrap();
// console::log_1(&config.borrow().cslol_dir.clone().into());
// })
} }
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool { let cslol_dir_ref = use_node_ref();
match msg { let wad_dir_ref = use_node_ref();
Msg::UpdateCSLoLDir(value) => { let extract_dir_ref = use_node_ref();
self.cslol_dir = value;
true let on_submit = {
}, let cslol_dir_ref = cslol_dir_ref.clone();
Msg::UpdateWadDir(value) => { let wad_dir_ref = wad_dir_ref.clone();
self.wad_dir = value; let extract_dir_ref = extract_dir_ref.clone();
true
}, Callback::from(move |event: SubmitEvent| {
Msg::UpdateExtractDir(value) => { event.prevent_default();
self.extract_dir = value;
true let cslol_dir = cslol_dir_ref.cast::<HtmlInputElement>().unwrap().value();
}, let wad_dir = wad_dir_ref.cast::<HtmlInputElement>().unwrap().value();
Msg::Submit => { let extract_dir = extract_dir_ref.cast::<HtmlInputElement>().unwrap().value();
write_config(&self);
false 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());
fn view(&self, ctx: &Context<Self>) -> Html {
html! { html! {
<div class="content"> <div class="content">
<h2>{"Settings"}</h2> <h2>{"Settings"}</h2>
<form onsubmit={ctx.link().callback(|e: SubmitEvent| {
e.prevent_default();
Msg::Submit
})}>
<div class="option"> <div class="option">
<form onsubmit={on_submit}>
<div>
<label for="cslol_dir">{"Location of CSLoL"}</label> <label for="cslol_dir">{"Location of CSLoL"}</label>
<br/> <br/>
<input type="text" <input id="cslol_dir" ref={cslol_dir_ref} value={tconfig.cslol_dir.clone()}/>
id="cslol_dir"
oninput={ctx.link().callback(|e: InputEvent| Msg::UpdateCSLoLDir(e.target_unchecked_into::<HtmlInputElement>().value()))}
value={self.cslol_dir.clone()} />
</div> </div>
<div class="option">
<div>
<label for="wad_dir">{"Custom location of Champion Wads"}</label> <label for="wad_dir">{"Custom location of Champion Wads"}</label>
<br/> <br/>
<input type="text" <input id="wad_dir" ref={wad_dir_ref} />
id="wad_dir"
oninput={ctx.link().callback(|e: InputEvent| Msg::UpdateWadDir(e.target_unchecked_into::<HtmlInputElement>().value()))}
value={self.wad_dir.clone()} />
</div> </div>
<div class="option">
<div>
<label for="extract_dir">{"Custom location to Extract to"}</label> <label for="extract_dir">{"Custom location to Extract to"}</label>
<br/> <br/>
<input type="text" <input id="extract_dir" ref={extract_dir_ref} />
id="extract_dir"
oninput={ctx.link().callback(|e: InputEvent| Msg::UpdateExtractDir(e.target_unchecked_into::<HtmlInputElement>().value()))}
value={self.extract_dir.clone()} />
</div> </div>
<div class="option">
<button type="submit">{"Save Settings"}</button> <button type="submit">{"Save Settings"}</button>
</div>
</form> </form>
</div> </div>
} </div>
} }
} }
// #[function_component(Settings)]
// pub fn settings() -> Html {
// html! {
// <div class="content">
// <h2>{"Settings"}</h2>
// <form action="/write-settings" method="post">
// <div class="option">
// <label for="cslol_dir">{"Location of CSLoL"}</label>
// <br/>
// <input type="text" name="cslol_dir" id="cslol_dir" value="{{ config.cslol_dir }}"/>
// </div>
// <div class="option">
// <label for="wad_dir">{"Custom location of Champion Wads"}</label>
// <br/>
// <input type="text" name="wad_dir" id="wad_dir" value="{{ config.wad_dir }}"/>
// </div>
// <div class="option">
// <label for="extract_dir">{"Custom location to Extract to"}</label>
// <br/>
// <input type="text" name="extract_dir" id="extract_dir" value="{{ config.extract_dir }}"/>
// </div>
// <div class="option">
// <button type="submit">{"Save Settings"}</button>
// </div>
// </form>
// </div>
// }
// }