Skip to content

Commit

Permalink
Implement changes required for BepInEx integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bbepis committed May 24, 2020
1 parent eef138d commit 1448ee6
Show file tree
Hide file tree
Showing 28 changed files with 288 additions and 2,643 deletions.
48 changes: 21 additions & 27 deletions BSIPA.Loader/BSIPA.Loader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BSIPA.Loader</RootNamespace>
<AssemblyName>BSIPA.Loader</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>..\bin\plugins\BSIPA\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>8</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>..\bin\plugins\BSIPA\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>8</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\BepInEx\bin\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AsyncBridge, Version=0.3.1.0, Culture=neutral, PublicKeyToken=b3b1c0202c0d6a87, processorArchitecture=MSIL">
<HintPath>..\packages\AsyncBridge.0.3.1\lib\net35-client\AsyncBridge.dll</HintPath>
</Reference>
<Reference Include="BepInEx">
<HintPath>..\..\BepInEx\bin\BepInEx.dll</HintPath>
<Private>False</Private>
Expand All @@ -52,33 +52,35 @@
</Reference>
<Reference Include="Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c, processorArchitecture=MSIL">
<HintPath>..\packages\Ionic.Zip.1.9.1.8\lib\Ionic.Zip.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Mono.Cecil, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net40\Mono.Cecil.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Cecil.Mdb, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Mdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Cecil.Pdb, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Pdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Rocks.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.4\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="SemVer, Version=1.2.2.0, Culture=neutral, PublicKeyToken=a89bb7dc6f7a145c, processorArchitecture=MSIL">
<HintPath>..\packages\SemanticVersioning.1.2.2\lib\net35\SemVer.dll</HintPath>
<HintPath>..\packages\SemanticVersioning.1.2.2\lib\net45\SemVer.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\TaskParallelLibrary.1.0.2856.0\lib\Net35\System.Threading.dll</HintPath>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -152,15 +154,6 @@
<Compile Include="Logging\Printers\PluginLogFilePrinter.cs" />
<Compile Include="Logging\Printers\PluginSubLogPrinter.cs" />
<Compile Include="Logging\StandardLogger.cs" />
<Compile Include="Net3Proxy\Array.cs" />
<Compile Include="Net3Proxy\CompilerServices.cs" />
<Compile Include="Net3Proxy\ExpressionEx.cs" />
<Compile Include="Net3Proxy\Extensions.cs" />
<Compile Include="Net3Proxy\ReadOnlyCollections.cs" />
<Compile Include="Net3Proxy\TaskEx6.cs" />
<Compile Include="Net3Proxy\Tuple.cs" />
<Compile Include="Net3Proxy\TypeUtils.cs" />
<Compile Include="Net3Proxy\WeakReference.cs" />
<Compile Include="PluginInterfaces\Attributes\LifecycleAttributes.cs" />
<Compile Include="PluginInterfaces\Attributes\PluginAttribute.cs" />
<Compile Include="PluginInterfaces\IPA\IEnhancedPlugin.cs" />
Expand All @@ -185,8 +178,9 @@
<ItemGroup>
<EmbeddedResource Include="Loader\description.md" />
<EmbeddedResource Include="Loader\manifest.json" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
10 changes: 1 addition & 9 deletions BSIPA.Loader/Config/ConfigRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using IPA.Utilities;
using IPA.Utilities.Async;
using System.IO;
using System.Runtime.CompilerServices;
using IPA.Logging;
using UnityEngine;
using Logger = IPA.Logging.Logger;
#if NET4
using Task = System.Threading.Tasks.Task;
using TaskEx = System.Threading.Tasks.Task;
#endif

namespace IPA.Config
{
Expand Down Expand Up @@ -161,7 +153,7 @@ public static Task TriggerFileLoad(Config config)
=> loadFactory.StartNew(() => LoadTask(config));

public static Task TriggerLoadAll()
=> TaskEx.WhenAll(configs.Select(TriggerFileLoad));
=> Task.WhenAll(configs.Select(TriggerFileLoad));

/// <summary>
/// this is synchronous, unlike <see cref="TriggerFileLoad(Config)"/>
Expand Down
2 changes: 1 addition & 1 deletion BSIPA.Loader/Config/Data/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void AddRange(IEnumerable<Value> vals)
/// </summary>
/// <returns>a comma-seperated list of the result of <see cref="Value.ToString"/> wrapped in square brackets</returns>
public override string ToString()
=> $"[{string.Join(",",this.Select(v => v?.ToString() ?? "null").StrJP())}]";
=> $"[{string.Join(",",this.Select(v => v?.ToString() ?? "null"))}]";

IEnumerator IEnumerable.GetEnumerator() => ((IList<Value>)values).GetEnumerator();
}
Expand Down
2 changes: 1 addition & 1 deletion BSIPA.Loader/Config/Data/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool ICollection<KeyValuePair<string, Value>>.Remove(KeyValuePair<string, Value>
/// </summary>
/// <returns>a JSON-like set of key-value pairs</returns>
public override string ToString()
=> $"{{{string.Join(",", this.Select(p => $"\"{p.Key}\":{p.Value?.ToString() ?? "null"}").StrJP())}}}";
=> $"{{{string.Join(",", this.Select(p => $"\"{p.Key}\":{p.Value?.ToString() ?? "null"}"))}}}";
}


