Skip to content

Commit

Permalink
refector: Remove unused methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed May 16, 2024
1 parent 9d463c7 commit 54dbdca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 83 deletions.
15 changes: 3 additions & 12 deletions src/EntryPointAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@ namespace Hosihikari.PluginManagement;

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

[AttributeUsage(AttributeTargets.Assembly)]
public sealed class EntryPointAttribute<T> : EntryPointAttributeBase where T : IEntryPoint, new()
public class EntryPointAttribute<T> : EntryPointAttributeBase where T : IEntryPoint, new()
{
public override IEntryPoint CreateInstance()
internal 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.");
}
}
83 changes: 17 additions & 66 deletions src/Main.cs → src/Export.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,10 @@
using Hosihikari.PluginManager;
using System.Collections.Immutable;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace Hosihikari.PluginManagement;

internal unsafe static class Main
internal unsafe static class Export
{
[UnmanagedCallersOnly]
public static void Initialize()
{
try
{
DirectoryInfo pluginsDirectory = new(AssemblyPlugin.PluginDirectoryPath);
if (!pluginsDirectory.Exists)
{
return;
}

Queue<DirectoryInfo> directoryQueue = new();
directoryQueue.Enqueue(pluginsDirectory);
while (directoryQueue.TryDequeue(out DirectoryInfo? directoryInfo))
{
foreach (DirectoryInfo subdirectory in directoryInfo.EnumerateDirectories())
{
directoryQueue.Enqueue(subdirectory);
}

foreach (FileInfo file in directoryInfo.EnumerateFiles())
{
LoadPlugin(file);
}
}

foreach (string pluginName in (from plugin in AssemblyPlugin.Plugins select plugin.Name).ToImmutableArray())
{
if (string.IsNullOrWhiteSpace(pluginName))
{
throw new NullReferenceException();
}

Manager.Initialize(pluginName);
}
}
catch (Exception ex)
{
Environment.FailFast(default, ex);
}
}

public static AssemblyPlugin LoadPlugin(FileInfo file)
{
AssemblyPlugin plugin = new(file);
Expand All @@ -61,16 +17,19 @@ public static unsafe void LoadPluginUnmanaged(byte* pathStr, void** handle, void
{
try
{
var path = Utf8StringMarshaller.ConvertToManaged(pathStr);
string? path = Utf8StringMarshaller.ConvertToManaged(pathStr);
if (string.IsNullOrWhiteSpace(path))
{
throw new NullReferenceException("Path is null or whitespace.");
var temp = new PluginHandle(LoadPlugin(new FileInfo(path)));
}

PluginHandle temp = new(LoadPlugin(new FileInfo(path)));
*handle = (void*)temp.Handle;
fptr(arg, true, null);
}
catch (Exception ex)
{
var str = Utf8StringMarshaller.ConvertToUnmanaged(ex.ToString());
byte* str = Utf8StringMarshaller.ConvertToUnmanaged(ex.ToString());
fptr(arg, false, str);
Utf8StringMarshaller.Free(str);
}
Expand All @@ -81,64 +40,56 @@ public unsafe static void Load(void* handle, void* arg, delegate* unmanaged[Stdc
{
try
{
var target = GCHandle.FromIntPtr((nint)handle).Target as Plugin
?? throw new NullReferenceException("Plugin is null");
target.Load();
target.Initialize();

Plugin target = GCHandle.FromIntPtr((nint)handle).Target as Plugin ?? throw new NullReferenceException("Plugin is null");
Manager.Initialize(target.Name);
fptr(arg, true, null);
return;
}
catch (Exception ex)
{
var str = Utf8StringMarshaller.ConvertToUnmanaged(ex.ToString());
byte* str = Utf8StringMarshaller.ConvertToUnmanaged(ex.ToString());
fptr(arg, false, str);
Utf8StringMarshaller.Free(str);
return;
}
}

[UnmanagedCallersOnly]
public unsafe static void Enable(void* handle, void* arg, delegate* unmanaged[Stdcall]<void*, /* bool */bool, /* char const* */byte*, void> fptr)
{
fptr(arg, true, null);
return;
}

[UnmanagedCallersOnly]
public unsafe static void Disable(void* handle, void* arg, delegate* unmanaged[Stdcall]<void*, /* bool */bool, /* char const* */byte*, void> fptr)
{
fptr(arg, true, null);
return;
}

[UnmanagedCallersOnly]
public unsafe static void Unload(void* handle, void* arg, delegate* unmanaged[Stdcall]<void*, /* bool */bool, /* char const* */byte*, void> fptr)
{
try
{
var target = GCHandle.FromIntPtr((nint)handle).Target as Plugin
?? throw new NullReferenceException("Plugin is null");
target.Unload();
Plugin target = GCHandle.FromIntPtr((nint)handle).Target as Plugin ?? throw new NullReferenceException("Plugin is null");
Manager.Unload(target.Name);
fptr(arg, true, null);
return;
}
catch (Exception ex)
{
var str = Utf8StringMarshaller.ConvertToUnmanaged(ex.ToString());
byte* str = Utf8StringMarshaller.ConvertToUnmanaged(ex.ToString());
fptr(arg, false, str);
Utf8StringMarshaller.Free(str);
return;
}
}

[UnmanagedCallersOnly]
public unsafe static void ReleaseHandle(void* handle, void* arg, delegate* unmanaged[Stdcall]<void*, /* bool */bool, /* char const* */byte*, void> fptr)
{
var gch = GCHandle.FromIntPtr((nint)handle);
GCHandle gch = GCHandle.FromIntPtr((nint)handle);
if (gch.IsAllocated)
{
gch.Free();
}

fptr(arg, true, null);
return;
}
}
9 changes: 4 additions & 5 deletions src/PluginHandle.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Hosihikari.PluginManagement;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace Hosihikari.PluginManager;
namespace Hosihikari.PluginManagement;

[StructLayout(LayoutKind.Sequential)]
public readonly unsafe struct PluginHandle(Plugin plugin)
internal readonly unsafe struct PluginHandle(Plugin plugin)
{
public readonly nint Handle = GCHandle.ToIntPtr(GCHandle.Alloc(plugin));
}
}

0 comments on commit 54dbdca

Please sign in to comment.