Skip to content

Commit

Permalink
Merge pull request #19 from yeastplume/binary_project_file
Browse files Browse the repository at this point in the history
Binary project files + View command refactor
  • Loading branch information
yeastplume committed Jun 5, 2020
2 parents 58459d2 + 2c46fa1 commit 4481f91
Show file tree
Hide file tree
Showing 35 changed files with 610 additions and 242 deletions.
129 changes: 69 additions & 60 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion proj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ backtrace = "0.3"
lazy_static = "1"
serde = "1"
serde_derive = "1"
serde_json = "1"
log4rs = { version = "0.8.1", features = ["rolling_file_appender", "compound_policy", "size_trigger", "fixed_window_roller"] }
log = "0.4"
failure = "0.1"
failure_derive = "0.1"
bincode = "1.2"

aloevera_util = { path = "../util", version = "0.2.3" }
aloevera_vera = { path = "../vera", version = "0.2.3" }
17 changes: 12 additions & 5 deletions proj/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Error {
}

/// Wallet errors, mostly wrappers around underlying crypto or I/O errors.
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
#[derive(Debug, Fail)]
pub enum ErrorKind {
/// IO Error
#[fail(display = "I/O error: {}", _0)]
Expand All @@ -42,6 +42,9 @@ pub enum ErrorKind {
/// Argument Error
#[fail(display = "Argument Error: {}", _0)]
ArgumentError(String),
/// Errors from the Bincode crate
#[fail(display = "Bincode Error: {}", _0)]
BincodeError(bincode::Error),
/// Other
#[fail(display = "Generic error: {}", _0)]
GenericError(String),
Expand Down Expand Up @@ -74,10 +77,6 @@ impl Display for Error {
}

impl Error {
/// get kind
pub fn kind(&self) -> ErrorKind {
self.inner.get_context().clone()
}
/// get cause string
pub fn cause_string(&self) -> String {
match self.cause() {
Expand Down Expand Up @@ -124,3 +123,11 @@ impl From<ParseIntError> for Error {
}
}
}

impl From<bincode::Error> for Error {
fn from(error: bincode::Error) -> Error {
Error {
inner: Context::new(ErrorKind::BincodeError(error)),
}
}
}
13 changes: 6 additions & 7 deletions proj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
#![deny(unused_mut)]
#![warn(missing_docs)]

#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

extern crate aloevera_util as util;
extern crate aloevera_vera as vera;
Expand All @@ -36,8 +33,10 @@ pub use error::{Error, ErrorKind};

pub use project::AloeVeraProject;

/// Just a wrapper around a serializable object
pub trait Jsonable {
/// to json
fn to_json(&self) -> Result<String, Error>;
/// And around the binary version
pub trait Binable {
/// to binary
fn to_bin(&self) -> Result<Vec<u8>, Error>;
/// from binary
fn from_bin(encoded: &Vec<u8>) -> Result<Box<Self>, Error>;
}
29 changes: 11 additions & 18 deletions proj/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.
//! Top Level Project file definition

use crate::Jsonable;
use crate::{Error, ErrorKind};
use crate::Binable;
use crate::Error;
use std::collections::BTreeMap;
use vera::{VeraBitmap, VeraImageSet, VeraPalette, VeraSprite, VeraTileMap};

Expand All @@ -35,13 +35,15 @@ pub struct AloeVeraProject<'a> {
pub bitmaps: BTreeMap<String, VeraBitmap<'a>>,
}

impl<'a> Jsonable for AloeVeraProject<'a> {
fn to_json(&self) -> Result<String, Error> {
serde_json::to_string_pretty(&self).map_err(|e| {
let msg = format!("Unable to create JSON: {}", e);
error!("{}", msg);
ErrorKind::JSONError(msg).into()
})
impl<'a> Binable for AloeVeraProject<'a> {
fn to_bin(&self) -> Result<Vec<u8>, Error> {
let encoded = bincode::serialize(&self)?;
Ok(encoded)
}

fn from_bin(encoded: &Vec<u8>) -> Result<Box<Self>, Error> {
let decoded = bincode::deserialize(&encoded[..])?;
Ok(Box::new(decoded))
}
}

Expand All @@ -57,13 +59,4 @@ impl<'a> AloeVeraProject<'a> {
bitmaps: BTreeMap::new(),
}
}

/// from json
pub fn new_from_json(json: &str) -> Result<Self, Error> {
serde_json::from_str(&json).map_err(|e| {
let msg = format!("Unable to parse Project JSON: {}", e);
error!("{}", msg);
ErrorKind::JSONError(msg).into()
})
}
}
27 changes: 16 additions & 11 deletions src/bin/aloevera.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: aloevera
about: Graphic Asset Pipeline for the Commander X1
about: Graphic Asset Pipeline for the Commander X16
author: Revcore Technologies Ltd.

args:
Expand Down Expand Up @@ -84,8 +84,6 @@ subcommands:
- input_file:
help: Input PNG file name
index: 2
- list:
about: List palettes in a project file
- imageset:
about: Import and manipulate image sets
subcommands:
Expand Down Expand Up @@ -116,8 +114,6 @@ subcommands:
- pixel_depth:
help: Target Pixel Depth (8, 4, 2 or 1)
index: 3
- list:
about: List image sets in a project file
- tilemap:
about: Import and manipulate tilemaps
subcommands:
Expand Down Expand Up @@ -187,8 +183,6 @@ subcommands:
short: c
takes_value: true
default_value: "0"
- list:
about: List tilemaps in a project file
- sprite:
about: Interpret an existing Imageset as a Sprite
subcommands:
Expand All @@ -201,8 +195,6 @@ subcommands:
- imageset_id:
help: ID of the existing, formatted Imageset to use as a Sprite
index: 2
- list:
about: List sprites in a project file
- bitmap:
about: Interpret an existing Imageset as a Bitmap
subcommands:
Expand All @@ -215,5 +207,18 @@ subcommands:
- imageset_id:
help: ID of the existing, formatted Imageset to use as a Bitmap
index: 2
- list:
about: List bitmaps in a project file
- list:
about: List elements in a project file
args:
- object_type:
help: Type of elements to view
possible_values:
- all
- palettes
- imagesets
- tilemaps
- sprites
- bitmaps
default_value: "all"
index: 1

22 changes: 4 additions & 18 deletions src/cmd/bitmap/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::{Error, ErrorKind};
use proj::{AloeVeraProject, Jsonable};
use proj::{AloeVeraProject, Binable};

use crate::cmd::common::{self, GlobalArgs};
use vera::VeraBitmap;
Expand All @@ -34,8 +34,8 @@ pub fn bitmap_init(g_args: &GlobalArgs, args: &BitmapInitArgs) -> Result<(), Err
}
};
info!("Adding bitmap into project: {}", project_file);
let proj_json = common::read_file_string(&project_file)?;
let mut proj = AloeVeraProject::new_from_json(&proj_json)?;
let encoded = common::read_file_bin(&project_file)?;
let mut proj = *AloeVeraProject::from_bin(&encoded)?;
let imageset = match proj.imagesets.get(&args.imageset_id) {
Some(i) => i,
None => {
Expand All @@ -48,21 +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(), &None)?;
common::output_to_file(&project_file, &proj.to_bin()?, &None)?;

Ok(())
}

pub fn bitmap_list(g_args: &GlobalArgs) -> Result<(), Error> {
let proj = common::load_project(g_args.project_file.clone())?;
println!("Bitmaps:");
for (id, bitmap) in proj.bitmaps {
println!(
" {}: width {} depth {}",
id,
bitmap.width.val_as_u32(),
bitmap.depth
);
}
Ok(())
}
1 change: 0 additions & 1 deletion src/cmd/bitmap/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub fn execute_bitmap_command(g_args: &GlobalArgs, args: &ArgMatches) -> Result<
let a = arg_parse!(parse_bitmap_init_args(g_args, args));
command::bitmap_init(g_args, &a)
}
("list", Some(_)) => command::bitmap_list(g_args),
_ => {
let msg = format!("Unknown sub command, use 'aloevera bitmap --help' for details");
return Err(ErrorKind::ArgumentError(msg).into());
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::fs::{self, File};
use std::io::{Read, Write};
use util::fat;

use proj::AloeVeraProject;
use proj::{AloeVeraProject, Binable};

use clap::ArgMatches;
// define what to do on argument error
Expand Down Expand Up @@ -127,7 +127,7 @@ pub fn load_project<'a>(project_file: Option<String>) -> Result<AloeVeraProject<
return Err(ErrorKind::ArgumentError("Missing project file name".to_string()).into())
}
};
let proj_json = read_file_string(&project_file)?;
let res = AloeVeraProject::new_from_json(&proj_json)?;
let encoded = read_file_bin(&project_file)?;
let res = *AloeVeraProject::from_bin(&encoded)?;
Ok(res)
}
5 changes: 2 additions & 3 deletions src/cmd/create/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::Error;
use proj::{AloeVeraProject, Jsonable};
use proj::{AloeVeraProject, Binable};
use util::fat;

