Skip to content

Commit

Permalink
add ability to specifically place assets during the output command
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Feb 21, 2020
1 parent 3c9dbf2 commit a023ec7
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
.build/
*output/
*.assembled.bas
*.img
2 changes: 2 additions & 0 deletions samples/bitmap/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ project.av: $(RESOURCES)
$(ALOEVERA) -p project.av bitmap init kq5_bmp kq5_screen
$(ALOEVERA) -p project.av asm ./output/ all
$(ALOEVERA) -p project.av asm -f basic ./output/ all
#$(ALOEVERA) create sd_image kq5.img
#$(ALOEVERA) -p project.av asm -f bin . -s kq5.img select kq5_bmp kq5_bmp.bin

generate_resources: project.av

Expand Down
1 change: 1 addition & 0 deletions samples/sprites/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project.av: $(RESOURCES)
$(ALOEVERA) -p project.av sprite init terra_sprite terra_imageset
$(ALOEVERA) -p project.av asm ./output/ all
$(ALOEVERA) -p project.av asm -f basic ./output/ all
$(ALOEVERA) -p project.av asm -f bin ./output/ all
# $(ALOEVERA) create sd_image terra.img
# $(ALOEVERA) -p project.av asm -f bin ./output/ -s terra.img all

Expand Down
14 changes: 12 additions & 2 deletions src/bin/aloevera.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ subcommands:
about: Assembles all resources in the project file
args:
- out_dir:
help: directory into which to place exported assembly
help: directory into which to place exported assembly. If using the `select` command, `out_file` will be added to this
index: 1
- format:
help: Assembly export format
Expand All @@ -49,7 +49,17 @@ subcommands:
short: s
takes_value: true
subcommands:
- all: Export all assets in the project file
- all:
about: Export all assets in the project file
- select:
about: Select an asset ID to output and its target locatiom
args:
- asset_id:
help: The ID of the asset to output
index: 1
- out_file:
help: Output asset location
index: 2
- palette:
about: Import and manipulate palette files
subcommands:
Expand Down
120 changes: 116 additions & 4 deletions src/cmd/asm/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::cmd::common::{self, GlobalArgs};
use crate::{Error, ErrorKind};
use vera::{AsmFormat, Assemblable, VeraBitmap, VeraSprite};

/// Arguments for asm command
/// Arguments for asm commands
pub struct AsmArgs {
pub out_dir: String,
pub format: AsmFormat,
Expand All @@ -29,6 +29,7 @@ fn perform_assemble<T>(
values: &mut dyn Iterator<Item = &T>,
output_format: &AsmFormat,
out_dir: &str,
file_name: Option<&str>,
line_start: &mut usize,
sd_image: &Option<String>,
) -> Result<(), Error>
Expand All @@ -40,8 +41,11 @@ where
let asm_meta = code.assemble_meta(output_format.clone())?;
let meta_lc = asm_meta.line_count();
let (output, ext) = if *output_format == AsmFormat::Bin {
let file_name = format!("{}/{}.bin", out_dir, v.id());
common::output_to_file(&file_name, &code.data, sd_image)?;
let file_name = match file_name {
Some(f) => f.into(),
None => format!("{}/{}.bin", out_dir, v.id()),
};
common::output_to_file(&file_name, &code.data_as_bin(None), sd_image)?;
(asm_meta.to_string(None)?, "meta")
} else {
let asm_data = code.assemble_data(output_format.clone())?;
Expand All @@ -55,7 +59,10 @@ where
*line_start += meta_lc + data_lc;
(res, "inc")
};
let file_name = format!("{}/{}.{}.{}", out_dir, v.id(), output_format, ext);
let file_name = match file_name {
Some(f) => format!("{}.meta", f),
None => format!("{}/{}.{}.{}", out_dir, v.id(), output_format, ext),
};
common::output_to_file(&file_name, output.as_bytes(), sd_image)?;
}
Ok(())
Expand All @@ -75,6 +82,7 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&mut proj.palettes.values(),
&args.format,
&pal_dir,
None,
&mut line_start,
&args.sd_image,
)?;
Expand All @@ -86,6 +94,7 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&mut proj.imagesets.values(),
&args.format,
&img_dir,
None,
&mut line_start,
&args.sd_image,
)?;
Expand All @@ -97,6 +106,7 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&mut proj.tilemaps.values(),
&args.format,
&tm_dir,
None,
&mut line_start,
&args.sd_image,
)?;
Expand Down Expand Up @@ -124,6 +134,7 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&mut sprites.iter(),
&args.format,
&sp_dir,
None,
&mut line_start,
&args.sd_image,
)?;
Expand Down Expand Up @@ -151,10 +162,111 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&mut bitmaps.iter(),
&args.format,
&bm_dir,
None,
&mut line_start,
&args.sd_image,
)?;
}

Ok(())
}

/// Arguments for asm commands
pub struct AsmSelectArgs {
pub asset_id: String,
pub out_file: String,
}

