Skip to content

Commit b96d07f

Browse files
committed
fix crash if a mod's entry DLL is missing or invalid
1 parent fc1b1ce commit b96d07f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

docs/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* For players:
66
* Updated for Stardew Valley 1.6.14.
77
* Updated mod compatibility list.
8+
* Fixed crash if a mod has a missing or invalid DLL.
89

910
## 4.1.6
1011
Released 07 November 2024 for Stardew Valley 1.6.10 or later.

src/SMAPI/Framework/SCore.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,11 +1929,8 @@ private bool TryLoadMod(IModMetadata mod, IModMetadata[] mods, AssemblyLoader as
19291929
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}, ID: {manifest?.UniqueID}) [content pack]...");
19301930
else if (manifest?.EntryDll != null)
19311931
{
1932-
FileVersionInfo version = FileVersionInfo.GetVersionInfo(assemblyFile!.FullName);
1933-
string versionStr = version.FilePrivatePart == 0
1934-
? $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}"
1935-
: $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}.{version.FilePrivatePart}";
1936-
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}{Path.DirectorySeparatorChar}{manifest.EntryDll}, ID: {manifest.UniqueID}, assembly version: {versionStr})..."); // don't use Path.Combine here, since EntryDLL might not be valid
1932+
this.TryGetAssemblyVersion(assemblyFile?.FullName, out string? assemblyVersion);
1933+
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}{Path.DirectorySeparatorChar}{manifest.EntryDll}, ID: {manifest.UniqueID}, assembly version: {assemblyVersion ?? "<unknown>"})..."); // don't use Path.Combine here, since EntryDLL might not be valid
19371934
}
19381935
else
19391936
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}, ID: {manifest?.UniqueID ?? "<unknown>"})...");
@@ -2088,6 +2085,34 @@ IContentPack[] GetContentPacks()
20882085
}
20892086
}
20902087

2088+
/// <summary>Get the display version for an assembly file, if it's valid.</summary>
2089+
/// <param name="filePath">The absolute path to the assembly file.</param>
2090+
/// <param name="versionString">The extracted display version, if valid.</param>
2091+
/// <returns>Returns whether the assembly version was successfully extracted.</returns>
2092+
private bool TryGetAssemblyVersion(string? filePath, [NotNullWhen(true)] out string? versionString)
2093+
{
2094+
if (filePath is null || !File.Exists(filePath))
2095+
{
2096+
versionString = null;
2097+
return false;
2098+
}
2099+
2100+
try
2101+
{
2102+
FileVersionInfo version = FileVersionInfo.GetVersionInfo(filePath);
2103+
versionString = version.FilePrivatePart == 0
2104+
? $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}"
2105+
: $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}.{version.FilePrivatePart}";
2106+
return true;
2107+
}
2108+
catch (Exception ex)
2109+
{
2110+
this.Monitor.Log($"Error extracting assembly version from '{filePath}': {ex.GetLogSummary()}");
2111+
versionString = null;
2112+
return false;
2113+
}
2114+
}
2115+
20912116
/// <summary>Create a fake content pack instance for a parent mod.</summary>
20922117
/// <param name="packDirPath">The absolute path to the fake content pack's directory.</param>
20932118
/// <param name="packManifest">The fake content pack's manifest.</param>

0 commit comments

Comments
 (0)