Skip to content

Commit febdd65

Browse files
committed
Revert "Clean up NetLeaf.Bridge."
This reverts commit 7dd6d29.
1 parent 7dd6d29 commit febdd65

5 files changed

Lines changed: 24 additions & 41 deletions

File tree

NetLeaf.Bridge/Assemblies.cs

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

55
public static class Assemblies
66
{
7-
/// <summary>
8-
/// Represents a loaded plugin/assembly.
9-
/// </summary>
107
private class LoadedPlugin
118
{
129
public PluginLoadContext Context;
1310
public Assembly Assembly;
1411
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);
2012
}
2113

2214
private static readonly List<LoadedPlugin> _loadedPlugins = new();
2315

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

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

3725
if (!File.Exists(fullPath))
3826
{
@@ -43,15 +31,16 @@ public static void LoadAssembly(string path)
4331
var context = new PluginLoadContext(fullPath);
4432
var assembly = context.LoadFromAssemblyPath(fullPath);
4533

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

4841
Console.WriteLine($"[NetLeaf] Assembly loaded: {fullPath}");
4942
}
5043

51-
/// <summary>
52-
/// Unload an assembly.
53-
/// </summary>
54-
/// <param name="path">Path to the assembly.</param>
5544
public static void UnloadAssembly(string path)
5645
{
5746
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) ?? throw new Exception("Failed to create instance.");
35+
object instance = Activator.CreateInstance(type);
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
namespace NetLeaf.Bridge;
44

5-
/// <summary>
6-
/// Struct used for safetly passing a methods return value to unmanaged code.
7-
/// </summary>
5+
// Struct for passing a methods return value back to C++
86
[StructLayout(LayoutKind.Sequential)]
97
public struct MethodReturnValue
108
{
11-
public IntPtr StringResult;
9+
public IntPtr StringResult; // Pointer to marshaled string
1210
public float FloatResult;
1311
public uint UIntResult;
1412
public int IntResult;
1513

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

1917
public enum ReturnType : int

NetLeaf.Bridge/Methods.cs

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

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-
31+
string methodNamespaceStr = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
32+
? Marshal.PtrToStringUni(methodNamespace)
33+
: Marshal.PtrToStringUTF8(methodNamespace);
4034

4135
if (!TryParseMethodNamespace(methodNamespaceStr, out string fullMethodPath, out string[] argStrings))
4236
{

NetLeaf.Bridge/PluginLoadContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ 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) ?? throw new Exception("Failed to create instance.");
17+
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
1818
if (assemblyPath != null)
19+
{
1920
return LoadFromAssemblyPath(assemblyPath);
21+
}
2022

2123
return null;
2224
}

0 commit comments

Comments
 (0)