extern crate sdl2;
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::{Path};
pub fn parse_assets(filename : &str) -> Result<HashMap<String, Vec<String>>, std::io::Error> {
let mut intents : HashMap<String, Vec<String>> = HashMap::new();
let mut files : Vec<String> = Vec::new();
let file = File::open(filename)?;
let reader = BufReader::new(file);
let mut entry = String::new();
for line in reader.lines() {
let line = line.unwrap();
if ! line.is_empty() { if line.starts_with("[") { if ! entry.is_empty() { intents.insert(entry, files.clone()); } files.clear();
entry = line.replace("[", "").replace("]", "").trim().to_owned();
} else { assert_ne!(entry, "", "Tried to parse a file entry without an associated intent ([intent_name]...) above.");
files.push(line);
}
}
}
intents.insert(entry.clone(), files);
Ok(intents)
}
pub fn parse_timings(filename : &str) -> Result<HashMap<String, u64>, std::io::Error> {
let mut intents : HashMap<String, u64> = HashMap::new();
let file = File::open(filename)?;
let reader = BufReader::new(file);
let mut entry = String::new();
let mut val = 0;
for line in reader.lines() {
let line = line.unwrap();
if ! line.is_empty() { if line.starts_with("[") { entry = line.replace("[", "").replace("]", "").trim().to_owned();
} else { assert_ne!(entry, "", "Tried to parse a file entry without an associated intent ([intent_name]...) above.");
val = line.parse::<u64>().expect("Couldn't parse one of the timings, please ensure that it's a valid number (200, 3400...)");
intents.insert(entry.clone(), val);
}
}
}
intents.insert(entry.clone(), val);
Ok(intents)
}
pub fn load_assets(assets : &HashMap<String, Vec<String>>) -> Result<HashMap<String, Vec<Vec<u8>>>, std::io::Error> {
let mut result : HashMap<String, Vec<Vec<u8>>> = HashMap::new();
for tuple in assets.iter() {
let intent = tuple.0;
let mut files : Vec<Vec<u8>> = Vec::new();
for path in tuple.1.iter() {
let path = Path::new(path);
let file = fs::read(path)?;
files.push(file);
}
result.insert(intent.clone(), files);
}
Ok(result)
}