potential background getting

This commit is contained in:
SpikeHD 2022-05-18 18:23:33 -07:00
parent ad41dff1a1
commit 480fc48c75
3 changed files with 81 additions and 12 deletions

View File

@ -20,6 +20,6 @@ pub fn rename(path: String, new_name: String) {
}
#[tauri::command]
pub fn dir_exists(path: String) -> bool {
pub fn dir_exists(path: &str) -> bool {
return fs::metadata(&path).is_ok();
}

View File

@ -162,8 +162,51 @@ async fn req_get(url: String) -> String {
}
#[tauri::command]
async fn get_bg_file() -> String {
let query = web::query("https://api.grasscutters.xyz/cultivation/query").await;
let response_data: APIQuery = serde_json::from_str(&query).unwrap();
return response_data.backgroundFile;
async fn get_bg_file(bg_path: String) -> String {
let query = web::query("https://api.grasscutters.xyz/cultivation/query").await;
let response_data: APIQuery = match serde_json::from_str(&query) {
Ok(data) => data,
Err(e) => {
println!("Failed to parse response: {}", e);
return "".to_string();
}
};
let file_name = response_data.backgroundFile.to_string() + ".png";
// First we see if the file already exists in our local bg folder
if file_helpers::dir_exists(format!(".\\bg\\{}", file_name).as_str()) {
let cwd = std::env::current_dir().unwrap();
return format!("{}\\{}", cwd.display(), response_data.backgroundFile.as_str());
}
// Now we check if the bg folder, which is one directory above the game_path, exists.
let bg_img_path = format!("{}\\{}", bg_path.clone().to_string(), file_name.as_str());
// If it doesn't, then we do not have backgrounds to grab.
if !file_helpers::dir_exists(&bg_path) {
return "".to_string();
}
// BG folder does exist, lets see if the image exists
if !file_helpers::dir_exists(&bg_img_path) {
// Image doesn't exist
return "".to_string();
}
// The image exists, lets copy it to our local \bg folder
let bg_img_path_local = format!(".\\bg\\{}", file_name.as_str());
match std::fs::copy(bg_img_path, bg_img_path_local) {
Ok(_) => {
// Copy was successful, lets return true
let cwd = std::env::current_dir().unwrap();
return format!("{}\\{}", cwd.display(), response_data.backgroundFile.as_str());
},
Err(e) => {
// Copy failed, lets return false
println!("Failed to copy background image: {}", e);
return "".to_string();
}
};
}

View File

@ -14,17 +14,19 @@ import DownloadList from './components/common/DownloadList'
import Downloads from './components/menu/Downloads'
import NewsSection from './components/news/NewsSection'
import RightBar from './components/RightBar'
import { setConfigOption } from '../utils/configuration'
import { getConfigOption, setConfigOption } from '../utils/configuration'
import { invoke } from '@tauri-apps/api'
interface IProps {
[key: string]: never;
[key: string]: never;
}
interface IState {
isDownloading: boolean;
optionsOpen: boolean;
miniDownloadsOpen: boolean;
downloadsOpen: boolean;
isDownloading: boolean;
optionsOpen: boolean;
miniDownloadsOpen: boolean;
downloadsOpen: boolean;
bgFile: string;
}
const downloadHandler = new DownloadHandler()
@ -37,6 +39,7 @@ class App extends React.Component<IProps, IState> {
optionsOpen: false,
miniDownloadsOpen: false,
downloadsOpen: false,
bgFile: '',
}
listen('lang_error', (payload) => {
@ -48,9 +51,32 @@ class App extends React.Component<IProps, IState> {
})
}
async componentDidMount() {
const game_exe = await getConfigOption('game_install_path')
const game_path = game_exe.substring(0, game_exe.lastIndexOf('\\'))
const root_path = game_path.substring(0, game_path.lastIndexOf('\\'))
if (game_path) {
// Get the bg by invoking, then set the background to that bg
const bgLoc: string = await invoke('get_bg_file', {
bgPath: root_path,
})
if (bgLoc) {
this.setState({
bgFile: bgLoc
})
}
}
}
render() {
return (
<div className="App">
<div className="App" style={
this.state.bgFile ? {
backgroundImage: `url(${this.state.bgFile})`,
} : {}
}>
<TopBar
optFunc={() => {
this.setState({ optionsOpen: !this.state.optionsOpen })