This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ljoonal/libraries-loading
Allow loading libraries from `nml_libs` folder
- Loading branch information
Showing
9 changed files
with
153 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace NeosModLoader | ||
{ | ||
internal static class AssemblyLoader | ||
{ | ||
private static AssemblyFile[] GetAssembliesFromDir(string dirName) | ||
{ | ||
string assembliesDirectory = Path.Combine(Directory.GetCurrentDirectory(), dirName); | ||
|
||
Logger.MsgInternal($"loading assemblies from {dirName}"); | ||
|
||
AssemblyFile[] assembliesToLoad = null; | ||
try | ||
{ | ||
assembliesToLoad = Directory.GetFiles(assembliesDirectory, "*.dll") | ||
.Select(file => new AssemblyFile(file)) | ||
.ToArray(); | ||
|
||
Array.Sort(assembliesToLoad, (a, b) => string.CompareOrdinal(a.File, b.File)); | ||
} | ||
catch (Exception e) | ||
{ | ||
if (e is DirectoryNotFoundException) | ||
{ | ||
Logger.MsgInternal($"{dirName} directory not found, creating it now."); | ||
try | ||
{ | ||
Directory.CreateDirectory(assembliesDirectory); | ||
} | ||
catch (Exception e2) | ||
{ | ||
Logger.ErrorInternal($"Error creating ${dirName} directory:\n{e2}"); | ||
} | ||
} | ||
else | ||
{ | ||
Logger.ErrorInternal($"Error enumerating ${dirName} directory:\n{e}"); | ||
} | ||
} | ||
return assembliesToLoad; | ||
} | ||
|
||
private static void LoadAssembly(AssemblyFile assemblyFile) | ||
{ | ||
string filename = Path.GetFileName(assemblyFile.File); | ||
SplashChanger.SetCustom($"Loading file: {filename}"); | ||
Assembly assembly; | ||
try | ||
{ | ||
Logger.DebugInternal($"load assembly {filename}"); | ||
assembly = Assembly.LoadFile(assemblyFile.File); | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.ErrorInternal($"error loading assembly from {assemblyFile.File}: {e}"); | ||
return; | ||
} | ||
if (assembly == null) | ||
{ | ||
Logger.ErrorInternal($"unexpected null loading assembly from {assemblyFile.File}"); | ||
return; | ||
} | ||
assemblyFile.Assembly = assembly; | ||
} | ||
|
||
internal static AssemblyFile[] LoadAssembliesFromDir(string dirName) { | ||
var assembliesOrNull = GetAssembliesFromDir(dirName); | ||
if (assembliesOrNull is AssemblyFile[] assembliesToLoad) { | ||
foreach (AssemblyFile assemblyFile in assembliesToLoad) | ||
{ | ||
try | ||
{ | ||
LoadAssembly(assemblyFile); | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.ErrorInternal($"Unexpected exception loading assembly from {assemblyFile.File}:\n{e}"); | ||
} | ||
} | ||
} | ||
|
||
return assembliesOrNull; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters