Skip to content

Commit

Permalink
fix: offline/online shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed May 8, 2024
1 parent e7097d6 commit b7670bd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
6 changes: 4 additions & 2 deletions src-tauri/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::str::FromStr;
use tauri::Manager;

use crate::config;
#[cfg(feature = "offline")]
use crate::offline;

#[tauri::command]
pub fn launch(win: tauri::Window) -> bool {
pub fn launch(app: tauri::AppHandle) -> bool {
let config = config::get_config();
let url;

Expand All @@ -30,7 +32,7 @@ pub fn launch(win: tauri::Window) -> bool {
}
}

if let Some(mut win) = win.get_webview_window("main") {
if let Some(mut win) = app.get_webview_window("main") {
win.navigate(url);
}

Expand Down
37 changes: 22 additions & 15 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::str::FromStr;

use tauri::{Manager, Url};
use tauri::Url;

mod config;
mod game;
#[cfg(feature = "offline")]
mod offline;
mod util;

Expand All @@ -13,31 +14,37 @@ static LOCAL_URL: &str = "http://localhost:7653";
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let config = config::get_config();
let mut maybe_navigate: Option<Url> = None;

if config.skip_splash.unwrap_or(false) {
// If online, point context to the site, otherwise point to the soon-to-be-running local version
if config.offline.unwrap_or(false) {
if let Ok(url) = Url::from_str(LOCAL_URL) {
maybe_navigate = Some(url);
}
} else if let Ok(url) = Url::from_str(REMOTE_URL) {
maybe_navigate = Some(url);
}
}

tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
util::support::supports_offline,
config::read_config_file,
config::write_config_file,
config::default_config,
config::get_config,
game::launch
])
.setup(move |app| {
if let Some(navigate) = maybe_navigate {
game::launch(app.get_window("main").unwrap(), navigate);
if config.skip_splash.unwrap_or(false) {
if config.offline.unwrap_or(false) {
#[cfg(feature = "offline")]
game::launch(app.handle().clone());

Check warning on line 33 in src-tauri/src/lib.rs

View workflow job for this annotation

GitHub Actions / rustfmt (ubuntu-20.04)

Diff in /home/runner/work/RogueTop/RogueTop/src-tauri/src/lib.rs

Check warning on line 33 in src-tauri/src/lib.rs

View workflow job for this annotation

GitHub Actions / rustfmt (macos-latest)

Diff in /Users/runner/work/RogueTop/RogueTop/src-tauri/src/lib.rs

Check warning on line 33 in src-tauri/src/lib.rs

View workflow job for this annotation

GitHub Actions / rustfmt (windows-latest)

Diff in \\?\D:\a\RogueTop\RogueTop\src-tauri\src\lib.rs
#[cfg(not(feature = "offline"))]
{
warn!("Offline mode requested, but feature is not enabled. Opening options panel instead.");

// Write to the config that we should run in online mode
let mut config = config::get_config();
config.offline = Some(false);
let config_str = serde_json::to_string(&config).expect("Failed to serialize config!");

config::write_config_file(config_str);
}
} else {
game::launch(app.handle().clone());
}
}

Ok(())
Expand Down
8 changes: 7 additions & 1 deletion src-tauri/src/offline/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use rust_embed::RustEmbed;

#[cfg(target_os = "windows")]
#[derive(RustEmbed)]
#[folder = "../src-ext/dist"]
#[folder = r"..\src-ext\dist"]
pub struct GameFiles;

#[cfg(not(target_os = "windows"))]
#[derive(RustEmbed)]

Check warning on line 9 in src-tauri/src/offline/game.rs

View workflow job for this annotation

GitHub Actions / rustfmt (ubuntu-20.04)

Diff in /home/runner/work/RogueTop/RogueTop/src-tauri/src/offline/game.rs

Check warning on line 9 in src-tauri/src/offline/game.rs

View workflow job for this annotation

GitHub Actions / rustfmt (macos-latest)

Diff in /Users/runner/work/RogueTop/RogueTop/src-tauri/src/offline/game.rs

Check warning on line 9 in src-tauri/src/offline/game.rs

View workflow job for this annotation

GitHub Actions / rustfmt (windows-latest)

Diff in \\?\D:\a\RogueTop\RogueTop\src-tauri\src\offline\game.rs
#[folder = "../src-ext/dist"]
pub struct GameFiles;
1 change: 1 addition & 0 deletions src-tauri/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod log;
pub mod paths;
pub mod support;
4 changes: 4 additions & 0 deletions src-tauri/src/util/support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[tauri::command]
pub fn supports_offline() -> bool {

Check warning on line 2 in src-tauri/src/util/support.rs

View workflow job for this annotation

GitHub Actions / rustfmt (ubuntu-20.04)

Diff in /home/runner/work/RogueTop/RogueTop/src-tauri/src/util/support.rs

Check warning on line 2 in src-tauri/src/util/support.rs

View workflow job for this annotation

GitHub Actions / rustfmt (macos-latest)

Diff in /Users/runner/work/RogueTop/RogueTop/src-tauri/src/util/support.rs
cfg!(feature = "offline")
}
33 changes: 20 additions & 13 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import './App.css'
function App() {
const [selected, setSelected] = useState('online')
const [alwaysUse, setAlwaysUse] = useState(true)
const [supportsOffline, setSupportsOffline] = useState(false)

// Load the config
useEffect(() => {
(async () => {
const config: Config = await invoke('get_config')
const supportsOffline: boolean = await invoke('poke_supports_offline')
setSupportsOffline(supportsOffline)
setAlwaysUse(config.skip_splash)
setSelected(config.offline ? 'offline' : 'online')
})()
Expand Down Expand Up @@ -43,20 +46,24 @@ function App() {
</div>
</div>

<div
class={'mode ' + (selected === 'offline' ? 'selected' : '')}
id="offline"
onClick={() => {
setSelected('offline')
setConfig('offline', true)
}}
>
<span class="mode-title">Offline (LOCAL)</span>
{
supportsOffline && (
<div
class={'mode ' + (selected === 'offline' ? 'selected' : '')}
id="offline"
onClick={() => {
setSelected('offline')
setConfig('offline', true)
}}
>
<span class="mode-title">Offline (LOCAL)</span>

<div class="mode-img">
<img src="arrow.svg" alt="Offline" />
</div>
</div>
<div class="mode-img">
<img src="arrow.svg" alt="Offline" />
</div>
</div>
)
}

<div class="mode-lock">
<Checkbox
Expand Down

0 comments on commit b7670bd

Please sign in to comment.