Cleanup CueFileResolver

also closes #3532
This commit is contained in:
Morilli 2024-08-18 02:38:23 +02:00
parent 7c89bda312
commit e893e3a1ff
2 changed files with 16 additions and 65 deletions

View File

@ -10,56 +10,18 @@ namespace BizHawk.Emulation.DiscSystem.CUE
public class CueFileResolver
{
public bool caseSensitive = false;
public bool IsHardcodedResolve { get; private set; }
private string baseDir;
/// <summary>
/// Retrieving the FullName from a FileInfo can be slow (and probably other operations), so this will cache all the needed values
/// TODO - could we treat it like an actual cache and only fill the FullName if it's null?
/// </summary>
private struct MyFileInfo
{
public string FullName;
public FileInfo FileInfo;
}
private DirectoryInfo diBasedir;
private MyFileInfo[] fisBaseDir;
private string _baseDir;
private string[] _baseDirPaths;
/// <summary>
/// sets the base directory and caches the list of files in the directory
/// </summary>
public void SetBaseDirectory(string baseDir)
{
this.baseDir = baseDir;
diBasedir = new DirectoryInfo(baseDir);
this._baseDir = baseDir;
//list all files, so we don't scan repeatedly.
fisBaseDir = MyFileInfosFromFileInfos(diBasedir.GetFiles());
}
/// <summary>
/// TODO - doesnt seem like we're using this...
/// </summary>
public void SetHardcodeResolve(IDictionary<string, string> hardcodes)
{
IsHardcodedResolve = true;
fisBaseDir = new MyFileInfo[hardcodes.Count];
var i = 0;
foreach (var kvp in hardcodes)
{
fisBaseDir[i++] = new() { FullName = kvp.Key, FileInfo = new(kvp.Value) };
}
}
private MyFileInfo[] MyFileInfosFromFileInfos(FileInfo[] fis)
{
var myfis = new MyFileInfo[fis.Length];
for (var i = 0; i < fis.Length; i++)
{
myfis[i].FileInfo = fis[i];
myfis[i].FullName = fis[i].FullName;
}
return myfis;
_baseDirPaths = Directory.GetFiles(baseDir).Select(Path.GetFullPath).ToArray();
}
/// <summary>
@ -74,25 +36,14 @@ namespace BizHawk.Emulation.DiscSystem.CUE
string targetFile = Path.GetFileName(path);
string targetFragment = Path.GetFileNameWithoutExtension(path);
DirectoryInfo di = null;
MyFileInfo[] fileInfos;
if (!string.IsNullOrEmpty(Path.GetDirectoryName(path)))
{
di = new FileInfo(path).Directory;
//fileInfos = di.GetFiles(Path.GetFileNameWithoutExtension(path)); //does this work?
fileInfos = MyFileInfosFromFileInfos(di.GetFiles()); //we (probably) have to enumerate all the files to do a search anyway, so might as well do this
//TODO - don't do the search until a resolve fails
}
else
{
di = diBasedir;
fileInfos = fisBaseDir;
}
var directory = Path.GetDirectoryName(path);
var filePaths = Directory.Exists(directory) ? Directory.GetFiles(directory).Select(Path.GetFullPath) : _baseDirPaths;
//TODO - don't do the search until a resolve fails // leftover comment from 3c26d48a59f64a7a94bda57fbcfd13eca49d8b9d, is this still relevant?
var results = new List<FileInfo>();
foreach (var fi in fileInfos)
var results = new List<string>();
foreach (var filePath in filePaths)
{
var ext = Path.GetExtension(fi.FullName).ToLowerInvariant();
var ext = Path.GetExtension(path).ToLowerInvariant();
//some choices are always bad: (we're looking for things like .bin and .wav)
//it's a little unclear whether we should go for a whitelist or a blacklist here.
@ -106,7 +57,7 @@ namespace BizHawk.Emulation.DiscSystem.CUE
if (ext is ".7z" or ".rar" or ".zip" or ".bz2" or ".gz")
continue;
var fragment = Path.GetFileNameWithoutExtension(fi.FullName);
var fragment = Path.GetFileNameWithoutExtension(filePath);
//match files with differing extensions
var cmp = string.Compare(fragment, targetFragment, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
if (cmp != 0)
@ -115,14 +66,14 @@ namespace BizHawk.Emulation.DiscSystem.CUE
if (cmp == 0)
{
//take care to add an exact match at the beginning
if (string.Equals(fi.FullName, Path.Combine(baseDir, path), StringComparison.OrdinalIgnoreCase))
results.Insert(0, fi.FileInfo);
if (string.Equals(filePath, Path.Combine(_baseDir, path), StringComparison.OrdinalIgnoreCase))
results.Insert(0, filePath);
else
results.Add(fi.FileInfo);
results.Add(filePath);
}
}
return results.Select(fi => fi.FullName).ToList();
return results;
}
}
}

View File

@ -131,7 +131,7 @@ namespace BizHawk.Emulation.DiscSystem
var cfr = new CueFileResolver();
var cueContext = new CUE_Context { DiscMountPolicy = IN_DiscMountPolicy, Resolver = cfr };
if (!cfr.IsHardcodedResolve) cfr.SetBaseDirectory(cueDirPath);
cfr.SetBaseDirectory(cueDirPath);
// parse the cue file
var parseJob = new ParseCueJob(cueContent);