Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Ipc to fetch file list / urls from the backend #67

Merged
merged 6 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions gui/wizard/src/db/fetch.rs

This file was deleted.

1 change: 0 additions & 1 deletion gui/wizard/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod fetch;
pub mod search;
pub mod project;
pub mod global;
Expand Down
68 changes: 23 additions & 45 deletions gui/wizard/src/frame/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use fltk::{
use url as Url;
use anyhow::anyhow as ah;

use crate::gameimage;
use crate::dimm;
use crate::frame;
use crate::lib;
Expand All @@ -26,7 +27,11 @@ use crate::common::FltkSenderExt;
use crate::common::WidgetExtExtra;
use crate::common::PathBufExt;
use crate::log;
use crate::db;

macro_rules! log_return
{
($($arg:tt)*) => { { log!($($arg)*); return; } }
} // log_return

// fn url_basename() {{{
fn url_basename(url : Url::Url) -> anyhow::Result<String>
Expand Down Expand Up @@ -54,45 +59,16 @@ struct Data
btn_fetch : Button,
} // struct }}}

// fn verify_and_configure() {{{
fn verify_and_configure(mut output : Output) -> anyhow::Result<()>
// fn backend_validate_and_configure() {{{
fn backend_validate_and_configure(mut output : Output) -> anyhow::Result<()>
{
// Get platform
let str_platform = env::var("GIMG_PLATFORM")?.to_lowercase();

// Run backend to merge files
output.set_value("Validating and extracting...");

let arg_url_dwarfs;
let mut args = vec![
"fetch"
, "--platform"
, &str_platform
];

if let Ok(url) = env::var("GIMG_FETCH_URL_DWARFS")
{
arg_url_dwarfs = format!("--url-dwarfs={}", url);
args.push(&arg_url_dwarfs);
} // if

args.push("--sha");

// Verify sha
if common::gameimage_sync(args.clone()) != 0
{
log!("Failed to verify files with backend");
return Err(ah!("Failed to verify files with backend"));
} // if

let _ = args.pop();
// Run backend to merge files
gameimage::fetch::validate()?;

// Process downloaded files
if common::gameimage_sync(args.clone()) != 0
{
log!("Failed to configure downloaded files");
return Err(ah!("Failed to configure downloaded files"));
} // if
// Use backend to configure downloaded files
gameimage::fetch::configure()?;

Ok(())
}
Expand Down Expand Up @@ -139,15 +115,19 @@ pub fn fetch(tx: Sender<common::Msg>, title: &str)
// Populate 'vec_fetch' with links and download paths
let mut base = frame_content.as_base_widget();

let result_pairs_values = db::fetch::get();
let vec_files = match gameimage::fetch::query_files()
{
Ok(vec_files) => vec_files,
Err(e) => log_return!("Could not fetch url list from backend: {}", e),
};

if result_pairs_values.is_err()
let vec_urls = match gameimage::fetch::query_urls()
{
log!("Could not fetch file list, '{}'", result_pairs_values.unwrap_err().to_string());
return;
} // if
Ok(vec_urls) => vec_urls,
Err(e) => log_return!("Could not fetch file list from backend: {}", e),
};

for (entry_path, entry_url) in result_pairs_values.unwrap()
for (entry_path, entry_url) in std::iter::zip(vec_files, vec_urls)
{
// Get full path to save the file into
let file_dest = std::path::Path::new(&entry_path).to_path_buf();
Expand Down Expand Up @@ -327,7 +307,7 @@ pub fn fetch(tx: Sender<common::Msg>, title: &str)
std::thread::spawn(move ||
{
// Draw package creator
if verify_and_configure(clone_output_status.clone()).is_err()
if backend_validate_and_configure(clone_output_status.clone()).is_err()
{
log!("Failed to verify and configure downloaded files");
clone_output_status.set_value("Download the required files to proceed");
Expand All @@ -342,8 +322,6 @@ pub fn fetch(tx: Sender<common::Msg>, title: &str)

clone_tx.send_awake(common::Msg::DrawCreator);
});

// Export name for expected image path
});
}
// }}}
Expand Down
58 changes: 16 additions & 42 deletions gui/wizard/src/frame/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::common;
use crate::common::WidgetExtExtra;
use crate::common::FltkSenderExt;
use crate::log;
use crate::gameimage;

// enum Platform {{{
#[derive(PartialEq, Clone)]
Expand Down Expand Up @@ -166,7 +167,10 @@ pub fn platform(tx: Sender<common::Msg>, title: &str)
if *lock != Some(Platform::WineUrl) && let Ok(mut guard) = URL.lock()
{
*guard = None;
env::remove_var("GIMG_FETCH_URL_DWARFS");
if let Err(e) = gameimage::fetch::url_clear()
{
log!("Could not clear custom url: {}", e);
} // if
} // if

