Skip to content

Commit

Permalink
output resources to sd card image working at a basic level
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Feb 21, 2020
1 parent d926c91 commit 3c9dbf2
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 20 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion samples/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ run_bas: $(NAME).assembled.bas

all: OBJS_$(NAME)
clean:
rm -rf ./output *.o *.prg *.map *.av *.log *.assembled.bas
rm -rf ./output *.o *.prg *.map *.av *.log *.assembled.bas *.img
3 changes: 2 additions & 1 deletion samples/sprites/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ 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

generate_resources: project.av

Expand Down
4 changes: 3 additions & 1 deletion src/bin/aloevera.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ subcommands:
- output_file:
help: Output file name
index: 1
- sdimage:
- sd_image:
about: Create a new FAT32 SDCard Image in which to insert assembled binary assets
args:
- output_file:
Expand All @@ -46,6 +46,8 @@ subcommands:
takes_value: true
- sd_image:
help: If provided, output assets into the given FAT32 Image file instead of the local filesystem
short: s
takes_value: true
subcommands:
- all: Export all assets in the project file
- palette:
Expand Down
25 changes: 21 additions & 4 deletions src/cmd/asm/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ use vera::{AsmFormat, Assemblable, VeraBitmap, VeraSprite};
pub struct AsmArgs {
pub out_dir: String,
pub format: AsmFormat,
pub sd_image: Option<String>,
}

fn perform_assemble<T>(
values: &mut dyn Iterator<Item = &T>,
output_format: &AsmFormat,
out_dir: &str,
line_start: &mut usize,
sd_image: &Option<String>,
) -> Result<(), Error>
where
T: Assemblable,
Expand All @@ -39,7 +41,7 @@ where
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)?;
common::output_to_file(&file_name, &code.data, sd_image)?;
(asm_meta.to_string(None)?, "meta")
} else {
let asm_data = code.assemble_data(output_format.clone())?;
Expand All @@ -54,7 +56,7 @@ where
(res, "inc")
};
let file_name = format!("{}/{}.{}.{}", out_dir, v.id(), output_format, ext);
common::output_to_file(&file_name, output.as_bytes())?;
common::output_to_file(&file_name, output.as_bytes(), sd_image)?;
}
Ok(())
}
Expand All @@ -74,6 +76,7 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&args.format,
&pal_dir,
&mut line_start,
&args.sd_image,
)?;
}
if !proj.imagesets.is_empty() {
Expand All @@ -84,6 +87,7 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&args.format,
&img_dir,
&mut line_start,
&args.sd_image,
)?;
}
if !proj.tilemaps.is_empty() {
Expand All @@ -94,6 +98,7 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
&args.format,
&tm_dir,
&mut line_start,
&args.sd_image,
)?;
}
let mut sprites = vec![];
Expand All @@ -115,7 +120,13 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
let sprite = VeraSprite::init_from_imageset(&s.id, &imageset)?;
sprites.push(sprite);
}
perform_assemble(&mut sprites.iter(), &args.format, &sp_dir, &mut line_start)?;
perform_assemble(
&mut sprites.iter(),
&args.format,
&sp_dir,
&mut line_start,
&args.sd_image,
)?;
}
let mut bitmaps = vec![];
if !proj.bitmaps.is_empty() {
Expand All @@ -136,7 +147,13 @@ pub fn asm_all(g_args: &GlobalArgs, args: &AsmArgs) -> Result<(), Error> {
let bitmap = VeraBitmap::init_from_imageset(&b.id, &imageset)?;
bitmaps.push(bitmap);
}
perform_assemble(&mut bitmaps.iter(), &args.format, &bm_dir, &mut line_start)?;
perform_assemble(
&mut bitmaps.iter(),
&args.format,
&bm_dir,
&mut line_start,
&args.sd_image,
)?;
}

Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/asm/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ pub fn parse_asm_args(g_args: &GlobalArgs, args: &ArgMatches) -> Result<AsmArgs,
}
let out_dir = common::parse_required(args, "out_dir")?;
let asm_format = common::parse_required(args, "format")?;
let sd_image = match args.value_of("sd_image") {
Some(s) => Some(s.into()),
None => None,
};
Ok(AsmArgs {
out_dir: out_dir.into(),
format: AsmFormat::from_str(asm_format)?,
sd_image,
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/bitmap/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn bitmap_init(g_args: &GlobalArgs, args: &BitmapInitArgs) -> Result<(), Err
};
let bitmap = VeraBitmap::init_from_imageset(&args.id, &imageset)?;
proj.bitmaps.insert(args.id.clone(), bitmap);
common::output_to_file(&project_file, &proj.to_json()?.as_bytes())?;
common::output_to_file(&project_file, &proj.to_json()?.as_bytes(), &None)?;

Ok(())
}
14 changes: 11 additions & 3 deletions src/cmd/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use crate::{Error, ErrorKind};
use std::fs::{self, File};
use std::io::{Read, Write};
use util::fat;

use proj::AloeVeraProject;

Expand Down Expand Up @@ -81,9 +82,16 @@ pub fn parse_global_args(args: &ArgMatches) -> Result<GlobalArgs, Error> {
})
}

