Finds a similar-ish wad txt file

This commit is contained in:
2023-07-24 23:20:54 +01:00
parent d69aa1fef3
commit 07f7b67119
2 changed files with 35 additions and 10 deletions

View File

@@ -13,4 +13,5 @@ native-dialog = "0.6.3"
rfd = "0.11.4"
serde = "1.0.163"
serde_derive = "1.0.163"
strsim = "0.10.0"
toml = "0.7.4"

View File

@@ -2,8 +2,10 @@ use bincode;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::fs;
use std::io::{BufReader, Seek, SeekFrom};
use std::path::PathBuf;
use strsim;
/*
A great document I've used as a reference is The Unofficial Doom Specs v1.666
@@ -57,11 +59,26 @@ pub struct OpenWad {
}
pub fn find_txt_file(path: &PathBuf) -> Option<PathBuf> {
// get folder WAD is in
// get name of WAD
// iterate through all txt/TXT files
// if name is like 50% the same/similar, return match
// return if found anything, otherwise None
if !path.exists() || !path.is_file() {
return None
}
if let (Some(f), Some(p)) = (path.file_name(), path.parent()) {
let files_in_folder = fs::read_dir(&p).unwrap();
let mut txt_file = PathBuf::from(f);
txt_file.set_extension(".txt");
let txt_file = txt_file.into_os_string().into_string().unwrap().to_lowercase();
for f in files_in_folder {
// Jesus this can't be the right way to do things
let f1 = f.unwrap().path().file_name().unwrap().to_os_string().into_string().unwrap();
let f2 = f1.clone().to_lowercase();
let result = strsim::normalized_levenshtein(&txt_file, &f2);
if result > 0.5 {
let mut f3 = PathBuf::from(p);
f3.push(f1);
return Some(f3);
}
}
}
None
}
@@ -276,7 +293,7 @@ mod tests {
#[test]
fn test_open_wad() {
let freedoom_iwad = PathBuf::from("freedoom1.wad");
let freedoom_iwad = PathBuf::from("test_files/freedoom1.wad");
assert!(
freedoom_iwad.exists(),
"WAD test need freedoom1.wad - get it from here https://freedoom.github.io"
@@ -288,7 +305,7 @@ mod tests {
#[test]
fn test_num_levels_correct() {
let freedoom_iwad = PathBuf::from("freedoom1.wad");
let freedoom_iwad = PathBuf::from("test_files/freedoom1.wad");
let ow = open_wad(&freedoom_iwad);
let summary = get_enemies_and_health_per_level(ow);
assert_eq!(summary.len(), 9 * 4);
@@ -296,7 +313,7 @@ mod tests {
#[test]
fn test_enemies_correct() {
let freedoom_iwad = PathBuf::from("freedoom1.wad");
let freedoom_iwad = PathBuf::from("test_files/freedoom1.wad");
let ow = open_wad(&freedoom_iwad);
let summary = get_enemies_and_health_per_level(ow);
let (c1m1e, _) = summary.get("E1M1\0\0\0\0").unwrap();
@@ -305,7 +322,7 @@ mod tests {
#[test]
fn test_health_correct() {
let freedoom_iwad = PathBuf::from("freedoom1.wad");
let freedoom_iwad = PathBuf::from("test_files/freedoom1.wad");
let ow = open_wad(&freedoom_iwad);
let summary = get_enemies_and_health_per_level(ow);
let (_, c1m1h) = summary.get("E1M1\0\0\0\0").unwrap();
@@ -318,7 +335,7 @@ mod tests {
#[test]
#[ignore]
fn test_guess_at_difficulty() {
let freedoom_iwad = PathBuf::from("freedoom1.wad");
let freedoom_iwad = PathBuf::from("test_files/freedoom1.wad");
let ow = open_wad(&freedoom_iwad);
let summary = get_enemies_and_health_per_level(ow);
let mut levels = Vec::new();
@@ -338,6 +355,13 @@ mod tests {
panic!();
}
#[test]
fn test_txt_file_finding() {
let wad_path = PathBuf::from("test_files/10amBreakM.wad");
let txt_file = find_txt_file(&wad_path);
assert_eq!(txt_file, Some(PathBuf::from("test_files/10amBreakM.txt")));
}
#[test]
fn test_struct_size() {
// This probably isn't really neccesary... but it might catch a mistake, maybe?