Browse Source

Fixed Config class

pull/1/head
Anairkoen Schno 5 years ago
parent
commit
4081cc05ef
4 changed files with 105 additions and 81 deletions
  1. +79
    -70
      IPA.Injector/Injector.cs
  2. +2
    -2
      IPA.Loader/Config/Config.cs
  3. +9
    -8
      IPA.Loader/Loader/PluginLoader.cs
  4. +15
    -1
      IPA.Loader/Utilities/Ref.cs

+ 79
- 70
IPA.Injector/Injector.cs View File

@ -51,6 +51,13 @@ namespace IPA.Injector
}
}
private static void SetupLibraryLoading()
{
if (_loadingDone) return;
_loadingDone = true;
AppDomain.CurrentDomain.AssemblyResolve += LibLoader.AssemblyLibLoader;
}
private static void InstallBootstrapPatch()
{
var cAsmName = Assembly.GetExecutingAssembly().GetName();
@ -62,93 +69,103 @@ namespace IPA.Injector
loader.Warn("No backup found! Was BSIPA installed using the installer?");
loader.Debug("Ensuring patch on UnityEngine.CoreModule exists");
#region Insert patch into UnityEngine.CoreModule.dll
var unityPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "UnityEngine.CoreModule.dll");
var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath, new ReaderParameters
{
ReadWrite = false,
InMemory = true,
ReadingMode = ReadingMode.Immediate
});
var unityModDef = unityAsmDef.MainModule;
bool modified = false;
foreach (var asmref in unityModDef.AssemblyReferences)
#region Insert patch into UnityEngine.CoreModule.dll
{
if (asmref.Name == cAsmName.Name)
var unityPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed",
"UnityEngine.CoreModule.dll");
var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath, new ReaderParameters
{
if (asmref.Version != cAsmName.Version)
ReadWrite = false,
InMemory = true,
ReadingMode = ReadingMode.Immediate
});
var unityModDef = unityAsmDef.MainModule;
bool modified = false;
foreach (var asmref in unityModDef.AssemblyReferences)
{
if (asmref.Name == cAsmName.Name)
{
asmref.Version = cAsmName.Version;
modified = true;
if (asmref.Version != cAsmName.Version)
{
asmref.Version = cAsmName.Version;
modified = true;
}
}
}
}
var application = unityModDef.GetType("UnityEngine", "Application");
var application = unityModDef.GetType("UnityEngine", "Application");
MethodDefinition cctor = null;
foreach (var m in application.Methods)
if (m.IsRuntimeSpecialName && m.Name == ".cctor")
cctor = m;
MethodDefinition cctor = null;
foreach (var m in application.Methods)
if (m.IsRuntimeSpecialName && m.Name == ".cctor")
cctor = m;
var cbs = unityModDef.ImportReference(((Action)CreateBootstrapper).Method);
var cbs = unityModDef.ImportReference(((Action) CreateBootstrapper).Method);
if (cctor == null)
{
cctor = new MethodDefinition(".cctor", MethodAttributes.RTSpecialName | MethodAttributes.Static | MethodAttributes.SpecialName, unityModDef.TypeSystem.Void);
application.Methods.Add(cctor);
modified = true;
var ilp = cctor.Body.GetILProcessor();
ilp.Emit(OpCodes.Call, cbs);
ilp.Emit(OpCodes.Ret);
}
else
{
var ilp = cctor.Body.GetILProcessor();
for (var i = 0; i < Math.Min(2, cctor.Body.Instructions.Count); i++)
if (cctor == null)
{
cctor = new MethodDefinition(".cctor",
MethodAttributes.RTSpecialName | MethodAttributes.Static | MethodAttributes.SpecialName,
unityModDef.TypeSystem.Void);
application.Methods.Add(cctor);
modified = true;
var ilp = cctor.Body.GetILProcessor();
ilp.Emit(OpCodes.Call, cbs);
ilp.Emit(OpCodes.Ret);
}
else
{
var ins = cctor.Body.Instructions[i];
switch (i)
var ilp = cctor.Body.GetILProcessor();
for (var i = 0; i < Math.Min(2, cctor.Body.Instructions.Count); i++)
{
case 0 when ins.OpCode != OpCodes.Call:
ilp.Replace(ins, ilp.Create(OpCodes.Call, cbs));
modified = true;
break;
case 0:
var ins = cctor.Body.Instructions[i];
switch (i)
{
var methodRef = ins.Operand as MethodReference;
if (methodRef?.FullName != cbs.FullName)
{
case 0 when ins.OpCode != OpCodes.Call:
ilp.Replace(ins, ilp.Create(OpCodes.Call, cbs));
modified = true;
break;
case 0:
{
var methodRef = ins.Operand as MethodReference;
if (methodRef?.FullName != cbs.FullName)
{
ilp.Replace(ins, ilp.Create(OpCodes.Call, cbs));
modified = true;
}
break;
}
break;
case 1 when ins.OpCode != OpCodes.Ret:
ilp.Replace(ins, ilp.Create(OpCodes.Ret));
modified = true;
break;
}
case 1 when ins.OpCode != OpCodes.Ret:
ilp.Replace(ins, ilp.Create(OpCodes.Ret));
modified = true;
break;
}
}
}
if (modified)
{
bkp?.Add(unityPath);
unityAsmDef.Write(unityPath);
if (modified)
{
bkp?.Add(unityPath);
unityAsmDef.Write(unityPath);
}
}
#endregion
loader.Debug("Ensuring Assembly-CSharp is virtualized");
#region Virtualize Assembly-CSharp.dll
var ascPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "Assembly-CSharp.dll");
var ascModule = VirtualizedModule.Load(ascPath);
ascModule.Virtualize(cAsmName, () => bkp?.Add(ascPath));
{
var ascPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed",
"Assembly-CSharp.dll");
var ascModule = VirtualizedModule.Load(ascPath);
ascModule.Virtualize(cAsmName, () => bkp?.Add(ascPath));
}
#endregion
}
@ -173,14 +190,6 @@ namespace IPA.Injector
}
private static bool _loadingDone;
private static void SetupLibraryLoading()
{
if (_loadingDone) return;
_loadingDone = true;
AppDomain.CurrentDomain.AssemblyResolve += LibLoader.AssemblyLibLoader;
}
private static void Bootstrapper_Destroyed()
{
// wait for plugins to finish loading


+ 2
- 2
IPA.Loader/Config/Config.cs View File

@ -128,8 +128,8 @@ namespace IPA.Config
return GetProviderFor(filename, prefs);
}
private static SortedDictionary<IConfigProvider, Action> linkedProviders =
new SortedDictionary<IConfigProvider, Action>();
private static Dictionary<IConfigProvider, Action> linkedProviders =
new Dictionary<IConfigProvider, Action>();
/// <summary>
/// Creates a linked <see cref="Ref{T}"/> for the config provider. This <see cref="Ref{T}"/> will be automatically updated whenever the file on-disk changes.