pub fn output_to_file(path: &str, data: &[u8]) -> Result<(), Error> {
let mut file = File::create(&path)?;
file.write_all(data)?;
pub fn output_to_file(path: &str, data: &[u8], sd_image: &Option<String>) -> Result<(), Error> {
match sd_image {
None => {
let mut file = File::create(&path)?;
file.write_all(data)?;
}
Some(i) => {
fat::write_file_to_image(i, path, data)?;
}
}
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/create/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn create_project(args: &CreateProjectArgs) -> Result<(), Error> {

let proj = AloeVeraProject::new(id);
let json = proj.to_json()?;
crate::cmd::common::output_to_file(&args.output_file, &json.as_bytes())?;
crate::cmd::common::output_to_file(&args.output_file, &json.as_bytes(), &None)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/create/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn execute_create_command(args: &ArgMatches) -> Result<(), Error> {
let a = arg_parse!(parse_create_project_args(args));
command::create_project(&a)
}
("sdimage", Some(args)) => {
("sd_image", Some(args)) => {
let a = arg_parse!(parse_create_sd_image_args(args));
command::create_sd_image(&a)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/imageset/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn insert_imageset(
//info!("Inserting imageset into project: {}", project_file);
let mut proj = crate::cmd::common::load_project(project_file.clone())?;
proj.imagesets.insert(id.into(), imageset.clone());
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_json()?.as_bytes())?;
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_json()?.as_bytes(), &None)?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/palette/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn palette_import(g_args: &GlobalArgs, args: &PaletteImportArgs) -> Result<(
let proj_json = common::read_file_string(&project_file)?;
let mut proj = AloeVeraProject::new_from_json(&proj_json)?;
proj.palettes.insert(palette.id.clone(), palette);
common::output_to_file(&project_file, &proj.to_json()?.as_bytes())?;
common::output_to_file(&project_file, &proj.to_json()?.as_bytes(), &None)?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/cmd/sprite/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn sprite_init(g_args: &GlobalArgs, args: &SpriteInitArgs) -> Result<(), Err
};
let sprite = VeraSprite::init_from_imageset(&args.id, &imageset)?;
proj.sprites.insert(args.id.clone(), sprite);
common::output_to_file(&project_file, &proj.to_json()?.as_bytes())?;
common::output_to_file(&project_file, &proj.to_json()?.as_bytes(), &None)?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/cmd/tilemap/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn insert_tilemap(
) -> Result<(), Error> {
let mut proj = crate::cmd::common::load_project(project_file.clone())?;
proj.tilemaps.insert(id.into(), tilemap.clone());
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_json()?.as_bytes())?;
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_json()?.as_bytes(), &None)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ zip = { version = "0.5", default-features = false }
parking_lot = {version = "0.6"}
fatfs = "0.3"
flate2 = "1.0"
fscommon = "0.1"
2 changes: 1 addition & 1 deletion util/src/data/create.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
./dir2fat32.sh -f fat32_template.img 32 contents
./dir2fat32.sh -f -S 512 fat32_template.img 33 contents
gzip fat32_template.img
2 changes: 1 addition & 1 deletion util/src/data/dir2fat32.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ $WRITE

mkpartition() {
fallocate -l ${SIZE}M "$PARTITION"
mkfs.fat -F32 -S"$LOGICAL_SECTOR_SIZE" "$PARTITION" >/dev/null
mkfs.fat -F32 -S"$LOGICAL_SECTOR_SIZE" -n "ALOEVERA" "$PARTITION" >/dev/null
}

copyfiles() {
Expand Down
Binary file modified util/src/data/fat32_template.img.gz
Binary file not shown.
47 changes: 46 additions & 1 deletion util/src/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

use fatfs;
use flate2::write::GzDecoder;
use std::fs::File;
use fscommon::{BufStream, StreamSlice};
use std::fs::{self, File, OpenOptions};
use std::io::prelude::*;
use std::io::BufWriter;
use std::path::Path;

/// Store our template fat32 image nice and compressed
/// in the binary, (it's mostly zeroes so it compresses well)
Expand All @@ -34,3 +36,46 @@ pub fn create_fat_image(filename: &str) -> Result<(), std::io::Error> {
decoder.try_finish()?;
Ok(())
}

/// Write bytes to a file in the image
pub fn write_file_to_image(
image_file: &str,
file_name: &str,
bytes: &[u8],
) -> Result<(), std::io::Error> {
let md = fs::metadata(image_file)?;
let img_file = OpenOptions::new().read(true).write(true).open(image_file)?;
// TODO: The image files we're creating have their partition
// aligned to 8MB, look into how to deal with this better
//let first_lba = 8*1024*1024;
let first_lba = 8 * 1024 * 1024;
let last_lba = md.len() - first_lba;
let partition = StreamSlice::new(img_file, first_lba, last_lba + 1)?;
let buf_wrt = BufStream::new(partition);

let options = fatfs::FsOptions::new().update_accessed_date(true);
let fs = fatfs::FileSystem::new(buf_wrt, options)?;

// have to create all directories manually
// TODO: Fix unwrap messes
let path = Path::new(file_name).to_path_buf();
let name = path.file_name().unwrap().to_str();
let mut path = Path::new(file_name).to_path_buf();
path.pop();

let mut path_str = String::from("");
for d in path.iter() {
let elem = d.to_str().unwrap();
if elem == "." {
continue;
};
path_str += &format!("{}", d.to_str().unwrap());
fs.root_dir().create_dir(&path_str)?;
path_str += "/";
}

path_str += name.unwrap();
let mut file = fs.root_dir().create_file(&path_str)?;
file.write_all(bytes)?;
Ok(())
}

0 comments on commit 3c9dbf2

Please sign in to comment.