From 011b15c8d99e026cbe359499074ebcbb12c56234 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Thu, 21 Jul 2022 22:41:15 -0700 Subject: [PATCH] split main launcher into seperate file --- src-tauri/src/gamebanana.rs | 18 ++++ src-tauri/src/main.rs | 2 + src-tauri/src/web.rs | 3 +- src/ui/App.tsx | 173 +++----------------------------- src/ui/Debug.tsx | 2 +- src/ui/Main.tsx | 187 +++++++++++++++++++++++++++++++++++ src/ui/components/TopBar.tsx | 4 + 7 files changed, 229 insertions(+), 160 deletions(-) create mode 100644 src-tauri/src/gamebanana.rs create mode 100644 src/ui/Main.tsx diff --git a/src-tauri/src/gamebanana.rs b/src-tauri/src/gamebanana.rs new file mode 100644 index 0000000..78e71e1 --- /dev/null +++ b/src-tauri/src/gamebanana.rs @@ -0,0 +1,18 @@ +use crate::web; + +static API_URL: &str = "https://api.gamebanana.com"; +static SITE_URL: &str = "https://gamebanana.com"; + +#[tauri::command] +pub async fn list_submissions(mode: String) -> String { + let res = web::query( + format!( + "{}/apiv9/Util/Game/Submissions?_idGameRow=8552&_nPage=1&_nPerpage=50&_sMode={}", + SITE_URL, mode + ) + .as_str(), + ) + .await; + + res +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 9a0b715..4ef14da 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -12,6 +12,7 @@ use sysinfo::{System, SystemExt}; mod downloader; mod file_helpers; +mod gamebanana; mod lang; mod metadata_patcher; mod proxy; @@ -59,6 +60,7 @@ fn main() { lang::get_languages, web::valid_url, web::web_get, + gamebanana::list_submissions, metadata_patcher::patch_metadata ]) .run(tauri::generate_context!()) diff --git a/src-tauri/src/web.rs b/src-tauri/src/web.rs index c56878f..29193ee 100644 --- a/src-tauri/src/web.rs +++ b/src-tauri/src/web.rs @@ -1,4 +1,4 @@ -use reqwest::header::USER_AGENT; +use reqwest::header::{CONTENT_TYPE, USER_AGENT}; pub(crate) async fn query(site: &str) -> String { let client = reqwest::Client::new(); @@ -6,6 +6,7 @@ pub(crate) async fn query(site: &str) -> String { let response = client .get(site) .header(USER_AGENT, "cultivation") + .header(CONTENT_TYPE, "application/json") .send() .await .unwrap(); diff --git a/src/ui/App.tsx b/src/ui/App.tsx index da2ce7d..f2e034f 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -1,101 +1,32 @@ import React from 'react' -import { listen } from '@tauri-apps/api/event' import './App.css' import DownloadHandler from '../utils/download' - -// Major Components -import TopBar from './components/TopBar' -import ServerLaunchSection from './components/ServerLaunchSection' -import MainProgressBar from './components/common/MainProgressBar' -import Options from './components/menu/Options' -import MiniDialog from './components/MiniDialog' -import DownloadList from './components/common/DownloadList' -import Downloads from './components/menu/Downloads' -import NewsSection from './components/news/NewsSection' -import Game from './components/menu/Game' - -import RightBar from './components/RightBar' -import { getConfigOption, setConfigOption } from '../utils/configuration' -import { invoke } from '@tauri-apps/api' -import { dataDir } from '@tauri-apps/api/path' -import { appWindow } from '@tauri-apps/api/window' -import { convertFileSrc } from '@tauri-apps/api/tauri' +import { getConfigOption } from '../utils/configuration' import { getTheme, loadTheme } from '../utils/themes' -import { unpatchGame } from '../utils/metadata' - -interface IProps { - [key: string]: never -} +import { convertFileSrc, invoke } from '@tauri-apps/api/tauri' +import { dataDir } from '@tauri-apps/api/path' +import { Main } from './Main' interface IState { - isDownloading: boolean - optionsOpen: boolean - miniDownloadsOpen: boolean - downloadsOpen: boolean - gameDownloadsOpen: boolean + moddingOpen: boolean bgFile: string } +const downloadHandler = new DownloadHandler() const DEFAULT_BG = 'https://api.grasscutter.io/cultivation/bgfile' -const downloadHandler = new DownloadHandler() - -class App extends React.Component { - constructor(props: IProps) { +class App extends React.Component, IState> { + constructor(props: Readonly) { super(props) + this.state = { - isDownloading: false, - optionsOpen: false, - miniDownloadsOpen: false, - downloadsOpen: false, - gameDownloadsOpen: false, + moddingOpen: false, bgFile: DEFAULT_BG, } - - listen('lang_error', (payload) => { - console.log(payload) - }) - - listen('jar_extracted', ({ payload }: { payload: string }) => { - setConfigOption('grasscutter_path', payload) - }) - - // Emitted for metadata replacing-purposes - listen('game_closed', async () => { - const wasPatched = await getConfigOption('patch_metadata') - - if (wasPatched) { - const unpatched = await unpatchGame() - - console.log(`unpatched game? ${unpatched}`) - - if (!unpatched) { - alert( - `Could not unpatch game! (You should be able to find your metadata backup in ${await dataDir()}\\cultivation\\)` - ) - } - } - }) - - let min = false - - // periodically check if we need to min/max based on whether the game is open - setInterval(async () => { - const gameOpen = await invoke('is_game_running') - - if (gameOpen && !min) { - appWindow.minimize() - min = true - } else if (!gameOpen && min) { - appWindow.unminimize() - min = false - } - }, 1000) } async componentDidMount() { - const cert_generated = await getConfigOption('cert_generated') const game_exe = await getConfigOption('game_install_path') const game_path = game_exe?.substring(0, game_exe.replace(/\\/g, '/').lastIndexOf('/')) || '' const root_path = game_path?.substring(0, game_path.replace(/\\/g, '/').lastIndexOf('/')) || '' @@ -154,22 +85,6 @@ class App extends React.Component { ) } } - - if (!cert_generated) { - // Generate the certificate - await invoke('generate_ca_files', { - path: (await dataDir()) + 'cultivation', - }) - - await setConfigOption('cert_generated', true) - } - - // Period check to only show progress bar when downloading files - setInterval(() => { - this.setState({ - isDownloading: downloadHandler.getDownloads().filter((d) => d.status !== 'finished')?.length > 0, - }) - }, 1000) } render() { @@ -184,69 +99,11 @@ class App extends React.Component { : {} } > - { - this.setState({ optionsOpen: !this.state.optionsOpen }) - }} - downFunc={() => this.setState({ downloadsOpen: !this.state.downloadsOpen })} - gameFunc={() => this.setState({ gameDownloadsOpen: !this.state.gameDownloadsOpen })} - /> - - - - - - { - // Mini downloads section - this.state.miniDownloadsOpen ? ( -
- { - this.setState({ miniDownloadsOpen: false }) - }} - > - - -
-
- ) : null - } - - { - // Download menu - this.state.downloadsOpen ? ( - this.setState({ downloadsOpen: false })} /> - ) : null - } - - { - // Options menu - this.state.optionsOpen ? ( - this.setState({ optionsOpen: !this.state.optionsOpen })} - /> - ) : null - } - - { - // Game downloads menu - this.state.gameDownloadsOpen ? ( - this.setState({ gameDownloadsOpen: false })} /> - ) : null - } - -
- - -
this.setState({ miniDownloadsOpen: !this.state.miniDownloadsOpen })} - > - {this.state.isDownloading ? : null} -
-
+ {this.state.moddingOpen ? null : ( + <> +
+ + )} ) } diff --git a/src/ui/Debug.tsx b/src/ui/Debug.tsx index 11d78de..74960bb 100644 --- a/src/ui/Debug.tsx +++ b/src/ui/Debug.tsx @@ -44,7 +44,7 @@ class Debug extends React.Component { render() { return (
- + diff --git a/src/ui/Main.tsx b/src/ui/Main.tsx new file mode 100644 index 0000000..8990ae1 --- /dev/null +++ b/src/ui/Main.tsx @@ -0,0 +1,187 @@ +import React from 'react' + +// Major Components +import TopBar from './components/TopBar' +import ServerLaunchSection from './components/ServerLaunchSection' +import MainProgressBar from './components/common/MainProgressBar' +import Options from './components/menu/Options' +import MiniDialog from './components/MiniDialog' +import DownloadList from './components/common/DownloadList' +import Downloads from './components/menu/Downloads' +import NewsSection from './components/news/NewsSection' +import Game from './components/menu/Game' +import RightBar from './components/RightBar' + +import { getConfigOption, setConfigOption } from '../utils/configuration' +import { invoke } from '@tauri-apps/api' +import { listen } from '@tauri-apps/api/event' +import { dataDir } from '@tauri-apps/api/path' +import { appWindow } from '@tauri-apps/api/window' +import { unpatchGame } from '../utils/metadata' +import DownloadHandler from '../utils/download' + +interface IProps { + downloadHandler: DownloadHandler +} + +interface IState { + isDownloading: boolean + optionsOpen: boolean + miniDownloadsOpen: boolean + downloadsOpen: boolean + gameDownloadsOpen: boolean + moddingOpen: boolean +} + +export class Main extends React.Component { + constructor(props: IProps) { + super(props) + this.state = { + isDownloading: false, + optionsOpen: false, + miniDownloadsOpen: false, + downloadsOpen: false, + gameDownloadsOpen: false, + moddingOpen: false, + } + + invoke('list_submissions') + + listen('lang_error', (payload) => { + console.log(payload) + }) + + listen('jar_extracted', ({ payload }: { payload: string }) => { + setConfigOption('grasscutter_path', payload) + }) + + // Emitted for metadata replacing-purposes + listen('game_closed', async () => { + const wasPatched = await getConfigOption('patch_metadata') + + if (wasPatched) { + const unpatched = await unpatchGame() + + console.log(`unpatched game? ${unpatched}`) + + if (!unpatched) { + alert( + `Could not unpatch game! (You should be able to find your metadata backup in ${await dataDir()}\\cultivation\\)` + ) + } + } + }) + + let min = false + + // periodically check if we need to min/max based on whether the game is open + setInterval(async () => { + const gameOpen = await invoke('is_game_running') + + if (gameOpen && !min) { + appWindow.minimize() + min = true + } else if (!gameOpen && min) { + appWindow.unminimize() + min = false + } + }, 1000) + } + + async componentDidMount() { + const cert_generated = await getConfigOption('cert_generated') + + if (!cert_generated) { + // Generate the certificate + await invoke('generate_ca_files', { + path: (await dataDir()) + 'cultivation', + }) + + await setConfigOption('cert_generated', true) + } + + // Period check to only show progress bar when downloading files + setInterval(() => { + this.setState({ + isDownloading: this.props.downloadHandler.getDownloads().filter((d) => d.status !== 'finished')?.length > 0, + }) + }, 1000) + } + + render() { + return ( + <> + { + this.setState({ optionsOpen: !this.state.optionsOpen }) + }} + downFunc={() => this.setState({ downloadsOpen: !this.state.downloadsOpen })} + gameFunc={() => this.setState({ gameDownloadsOpen: !this.state.gameDownloadsOpen })} + modFunc={() => this.setState({ moddingOpen: !this.state.moddingOpen })} + /> + + + + + + { + // Mini downloads section + this.state.miniDownloadsOpen ? ( +
+ { + this.setState({ miniDownloadsOpen: false }) + }} + > + + +
+
+ ) : null + } + + { + // Download menu + this.state.downloadsOpen ? ( + this.setState({ downloadsOpen: false })} + /> + ) : null + } + + { + // Options menu + this.state.optionsOpen ? ( + this.setState({ optionsOpen: !this.state.optionsOpen })} + /> + ) : null + } + + { + // Game downloads menu + this.state.gameDownloadsOpen ? ( + this.setState({ gameDownloadsOpen: false })} + /> + ) : null + } + +
+ + +
this.setState({ miniDownloadsOpen: !this.state.miniDownloadsOpen })} + > + {this.state.isDownloading ? : null} +
+
+ + ) + } +} diff --git a/src/ui/components/TopBar.tsx b/src/ui/components/TopBar.tsx index f1db052..8b2f55e 100644 --- a/src/ui/components/TopBar.tsx +++ b/src/ui/components/TopBar.tsx @@ -15,6 +15,7 @@ interface IProps { optFunc: () => void downFunc: () => void gameFunc: () => void + modFunc: () => void } interface IState { @@ -116,6 +117,9 @@ export default class TopBar extends React.Component {
downloads
+
+ MODS +
{/*
game
*/}