pub fn asm_select(
g_args: &GlobalArgs,
asm_args: &AsmArgs,
args: &AsmSelectArgs,
) -> Result<(), Error> {
let proj = common::load_project(g_args.project_file.clone())?;
let mut line_start = 10000;
// now look for the ID
if proj.palettes.contains_key(&args.asset_id) {
perform_assemble(
&mut proj.palettes.values().filter(|v| v.id == args.asset_id),
&asm_args.format,
".",
Some(&args.out_file),
&mut line_start,
&asm_args.sd_image,
)?;
return Ok(());
}
if proj.imagesets.contains_key(&args.asset_id) {
perform_assemble(
&mut proj.imagesets.values().filter(|v| v.id == args.asset_id),
&asm_args.format,
".",
Some(&args.out_file),
&mut line_start,
&asm_args.sd_image,
)?;
return Ok(());
}
if proj.tilemaps.contains_key(&args.asset_id) {
perform_assemble(
&mut proj.tilemaps.values().filter(|v| v.id == args.asset_id),
&asm_args.format,
".",
Some(&args.out_file),
&mut line_start,
&asm_args.sd_image,
)?;
return Ok(());
}
if proj.sprites.contains_key(&args.asset_id) {
let sprite = proj.sprites.get(&args.asset_id).unwrap();
let imageset = match proj.imagesets.get(&sprite.imageset_id) {
Some(i) => i,
None => {
let msg = format!(
"Imageset with id {} needed by sprite {} does not exist in project file.",
sprite.id, sprite.imageset_id
);
return Err(ErrorKind::ArgumentError(msg).into());
}
};
let sprite = VeraSprite::init_from_imageset(&sprite.id, &imageset)?;
perform_assemble(
&mut [sprite].to_vec().iter(),
&asm_args.format,
".",
Some(&args.out_file),
&mut line_start,
&asm_args.sd_image,
)?;
return Ok(());
}
if proj.bitmaps.contains_key(&args.asset_id) {
let bitmap = proj.bitmaps.get(&args.asset_id).unwrap();
let imageset = match proj.imagesets.get(&bitmap.imageset_id) {
Some(i) => i,
None => {
let msg = format!(
"Imageset with id {} needed by bitmap {} does not exist in project file.",
bitmap.id, bitmap.imageset_id
);
return Err(ErrorKind::ArgumentError(msg).into());
}
};
let bitmap = VeraBitmap::init_from_imageset(&bitmap.id, &imageset)?;
perform_assemble(
&mut [bitmap].to_vec().iter(),
&asm_args.format,
".",
Some(&args.out_file),
&mut line_start,
&asm_args.sd_image,
)?;
return Ok(());
}
let msg = format!(
"Asset with id {} does not exist in project file.",
args.asset_id,
);
Err(ErrorKind::ArgumentError(msg).into())
}
27 changes: 23 additions & 4 deletions src/cmd/asm/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::str::FromStr;

use clap::ArgMatches;

use super::command::{self, AsmArgs};
use super::command::{self, AsmArgs, AsmSelectArgs};
use crate::cmd::common::{self, GlobalArgs};
use crate::{Error, ErrorKind};

Expand All @@ -39,11 +39,30 @@ pub fn parse_asm_args(g_args: &GlobalArgs, args: &ArgMatches) -> Result<AsmArgs,
})
}

pub fn parse_asm_select_args(
asm_args: &AsmArgs,
args: &ArgMatches,
) -> Result<AsmSelectArgs, Error> {
let asset_id = common::parse_required(args, "asset_id")?;
let out_file = common::parse_required(args, "out_file")?;
Ok(AsmSelectArgs {
asset_id: asset_id.into(),
out_file: format!(
"{}{}{}",
asm_args.out_dir,
std::path::MAIN_SEPARATOR,
out_file
),
})
}

pub fn execute_asm_command(g_args: &GlobalArgs, args: &ArgMatches) -> Result<(), Error> {
let a = arg_parse!(parse_asm_args(g_args, args));
match args.subcommand() {
("all", Some(_a)) => {
let a = arg_parse!(parse_asm_args(g_args, args));
command::asm_all(g_args, &a)
("all", Some(_)) => command::asm_all(g_args, &a),
("select", Some(args)) => {
let s = parse_asm_select_args(&a, &args)?;
command::asm_select(g_args, &a, &s)
}
_ => {
let msg = format!("Unknown sub command, use 'aloevera asm --help' for details");
Expand Down
18 changes: 17 additions & 1 deletion vera/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub struct AssembledPrimitive {
/// to load
meta: Vec<String>,
/// Assembled raw binary data
pub data: Vec<u8>,
data: Vec<u8>,
}

impl AssembledPrimitive {
Expand Down Expand Up @@ -149,6 +149,22 @@ impl AssembledPrimitive {
}
}

/// retrieve bin data formatted as bin data
pub fn data_as_bin(&self, address_bytes: Option<[u8; 2]>) -> Vec<u8> {
//TODO: Some other method that doesn't involve cloning data
let mut address_bytes = match address_bytes {
Some(b) => b.to_vec(),
None => [0, 0].to_vec(),
};
address_bytes.append(&mut self.data.clone());
address_bytes
}

/// retrieve the raw bin data
pub fn data_raw(&self) -> &Vec<u8> {
&self.data
}

/// Output Meta, formatted for assembly target
pub fn assemble_meta(&self, out_format: AsmFormat) -> Result<AssembledString, Error> {
let mut retval = AssembledString::new(&out_format);
Expand Down

0 comments on commit a023ec7

Please sign in to comment.