Skip to content

Commit

Permalink
Wrap: Improve file performance lookup with pre-initialized Dictionary (
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtang95 authored Dec 3, 2022
1 parent 34c7395 commit 5789085
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 11 deletions.
5 changes: 5 additions & 0 deletions 7thWrapperLib/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ private IEnumerable<string> GetPathOverrideNamesInt(string path)
}
}
}

public IrosArc getArchive()
{
return _archive;
}
}

public abstract class ActiveWhen
Expand Down
19 changes: 10 additions & 9 deletions 7thWrapperLib/VFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,16 @@ public void Ping() {
}
}

public static OverrideFile MapFile(string file, RuntimeProfile profile) {
foreach (var item in profile.Mods) {
foreach(var entry in item.GetOverrides(file)) {
if (entry.CFolder == null || entry.CFolder.IsActive(file)) {
DebugLogger.WriteLine($"File {file} overridden by {entry.Archive}{entry.File}");
return entry;
}
}
}
public static OverrideFile MapFile(string file, Dictionary<string, List<OverrideFile>> mappedFiles) {
if (mappedFiles.TryGetValue(file.ToLower(), out List<OverrideFile> mappedList))
{
foreach (var overrideFile in mappedList)
{
if (overrideFile.CFolder == null || overrideFile.CFolder.IsActive(file))
return overrideFile;
}
}

return null;
}

Expand Down
93 changes: 91 additions & 2 deletions 7thWrapperLib/Wrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The original developer is Iros <irosff@outlook.com>
namespace _7thWrapperLib {
public static class Wrap {
private static Dictionary<IntPtr, VArchiveData> _varchives = new();
private static Dictionary<string, List<OverrideFile>> _mappedFiles = new();
private static RuntimeProfile _profile;
private static Process _process;

Expand Down Expand Up @@ -136,12 +137,100 @@ public static void Run(Process currentProcess, string profileFile = ".7thWrapper
}
}
}

foreach (var mod in _profile.Mods)
{
foreach (var cFolder in mod.Conditionals)
{
var archive = mod.getArchive();
if (archive == null)
AddFolderFilesToMappedFiles(Path.Combine(mod.BaseFolder, cFolder.Folder), cFolder);
else
AddIROFilesToMappedFiles(cFolder.Folder, cFolder, archive);
}

foreach (var folder in mod.ExtraFolders)
{
var archive = mod.getArchive();
if (archive == null)
AddFolderFilesToMappedFiles(Path.Combine(mod.BaseFolder, folder), null);
else
AddIROFilesToMappedFiles(folder, null, archive);
}

if (mod.Conditionals.Count + mod.ExtraFolders.Count == 0)
{
var archive = mod.getArchive();
if (archive == null)
AddFolderFilesToMappedFiles(mod.BaseFolder, null);
else
AddIROFilesToMappedFiles("", null, archive);
}
}
} catch (Exception e) {
DebugLogger.WriteLine(e.ToString());
return;
}
}

private static void AddIROFilesToMappedFiles(string folderPath, ConditionalFolder cFolder, IrosArc archive)
{
foreach (string filename in archive.AllFileNames())
{
if (filename.StartsWith(folderPath, StringComparison.InvariantCultureIgnoreCase))
{
int pathOffset = 1;
if (folderPath.Length == 0) // Fix the offset when folderPath is empty string (no need to skip "/")
pathOffset = 0;
string fileKey = filename.Substring(folderPath.Length + pathOffset).ToLower();
if (!_mappedFiles.ContainsKey(fileKey))
_mappedFiles.Add(fileKey, new List<OverrideFile>());

if (_mappedFiles.TryGetValue(fileKey, out List<OverrideFile> overrideFiles))
{
overrideFiles.Add(new OverrideFile()
{
File = filename,
CFolder = cFolder,
CName = fileKey,
Size = archive.GetFileSize(filename),
Archive = archive
});
}
}
}
}

private static void AddFolderFilesToMappedFiles(string folderPath, ConditionalFolder conditionalFolder)
{
try
{
DirectoryInfo di = new DirectoryInfo(folderPath);

foreach (FileInfo fi in di.GetFiles("*", SearchOption.AllDirectories))
{
string fileKey = fi.FullName.Substring(folderPath.Length + 1).ToLower();
if (!_mappedFiles.ContainsKey(fileKey))
_mappedFiles.Add(fileKey, new List<OverrideFile>());

if (_mappedFiles.TryGetValue(fileKey, out List<OverrideFile> overrideFiles))
{
overrideFiles.Add(new OverrideFile()
{
File = fi.FullName,
CFolder = conditionalFolder,
CName = fileKey
});
}
}
}
catch (DirectoryNotFoundException e)
{
DebugLogger.WriteLine(e.ToString());
}
}


// ------------------------------------------------------------------------------------------------------
public static int HCloseHandle(IntPtr hObject)
{
Expand Down Expand Up @@ -248,15 +337,15 @@ public static IntPtr HCreateFileW(
if (lpFileName.StartsWith(path, StringComparison.InvariantCultureIgnoreCase))
{
string match = lpFileName.Substring(path.Length);
OverrideFile mapped = LGPWrapper.MapFile(match, _profile);
OverrideFile mapped = LGPWrapper.MapFile(match, _mappedFiles);

//DebugLogger.WriteLine($"Attempting match '{match}' for {lpFileName}...");

if (mapped == null)
{
// Attempt a second round, this time relaxing the path match replacing only the game folder path.
match = lpFileName.Substring(_profile.FF7Path.Length + 1);
mapped = LGPWrapper.MapFile(match, _profile);
mapped = LGPWrapper.MapFile(match, _mappedFiles);

//DebugLogger.WriteLine($"Attempting match '{match}' for {lpFileName}...");
}
Expand Down

0 comments on commit 5789085

Please sign in to comment.