Skip to content

Commit

Permalink
move texture instead of copy
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWaddles committed Feb 14, 2019
1 parent 8a5254d commit a19d5ed
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "john-wick-parse"
version = "0.4.0"
version = "0.4.1"
authors = ["Waddlesworth <[email protected]>"]
edition = "2018"

Expand All @@ -11,4 +11,4 @@ serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
erased-serde = "0.3"
image ="0.21"
image = { git = "https://github.com/SirWaddles/image" }
40 changes: 33 additions & 7 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ enum FPropertyTagType {
IntProperty(i32),
UInt16Property(u16),
UInt32Property(u32),
UInt64Property(u64),
ArrayProperty(UScriptArray),
MapProperty(UScriptMap),
ByteProperty(u8),
Expand Down Expand Up @@ -1272,6 +1273,7 @@ impl FPropertyTagType {
"IntProperty" => FPropertyTagType::IntProperty(reader.read_i32::<LittleEndian>()?),
"UInt16Property" => FPropertyTagType::UInt16Property(reader.read_u16::<LittleEndian>()?),
"UInt32Property" => FPropertyTagType::UInt32Property(reader.read_u32::<LittleEndian>()?),
"UInt64Property" => FPropertyTagType::UInt64Property(reader.read_u64::<LittleEndian>()?),
"ArrayProperty" => match tag_data.unwrap() {
FPropertyTagData::ArrayProperty(inner_type) => FPropertyTagType::ArrayProperty(
UScriptArray::new(reader, inner_type, name_map, import_map)?
Expand Down Expand Up @@ -1476,11 +1478,16 @@ impl Newable for FTexture2DMipMap {
}
}

#[allow(dead_code)]
impl FTexture2DMipMap {
pub fn get_bytes(&self) -> &Vec<u8> {
&self.data.data
}

pub fn get_bytes_move(self) -> Vec<u8> {
self.data.data
}

pub fn get_width(&self) -> u32 {
self.size_x as u32
}
Expand Down Expand Up @@ -1613,24 +1620,35 @@ impl Texture2D {
})
}

pub fn get_pixel_format(&self) -> Option<&str> {
pub fn get_pixel_format(&self) -> ParserResult<&str> {
let pdata = match self.textures.get(0) {
Some(data) => data,
None => return None,
None => return Err(ParserError::new(format!("No textures found"))),
};
Some(&pdata.pixel_format)
Ok(&pdata.pixel_format)
}

pub fn get_texture(&self) -> Option<&FTexture2DMipMap> {
pub fn get_texture(&self) -> ParserResult<&FTexture2DMipMap> {
let pdata = match self.textures.get(0) {
Some(data) => data,
None => return None,
None => return Err(ParserError::new(format!("No textures part of export"))),
};
let texture = match pdata.mips.get(0) {
Some(data) => data,
None => return None,
None => return Err(ParserError::new(format!("No mipmaps part of texture"))),
};
Some(texture)
Ok(texture)
}

pub fn get_texture_move(mut self) -> ParserResult<FTexture2DMipMap> {
if self.textures.len() <= 0 {
return Err(ParserError::new(format!("No textures part of export")));
}
let mut texture = self.textures.swap_remove(0);
if texture.mips.len() <= 0 {
return Err(ParserError::new(format!("No mipmaps part of texture")));
}
Ok(texture.mips.swap_remove(0))
}
}

Expand Down Expand Up @@ -1732,6 +1750,14 @@ impl Package {
None => return Err(ParserError::new(format!("index {} out of range", index))),
}.as_ref())
}

pub fn get_export_move(mut self, index: usize) -> ParserResult<Box<dyn Any>> {
if index < self.exports.len() {
Ok(self.exports.swap_remove(index))
} else {
Err(ParserError::new(format!("No exports found")))
}
}
}

impl Serialize for Package {
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ pub fn read_asset(asset: Vec<u8>, uexp: Vec<u8>) -> ParserResult<Package> {
}

/// Extracts a raw RGBA texture from a Package struct
pub fn read_texture(package: &Package) -> ParserResult<Vec<u8>> {
let texture = match package.get_export(0)?.downcast_ref::<Texture2D>() {
Some(data) => data,
None => return Err(ParserError::new(format!("Package does not export texture"))),
pub fn read_texture(package: Package) -> ParserResult<Vec<u8>> {
let package_export = package.get_export_move(0)?;
let texture = match package_export.downcast::<Texture2D>() {
Ok(data) => data,
Err(_) => return Err(ParserError::new(format!("Export is not texture"))),
};

texture::decode_texture(texture)
texture::decode_texture(*texture)
}
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ fn texture(params: &[String]) -> CommandResult {
};

let package = assets::Package::from_file(path)?;
let texture = match package.get_export(0)?.downcast_ref::<assets::Texture2D>() {
Some(data) => data,
None => return cerr("Package not exporting texture"),
let package_export = package.get_export_move(0)?;
let texture = match package_export.downcast::<assets::Texture2D>() {
Ok(data) => data,
Err(_) => return cerr("Package not exporting texture"),
};

let texture_bytes = texture::decode_texture(texture)?;
let texture_bytes = texture::decode_texture(*texture)?;

let save_path = path.clone() + ".png";
let mut file = fs::File::create(save_path).unwrap();
Expand Down
19 changes: 6 additions & 13 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,24 @@ impl From<ImageError> for ParserError {
}
}

pub fn decode_texture(texture: &Texture2D) -> ParserResult<Vec<u8>> {
let pixel_format = match texture.get_pixel_format() {
Some(data) => data,
None => return Err(ParserError::new(format!("Could not find format"))),
};
let texture_mipmap = match texture.get_texture() {
Some(data) => data,
None => return Err(ParserError::new(format!("Could not find texture"))),
};
pub fn decode_texture(texture: Texture2D) -> ParserResult<Vec<u8>> {
let pixel_format = texture.get_pixel_format()?.to_owned();
let texture_mipmap = texture.get_texture_move()?;

let data = TextureData {
width: texture_mipmap.get_width(),
height: texture_mipmap.get_height(),
data: texture_mipmap.get_bytes().clone(),
data: texture_mipmap.get_bytes_move(),
};

let bytes: Vec<u8> = match pixel_format {
let bytes: Vec<u8> = match pixel_format.as_ref() {
"PF_DXT5" => decode_texture_dxt5(&data)?,
"PF_DXT1" => decode_texture_dxt1(&data)?,
"PF_B8G8R8A8" => data.data,
_ => return Err(ParserError::new(format!("Unsupported pixel format: {}", pixel_format))),
};

let colour_type = match pixel_format {
let colour_type = match pixel_format.as_ref() {
"PF_B8G8R8A8" => image::BGRA(8),
_ => image::RGBA(8),
};
Expand All @@ -56,7 +50,6 @@ pub fn decode_texture(texture: &Texture2D) -> ParserResult<Vec<u8>> {

fn decode_texture_dxt5(data: &TextureData) -> ParserResult<Vec<u8>> {
let reader = Cursor::new(&data.data);
println!("Size: {} {}", data.width, data.height);
let decoder = dxt::DXTDecoder::new(reader, data.width, data.height, dxt::DXTVariant::DXT5)?;

let bytes = decoder.read_image()?;
Expand Down

0 comments on commit a19d5ed

Please sign in to comment.