Skip to content

Commit

Permalink
Handle empty/no-pk4 TDM fm dirs
Browse files Browse the repository at this point in the history
On a clean install of TDM 2.11, there's an empty "saintlucia" dir (the real one is "stlucia")
  • Loading branch information
FenPhoenix committed Oct 27, 2023
1 parent a75b47a commit 7bc7c66
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
10 changes: 6 additions & 4 deletions AngelLoader/Common/Native/FastIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ internal static List<string> GetDirsTopOnly(
string searchPattern,
bool ignoreReparsePoints = false,
bool pathIsKnownValid = false,
bool returnFullPaths = true)
bool returnFullPaths = true,
int preallocate = 16)
{
var ret = new List<string>(16);
var ret = new List<string>(preallocate);
GetFilesTopOnlyInternal(
path,
searchPattern,
Expand All @@ -58,9 +59,10 @@ internal static List<string> GetFilesTopOnly(
string path,
string searchPattern,
bool pathIsKnownValid = false,
bool returnFullPaths = true)
bool returnFullPaths = true,
int preallocate = 16)
{
var ret = new List<string>(16);
var ret = new List<string>(preallocate);
GetFilesTopOnlyInternal(
path,
searchPattern,
Expand Down
31 changes: 27 additions & 4 deletions AngelLoader/Common/Utility/MiscUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
Expand Down Expand Up @@ -278,17 +279,39 @@ internal static bool BackupPathInvalid(string backupPath)
}

// @TDM: See if we want to move this to a TDM utils class or whatever we end up with
internal static bool IsValidTdmFM(string fileOrDirName)
internal static bool IsValidTdmFM(string fmsPath, string fileOrDirName)
{
/*
The Dark Mod Wiki also mentions _i18n.pk4 files, but the game (as of v2.11 anyway) doesn't seem to do
anything with these, not putting them automatically into the matching game dir or anything. So it could
be these are deprecated file names, but regardless, it looks like we can ignore them.
*/
// @TDM_CASE
return !fileOrDirName.EqualsI(Paths.TDMMissionShots) &&
!fileOrDirName.EndsWithI("_l10n") &&
!fileOrDirName.EndsWithI("_l10n.pk4");
if (fileOrDirName.EqualsI(Paths.TDMMissionShots) ||
fileOrDirName.EndsWithI("_l10n") ||
fileOrDirName.EndsWithI("_l10n.pk4"))
{
return false;
}

try
{
string fullDir = Path.Combine(fmsPath, fileOrDirName);
if (Directory.Exists(fullDir))
{
List<string> pk4Files = FastIO.GetFilesTopOnly(fullDir, "*.pk4", preallocate: 1);
if (pk4Files.Count == 0)
{
return false;
}
}
}
catch
{
return false;
}

return true;
}

#if DateAccTest
Expand Down
12 changes: 6 additions & 6 deletions AngelLoader/FindFMs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ it would change/get screwed up.
fmDataIniListTDM_Dict[fm.TDMInstalledDir] = fm;
}

string instPath = Config.GetFMInstallPath(GameIndex.TDM);
if (Directory.Exists(instPath))
string fmsPath = Config.GetFMInstallPath(GameIndex.TDM);
if (Directory.Exists(fmsPath))
{
try
{
Expand All @@ -482,8 +482,8 @@ it would change/get screwed up.
var filesPK4 = new List<string>();
var dateTimesPK4 = new List<ExpandableDate_FromTicks>();

FastIO.GetDirsTopOnly_FMs(instPath, "*", dirs, dateTimes);
FastIO.GetFilesTopOnly_FMs(instPath, "*.pk4", filesPK4, dateTimesPK4);
FastIO.GetDirsTopOnly_FMs(fmsPath, "*", dirs, dateTimes);
FastIO.GetFilesTopOnly_FMs(fmsPath, "*.pk4", filesPK4, dateTimesPK4);

HashSetI dirsHash = dirs.ToHashSetI();

Expand All @@ -507,7 +507,7 @@ it would change/get screwed up.
for (int fileIndex = 0; fileIndex < finalFilesList.Count; fileIndex++)
{
string fmDir = finalFilesList[fileIndex];
if (IsValidTdmFM(fmDir))
if (IsValidTdmFM(fmsPath, fmDir))
{
if (fmDataIniListTDM_Dict.TryGetValue(fmDir, out FanMission dictFM))
{
Expand All @@ -530,7 +530,7 @@ it would change/get screwed up.
}
catch (Exception ex)
{
Log(ErrorText.ExGet + "directories in " + instPath, ex);
Log(ErrorText.ExGet + "directories in " + fmsPath, ex);
}
}

Expand Down
2 changes: 1 addition & 1 deletion AngelLoader/TDM/TDMWatchers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private static bool TdmFMSetChanged()

for (int i = 0; i < finalFilesList.Count; i++)
{
if (!IsValidTdmFM(finalFilesList[i]))
if (!IsValidTdmFM(fmsPath, finalFilesList[i]))
{
finalFilesList.RemoveAt(i);
break;
Expand Down
34 changes: 31 additions & 3 deletions FMScanner/Core/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,24 @@ List<ZipArchiveFastEntry> GetZipBaseDirEntries()

string fmAsPK4File = fm.Path.TrimEnd(CA_BS_FS) + ".pk4";

zipPath = !Directory.Exists(_fmWorkingPath) && File.Exists(fmAsPK4File)
? fmAsPK4File
: Path.Combine(fm.Path, fmData.ArchiveName + ".pk4");
if (!Directory.Exists(_fmWorkingPath) && File.Exists(fmAsPK4File))
{
zipPath = fmAsPK4File;
}
else
{
zipPath = Path.Combine(fm.Path, fmData.ArchiveName + ".pk4");
if (!File.Exists(zipPath))
{
return UnsupportedTDM(
archivePath: fm.Path,
fen7zResult: null,
ex: null,
errorInfo: "Found a TDM FM directory with no pk4 in it. Invalid FM or empty FM directory. Returning 'Unsupported'."
);
}
}


bool scanTitleForAuthorPurposesOnly = SetupAuthorRequiredTitleScan();

Expand Down Expand Up @@ -1783,6 +1798,19 @@ Game game

#region Fail return functions

private static ScannedFMDataAndError UnsupportedTDM(string archivePath, Fen7z.Result? fen7zResult, Exception? ex, string errorInfo) => new()
{
ScannedFMData = new ScannedFMData
{
ArchiveName = Path.GetFileName(archivePath),
Game = Game.Unsupported,
MissionCount = 0
},
Fen7zResult = fen7zResult,
Exception = ex,
ErrorInfo = errorInfo
};

private static ScannedFMDataAndError UnsupportedZip(string archivePath, Fen7z.Result? fen7zResult, Exception? ex, string errorInfo) => new()
{
ScannedFMData = new ScannedFMData
Expand Down

0 comments on commit 7bc7c66

Please sign in to comment.