Skip to content

Commit

Permalink
Reworked Melon Folder Scanning to be manifest json aware
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Nov 4, 2024
1 parent 9f135c1 commit bbce3a6
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions MelonLoader/Melons/MelonFolderHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ internal class MelonFolderHandler
{
private static bool firstSpacer = false;

internal enum eScanType
{
UserLibs,
Plugins,
Mods,
}

internal static void ScanUserLibs(string path)
{
// Get Full Directory Path
Expand All @@ -22,7 +29,7 @@ internal static void ScanUserLibs(string path)
// Parse Folders
bool hasWroteLine = false;
List<MelonAssembly> melonAssemblies = new();
ProcessFolder(false, path, true, ref hasWroteLine, ref melonAssemblies);
ProcessFolder(eScanType.UserLibs, path, ref hasWroteLine, ref melonAssemblies);
}

internal static void ScanMelons<T>(string path) where T : MelonTypeBase<T>
Expand All @@ -40,7 +47,7 @@ internal static void ScanMelons<T>(string path) where T : MelonTypeBase<T>
bool isMod = melonType == typeof(MelonMod);
bool hasWroteLine = false;
List<MelonAssembly> melonAssemblies = new();
ProcessFolder(isMod, path, false, ref hasWroteLine, ref melonAssemblies);
ProcessFolder(isMod ? eScanType.Mods : eScanType.Plugins, path, ref hasWroteLine, ref melonAssemblies);

// Parse Queue
var melons = new List<T>();
Expand Down Expand Up @@ -118,9 +125,8 @@ private static void LoadFolder(string path,
}
}

private static void ProcessFolder(bool isMod,
private static void ProcessFolder(eScanType scanType,
string path,
bool userLibsOnly,
ref bool hasWroteLine,
ref List<MelonAssembly> melonAssemblies)
{
Expand All @@ -131,10 +137,10 @@ private static void ProcessFolder(bool isMod,
// Scan Directories
List<string> melonDirectories = new();
List<string> userLibDirectories = new();
ScanFolder(isMod, path, userLibsOnly, ref melonDirectories, ref userLibDirectories);
ScanFolder(scanType, path, ref melonDirectories, ref userLibDirectories);

// Add Base Path to End of Directories List
if (userLibsOnly)
if (scanType == eScanType.UserLibs)
userLibDirectories.Add(path);
else
melonDirectories.Add(path);
Expand All @@ -145,7 +151,7 @@ private static void ProcessFolder(bool isMod,
MelonUtils.AddNativeDLLDirectory(directory);
Resolver.MelonAssemblyResolver.AddSearchDirectory(directory);
}
if (!userLibsOnly)
if (scanType != eScanType.UserLibs)
foreach (string directory in melonDirectories)
Resolver.MelonAssemblyResolver.AddSearchDirectory(directory);

Expand All @@ -154,15 +160,14 @@ private static void ProcessFolder(bool isMod,
LoadFolder(dir, false, ref hasWroteLine, ref melonAssemblies);

// Load Melons from Folders
if (!userLibsOnly)
if (scanType != eScanType.UserLibs)
foreach (var dir in melonDirectories)
LoadFolder(dir, true, ref hasWroteLine, ref melonAssemblies);
}

private static void ScanFolder(bool isMod,
private static void ScanFolder(eScanType scanType,
string path,
bool userLibsOnly,
ref List<string> melonDirectories,
ref List<string> melonDirectories,
ref List<string> userLibDirectories)
{
// Get Directories
Expand All @@ -175,40 +180,39 @@ private static void ScanFolder(bool isMod,
foreach (var dir in directories)
{
// Validate Path
if (!Directory.Exists(dir)
|| userLibDirectories.Contains(dir)
|| melonDirectories.Contains(dir))
if (!Directory.Exists(dir))
continue;

// Validate Folder
if (IsDisabledFolder(dir, out string dirNameLower))
// Validate Manifest
string manifestPath = Path.Combine(dir, "manifest.json");
if (!File.Exists(manifestPath))
continue;

// Check for UserLibs
if (userLibsOnly || IsUserLibsFolder(dirNameLower))
userLibDirectories.Add(dir);
// Check for Deeper UserLibs
string userLibsPath = Path.Combine(dir, "UserLibs");
if (Directory.Exists(userLibsPath))
{
userLibDirectories.Add(userLibsPath);
ScanFolder(eScanType.UserLibs, userLibsPath, ref melonDirectories, ref userLibDirectories);
}

// Is UserLibs Scan?
if (scanType == eScanType.UserLibs)
userLibDirectories.Add(dir); // Add to Directories List
else
melonDirectories.Add(dir);
{
// Check for Deeper Melon Folder
string melonPath = Path.Combine(dir, (scanType == eScanType.Plugins) ? "Plugins" : "Mods");
if (Directory.Exists(melonPath))
{
melonDirectories.Add(melonPath);
ScanFolder(scanType, melonPath, ref melonDirectories, ref userLibDirectories);
}

ScanFolder(isMod, dir, userLibsOnly, ref melonDirectories, ref userLibDirectories);
// Add to Directories List
melonDirectories.Add(dir);
}
}
}

private static bool StartsOrEndsWith(string dirNameLower, string target)
=> dirNameLower.StartsWith(target)
|| dirNameLower.EndsWith(target);

private static bool IsUserLibsFolder(string dirNameLower)
=> StartsOrEndsWith(dirNameLower, "userlibs");

private static bool IsDisabledFolder(string path,
out string dirNameLower)
{
string dirName = new DirectoryInfo(path).Name;
dirNameLower = dirName.ToLowerInvariant();
return StartsOrEndsWith(dirNameLower, "disabled")
|| StartsOrEndsWith(dirNameLower, "old")
|| StartsOrEndsWith(dirNameLower, "~");
}
}
}

0 comments on commit bbce3a6

Please sign in to comment.