Skip to content

Commit

Permalink
fix: Fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Pd233 committed May 15, 2024
1 parent d591e9d commit 9d463c7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/AssemblyPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected internal override void Load()
if (_attribute is null)
{
Unload();
throw new EntryPointNotFoundException();
throw new EntryPointNotFoundException("Entry point not found.");
}

AssemblyName name = _assembly.GetName();
Expand Down
13 changes: 11 additions & 2 deletions src/EntryPointAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ namespace Hosihikari.PluginManagement;

public abstract class EntryPointAttributeBase : Attribute
{
internal abstract IEntryPoint CreateInstance();
public abstract IEntryPoint CreateInstance();
}

[AttributeUsage(AttributeTargets.Assembly)]
public sealed class EntryPointAttribute<T> : EntryPointAttributeBase where T : IEntryPoint, new()
{
internal override IEntryPoint CreateInstance()
public override IEntryPoint CreateInstance()
{
return new T();
}
}

[AttributeUsage(AttributeTargets.Assembly)]
public sealed class EntryPointAttribute(Type pluginType) : EntryPointAttributeBase
{
public override IEntryPoint CreateInstance()
{
return (Activator.CreateInstance(pluginType) as IEntryPoint) ?? throw new EntryPointNotFoundException("Entry point not found.");
}
}
14 changes: 6 additions & 8 deletions src/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,22 @@ public static AssemblyPlugin LoadPlugin(FileInfo file)
}

[UnmanagedCallersOnly]
public static unsafe nint LoadPluginUnmanaged(byte* pathStr, delegate* unmanaged[Stdcall]<byte*, void> errorHandler)
public static unsafe void LoadPluginUnmanaged(byte* pathStr, void** handle, void* arg, delegate* unmanaged[Stdcall]<void*, /* bool */bool, /* char const* */byte*, void> fptr)
{
try
{
var path = Utf8StringMarshaller.ConvertToManaged(pathStr);
if (string.IsNullOrWhiteSpace(path))
throw new NullReferenceException("Path is null or whitespace.");
var file = new FileInfo(path);
var plugin = LoadPlugin(file);
var temp = new PluginHandle(plugin);
return temp.Handle;
var temp = new PluginHandle(LoadPlugin(new FileInfo(path)));
*handle = (void*)temp.Handle;
fptr(arg, true, null);
}
catch (Exception ex)
{
var str = Utf8StringMarshaller.ConvertToUnmanaged(ex.Message);
errorHandler(str);
var str = Utf8StringMarshaller.ConvertToUnmanaged(ex.ToString());
fptr(arg, false, str);
Utf8StringMarshaller.Free(str);
throw;
}
}

Expand Down

0 comments on commit 9d463c7

Please sign in to comment.