2020-05-05 13:27:10 +00:00
|
|
|
use futures::executor::block_on;
|
2020-05-05 21:33:37 +00:00
|
|
|
use image::RgbaImage;
|
2020-05-05 21:53:55 +00:00
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
2020-05-05 13:27:10 +00:00
|
|
|
use ruffle_core::backend::audio::NullAudioBackend;
|
|
|
|
use ruffle_core::backend::input::NullInputBackend;
|
|
|
|
use ruffle_core::backend::navigator::NullNavigatorBackend;
|
2020-06-15 16:53:19 +00:00
|
|
|
use ruffle_core::backend::storage::MemoryStorageBackend;
|
2020-05-05 13:27:10 +00:00
|
|
|
use ruffle_core::tag_utils::SwfMovie;
|
|
|
|
use ruffle_core::Player;
|
|
|
|
use ruffle_render_wgpu::target::TextureTarget;
|
|
|
|
use ruffle_render_wgpu::WgpuRenderBackend;
|
2020-05-05 21:33:37 +00:00
|
|
|
use std::error::Error;
|
2020-05-05 13:27:10 +00:00
|
|
|
use std::fs::create_dir_all;
|
|
|
|
use std::path::{Path, PathBuf};
|
2020-05-05 22:17:01 +00:00
|
|
|
use std::rc::Rc;
|
2020-05-05 13:27:10 +00:00
|
|
|
use structopt::StructOpt;
|
2020-05-05 21:33:37 +00:00
|
|
|
use walkdir::{DirEntry, WalkDir};
|
2020-05-05 13:27:10 +00:00
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
#[derive(StructOpt, Debug, Copy, Clone)]
|
|
|
|
struct SizeOpt {
|
|
|
|
/// The amount to scale the page size with
|
|
|
|
#[structopt(long = "scale", default_value = "1.0")]
|
|
|
|
scale: f32,
|
|
|
|
|
|
|
|
/// Optionaly override the output width
|
|
|
|
#[structopt(long = "width")]
|
|
|
|
width: Option<u32>,
|
|
|
|
|
|
|
|
/// Optionaly override the output height
|
|
|
|
#[structopt(long = "height")]
|
|
|
|
height: Option<u32>,
|
|
|
|
}
|
|
|
|
|
2020-05-05 13:27:10 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
struct Opt {
|
2020-05-05 21:33:37 +00:00
|
|
|
/// The file or directory of files to export frames from
|
2020-05-05 13:27:10 +00:00
|
|
|
#[structopt(name = "swf", parse(from_os_str))]
|
|
|
|
swf: PathBuf,
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
/// The file or directory (if multiple frames/files) to store the capture in.
|
|
|
|
/// The default value will either be:
|
|
|
|
/// - If given one swf and one frame, the name of the swf + ".png"
|
|
|
|
/// - If given one swf and multiple frames, the name of the swf as a directory
|
|
|
|
/// - If given multiple swfs, this field is required.
|
2020-05-05 13:27:10 +00:00
|
|
|
#[structopt(name = "output", parse(from_os_str))]
|
|
|
|
output_path: Option<PathBuf>,
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
/// Number of frames to capture per file
|
2020-05-05 13:27:10 +00:00
|
|
|
#[structopt(short = "f", long = "frames", default_value = "1")]
|
|
|
|
frames: u32,
|
2020-05-05 21:53:55 +00:00
|
|
|
|
2020-05-22 15:58:51 +00:00
|
|
|
/// Number of frames to skip
|
|
|
|
#[structopt(long = "skipframes", default_value = "0")]
|
|
|
|
skipframes: u32,
|
|
|
|
|
2020-05-05 21:53:55 +00:00
|
|
|
/// Don't show a progress bar
|
|
|
|
#[structopt(short, long)]
|
|
|
|
silent: bool,
|
2020-06-13 19:55:55 +00:00
|
|
|
|
|
|
|
#[structopt(flatten)]
|
|
|
|
size: SizeOpt,
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
fn take_screenshot(
|
2020-05-05 22:17:01 +00:00
|
|
|
device: Rc<wgpu::Device>,
|
|
|
|
queue: Rc<wgpu::Queue>,
|
2020-05-05 21:33:37 +00:00
|
|
|
swf_path: &Path,
|
2020-05-05 13:27:10 +00:00
|
|
|
frames: u32,
|
2020-05-22 15:58:51 +00:00
|
|
|
skipframes: u32,
|
2020-05-05 21:53:55 +00:00
|
|
|
progress: &Option<ProgressBar>,
|
2020-06-13 19:55:55 +00:00
|
|
|
size: SizeOpt,
|
2020-05-05 21:33:37 +00:00
|
|
|
) -> Result<Vec<RgbaImage>, Box<dyn std::error::Error>> {
|
2020-05-05 21:53:55 +00:00
|
|
|
let movie = SwfMovie::from_path(&swf_path)?;
|
2020-05-05 13:27:10 +00:00
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
let width = size.width.unwrap_or_else(|| movie.width());
|
|
|
|
let width = (width as f32 * size.scale).round() as u32;
|
|
|
|
|
|
|
|
let height = size.height.unwrap_or_else(|| movie.height());
|
|
|
|
let height = (height as f32 * size.scale).round() as u32;
|
|
|
|
|
|
|
|
let target = TextureTarget::new(&device, (width, height));
|
2020-05-05 13:27:10 +00:00
|
|
|
let player = Player::new(
|
|
|
|
Box::new(WgpuRenderBackend::new(device, queue, target)?),
|
|
|
|
Box::new(NullAudioBackend::new()),
|
|
|
|
Box::new(NullNavigatorBackend::new()),
|
|
|
|
Box::new(NullInputBackend::new()),
|
|
|
|
movie,
|
2020-06-15 16:53:19 +00:00
|
|
|
Box::new(MemoryStorageBackend::default()),
|
2020-05-05 13:27:10 +00:00
|
|
|
)?;
|
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
player
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.set_viewport_dimensions(width, height);
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
let mut result = Vec::new();
|
2020-05-22 15:58:51 +00:00
|
|
|
let totalframes = frames + skipframes;
|
|
|
|
|
|
|
|
for i in 0..totalframes {
|
2020-05-05 21:53:55 +00:00
|
|
|
if let Some(progress) = &progress {
|
|
|
|
progress.set_message(&format!(
|
|
|
|
"{} frame {}",
|
|
|
|
swf_path.file_stem().unwrap().to_string_lossy(),
|
|
|
|
i
|
|
|
|
));
|
|
|
|
}
|
2020-05-05 13:27:10 +00:00
|
|
|
player.lock().unwrap().run_frame();
|
2020-05-22 15:58:51 +00:00
|
|
|
if i >= skipframes {
|
|
|
|
player.lock().unwrap().render();
|
|
|
|
let mut player = player.lock().unwrap();
|
|
|
|
let renderer = player
|
|
|
|
.renderer_mut()
|
|
|
|
.downcast_mut::<WgpuRenderBackend<TextureTarget>>()
|
|
|
|
.unwrap();
|
|
|
|
let target = renderer.target();
|
|
|
|
if let Some(image) = target.capture(renderer.device()) {
|
|
|
|
result.push(image);
|
|
|
|
} else {
|
|
|
|
return Err(format!("Unable to capture frame {} of {:?}", i, swf_path).into());
|
|
|
|
}
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|
2020-05-05 21:53:55 +00:00
|
|
|
|
|
|
|
if let Some(progress) = &progress {
|
|
|
|
progress.inc(1);
|
|
|
|
}
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
Ok(result)
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:55 +00:00
|
|
|
fn find_files(root: &Path, with_progress: bool) -> Vec<DirEntry> {
|
|
|
|
let progress = if with_progress {
|
|
|
|
Some(ProgressBar::new_spinner())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-05-05 21:33:37 +00:00
|
|
|
let mut results = Vec::new();
|
|
|
|
|
|
|
|
for entry in WalkDir::new(root)
|
|
|
|
.follow_links(true)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
{
|
|
|
|
let f_name = entry.file_name().to_string_lossy();
|
|
|
|
|
|
|
|
if f_name.ends_with(".swf") {
|
|
|
|
results.push(entry);
|
2020-05-05 21:53:55 +00:00
|
|
|
if let Some(progress) = &progress {
|
|
|
|
progress.set_message(&format!("Searching for swf files... {}", results.len()));
|
|
|
|
}
|
2020-05-05 21:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:55 +00:00
|
|
|
if let Some(progress) = &progress {
|
|
|
|
progress.finish_with_message(&format!("Found {} swf files to export", results.len()));
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
results
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capture_single_swf(
|
2020-05-05 22:17:01 +00:00
|
|
|
device: Rc<wgpu::Device>,
|
|
|
|
queue: Rc<wgpu::Queue>,
|
2020-06-13 19:55:55 +00:00
|
|
|
opt: &Opt,
|
2020-05-05 21:33:37 +00:00
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-06-13 19:55:55 +00:00
|
|
|
let output = opt.output_path.clone().unwrap_or_else(|| {
|
2020-05-05 13:27:10 +00:00
|
|
|
let mut result = PathBuf::new();
|
2020-06-13 19:55:55 +00:00
|
|
|
if opt.frames == 1 {
|
|
|
|
result.set_file_name(opt.swf.file_stem().unwrap());
|
2020-05-05 13:27:10 +00:00
|
|
|
result.set_extension("png");
|
|
|
|
} else {
|
2020-06-13 19:55:55 +00:00
|
|
|
result.set_file_name(opt.swf.file_stem().unwrap());
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
});
|
2020-05-05 21:33:37 +00:00
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
if opt.frames > 1 {
|
2020-05-05 13:27:10 +00:00
|
|
|
let _ = create_dir_all(&output);
|
|
|
|
}
|
2020-05-05 21:33:37 +00:00
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
let progress = if !opt.silent {
|
|
|
|
let progress = ProgressBar::new(opt.frames as u64);
|
2020-05-05 21:53:55 +00:00
|
|
|
progress.set_style(
|
|
|
|
ProgressStyle::default_bar()
|
|
|
|
.template(
|
|
|
|
"[{elapsed_precise}] {bar:40.cyan/blue} [{eta_precise}] {pos:>7}/{len:7} {msg}",
|
|
|
|
)
|
|
|
|
.progress_chars("##-"),
|
|
|
|
);
|
|
|
|
Some(progress)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
let frames = take_screenshot(
|
|
|
|
device,
|
|
|
|
queue,
|
|
|
|
&opt.swf,
|
|
|
|
opt.frames,
|
|
|
|
opt.skipframes,
|
|
|
|
&progress,
|
|
|
|
opt.size,
|
|
|
|
)?;
|
2020-05-05 21:53:55 +00:00
|
|
|
|
|
|
|
if let Some(progress) = &progress {
|
2020-06-13 19:55:55 +00:00
|
|
|
progress.set_message(&opt.swf.file_stem().unwrap().to_string_lossy());
|
2020-05-05 21:53:55 +00:00
|
|
|
}
|
2020-05-05 21:33:37 +00:00
|
|
|
|
|
|
|
if frames.len() == 1 {
|
|
|
|
frames.get(0).unwrap().save(&output)?;
|
|
|
|
} else {
|
|
|
|
for (frame, image) in frames.iter().enumerate() {
|
|
|
|
let mut path = PathBuf::from(&output);
|
|
|
|
path.push(format!("{}.png", frame));
|
|
|
|
image.save(&path)?;
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|
2020-05-05 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let message = if frames.len() == 1 {
|
|
|
|
format!(
|
|
|
|
"Saved first frame of {} to {}",
|
2020-06-13 19:55:55 +00:00
|
|
|
opt.swf.to_string_lossy(),
|
2020-05-05 21:53:55 +00:00
|
|
|
output.to_string_lossy()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
2020-05-05 21:33:37 +00:00
|
|
|
"Saved first {} frames of {} to {}",
|
|
|
|
frames.len(),
|
2020-06-13 19:55:55 +00:00
|
|
|
opt.swf.to_string_lossy(),
|
2020-05-05 21:33:37 +00:00
|
|
|
output.to_string_lossy()
|
2020-05-05 21:53:55 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(progress) = progress {
|
|
|
|
progress.finish_with_message(&message);
|
|
|
|
} else {
|
|
|
|
println!("{}", message);
|
2020-05-05 21:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capture_multiple_swfs(
|
2020-05-05 22:17:01 +00:00
|
|
|
device: Rc<wgpu::Device>,
|
|
|
|
queue: Rc<wgpu::Queue>,
|
2020-06-13 19:55:55 +00:00
|
|
|
opt: &Opt,
|
2020-05-05 21:33:37 +00:00
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-06-13 19:55:55 +00:00
|
|
|
let output = opt.output_path.clone().unwrap();
|
|
|
|
let files = find_files(&opt.swf, !opt.silent);
|
2020-05-05 21:53:55 +00:00
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
let progress = if !opt.silent {
|
|
|
|
let progress = ProgressBar::new((files.len() as u64) * (opt.frames as u64));
|
2020-05-05 21:53:55 +00:00
|
|
|
progress.set_style(
|
|
|
|
ProgressStyle::default_bar()
|
|
|
|
.template(
|
|
|
|
"[{elapsed_precise}] {bar:40.cyan/blue} [{eta_precise}] {pos:>7}/{len:7} {msg}",
|
|
|
|
)
|
|
|
|
.progress_chars("##-"),
|
|
|
|
);
|
|
|
|
Some(progress)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-05-05 21:33:37 +00:00
|
|
|
|
|
|
|
for file in &files {
|
2020-05-05 22:17:01 +00:00
|
|
|
let frames = take_screenshot(
|
|
|
|
device.clone(),
|
|
|
|
queue.clone(),
|
|
|
|
&file.path(),
|
2020-06-13 19:55:55 +00:00
|
|
|
opt.frames,
|
|
|
|
opt.skipframes,
|
2020-05-05 22:17:01 +00:00
|
|
|
&progress,
|
2020-06-13 19:55:55 +00:00
|
|
|
opt.size,
|
2020-05-05 22:17:01 +00:00
|
|
|
)?;
|
2020-05-05 21:53:55 +00:00
|
|
|
|
|
|
|
if let Some(progress) = &progress {
|
|
|
|
progress.set_message(&file.path().file_stem().unwrap().to_string_lossy());
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
let mut relative_path = file
|
|
|
|
.path()
|
2020-06-13 19:55:55 +00:00
|
|
|
.strip_prefix(&opt.swf)
|
2020-05-05 21:33:37 +00:00
|
|
|
.unwrap_or_else(|_| &file.path())
|
|
|
|
.to_path_buf();
|
|
|
|
|
|
|
|
if frames.len() == 1 {
|
2020-06-13 19:55:55 +00:00
|
|
|
let mut destination = PathBuf::from(&output);
|
2020-05-05 21:33:37 +00:00
|
|
|
relative_path.set_extension("png");
|
|
|
|
destination.push(relative_path);
|
|
|
|
if let Some(parent) = destination.parent() {
|
|
|
|
let _ = create_dir_all(parent);
|
|
|
|
}
|
|
|
|
frames.get(0).unwrap().save(&destination)?;
|
|
|
|
} else {
|
2020-06-13 19:55:55 +00:00
|
|
|
let mut parent = PathBuf::from(&output);
|
2020-05-05 21:33:37 +00:00
|
|
|
relative_path.set_extension("");
|
|
|
|
parent.push(&relative_path);
|
|
|
|
let _ = create_dir_all(&parent);
|
|
|
|
for (frame, image) in frames.iter().enumerate() {
|
|
|
|
let mut destination = parent.clone();
|
|
|
|
destination.push(format!("{}.png", frame));
|
|
|
|
image.save(&destination)?;
|
|
|
|
}
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 21:33:37 +00:00
|
|
|
|
2020-06-13 19:55:55 +00:00
|
|
|
let message = if opt.frames == 1 {
|
2020-05-05 21:53:55 +00:00
|
|
|
format!(
|
2020-05-05 21:33:37 +00:00
|
|
|
"Saved first frame of {} files to {}",
|
|
|
|
files.len(),
|
|
|
|
output.to_string_lossy()
|
2020-05-05 21:53:55 +00:00
|
|
|
)
|
2020-05-05 21:33:37 +00:00
|
|
|
} else {
|
2020-05-05 21:53:55 +00:00
|
|
|
format!(
|
2020-05-05 21:33:37 +00:00
|
|
|
"Saved first {} frames of {} files to {}",
|
2020-06-13 19:55:55 +00:00
|
|
|
opt.frames,
|
2020-05-05 21:33:37 +00:00
|
|
|
files.len(),
|
|
|
|
output.to_string_lossy()
|
2020-05-05 21:53:55 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(progress) = progress {
|
|
|
|
progress.finish_with_message(&message);
|
|
|
|
} else {
|
|
|
|
println!("{}", message);
|
2020-05-05 21:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
let opt: Opt = Opt::from_args();
|
|
|
|
let adapter = block_on(wgpu::Adapter::request(
|
|
|
|
&wgpu::RequestAdapterOptions {
|
|
|
|
power_preference: wgpu::PowerPreference::Default,
|
|
|
|
compatible_surface: None,
|
|
|
|
},
|
|
|
|
wgpu::BackendBit::PRIMARY,
|
|
|
|
))
|
|
|
|
.ok_or_else(|| {
|
|
|
|
"This tool requires hardware acceleration, but no compatible graphics device was found."
|
|
|
|
})?;
|
|
|
|
|
2020-05-05 22:17:01 +00:00
|
|
|
let (device, queue) = block_on(adapter.request_device(&wgpu::DeviceDescriptor {
|
|
|
|
extensions: wgpu::Extensions {
|
|
|
|
anisotropic_filtering: false,
|
|
|
|
},
|
|
|
|
limits: wgpu::Limits::default(),
|
|
|
|
}));
|
|
|
|
|
2020-05-05 21:33:37 +00:00
|
|
|
if opt.swf.is_file() {
|
2020-06-13 19:55:55 +00:00
|
|
|
capture_single_swf(Rc::new(device), Rc::new(queue), &opt)?;
|
|
|
|
} else if opt.output_path.is_some() {
|
|
|
|
capture_multiple_swfs(Rc::new(device), Rc::new(queue), &opt)?;
|
2020-05-05 21:33:37 +00:00
|
|
|
} else {
|
|
|
|
return Err("Output directory is required when exporting multiple files.".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-05-05 13:27:10 +00:00
|
|
|
}
|