+ 9
- 8
IPA.Loader/Loader/PluginLoader.cs View File

@ -284,13 +284,6 @@ namespace IPA.Loader
PluginsMetadata = metadata;
}
internal static List<PluginInfo> LoadPlugins()
{
var list = PluginsMetadata.Select(LoadPlugin).Where(p => p != null).ToList();
return list;
}
internal static PluginInfo LoadPlugin(PluginMetadata meta)
{
if (meta.PluginType == null)
@ -313,7 +306,7 @@ namespace IPA.Loader
info.Metadata = meta;
info.Plugin = instance;
{
{
var init = type.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public);
if (init != null)
{
@ -342,6 +335,7 @@ namespace IPA.Loader
if (cfgProvider == null)
{
cfgProvider = Config.Config.GetProviderFor(Path.Combine("UserData", $"{meta.Name}"), param);
cfgProvider.Load();
}
initArgs.Add(cfgProvider);
}
@ -366,5 +360,12 @@ namespace IPA.Loader
return info;
}
internal static List<PluginInfo> LoadPlugins()
{
var list = PluginsMetadata.Select(LoadPlugin).Where(p => p != null).ToList();
return list;
}
}
}

+ 15
- 1
IPA.Loader/Utilities/Ref.cs View File

@ -8,7 +8,7 @@ namespace IPA.Utilities
/// A class to store a reference for passing to methods which cannot take ref parameters.
/// </summary>
/// <typeparam name="T">the type of the value</typeparam>
public class Ref<T>
public class Ref<T> : IComparable<T>, IComparable<Ref<T>>
{
private T _value;
/// <summary>
@ -76,6 +76,20 @@ namespace IPA.Utilities
{
if (Error != null) throw Error;
}
/// <inheritdoc />
public int CompareTo(T other)
{
if (Value is IComparable<T> compare)
return compare.CompareTo(other);
return Equals(Value, other) ? 0 : -1;
}
/// <inheritdoc />
public int CompareTo(Ref<T> other)
{
return CompareTo(other.Value);
}
}
internal static class ExceptionUtilities


Loading…
Cancel
Save