|
5 | 5 |
|
6 | 6 | namespace NeosModLoader |
7 | 7 | { |
8 | | - internal static class AssemblyLoader |
| 8 | + internal static class AssemblyLoader |
| 9 | + { |
| 10 | + private static string[]? GetAssemblyPathsFromDir(string dirName) |
9 | 11 | { |
10 | | - private static string[]? GetAssemblyPathsFromDir(string dirName) |
11 | | - { |
12 | | - string assembliesDirectory = Path.Combine(Directory.GetCurrentDirectory(), dirName); |
| 12 | + string assembliesDirectory = Path.Combine(Directory.GetCurrentDirectory(), dirName); |
13 | 13 |
|
14 | | - Logger.MsgInternal($"loading assemblies from {dirName}"); |
| 14 | + Logger.MsgInternal($"loading assemblies from {dirName}"); |
15 | 15 |
|
16 | | - string[]? assembliesToLoad = null; |
17 | | - try |
18 | | - { |
19 | | - assembliesToLoad = Directory.GetFiles(assembliesDirectory, "*.dll", SearchOption.AllDirectories); |
20 | | - Array.Sort(assembliesToLoad, (a, b) => string.CompareOrdinal(a, b)); |
21 | | - } |
22 | | - catch (Exception e) |
23 | | - { |
24 | | - if (e is DirectoryNotFoundException) |
25 | | - { |
26 | | - Logger.MsgInternal($"{dirName} directory not found, creating it now."); |
27 | | - try |
28 | | - { |
29 | | - Directory.CreateDirectory(assembliesDirectory); |
30 | | - } |
31 | | - catch (Exception e2) |
32 | | - { |
33 | | - Logger.ErrorInternal($"Error creating ${dirName} directory:\n{e2}"); |
34 | | - } |
35 | | - } |
36 | | - else |
37 | | - { |
38 | | - Logger.ErrorInternal($"Error enumerating ${dirName} directory:\n{e}"); |
39 | | - } |
40 | | - } |
41 | | - return assembliesToLoad; |
| 16 | + string[]? assembliesToLoad = null; |
| 17 | + try |
| 18 | + { |
| 19 | + assembliesToLoad = Directory.GetFiles(assembliesDirectory, "*.dll", SearchOption.AllDirectories); |
| 20 | + Array.Sort(assembliesToLoad, (a, b) => string.CompareOrdinal(a, b)); |
| 21 | + } |
| 22 | + catch (Exception e) |
| 23 | + { |
| 24 | + if (e is DirectoryNotFoundException) |
| 25 | + { |
| 26 | + Logger.MsgInternal($"{dirName} directory not found, creating it now."); |
| 27 | + try |
| 28 | + { |
| 29 | + Directory.CreateDirectory(assembliesDirectory); |
| 30 | + } |
| 31 | + catch (Exception e2) |
| 32 | + { |
| 33 | + Logger.ErrorInternal($"Error creating ${dirName} directory:\n{e2}"); |
| 34 | + } |
42 | 35 | } |
43 | | - |
44 | | - private static Assembly? LoadAssembly(string filepath) |
| 36 | + else |
45 | 37 | { |
46 | | - string filename = Path.GetFileName(filepath); |
47 | | - SplashChanger.SetCustom($"Loading file: {filename}"); |
48 | | - Assembly assembly; |
49 | | - try |
50 | | - { |
51 | | - Logger.MsgInternal( $"load assembly {filename} with sha256hash: {Util.GenerateSHA256(filepath)}"); |
52 | | - assembly = Assembly.LoadFile(filepath); |
53 | | - } |
54 | | - catch (Exception e) |
55 | | - { |
56 | | - Logger.ErrorInternal($"error loading assembly from {filepath}: {e}"); |
57 | | - return null; |
58 | | - } |
59 | | - if (assembly == null) |
60 | | - { |
61 | | - Logger.ErrorInternal($"unexpected null loading assembly from {filepath}"); |
62 | | - return null; |
63 | | - } |
64 | | - return assembly; |
| 38 | + Logger.ErrorInternal($"Error enumerating ${dirName} directory:\n{e}"); |
65 | 39 | } |
| 40 | + } |
| 41 | + return assembliesToLoad; |
| 42 | + } |
| 43 | + |
| 44 | + private static Assembly? LoadAssembly(string filepath) |
| 45 | + { |
| 46 | + string filename = Path.GetFileName(filepath); |
| 47 | + SplashChanger.SetCustom($"Loading file: {filename}"); |
| 48 | + Assembly assembly; |
| 49 | + try |
| 50 | + { |
| 51 | + Logger.MsgInternal($"load assembly {filename} with sha256hash: {Util.GenerateSHA256(filepath)}"); |
| 52 | + assembly = Assembly.LoadFile(filepath); |
| 53 | + } |
| 54 | + catch (Exception e) |
| 55 | + { |
| 56 | + Logger.ErrorInternal($"error loading assembly from {filepath}: {e}"); |
| 57 | + return null; |
| 58 | + } |
| 59 | + if (assembly == null) |
| 60 | + { |
| 61 | + Logger.ErrorInternal($"unexpected null loading assembly from {filepath}"); |
| 62 | + return null; |
| 63 | + } |
| 64 | + return assembly; |
| 65 | + } |
66 | 66 |
|
67 | | - internal static AssemblyFile[] LoadAssembliesFromDir(string dirName) |
| 67 | + internal static AssemblyFile[] LoadAssembliesFromDir(string dirName) |
| 68 | + { |
| 69 | + List<AssemblyFile> assemblyFiles = new(); |
| 70 | + if (GetAssemblyPathsFromDir(dirName) is string[] assemblyPaths) |
| 71 | + { |
| 72 | + foreach (string assemblyFilepath in assemblyPaths) |
68 | 73 | { |
69 | | - List<AssemblyFile> assemblyFiles = new(); |
70 | | - if (GetAssemblyPathsFromDir(dirName) is string[] assemblyPaths) |
| 74 | + try |
| 75 | + { |
| 76 | + if (LoadAssembly(assemblyFilepath) is Assembly assembly) |
71 | 77 | { |
72 | | - foreach (string assemblyFilepath in assemblyPaths) |
73 | | - { |
74 | | - try |
75 | | - { |
76 | | - if (LoadAssembly(assemblyFilepath) is Assembly assembly) |
77 | | - { |
78 | | - assemblyFiles.Add(new AssemblyFile(assemblyFilepath, assembly)); |
79 | | - } |
80 | | - } |
81 | | - catch (Exception e) |
82 | | - { |
83 | | - Logger.ErrorInternal($"Unexpected exception loading assembly from {assemblyFilepath}:\n{e}"); |
84 | | - } |
85 | | - } |
| 78 | + assemblyFiles.Add(new AssemblyFile(assemblyFilepath, assembly)); |
86 | 79 | } |
87 | | - |
88 | | - return assemblyFiles.ToArray(); |
| 80 | + } |
| 81 | + catch (Exception e) |
| 82 | + { |
| 83 | + Logger.ErrorInternal($"Unexpected exception loading assembly from {assemblyFilepath}:\n{e}"); |
| 84 | + } |
89 | 85 | } |
| 86 | + } |
| 87 | + |
| 88 | + return assemblyFiles.ToArray(); |
90 | 89 | } |
| 90 | + } |
91 | 91 | } |
0 commit comments