Skip to content

Commit 7dd6d29

Browse files
committed
Clean up NetLeaf.Bridge.
1 parent ec94e48 commit 7dd6d29

5 files changed

Lines changed: 41 additions & 24 deletions

File tree

NetLeaf.Bridge/Assemblies.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,35 @@ namespace NetLeaf.Bridge;
44

55
public static class Assemblies
66
{
7+
/// <summary>
8+
/// Represents a loaded plugin/assembly.
9+
/// </summary>
710
private class LoadedPlugin
811
{
912
public PluginLoadContext Context;
1013
public Assembly Assembly;
1114
public string Path;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="LoadedPlugin"/> class.
18+
/// </summary>
19+
public LoadedPlugin(PluginLoadContext context, Assembly assembly, string path) => (Context, Assembly, Path) = (context, assembly, path);
1220
}
1321

1422
private static readonly List<LoadedPlugin> _loadedPlugins = new();
1523

16-
public static IEnumerable<Assembly> LoadedAssemblies
17-
=> _loadedPlugins.Select(p => p.Assembly);
24+
/// <summary>
25+
/// All currently loaded assemblies.
26+
/// </summary>
27+
public static IEnumerable<Assembly> LoadedAssemblies => _loadedPlugins.Select(p => p.Assembly);
1828

29+
/// <summary>
30+
/// Load an assembly.
31+
/// </summary>
32+
/// <param name="path">Path to the assembly.</param>
1933
public static void LoadAssembly(string path)
2034
{
21-
string fullPath = Path.IsPathRooted(path)
22-
? path
23-
: System.IO.Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", path);
35+
string fullPath = Path.IsPathRooted(path) ? path : System.IO.Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", path);
2436

2537
if (!File.Exists(fullPath))
2638
{
@@ -31,16 +43,15 @@ public static void LoadAssembly(string path)
3143
var context = new PluginLoadContext(fullPath);
3244
var assembly = context.LoadFromAssemblyPath(fullPath);
3345

34-
_loadedPlugins.Add(new LoadedPlugin
35-
{
36-
Context = context,
37-
Assembly = assembly,
38-
Path = fullPath
39-
});
46+
_loadedPlugins.Add(new LoadedPlugin(context, assembly, fullPath));
4047

4148
Console.WriteLine($"[NetLeaf] Assembly loaded: {fullPath}");
4249
}
4350

51+
/// <summary>
52+
/// Unload an assembly.
53+
/// </summary>
54+
/// <param name="path">Path to the assembly.</param>
4455
public static void UnloadAssembly(string path)
4556
{
4657
var plugin = _loadedPlugins.Find(p => p.Path == path);

NetLeaf.Bridge/InstanceFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public static uint CreateInstance(string typeNamespace)
2525

2626
try
2727
{
28-
Type type = Assemblies.FindTypeInLoadedAssemblies(typeNamespace);
28+
Type type = Assemblies.FindTypeInLoadedAssemblies(typeNamespace)!;
2929
if (type == null)
3030
{
3131
Console.WriteLine($"[CreateInstance Error] Type '{typeNamespace}' not found in loaded assemblies.");
3232
return 0;
3333
}
3434

35-
object instance = Activator.CreateInstance(type);
35+
object instance = Activator.CreateInstance(type) ?? throw new Exception("Failed to create instance.");
3636
instanceMap[id] = instance;
3737
return id;
3838
}
@@ -78,7 +78,7 @@ public static void RunInstanceMethod(uint id, string methodCall)
7878
method.Invoke(instance, args);
7979
}
8080

81-
private static string ParseMethodName(string methodCall, out string[] args)
81+
private static string? ParseMethodName(string methodCall, out string[]? args)
8282
{
8383
int parenStart = methodCall.IndexOf('(');
8484
int parenEnd = methodCall.LastIndexOf(')');

NetLeaf.Bridge/MethodReturnValue.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
namespace NetLeaf.Bridge;
44

5-
// Struct for passing a methods return value back to C++
5+
/// <summary>
6+
/// Struct used for safetly passing a methods return value to unmanaged code.
7+
/// </summary>
68
[StructLayout(LayoutKind.Sequential)]
79
public struct MethodReturnValue
810
{
9-
public IntPtr StringResult; // Pointer to marshaled string
11+
public IntPtr StringResult;
1012
public float FloatResult;
1113
public uint UIntResult;
1214
public int IntResult;
1315

14-
public ReturnType Type; // Indicate the type of the return value
16+
public ReturnType Type;
1517
}
1618

1719
public enum ReturnType : int

NetLeaf.Bridge/Methods.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ public static void RunMethod(IntPtr methodNamespace, IntPtr resultPtr)
2828
return;
2929
}
3030

31-
string methodNamespaceStr = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
32-
? Marshal.PtrToStringUni(methodNamespace)
33-
: Marshal.PtrToStringUTF8(methodNamespace);
31+
string? methodNamespaceStr = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Marshal.PtrToStringUni(methodNamespace) : Marshal.PtrToStringUTF8(methodNamespace);
32+
33+
if (methodNamespaceStr == null)
34+
{
35+
Console.Error.WriteLine("[RunMethod] methodNamespaceStr was null.");
36+
WriteResult(resultPtr, result);
37+
return;
38+
}
39+
3440

3541
if (!TryParseMethodNamespace(methodNamespaceStr, out string fullMethodPath, out string[] argStrings))
3642
{

NetLeaf.Bridge/PluginLoadContext.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ public PluginLoadContext(string pluginPath) : base(isCollectible: true)
1212
_resolver = new AssemblyDependencyResolver(pluginPath);
1313
}
1414

15-
protected override Assembly Load(AssemblyName assemblyName)
15+
protected override Assembly? Load(AssemblyName assemblyName)
1616
{
17-
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
17+
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName) ?? throw new Exception("Failed to create instance.");
1818
if (assemblyPath != null)
19-
{
2019
return LoadFromAssemblyPath(assemblyPath);
21-
}
2220

2321
return null;
2422
}

0 commit comments

Comments
 (0)