Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
originalfoo committed Mar 4, 2020
2 parents 1aaa605 + 155e6ef commit cf1bd92
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ namespace DuplicateAssemblyScanner.Util {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine.SceneManagement;
using static ColossalFramework.Plugins.PluginManager;

public class Assemblies {
Expand All @@ -17,17 +14,17 @@ public class Assemblies {
/// Scans app domain assemblies and compiles a dictionary keyed by assembly name;
/// each value is a list of assemblies matching the name.
/// </summary>
///
///
/// <param name="duplicatesFound">Will be <c>true</c> if duplicates found.</param>
/// <returns>Dictionary of assembly lists keyed by assembly name.</returns>
public static Dictionary<string, List<string>> Scan(out int problemsFound) {
public static Dictionary<string, List<string>> Scan(out bool duplicatesFound) {
Log.Info("Scanning app domain assemblies for duplicates...");
problemsFound = 0;

duplicatesFound = false;

// assembly name -> list of assemblies with that name
Dictionary<string, List<string>> results = new Dictionary<string, List<string>>();

bool detailedLogging = SceneManager.GetActiveScene().name != "Game";

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly asm in assemblies) {
Expand All @@ -41,11 +38,10 @@ public static Dictionary<string, List<string>> Scan(out int problemsFound) {
if (results.TryGetValue(name, out List<string> matches)) {

if (matches.Count == 1) {
++problemsFound;
if (detailedLogging) {
FindModsWithAssembly(name);
}
duplicatesFound = true;
LogModsContainingAssembly(name);
}

matches.Add(ver);

} else {
Expand All @@ -61,12 +57,17 @@ public static Dictionary<string, List<string>> Scan(out int problemsFound) {
}
}

Log.Info($"{problemsFound} problem(s) found.");
return results;
}

internal static void FindModsWithAssembly(string matchName) {
Log.Info($"# '{matchName}' assembly exists in mods:");
/// <summary>
/// Given an assembly name, this attempts to find which mods contain that assembly.
/// Results are listed in the log file.
/// </summary>
///
/// <param name="targetName">The `GetName().Name` of the assembly to scan for.</param>
internal static void LogModsContainingAssembly(string targetName) {
Log.Info($"# '{targetName}' assembly exists in mods:");

PluginManager manager = Singleton<PluginManager>.instance;

Expand All @@ -86,7 +87,7 @@ internal static void FindModsWithAssembly(string matchName) {
contents = mod.GetAssemblies();
foreach (Assembly asm in contents) {
name = asm.GetName().Name;
if (name == matchName) {
if (name == targetName) {
ver = asm.GetName().Version.ToString();
path = Path.GetFileName(mod.modPath);
name = GetNameOfMod(mod);
Expand All @@ -104,7 +105,7 @@ internal static string GetNameOfMod(PluginInfo mod) {
try {
return mod?.userModInstance != null ? ((IUserMod)mod.userModInstance).Name : string.Empty;
} catch {
return "Error getting mod name!";
return "** error **";
}
}
}
Expand Down
38 changes: 33 additions & 5 deletions DuplicateAssemblyScanner/DuplicateAssemblyScanner/Util/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@ namespace DuplicateAssemblyScanner.Util {
using ColossalFramework.UI;
using ICities;
using System.Collections.Generic;
using System.Reflection;

public class Settings {

/// <summary>
/// Cache of assembly scan results.
/// </summary>
internal static Dictionary<string, List<string>> cacheDictionary_;

/// <summary>
/// Cache of whether duplicates were found by the scan.
/// </summary>
internal static bool cacheDuplicates_;

/// <summary>
/// Get (cached) results of assembly scan.
/// </summary>
///
/// <param name="duplicatesFound">Will be <c>true</c> if duplicates found.</param>
/// <returns>Dictionary of assembly lists keyed by assembly name.</returns>
internal static Dictionary<string, List<string>> CacheScanResults(out bool duplicatesFound) {

if (cacheDictionary_ == null) {
cacheDictionary_ = Assemblies.Scan(out bool issues);
cacheDuplicates_ = issues;
}

duplicatesFound = cacheDuplicates_;
return cacheDictionary_;
}

/// <summary>
/// Generate the options screen listing all the duplicates (if found).
/// </summary>
Expand All @@ -14,11 +40,13 @@ public class Settings {
public static void CreateUI(UIHelperBase helper) {

// assembly name -> list of assemblies with that name
Dictionary<string, List<string>> results = Assemblies.Scan(out int problemsFound);
Dictionary<string, List<string>> results = CacheScanResults(out bool duplicatesFound);

helper.AddGroup($"{problemsFound} problems found.");
helper.AddGroup(duplicatesFound
? "Duplicate assemblies were detected."
: "No duplicates.");

if (problemsFound > 0) {
if (duplicatesFound) {

UIHelperBase group;
UICheckBox checkbox;
Expand All @@ -35,8 +63,8 @@ public static void CreateUI(UIHelperBase helper) {
checkbox = (UICheckBox)group.AddCheckbox(ver, true, NoOp);
checkbox.isEnabled = false;
}

}

}
}
}
Expand Down

0 comments on commit cf1bd92

Please sign in to comment.