aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src-tauri/src/main.rs
blob: 402f58fab2abfa0cf61d2acf541800a8d9162543 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::State;

mod core;
mod launcher;

#[tauri::command]
async fn start_game() -> Result<String, String> {
    println!("Backend received StartGame");
    match core::manifest::fetch_version_manifest().await {
        Ok(manifest) => {
            let msg = format!(
                "Fetched manifest.\nLatest release: {}\nLatest snapshot: {}",
                manifest.latest.release, manifest.latest.snapshot
            );
            println!("{}", msg);
            Ok(msg)
        }
        Err(e) => {
            eprintln!("Error fetching manifest: {}", e);
            Err(e.to_string())
        }
    }
}

#[tauri::command]
async fn get_versions() -> Result<Vec<core::manifest::Version>, String> {
    match core::manifest::fetch_version_manifest().await {
        Ok(manifest) => Ok(manifest.versions),
        Err(e) => Err(e.to_string()),
    }
}

#[tauri::command]
async fn login_offline(
    state: State<'_, core::auth::AccountState>,
    username: String,
) -> Result<core::auth::OfflineAccount, String> {
    let uuid = core::auth::generate_offline_uuid(&username);
    let account = core::auth::OfflineAccount {
        username,
        uuid,
    };
    
    *state.active_account.lock().unwrap() = Some(account.clone());
    Ok(account)
}

#[tauri::command]
async fn get_active_account(
    state: State<'_, core::auth::AccountState>,
) -> Result<Option<core::auth::OfflineAccount>, String> {
    Ok(state.active_account.lock().unwrap().clone())
}

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .manage(core::auth::AccountState::new())
        .invoke_handler(tauri::generate_handler![start_game, get_versions, login_offline, get_active_account])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}