2023-04-09 21:47:57 -05:00
|
|
|
#![warn(clippy::pedantic)]
|
|
|
|
|
2023-03-28 11:00:15 -05:00
|
|
|
use std::env;
|
2023-04-04 23:05:05 -05:00
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
use std::io::{copy, BufWriter};
|
|
|
|
use std::path::{Path, PathBuf};
|
2023-03-31 11:46:12 -05:00
|
|
|
use std::sync::Arc;
|
2023-04-04 23:05:05 -05:00
|
|
|
use ureq::AgentBuilder;
|
|
|
|
use zip_extract::extract;
|
2023-03-28 11:00:15 -05:00
|
|
|
|
2023-03-30 11:17:09 -05:00
|
|
|
use tempfile::tempdir;
|
2023-03-29 22:21:36 -05:00
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let target = env::var("TARGET")?;
|
2023-03-30 11:17:09 -05:00
|
|
|
if target.contains("pc-windows-gnu") {
|
2023-03-29 22:21:36 -05:00
|
|
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
|
2023-04-04 23:05:05 -05:00
|
|
|
let temp = tempdir()?;
|
|
|
|
let temp_dir = temp.path();
|
2023-03-29 22:21:36 -05:00
|
|
|
|
2023-03-30 11:17:09 -05:00
|
|
|
//GETTING LIBRARY DIRECTORIES GIVEN THE BUILD TARGET
|
2023-03-28 11:00:15 -05:00
|
|
|
let mut lib_dir = manifest_dir.clone();
|
|
|
|
let mut dll_dir = manifest_dir.clone();
|
2023-04-04 23:05:05 -05:00
|
|
|
|
|
|
|
let mut zip_extract = manifest_dir.clone();
|
|
|
|
zip_extract.push("gnu-mingw");
|
2023-03-30 11:17:09 -05:00
|
|
|
lib_dir.push("gnu-mingw");
|
|
|
|
dll_dir.push("gnu-mingw");
|
2023-03-29 22:21:36 -05:00
|
|
|
|
2023-04-04 23:05:05 -05:00
|
|
|
let mut part = String::new();
|
2023-03-28 11:00:15 -05:00
|
|
|
if target.contains("x86_64") {
|
2023-04-04 23:05:05 -05:00
|
|
|
part += "x86_64-w64-mingw32";
|
|
|
|
lib_dir.push("x86_64-w64-mingw32");
|
|
|
|
dll_dir.push("x86_64-w64-mingw32");
|
2023-03-28 11:26:40 -05:00
|
|
|
} else {
|
2023-04-04 23:05:05 -05:00
|
|
|
part += "x86_64-w64-mingw32";
|
|
|
|
lib_dir.push("i686-w64-mingw32");
|
|
|
|
dll_dir.push("i686-w64-mingw32");
|
2023-03-28 11:00:15 -05:00
|
|
|
}
|
2023-04-04 23:05:05 -05:00
|
|
|
lib_dir.push("lib");
|
|
|
|
dll_dir.push("bin");
|
|
|
|
println!("DEBUG: Managed Dirs!");
|
2023-03-31 11:46:12 -05:00
|
|
|
|
2023-04-04 23:05:05 -05:00
|
|
|
if !zip_extract.exists() {
|
|
|
|
std::fs::create_dir_all(&zip_extract)?;
|
2023-03-31 11:46:12 -05:00
|
|
|
}
|
|
|
|
|
2023-04-04 23:05:05 -05:00
|
|
|
println!("DEBUG: Created Dirs!");
|
|
|
|
|
|
|
|
if !lib_dir.exists() {
|
|
|
|
//NOW THAT WE HAVE THE OUTPUT DIRECTORIES, WE NEED TO EXTRACT THE ZIP FILES INTO THE
|
|
|
|
//CORRECT DIRECTORIES
|
|
|
|
//returns zip files
|
|
|
|
let url_sdl = download_files(temp_dir,"https://github.com/libsdl-org/SDL/releases/download/release-2.26.4/SDL2-devel-2.26.4-mingw.zip")?;
|
|
|
|
url_sdl.sync_all()?;
|
|
|
|
let url_ttf = download_files(temp_dir,"https://github.com/libsdl-org/SDL_ttf/releases/download/release-2.20.2/SDL2_ttf-devel-2.20.2-mingw.zip")?;
|
|
|
|
url_ttf.sync_all()?;
|
|
|
|
let url_image = download_files(temp_dir,"https://github.com/libsdl-org/SDL_image/releases/download/release-2.6.3/SDL2_image-devel-2.6.3-mingw.zip")?;
|
|
|
|
url_image.sync_all()?;
|
|
|
|
|
|
|
|
println!("DEBUG: Downloaded Files!");
|
|
|
|
|
|
|
|
let zip_vec = vec![&url_sdl, &url_ttf, &url_image];
|
|
|
|
for file in zip_vec {
|
|
|
|
extract(file, &zip_extract, true)?;
|
|
|
|
println!("DEBUG: Extracted 'a' File");
|
|
|
|
}
|
|
|
|
temp.close()?;
|
|
|
|
}
|
2023-03-30 11:17:09 -05:00
|
|
|
|
|
|
|
//SEARCHES AND LINKS LIBRARIES WITH CARGO
|
2023-03-28 11:00:15 -05:00
|
|
|
println!("cargo:rustc-link-search=all={}", lib_dir.display());
|
2023-03-29 22:21:36 -05:00
|
|
|
|
2023-03-28 11:26:40 -05:00
|
|
|
for entry in std::fs::read_dir(dll_dir).expect("Can't read DLL dir") {
|
2023-03-28 11:00:15 -05:00
|
|
|
let entry_path = entry.expect("Invalid fs entry").path();
|
|
|
|
let file_name_result = entry_path.file_name();
|
|
|
|
let mut new_file_path = manifest_dir.clone();
|
|
|
|
if let Some(file_name) = file_name_result {
|
|
|
|
let file_name = file_name.to_str().unwrap();
|
2023-04-21 10:29:44 -05:00
|
|
|
if Path::new(file_name)
|
2023-04-09 21:47:57 -05:00
|
|
|
.extension()
|
2023-04-21 10:29:44 -05:00
|
|
|
.map_or(false, |ext| ext.eq_ignore_ascii_case("dll"))
|
2023-04-09 21:47:57 -05:00
|
|
|
{
|
2023-03-28 11:00:15 -05:00
|
|
|
new_file_path.push(file_name);
|
2023-03-28 11:26:40 -05:00
|
|
|
std::fs::copy(&entry_path, new_file_path.as_path())
|
|
|
|
.expect("Can't copy from DLL dir");
|
2023-03-28 11:00:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-29 22:21:36 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-09 22:09:46 -05:00
|
|
|
/// Downloads files and returns the file if it was able to be downloaded
|
2023-04-09 21:47:57 -05:00
|
|
|
/// # Panics
|
|
|
|
/// panics if no response or response lacks "content-disposition" header
|
|
|
|
/// # Errors
|
2023-04-21 10:29:44 -05:00
|
|
|
/// errors if
|
2023-04-09 22:09:46 -05:00
|
|
|
/// A: unable to get `TlsConnector`
|
2023-04-21 10:29:44 -05:00
|
|
|
/// B: A File is unable to be create
|
2023-04-09 22:09:46 -05:00
|
|
|
/// C: or if the reader is unable to be copied into the writer
|
2023-04-21 10:29:44 -05:00
|
|
|
pub fn download_files(path: &Path, url: &str) -> Result<File, Box<dyn std::error::Error>> {
|
2023-03-31 11:46:12 -05:00
|
|
|
let agent = AgentBuilder::new()
|
|
|
|
.tls_connector(Arc::new(native_tls::TlsConnector::new()?))
|
|
|
|
.build();
|
2023-03-29 22:21:36 -05:00
|
|
|
let resp = agent.get(url).call()?;
|
|
|
|
|
|
|
|
let content_disposition = resp.header("content-disposition").unwrap();
|
|
|
|
let file_name = content_disposition
|
|
|
|
.split("; ")
|
|
|
|
.find(|s| s.starts_with("filename="))
|
|
|
|
.unwrap()
|
2023-04-09 21:39:26 -05:00
|
|
|
.split('=')
|
2023-03-29 22:21:36 -05:00
|
|
|
.nth(1)
|
|
|
|
.unwrap()
|
|
|
|
.trim_matches('"');
|
|
|
|
|
|
|
|
// Create a new File object to store the downloaded zip file
|
2023-04-04 23:05:05 -05:00
|
|
|
|
|
|
|
let mut path = path.to_path_buf();
|
2023-04-09 21:39:26 -05:00
|
|
|
path.push(file_name);
|
2023-04-04 23:05:05 -05:00
|
|
|
let file = OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(&path)?;
|
2023-03-29 22:21:36 -05:00
|
|
|
|
|
|
|
// Use a BufWriter to efficiently write the contents of the response to the file
|
|
|
|
let mut writer = BufWriter::new(file);
|
|
|
|
copy(&mut resp.into_reader(), &mut writer)?;
|
|
|
|
|
|
|
|
Ok(writer.into_inner()?)
|
2023-03-28 11:00:15 -05:00
|
|
|
}
|