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 AssetWriter and AssetReader for correct path access #7

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ impl SlippyTilesSettings {
/// Used to initialize slippy tile settings without using default values.
/// Will also create the tiles directory immediately.
pub fn new(endpoint: &str, tiles_directory: &str) -> SlippyTilesSettings {
// Need to ensure tiles folder exists.
std::fs::create_dir_all(format!("assets/{tiles_directory}")).unwrap();

edouardpoitras marked this conversation as resolved.
Show resolved Hide resolved
SlippyTilesSettings {
endpoint: endpoint.to_owned(),
tiles_directory: PathBuf::from(tiles_directory),
Expand Down
43 changes: 37 additions & 6 deletions src/systems.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use bevy::{
asset::{
io::{AssetReaderError, AssetSourceId},
AssetServer, AsyncWriteExt as _,
},
ecs::event::EventReader,
prelude::{debug, EventWriter, Res, ResMut},
tasks::{futures_lite::future, IoTaskPool, Task},
Expand All @@ -18,6 +22,7 @@ pub fn download_slippy_tiles(
slippy_tiles_settings: Res<SlippyTilesSettings>,
mut slippy_tile_download_status: ResMut<SlippyTileDownloadStatus>,
mut slippy_tile_download_tasks: ResMut<SlippyTileDownloadTasks>,
asset_server: Res<AssetServer>,
) {
for download_slippy_tile in download_slippy_tile_events.read() {
let radius = download_slippy_tile.radius.0;
Expand All @@ -32,6 +37,7 @@ pub fn download_slippy_tiles(
&slippy_tiles_settings,
&mut slippy_tile_download_tasks,
&mut slippy_tile_download_status,
&asset_server,
);
}
}
Expand All @@ -45,6 +51,7 @@ fn handle_download_slippy_tile_event(
slippy_tiles_settings: &Res<SlippyTilesSettings>,
slippy_tile_download_tasks: &mut ResMut<SlippyTileDownloadTasks>,
slippy_tile_download_status: &mut ResMut<SlippyTileDownloadStatus>,
asset_server: &AssetServer,
) {
let spc = SlippyTileCoordinates { x, y };
let tiles_directory = slippy_tiles_settings.get_tiles_directory_string();
Expand All @@ -60,7 +67,7 @@ fn handle_download_slippy_tile_event(
download_slippy_tile_event.zoom_level,
download_slippy_tile_event.tile_size,
);
let file_exists = std::path::Path::new(&format!("assets/{filename}")).exists();
let file_exists = future::block_on(does_file_exist(asset_server, &filename));
match (
UseCache::new(download_slippy_tile_event.use_cache),
AlreadyDownloaded::new(already_downloaded),
Expand All @@ -83,6 +90,7 @@ fn handle_download_slippy_tile_event(
filename,
slippy_tile_download_tasks,
slippy_tile_download_status,
asset_server,
);
},
// Cache can be used and we have the file on disk.
Expand Down Expand Up @@ -114,6 +122,16 @@ fn get_tile_filename(
)
}

async fn does_file_exist(asset_server: &AssetServer, filename: &str) -> bool {
let asset_source = asset_server.get_source(AssetSourceId::Default).unwrap();
let asset_reader = asset_source.reader();
match asset_reader.read(Path::new(filename)).await {
Ok(_) => true,
Err(AssetReaderError::NotFound(_)) => false,
Err(err) => panic!("failed to check if the file {} exists: {:?}", filename, err),
}
}

fn download_and_track_slippy_tile(
spc: SlippyTileCoordinates,
zoom_level: ZoomLevel,
Expand All @@ -122,8 +140,16 @@ fn download_and_track_slippy_tile(
filename: String,
slippy_tile_download_tasks: &mut ResMut<SlippyTileDownloadTasks>,
slippy_tile_download_status: &mut ResMut<SlippyTileDownloadStatus>,
asset_server: &AssetServer,
) {
let task = download_slippy_tile(spc, zoom_level, tile_size, endpoint, filename.clone());
let task = download_slippy_tile(
spc,
zoom_level,
tile_size,
endpoint,
filename.clone(),
asset_server,
);
slippy_tile_download_tasks.insert(spc.x, spc.y, zoom_level, tile_size, task);
slippy_tile_download_status.insert_with_coords(
spc,
Expand All @@ -140,13 +166,14 @@ fn download_slippy_tile(
tile_size: TileSize,
endpoint: String,
filename: String,
asset_server: &AssetServer,
) -> Task<SlippyTileDownloadTaskResult> {
debug!(
"Fetching map tile at position {:?} with zoom level {:?} from {:?}",
spc, zoom_level, endpoint
);
let tile_url = get_tile_url(endpoint, tile_size, zoom_level, spc.x, spc.y);
spawn_slippy_tile_download_task(tile_url, filename)
spawn_slippy_tile_download_task(tile_url, filename, asset_server)
}

fn get_tile_url(
Expand All @@ -169,8 +196,10 @@ fn get_tile_url(
fn spawn_slippy_tile_download_task(
tile_url: String,
filename: String,
asset_server: &AssetServer,
) -> Task<SlippyTileDownloadTaskResult> {
let thread_pool = IoTaskPool::get();
let asset_server = asset_server.clone();
thread_pool.spawn(async move {
let request = ehttp::Request {
method: "GET".to_owned(),
Expand All @@ -181,9 +210,11 @@ fn spawn_slippy_tile_download_task(
let response = ehttp::fetch_async(request)
.await
.expect("Failed to fetch tile image");
let mut content = std::io::Cursor::new(response.bytes);
let mut file_out = std::fs::File::create(format!("assets/{filename}")).unwrap();
std::io::copy(&mut content, &mut file_out).unwrap();
let asset_source = asset_server.get_source(AssetSourceId::Default).unwrap();
let asset_writer = asset_source.writer().unwrap();
let mut writer = asset_writer.write(Path::new(&filename)).await.unwrap();
writer.write_all(&response.bytes).await.unwrap();
writer.close().await.unwrap();
SlippyTileDownloadTaskResult {
path: Path::new(&filename).to_path_buf(),
}
Expand Down
Loading