Expand Down
2 changes: 1 addition & 1 deletion BSIPA.Loader/Loader/DisabledConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected virtual void OnReload()
disableUpdateTask = disableUpdateTask.ContinueWith(t =>
{
// skip if another got here before the last finished
if (referToState != updateState) return TaskEx.WhenAll();
if (referToState != updateState) return Task.WhenAll();
else return UpdateDisabledMods(copy);
});
}
Expand Down
45 changes: 0 additions & 45 deletions BSIPA.Loader/Loader/HarmonyProtector.cs

This file was deleted.

78 changes: 11 additions & 67 deletions BSIPA.Loader/Loader/LibLoader.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Linq;
using IPA.Logging;
using IPA.Utilities;
using Mono.Cecil;
#if NET3
using Net3_Proxy;
using Directory = Net3_Proxy.Directory;
using Path = Net3_Proxy.Path;
using File = Net3_Proxy.File;
#endif

namespace IPA.Loader
{
Expand All @@ -27,8 +17,10 @@ public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderPar
{
LibLoader.SetupAssemblyFilenames();

if (name.Name == CurrentAssemblyName)
if (name.Name == CurrentAssemblyName || name.Name == "IPA.Loader")
{
return AssemblyDefinition.ReadAssembly(CurrentAssemblyPath, parameters);
}

if (LibLoader.FilenameLocations.TryGetValue($"{name.Name}.dll", out var path))
{
Expand All @@ -48,8 +40,8 @@ public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderPar

internal static class LibLoader
{
internal static string LibraryPath => Path.Combine(Environment.CurrentDirectory, "Libs");
internal static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
internal static string LibraryPath => UnityGame.LibraryPath;
internal static string NativeLibraryPath => UnityGame.NativeLibraryPath;
internal static Dictionary<string, string> FilenameLocations;

internal static void Configure()
Expand All @@ -69,42 +61,6 @@ internal static void SetupAssemblyFilenames(bool force = false)
if (FilenameLocations.ContainsKey(fn.Name))
Log(Logger.Level.Critical, $"Multiple instances of {fn.Name} exist in Libs! Ignoring {fn.FullName}");
else FilenameLocations.Add(fn.Name, fn.FullName);


if (!SetDefaultDllDirectories(LoadLibraryFlags.LOAD_LIBRARY_SEARCH_USER_DIRS | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_SYSTEM32
| LoadLibraryFlags.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_APPLICATION_DIR))
{
var err = new Win32Exception();
Log(Logger.Level.Critical, $"Error configuring DLL search path");
Log(Logger.Level.Critical, err);
return;
}

static void AddDir(string path)
{
var retPtr = AddDllDirectory(path);
if (retPtr == IntPtr.Zero)
{
var err = new Win32Exception();
Log(Logger.Level.Warning, $"Could not add DLL directory {path}");
Log(Logger.Level.Warning, err);
}
}

if (Directory.Exists(NativeLibraryPath))
{
AddDir(NativeLibraryPath);
TraverseTree(NativeLibraryPath, dir =>
{ // this is a terrible hack for iterating directories
AddDir(dir); return true;
}).All(f => true); // force it to iterate all
}

//var unityData = Directory.EnumerateDirectories(Environment.CurrentDirectory, "*_Data").First();
//AddDir(Path.Combine(unityData, "Plugins"));

foreach (var dir in Environment.GetEnvironmentVariable("path").Split(Path.PathSeparator))
AddDir(dir);
}
}

Expand All @@ -123,6 +79,12 @@ internal static Assembly LoadLibrary(AssemblyName asmName)
var testFile = $"{asmName.Name}.dll";
Log(Logger.Level.Debug, $"Looking for file {asmName.Name}.dll");

if (asmName.Name == "IPA.Loader")
{
Log(Logger.Level.Debug, "Resolved to self");
return typeof(BSIPALoaderPlugin).Assembly;
}

if (FilenameLocations.TryGetValue(testFile, out var path))
{
Log(Logger.Level.Debug, $"Found file {testFile} as {path}");
Expand Down Expand Up @@ -217,23 +179,5 @@ private static IEnumerable<FileInfo> TraverseTree(string root, Func<string, bool
}
}
}

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr AddDllDirectory(string lpPathName);

[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private enum LoadLibraryFlags : uint
{
None = 0,
LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200,
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000,
LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800,
LOAD_LIBRARY_SEARCH_USER_DIRS = 0x00000400,
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetDefaultDllDirectories(LoadLibraryFlags dwFlags);
}
}
Loading

0 comments on commit 1448ee6

Please sign in to comment.