Skip to content

Commit

Permalink
Fixed Plugin Folder Restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Dec 8, 2024
1 parent b013141 commit c4a93e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
2 changes: 0 additions & 2 deletions MelonLoader/InternalUtils/Il2CppAssemblyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using MelonLoader.Utils;
using MelonLoader.InternalUtils;

namespace MelonLoader.InternalUtils
{
Expand Down
80 changes: 49 additions & 31 deletions MelonLoader/Melons/MelonFolderHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,35 @@ internal static void Scan(eScanType type, string path)

// Find Melon Folders
List<string> userLibDirs = new();
List<string> melonDirs = new();
FindMelonFolders(type, path, ref melonDirs, ref userLibDirs);
List<string> pluginDirs = new();
List<string> modDirs = new();
FindMelonFolders(type, path, ref userLibDirs, ref pluginDirs, ref modDirs);

// Load UserLib Assemblies
bool hasWroteLine = false;
List<MelonAssembly> melonAssemblies = new();
MelonPreprocessor.LoadFolders(userLibDirs, (type == eScanType.UserLibs), ref hasWroteLine, ref melonAssemblies);
List<MelonAssembly> userLibAssemblies = new();
MelonPreprocessor.LoadFolders(userLibDirs, (type == eScanType.UserLibs), ref hasWroteLine, ref userLibAssemblies);

if (type != eScanType.UserLibs)
{
// Load Melon Assemblies
MelonPreprocessor.LoadFolders(melonDirs, true, ref hasWroteLine, ref melonAssemblies);
PreloadMelonAssemblies(melonAssemblies);
List<MelonAssembly> pluginAssemblies = new();
MelonPreprocessor.LoadFolders(pluginDirs, true, ref hasWroteLine, ref pluginAssemblies);
PreloadMelonAssemblies(pluginAssemblies);

// Load Plugins
List<MelonPlugin> pluginsLoaded =
LoadMelons<MelonPlugin>(path, (type == eScanType.Plugins), melonAssemblies);
LoadMelons<MelonPlugin>(pluginAssemblies);

// Load Mods
List<MelonMod> modsLoaded = (type == eScanType.Mods)
? LoadMelons<MelonMod>(path, false, melonAssemblies)
: null;
List<MelonMod> modsLoaded = null;
if (type == eScanType.Mods)
{
List<MelonAssembly> modAssemblies = new();
MelonPreprocessor.LoadFolders(modDirs, true, ref hasWroteLine, ref modAssemblies);
PreloadMelonAssemblies(modAssemblies);
modsLoaded = LoadMelons<MelonMod>(modAssemblies);
}

// Register and Sort Plugins
if (hasWroteLine)
Expand All @@ -61,7 +68,7 @@ internal static void Scan(eScanType type, string path)

// Log Count
int count = (type == eScanType.UserLibs)
? melonAssemblies.Count
? userLibAssemblies.Count
: ((type == eScanType.Mods) ? MelonMod.RegisteredMelons.Count : MelonPlugin.RegisteredMelons.Count);
string typeName = (type == eScanType.UserLibs)
? "UserLib".MakePlural(count)
Expand Down Expand Up @@ -92,9 +99,7 @@ private static void PreloadMelonAssemblies(List<MelonAssembly> melonAssemblies)
asm.LoadMelons();
}

private static List<T> LoadMelons<T>(string path,
bool logFailure,
List<MelonAssembly> melonAssemblies)
private static List<T> LoadMelons<T>(List<MelonAssembly> melonAssemblies)
where T : MelonTypeBase<T>
{
List<T> loadedMelons = new();
Expand All @@ -109,46 +114,56 @@ private static List<T> LoadMelons<T>(string path,
}

// Log Failure
if (logFailure)
MelonLogger.Warning($"Failed to load Melon '{m.Info.Name}' from '{path}': The given Melon is a {m.MelonTypeName} and cannot be loaded as a {MelonTypeBase<T>.TypeName}. Make sure it's in the right folder.");
MelonLogger.Warning($"Failed to load Melon '{m.Info.Name}' from '{m.MelonAssembly.Location}': The given Melon is a {m.MelonTypeName} and cannot be loaded as a {MelonTypeBase<T>.TypeName}. Make sure it's in the right folder.");
}
return loadedMelons;
}

private static void FindMelonFolders(
eScanType scanType,
string path,
ref List<string> melonDirectories,
ref List<string> userLibDirectories)
ref List<string> userLibDirectories,
ref List<string> pluginDirectories,
ref List<string> modDirectories)
{
// Validate Path
if (!Directory.Exists(path))
return;

// Scan Directories
ScanFolder(scanType, path, ref melonDirectories, ref userLibDirectories);
ScanFolder(scanType, path, ref userLibDirectories, ref pluginDirectories, ref modDirectories);

// Add Base Path to End of Directories List
if (scanType == eScanType.UserLibs)
userLibDirectories.Add(path);
userLibDirectories.Add(path);
else
melonDirectories.Add(path);
{
if (scanType == eScanType.Mods)
modDirectories.Add(path);
else
pluginDirectories.Add(path);
}

// Add Directories to Resolver
foreach (string directory in userLibDirectories)
{
MelonUtils.AddNativeDLLDirectory(directory);
Resolver.MelonAssemblyResolver.AddSearchDirectory(directory);
}

if (scanType != eScanType.UserLibs)
foreach (string directory in melonDirectories)
{
foreach (string directory in pluginDirectories)
Resolver.MelonAssemblyResolver.AddSearchDirectory(directory);
foreach (string directory in modDirectories)
Resolver.MelonAssemblyResolver.AddSearchDirectory(directory);
}
}

private static void ScanFolder(eScanType scanType,
string path,
ref List<string> melonDirectories,
ref List<string> userLibDirectories)
ref List<string> userLibDirectories,
ref List<string> pluginDirectories,
ref List<string> modDirectories)
{
// Get Directories
string[] directories = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
Expand All @@ -173,7 +188,7 @@ private static void ScanFolder(eScanType scanType,
if (Directory.Exists(userLibsPath))
{
userLibDirectories.Add(userLibsPath);
ScanFolder(eScanType.UserLibs, userLibsPath, ref melonDirectories, ref userLibDirectories);
ScanFolder(eScanType.UserLibs, userLibsPath, ref userLibDirectories, ref pluginDirectories, ref modDirectories);
}

// Is UserLibs Scan?
Expand All @@ -185,8 +200,8 @@ private static void ScanFolder(eScanType scanType,
string melonPath = Path.Combine(dir, "Plugins");
if (Directory.Exists(melonPath))
{
melonDirectories.Add(melonPath);
ScanFolder(scanType, melonPath, ref melonDirectories, ref userLibDirectories);
pluginDirectories.Add(melonPath);
ScanFolder(scanType, melonPath, ref userLibDirectories, ref pluginDirectories, ref modDirectories);
}

if (scanType == eScanType.Mods)
Expand All @@ -195,13 +210,16 @@ private static void ScanFolder(eScanType scanType,
melonPath = Path.Combine(dir, "Mods");
if (Directory.Exists(melonPath))
{
melonDirectories.Add(melonPath);
ScanFolder(scanType, melonPath, ref melonDirectories, ref userLibDirectories);
modDirectories.Add(melonPath);
ScanFolder(scanType, melonPath, ref userLibDirectories, ref pluginDirectories, ref modDirectories);
}
}

// Add to Directories List
melonDirectories.Add(dir);
if (scanType == eScanType.Mods)
modDirectories.Add(dir);
else
pluginDirectories.Add(dir);
}
}
}
Expand Down

0 comments on commit c4a93e8

Please sign in to comment.