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

Added parse_with_file_loader: overrides direct access to the filesystem while loading external tilesets. #100

Closed
wants to merge 1 commit into from

Conversation

koalefant
Copy link

@koalefant koalefant commented May 7, 2021

Hi, I would like to use external tileset in my game, yet my resources are not in the filesystem, they are packed in an archive. So I am adding a function parse_with_file_loader(reader, file_loader) that allows me to supply a closure that is invoked when external tileset is loaded. Do you think this can be upstreamed in any form?

@bjorn
Copy link
Member

bjorn commented Dec 23, 2021

@koalefant This looks very useful! Would you be able to rebase the change to resolve the conflict in lib.rs?

Also, @PieKing1215, I see you've already merged this change. Would you be available for providing a code review? I'm looking for additional people to help maintain this crate now that it has moved to the @mapeditor organization.

@@ -236,7 +236,7 @@ impl Map {
fn new<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
map_path: Option<&Path>,
mut external_file_loader: impl FnMut(&str)->Result<Vec<u8>, TiledError>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this function should return a TiledError as Err, since it's performing an external data fetch, not something actually related to the Tiled object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe instead of Vec<u8> it should be an object implementing Read, that way implementing async later would be easier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on what @alexdevteam said. In my use case, I needed to wrap the actual errors with TiledError::Other and needed to manually read_to_end on a perfectly good File. These two changes would move that bit of bloat from user code into the library.

@PieKing1215
Copy link
Member

Also, @PieKing1215, I see you've already merged this change. Would you be available for providing a code review? I'm looking for additional people to help maintain this crate now that it has moved to the @mapeditor organization.

Sure, I can review this as well.
I'm using this library pretty extensively in a project so I've liberally merged/fixed several things in my fork, including this PR which I needed for it to work well with ggez.
I have a few of my own small additions that I should really make PRs for as well.

@@ -1082,7 +1076,7 @@ fn parse_animation<R: Read>(parser: &mut EventReader<R>) -> Result<Vec<Frame>, T
fn parse_infinite_data<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
width: u32,
_width: u32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: should this be in this PR? (not sure how you want to handle tiny extra fixes like this)

Suggested change
_width: u32,
width: u32,

@@ -236,7 +236,7 @@ impl Map {
fn new<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
map_path: Option<&Path>,
mut external_file_loader: impl FnMut(&str)->Result<Vec<u8>, TiledError>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on what @alexdevteam said. In my use case, I needed to wrap the actual errors with TiledError::Other and needed to manually read_to_end on a perfectly good File. These two changes would move that bit of bloat from user code into the library.

@@ -1290,12 +1287,33 @@ fn parse_impl<R: Read>(reader: R, map_path: Option<&Path>) -> Result<Map, TiledE
}
}

fn default_file_loader(map_path: Option<PathBuf>)->impl FnMut(&str)->Result<Vec<u8>, TiledError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn default_file_loader(map_path: Option<PathBuf>)->impl FnMut(&str)->Result<Vec<u8>, TiledError> {
fn default_file_loader(map_path: Option<PathBuf>) -> impl FnMut(&str)->Result<Vec<u8>, TiledError> {

Might also want to do the same in all of the FnMut(&str)->Results.

Comment on lines +1290 to +1292
fn default_file_loader(map_path: Option<PathBuf>)->impl FnMut(&str)->Result<Vec<u8>, TiledError> {
move |source: &str| {
let tileset_path = map_path.as_ref()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this to avoid needing to_owned in the default_file_loader(Some(path.to_owned()))s below.

Suggested change
fn default_file_loader(map_path: Option<PathBuf>)->impl FnMut(&str)->Result<Vec<u8>, TiledError> {
move |source: &str| {
let tileset_path = map_path.as_ref()
fn default_file_loader(map_path: Option<&Path>)->impl FnMut(&str)->Result<Vec<u8>, TiledError> + '_ {
move |source: &str| {
let tileset_path = map_path

@@ -236,7 +236,7 @@ impl Map {
fn new<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
map_path: Option<&Path>,
mut external_file_loader: impl FnMut(&str)->Result<Vec<u8>, TiledError>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to extract this into a type alias but I think that would be blocked until either rust-lang/rust#41517 or rust-lang/rust#63063 is stable.

@PieKing1215 PieKing1215 mentioned this pull request Feb 14, 2022
@aleokdev
Copy link
Contributor

aleokdev commented Mar 3, 2022

Closing due to inactivity. Feel free to reopen.

@aleokdev aleokdev closed this Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Options for more easily opening a map with external files from a special source
4 participants