if let Some(platform) = lock.clone()
Expand Down Expand Up @@ -212,57 +216,27 @@ pub fn platform(tx: Sender<common::Msg>, title: &str)
let clone_tx = tx.clone();
clone_btn_next.set_callback(move |_|
{
// Get selected platform
let str_platform = if let Ok(guard) = PLATFORM.lock() && let Some(platform) = guard.clone()
{
platform.as_str()
} // if
else
{
clone_output_status.set_value("Could not determine chosen platform");
return;
}; // else

// Fetch files
clone_output_status.set_value("Fetching list of files to download");

// Disable window
clone_tx.send_awake(common::Msg::WindDeactivate);

// Fetch files
std::thread::spawn(move ||
// Set custom url if it was passed
if let Ok(guard) = URL.lock() && let Some(url) = guard.clone()
{
// Args for gameimage_sync
let arg_platform = format!("--platform={}", str_platform);
let arg_url_dwarfs;
let mut args = vec![
"fetch"
, arg_platform.as_str()
, "--json=gameimage.fetch.json"
];

if let Ok(guard) = URL.lock() && let Some(url) = guard.clone()
if let Err(e) = gameimage::fetch::set_url_dwarfs(&url)
{
arg_url_dwarfs = format!("--url-dwarfs={}", url);
args.push(arg_url_dwarfs.as_str());
env::set_var("GIMG_FETCH_URL_DWARFS", url);
} // match
else
{
log!("Failed to append --url-dwarfs for custom url");
} // else

// Ask back-end for the files to download for the selected platform
if common::gameimage_sync(args) != 0
{
log!("Failed to fetch");
clone_tx.send_awake(common::Msg::WindActivate);
return;
log!("Exit backend with error: {}", e);
} // if
} // match
else
{
log!("Could not set custom url");
} // else

// Draw next frame
clone_tx.send_awake(common::Msg::DrawFetch);
});
// Disable window
clone_tx.send_awake(common::Msg::DrawFetch);
});
}
// }}}
Expand Down
132 changes: 132 additions & 0 deletions gui/wizard/src/gameimage/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use anyhow::anyhow as ah;

use crate::log;
use crate::common;
use crate::lib;
use crate::gameimage;

macro_rules! log_return
{
($($arg:tt)*) => { { log!($($arg)*); return Err(ah!($($arg)*)); } }
} // log_return

// query() {{{
fn query(str_query : &str) -> anyhow::Result<Vec<String>>
{
let binary = gameimage::gameimage::binary()?;
let platform = gameimage::gameimage::platform()?;

let _ = gameimage::gameimage::gameimage_async(vec!
[
"fetch"
, "--platform", &platform
, "--ipc", &str_query
]);

let ipc = match lib::ipc::Ipc::new(binary, || {})
{
Ok(ipc) => ipc,
Err(e) => { log_return!("Could not create ipc instance: {}", e); },
}; // match

let mut vec = vec![];
while let Ok(msg) = ipc.recv()
{
vec.push(msg);
} // while

Ok(vec)
} // query() }}}

// query_urls() {{{
pub fn query_urls() -> anyhow::Result<Vec<String>>
{
Ok(query("urls")?)
} // query_urls() }}}

// query_files() {{{
pub fn query_files() -> anyhow::Result<Vec<String>>
{
Ok(query("files")?)
} // query_files() }}}

// url_clear() {{{
pub fn url_clear() -> anyhow::Result<i32>
{
let platform = gameimage::gameimage::platform()?;

let rc = gameimage::gameimage::gameimage_sync(vec!
[
"fetch"
, "--platform", &platform
, "--url-clear"
]);

if rc != 0 { return Err(ah!("backend exited with non-zero code: {}", rc)); } // if

Ok(rc)
} // url_clear() }}}

// set_url() {{{
fn set_url(str_type : &str, str_url : &str) -> anyhow::Result<i32>
{
let platform = gameimage::gameimage::platform()?;

let rc = gameimage::gameimage::gameimage_sync(vec!
[
"fetch"
, "--platform", &platform
, &str_type, &str_url
]);

if rc != 0 { return Err(ah!("backend exited with non-zero code: {}", rc)); } // if

Ok(rc)
} // set_url() }}}

// set_url_base() {{{
pub fn set_url_base(str_url : &str) -> anyhow::Result<i32>
{
Ok(set_url("--url-base", str_url)?)
} // set_url_base() }}}

// set_url_dwarfs() {{{
pub fn set_url_dwarfs(str_url : &str) -> anyhow::Result<i32>
{
Ok(set_url("--url-dwarfs", str_url)?)
} // set_url_dwarfs() }}}

// validate() {{{
pub fn validate() -> anyhow::Result<i32>
{
let platform = gameimage::gameimage::platform()?;

let rc = gameimage::gameimage::gameimage_sync(vec!
[
"fetch"
, "--platform", &platform
, "--sha"
]);

if rc == 0 { return Ok(rc); }

Err(ah!("Exit with error code {}", rc))
} // validate() }}}

// configure() {{{
pub fn configure() -> anyhow::Result<i32>
{
let platform = gameimage::gameimage::platform()?;

let rc = gameimage::gameimage::gameimage_sync(vec!
[
"fetch"
, "--platform", &platform
]);

if rc == 0 { return Ok(rc); }

Err(ah!("Exit with error code {}", rc))
} // configure() }}}

// vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :
Loading