use std::path::Path;
use walkdir::WalkDir;
use crate::parser::{
raws::{
creature::DFCreature, graphics::DFGraphic, info_txt::DFInfoFile, inorganic::DFInorganic,
plant::DFPlant, RawObjectKind,
},
TypedJsonSerializable,
};
use super::{raws::tile_page::DFTilePage, DFParser};
impl DFParser {
pub fn parse_raw_module_into_serializable<P: AsRef<Path>>(
raw_module_directory: &P,
) -> Vec<Box<dyn TypedJsonSerializable>> {
if !raw_module_directory.as_ref().exists() {
log::error!(
"Provided directory to parse raws does not exist: {:?}",
raw_module_directory.as_ref().to_string_lossy()
);
return Vec::new();
}
if !raw_module_directory.as_ref().is_dir() {
log::error!(
"Provided 'directory' to parse is not actually a directory! {:?}",
raw_module_directory.as_ref().to_string_lossy()
);
return Vec::new();
}
let info_txt_path = raw_module_directory.as_ref().join("info.txt");
if !info_txt_path.exists() {
let dir_name = raw_module_directory
.as_ref()
.file_name()
.unwrap_or_default();
let dir_name_str = dir_name.to_str().unwrap_or("");
if !(dir_name_str.eq("mod_upload")
|| dir_name_str.eq("examples and notes")
|| dir_name_str.eq("interaction examples"))
{
log::error!(
"No info.txt as expected in {:?}. Is this DF 50.xx?",
raw_module_directory
.as_ref()
.file_name()
.unwrap_or_default()
);
}
return Vec::new();
}
let dfraw_module_info = DFParser::parse_info_file(&info_txt_path);
log::info!(
"Parsing raws for {} v{}",
dfraw_module_info.get_identifier(),
dfraw_module_info.displayed_version
);
DFParser::parse_raws_into_serializable(raw_module_directory, &dfraw_module_info)
}
pub fn parse_raws_into_serializable<P: AsRef<Path>>(
raw_module_directory: &P,
info_text_file: &DFInfoFile,
) -> Vec<Box<dyn TypedJsonSerializable>> {
let objects_path = raw_module_directory.as_ref().join("objects");
if !objects_path.exists() {
log::debug!("No objects subdirectory, no raws to parse.");
}
if !objects_path.is_dir() {
log::error!("Objects subdirectory is not valid subdirectory! Unable to parse raws.");
}
let graphics_path = raw_module_directory.as_ref().join("graphics");
if !graphics_path.exists() {
log::debug!("No graphics subdirectory, no raws to parse.");
}
if !graphics_path.is_dir() {
log::error!("Graphics subdirectory is not valid subdirectory! Unable to parse raws.");
}
if !objects_path.exists() && !graphics_path.exists() {
return Vec::new();
}
let mut serializable_raws: Vec<Box<dyn TypedJsonSerializable>> = Vec::new();
for entry in WalkDir::new(objects_path)
.into_iter()
.filter_map(std::result::Result::ok)
{
let f_name = entry.file_name().to_string_lossy();
if f_name.ends_with(".txt") {
let entry_path = entry.path();
serializable_raws.extend(DFParser::parse_raws_from_single_file_into_serializable(
&entry_path,
info_text_file,
));
}
}
for entry in WalkDir::new(graphics_path)
.into_iter()
.filter_map(std::result::Result::ok)
{
let f_name = entry.file_name().to_string_lossy();
if f_name.ends_with(".txt") {
let entry_path = entry.path();
serializable_raws.extend(DFParser::parse_raws_from_single_file_into_serializable(
&entry_path,
info_text_file,
));
}
}
serializable_raws
}
pub fn parse_raws_from_single_file_into_serializable<P: AsRef<Path>>(
entry_path: &P,
info_text_file: &DFInfoFile,
) -> Vec<Box<dyn TypedJsonSerializable>> {
let mut serializable_raws: Vec<Box<dyn TypedJsonSerializable>> = Vec::new();
let caller = "parse_raws_into_serializable";
match DFParser::read_raw_file_type(entry_path) {
RawObjectKind::Creature => {
log::debug!("parsing {}", entry_path.as_ref().display());
let creature_raw_vec = DFCreature::parse_raw_file(entry_path, info_text_file);
creature_raw_vec
.into_iter()
.for_each(|c| serializable_raws.push(Box::new(c)));
}
RawObjectKind::Plant => {
log::debug!("parsing {}", entry_path.as_ref().display());
let plant_raw_vec = DFPlant::parse_raw_file(entry_path, info_text_file);
plant_raw_vec
.into_iter()
.for_each(|p| serializable_raws.push(Box::new(p)));
}
RawObjectKind::Inorganic => {
log::debug!("parsing {}", entry_path.as_ref().display());
let inorganic_raw_vec = DFInorganic::parse(entry_path, info_text_file);
inorganic_raw_vec
.into_iter()
.for_each(|i| serializable_raws.push(Box::new(i)));
}
RawObjectKind::Graphics => {
let sprite_vec = DFGraphic::parse(entry_path, info_text_file);
sprite_vec
.into_iter()
.for_each(|g| serializable_raws.push(Box::new(g)));
}
RawObjectKind::GraphicsTilePage => {
let tile_page_vec = DFTilePage::parse(entry_path, info_text_file);
tile_page_vec
.into_iter()
.for_each(|g| serializable_raws.push(Box::new(g)));
}
_ => log::trace!("{} - skipping {}", caller, entry_path.as_ref().display()),
}
serializable_raws
}
}