Skip to content

JSON file processing (MC assets) #175

@toqueteos

Description

@toqueteos

There's three big blobs (one on block_state.rs, two on model.rs) which could be a lot simpler if we used the auto-derive capabilities of json::decode.

Example:

extern crate "rustc-serialize" as rustc_serialize;

use std::collections::BTreeMap;
use std::io::File;
use std::io::fs::walk_dir;

use rustc_serialize::Decodable;
use rustc_serialize::json::{self, Json};

#[derive(RustcDecodable, PartialEq, Show)]
struct FaceInfo {
    cullface: Option<String>,
    rotation: Option<i64>,
    texture: String,
    tintindex: Option<i64>,
    uv: Option<Vec<f64>>,
}

#[derive(RustcDecodable, PartialEq, Show)]
struct Faces {
    down: Option<FaceInfo>,
    east: Option<FaceInfo>,
    north: Option<FaceInfo>,
    south: Option<FaceInfo>,
    up: Option<FaceInfo>,
    west: Option<FaceInfo>,
}

#[derive(RustcDecodable, PartialEq, Show)]
struct Rotation {
    angle: f64,
    axis: String,
    origin: Vec<f64>,
    rescale: Option<bool>,
}

#[derive(RustcDecodable, PartialEq, Show)]
struct Element {
    from: Vec<f64>,
    to: Vec<f64>,
    rotation: Option<Rotation>,
    shade: Option<bool>,
    faces: Faces,
}

#[derive(RustcDecodable, PartialEq, Show)]
struct BlockModel {
    parent : Option<String>,
    ambientocclusion: Option<bool>,
    textures: Option<BTreeMap<String, String>>,
    elements: Option<Vec<Element>>,
}

fn main() {
    let path = Path::new("src/bin/block");
    let mut n = 0;

    for p in walk_dir(&path).unwrap() {
        let mut file = File::open(&p).unwrap();
        let body = Json::from_reader(&mut file).unwrap();

        let mut dec = json::Decoder::new(body);
        let bm: BlockModel = Decodable::decode(&mut dec).unwrap();

        // Use bm here

        n += 1;
    }

    println!("Read {} files.", n);
}

This thing, seems huge but it's mostly struct definitions, with optional fields mostly. It reads all files inside assets/minecraft/models/block (assuming they are inside src/bin/block and counts them.

Of course it lacks the real logic so it's not usable with the graphics code but still it's way easier to understand. Here is the real thing.

Let me know what you think.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions