Skip to content

Commit

Permalink
fix crash if a mod's entry DLL is missing or invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Nov 12, 2024
1 parent fc1b1ce commit b96d07f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* For players:
* Updated for Stardew Valley 1.6.14.
* Updated mod compatibility list.
* Fixed crash if a mod has a missing or invalid DLL.

## 4.1.6
Released 07 November 2024 for Stardew Valley 1.6.10 or later.
Expand Down
35 changes: 30 additions & 5 deletions src/SMAPI/Framework/SCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1929,11 +1929,8 @@ private bool TryLoadMod(IModMetadata mod, IModMetadata[] mods, AssemblyLoader as
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}, ID: {manifest?.UniqueID}) [content pack]...");
else if (manifest?.EntryDll != null)
{
FileVersionInfo version = FileVersionInfo.GetVersionInfo(assemblyFile!.FullName);
string versionStr = version.FilePrivatePart == 0
? $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}"
: $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}.{version.FilePrivatePart}";
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
this.TryGetAssemblyVersion(assemblyFile?.FullName, out string? assemblyVersion);
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
}
else
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}, ID: {manifest?.UniqueID ?? "<unknown>"})...");
Expand Down Expand Up @@ -2088,6 +2085,34 @@ IContentPack[] GetContentPacks()
}
}

/// <summary>Get the display version for an assembly file, if it's valid.</summary>
/// <param name="filePath">The absolute path to the assembly file.</param>
/// <param name="versionString">The extracted display version, if valid.</param>
/// <returns>Returns whether the assembly version was successfully extracted.</returns>
private bool TryGetAssemblyVersion(string? filePath, [NotNullWhen(true)] out string? versionString)
{
if (filePath is null || !File.Exists(filePath))
{
versionString = null;
return false;
}

try
{
FileVersionInfo version = FileVersionInfo.GetVersionInfo(filePath);
versionString = version.FilePrivatePart == 0
? $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}"
: $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}.{version.FilePrivatePart}";
return true;
}
catch (Exception ex)
{
this.Monitor.Log($"Error extracting assembly version from '{filePath}': {ex.GetLogSummary()}");
versionString = null;
return false;
}
}

/// <summary>Create a fake content pack instance for a parent mod.</summary>
/// <param name="packDirPath">The absolute path to the fake content pack's directory.</param>
/// <param name="packManifest">The fake content pack's manifest.</param>
Expand Down

0 comments on commit b96d07f

Please sign in to comment.