diff --git a/src/reskit/level/converter.rs b/src/reskit/level/converter.rs index c8ea933..85d508b 100644 --- a/src/reskit/level/converter.rs +++ b/src/reskit/level/converter.rs @@ -1,4 +1,4 @@ -use std::{error::Error, fs::read_to_string}; +use std::{error::Error, fs::read_to_string, path::Path, borrow::Cow}; use image::{DynamicImage, GenericImageView}; use roxmltree::Node; use crate::reskit::utility::print_warning; @@ -102,10 +102,11 @@ fn get_layer( layer: Node, map_width: usize, map_height: usize ) -> Result Result> { +fn get_tiles( tileset: Node, working_directory: &str ) -> Result> { // Get the image for the tileset let image = tileset.descendants().find( | node | node.tag_name() == "image".into() ).ok_or( "invalid file: no image object" )?; - let image = image::open( image.attribute( "source" ).ok_or( "invalid file: no source attribute on image" )? )?; + let image_path = format!( "{}/{}", working_directory, image.attribute( "source" ).ok_or( "invalid file: no source attribute on image" )? ); + let image = image::open( image_path )?; // Image must be a multiple of 8 (--system md) if image.width() % 8 != 0 { return Err( "invalid file: tileset width not multiple of 8" )? } @@ -141,6 +142,16 @@ fn get_tiles( tileset: Node ) -> Result> { pub fn get_tiled_tilemap( path: &str ) -> Result> { let file = read_to_string( path )?; let document = roxmltree::Document::parse( &file )?; + let working_directory = { + let result = Path::new( path ).parent().unwrap_or( Path::new( "." ) ).to_string_lossy(); + + if result == "" { + Cow::from( "." ) + } else { + result + } + }; + println!( "working directory is {}/", working_directory ); let map = document.descendants().find( | node | node.tag_name() == "map".into() ); if let Some( map ) = map { @@ -181,7 +192,7 @@ pub fn get_tiled_tilemap( path: &str ) -> Result> { // Build tileset (current version assumes one tileset per level) let tileset = map.descendants().find( | node | node.tag_name() == "tileset".into() ).ok_or( "invalid file: no tileset" )?; let tileset_source_path = tileset.attribute( "source" ).ok_or( "invalid file: no tileset source" )?; - let tileset_file = read_to_string( tileset_source_path )?; + let tileset_file = read_to_string( format!( "{}/{}", working_directory, tileset_source_path ) )?; let tileset_document = roxmltree::Document::parse( &tileset_file )?; let tileset = tileset_document.descendants().find( | node | node.tag_name() == "tileset".into() ).ok_or( "invalid file: no tileset origin object" )?; @@ -195,7 +206,7 @@ pub fn get_tiled_tilemap( path: &str ) -> Result> { return Err( "invalid file: referenced tilemap doesn't have same per-tile width and height of parent tilemap" )? } - let tileset = get_tiles( tileset )?; + let tileset = get_tiles( tileset, &working_directory )?; // Get the layers let layers: Vec = map.descendants()