/// Arguments for the initial create project command
Expand All @@ -30,8 +30,7 @@ pub fn create_project(args: &CreateProjectArgs) -> Result<(), Error> {
info!("Creating new project file at: {}", args.output_file);

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

Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use clap::ArgMatches;

use crate::cmd::common::{self, GlobalArgs};
use crate::cmd::{asm, bitmap, create, imageset, palette, sprite, tilemap};
use crate::cmd::{asm, bitmap, create, imageset, list, palette, sprite, tilemap};
use crate::{Error, ErrorKind};

fn parse_and_execute(g_args: &GlobalArgs, args: &ArgMatches) -> Result<(), Error> {
Expand All @@ -27,6 +27,7 @@ fn parse_and_execute(g_args: &GlobalArgs, args: &ArgMatches) -> Result<(), Error
("bitmap", Some(args)) => bitmap::parse::execute_bitmap_command(g_args, &args),
("imageset", Some(args)) => imageset::parse::execute_imageset_command(g_args, &args),
("tilemap", Some(args)) => tilemap::parse::execute_tilemap_command(g_args, &args),
("list", Some(args)) => list::parse::execute_list_command(g_args, &args),
_ => {
let msg = format!("Unknown command, use 'aloevera --help' for details");
return Err(ErrorKind::ArgumentError(msg).into());
Expand Down
30 changes: 2 additions & 28 deletions src/cmd/imageset/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::{Error, ErrorKind};
use proj::Jsonable;
use proj::Binable;

use crate::cmd::common::{self, GlobalArgs};
use vera::{VeraImageSet, VeraImageSetLoadConfig, VeraPixelDepth};
Expand All @@ -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(), &None)?;
crate::cmd::common::output_to_file(&project_file.unwrap(), &proj.to_bin()?, &None)?;
Ok(())
}

Expand Down Expand Up @@ -79,29 +79,3 @@ pub fn imageset_format(g_args: &GlobalArgs, args: &ImageSetFormatArgs) -> Result

Ok(())
}

/// Imageset list
pub fn imageset_list(g_args: &GlobalArgs) -> Result<(), Error> {
let proj = common::load_project(g_args.project_file.clone())?;
println!("Image sets:");
for (id, imageset) in proj.imagesets {
match imageset.depth {
None => println!(
" {}: {} {}x{} frames",
id,
imageset.frame_data.len(),
imageset.frame_width,
imageset.frame_height
),
pixel_depth => print!(
" {}: {} {}x{} frames depth {}",
id,
imageset.frame_data.len(),
imageset.frame_width,
imageset.frame_height,
pixel_depth.unwrap()
),
}
}
Ok(())
}
1 change: 0 additions & 1 deletion src/cmd/imageset/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub fn execute_imageset_command(g_args: &GlobalArgs, args: &ArgMatches) -> Resul
let a = arg_parse!(parse_imageset_format_args(g_args, args));
command::imageset_format(g_args, &a)
}
("list", Some(_)) => command::imageset_list(g_args),
_ => {
let msg = format!("Unknown sub command, use 'aloevera imageset --help' for details");
return Err(ErrorKind::ArgumentError(msg).into());
Expand Down
Loading

0 comments on commit 4481f91

Please sign in to comment.