Pārlūkot izejas kodu

Basic plugin loader

Bepis 7 gadi atpakaļ
vecāks
revīzija
849e722c1a
3 mainītis faili ar 54 papildinājumiem un 7 dzēšanām
  1. 5 7
      BepInEx/BepInEx.csproj
  2. 48 0
      BepInEx/PluginLoader.cs
  3. 1 0
      BepInEx/Utility.cs

+ 5 - 7
BepInEx/BepInEx.csproj

@@ -38,10 +38,7 @@
   <ItemGroup>
     <Reference Include="Assembly-CSharp-original, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
-      <HintPath>M:\koikatu\KoikatuTrial_Data\Managed\Assembly-CSharp-original.dll</HintPath>
-    </Reference>
-    <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
-      <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net20\Newtonsoft.Json.dll</HintPath>
+      <HintPath>D:\koikatu\KoikatuTrial_Data\Managed\Assembly-CSharp-original.dll</HintPath>
     </Reference>
     <Reference Include="System" />
     <Reference Include="System.Windows.Forms" />
@@ -49,15 +46,15 @@
     <Reference Include="System.Xml" />
     <Reference Include="TextMeshPro-1.0.55.56.0b12, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
-      <HintPath>M:\koikatu\KoikatuTrial_Data\Managed\TextMeshPro-1.0.55.56.0b12.dll</HintPath>
+      <HintPath>D:\koikatu\KoikatuTrial_Data\Managed\TextMeshPro-1.0.55.56.0b12.dll</HintPath>
     </Reference>
     <Reference Include="UnityEngine-original, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
-      <HintPath>M:\koikatu\KoikatuTrial_Data\Managed\UnityEngine-original.dll</HintPath>
+      <HintPath>D:\koikatu\KoikatuTrial_Data\Managed\UnityEngine-original.dll</HintPath>
     </Reference>
     <Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
-      <HintPath>M:\koikatu\KoikatuTrial_Data\Managed\UnityEngine.UI.dll</HintPath>
+      <HintPath>D:\koikatu\KoikatuTrial_Data\Managed\UnityEngine.UI.dll</HintPath>
     </Reference>
   </ItemGroup>
   <ItemGroup>
@@ -75,6 +72,7 @@
     <Compile Include="Internal\UnlockedInputPlugin.cs" />
     <Compile Include="ITranslationPlugin.cs" />
     <Compile Include="IUnityPlugin.cs" />
+    <Compile Include="PluginLoader.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Utility.cs" />
   </ItemGroup>

+ 48 - 0
BepInEx/PluginLoader.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+
+namespace BepInEx
+{
+    public static class PluginLoader
+    {
+        public static ICollection<T> LoadPlugins<T>(string directory)
+        {
+            List<T> plugins = new List<T>();
+            Type pluginType = typeof(T);
+
+            foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
+            {
+                try
+                {
+                    AssemblyName an = AssemblyName.GetAssemblyName(dll);
+                    Assembly assembly = Assembly.Load(an);
+                    
+                    foreach (Type type in assembly.GetTypes())
+                    {
+                        if (type.IsInterface || type.IsAbstract)
+                        {
+                            continue;
+                        }
+                        else
+                        {
+                            if (type.GetInterface(pluginType.FullName) != null)
+                            {
+                                plugins.Add((T)Activator.CreateInstance(type));
+                            }
+                        }
+                    }
+                }
+                catch (BadImageFormatException ex)
+                {
+
+                }
+            }
+
+            return plugins;
+        }
+    }
+}

+ 1 - 0
BepInEx/Utility.cs

@@ -10,6 +10,7 @@ namespace BepInEx
     public static class Utility
     {
         public static string ExecutingDirectory => Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "");
+        public static string PluginsDirectory => Path.Combine(ExecutingDirectory, "BepInEx");
 
         //shamelessly stolen from Rei
         public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);