From e893e3a1ff51ebeae407ff13c3fa0ac7f2882506 Mon Sep 17 00:00:00 2001
From: Morilli <35152647+Morilli@users.noreply.github.com>
Date: Sun, 18 Aug 2024 02:38:23 +0200
Subject: [PATCH] Cleanup CueFileResolver
also closes #3532
---
.../DiscFormats/CUE/CueFileResolver.cs | 79 ++++---------------
.../DiscMountJob.cs | 2 +-
2 files changed, 16 insertions(+), 65 deletions(-)
diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CueFileResolver.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CueFileResolver.cs
index 2f38222355..4a885b6a8c 100644
--- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CueFileResolver.cs
+++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CueFileResolver.cs
@@ -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;
- ///
- /// 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?
- ///
- private struct MyFileInfo
- {
- public string FullName;
- public FileInfo FileInfo;
- }
-
- private DirectoryInfo diBasedir;
- private MyFileInfo[] fisBaseDir;
+ private string _baseDir;
+ private string[] _baseDirPaths;
///
/// sets the base directory and caches the list of files in the directory
///
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());
- }
-
- ///
- /// TODO - doesnt seem like we're using this...
- ///
- public void SetHardcodeResolve(IDictionary 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();
}
///
@@ -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();
- foreach (var fi in fileInfos)
+ var results = new List();
+ 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;
}
}
}
diff --git a/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs b/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs
index 42558deda3..fcf38d2d24 100644
--- a/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs
+++ b/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs
@@ -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);