Browse Source

Replace Newtonsoft.Json by System.Text.Json

feature/system-text-json
Meivyn 10 months ago
parent
commit
0d1453ae14
No known key found for this signature in database GPG Key ID: 8BDD3E48158B2F71
17 changed files with 802 additions and 820 deletions
  1. +331
    -332
      IPA.Injector/Injector.cs
  2. +63
    -94
      IPA.Loader/Config/Providers/JsonConfigProvider.cs
  3. +1
    -1
      IPA.Loader/Config/SelfConfig.cs
  4. +3
    -9
      IPA.Loader/IPA.Loader.csproj
  5. +7
    -10
      IPA.Loader/JsonConverters/AlmostVersionConverter.cs
  6. +19
    -16
      IPA.Loader/JsonConverters/FeaturesFieldConverter.cs
  7. +11
    -14
      IPA.Loader/JsonConverters/MultilineStringConverter.cs
  8. +7
    -7
      IPA.Loader/JsonConverters/SemverRangeConverter.cs
  9. +7
    -6
      IPA.Loader/JsonConverters/SemverVersionConverter.cs
  10. +8
    -6
      IPA.Loader/Loader/Features/ConfigProviderFeature.cs
  11. +12
    -8
      IPA.Loader/Loader/Features/DefineFeature.cs
  12. +5
    -6
      IPA.Loader/Loader/Features/Feature.cs
  13. +234
    -237
      IPA.Loader/Loader/LibLoader.cs
  14. +21
    -19
      IPA.Loader/Loader/PluginLoader.cs
  15. +62
    -41
      IPA.Loader/Loader/PluginManifest.cs
  16. +1
    -3
      IPA.Loader/Updating/BeatMods/ApiEndpoint.cs
  17. +10
    -11
      IPA.Loader/Updating/BeatMods/Updater.cs

+ 331
- 332
IPA.Injector/Injector.cs View File

@ -1,332 +1,331 @@
#nullable enable
using IPA.AntiMalware;
using IPA.Config;
using IPA.Injector.Backups;
using IPA.Loader;
using IPA.Logging;
using IPA.Utilities;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using static IPA.Logging.Logger;
using MethodAttributes = Mono.Cecil.MethodAttributes;
#if NET3
using Net3_Proxy;
using Path = Net3_Proxy.Path;
using File = Net3_Proxy.File;
using Directory = Net3_Proxy.Directory;
#endif
namespace IPA.Injector
{
/// <summary>
/// The entry point type for BSIPA's Doorstop injector.
/// </summary>
// ReSharper disable once UnusedMember.Global
internal static class Injector
{
private static Task? pluginAsyncLoadTask;
private static Task? permissionFixTask;
//private static string otherNewtonsoftJson = null;
// ReSharper disable once UnusedParameter.Global
internal static void Main(string[] args)
{ // entry point for doorstop
// At this point, literally nothing but mscorlib is loaded,
// and since this class doesn't have any static fields that
// aren't defined in mscorlib, we can control exactly what
// gets loaded.
_ = args;
try
{
var arguments = Environment.GetCommandLineArgs();
MaybeInitializeConsole(arguments);
SetupLibraryLoading();
EnsureDirectories();
// this is weird, but it prevents Mono from having issues loading the type.
// IMPORTANT: NO CALLS TO ANY LOGGER CAN HAPPEN BEFORE THIS
var unused = StandardLogger.PrintFilter;
#region // Above hack explanation
/*
* Due to an unknown bug in the version of Mono that Unity uses, if the first access to StandardLogger
* is a call to a constructor, then Mono fails to load the type correctly. However, if the first access is to
* the above static property (or maybe any, but I don't really know) it behaves as expected and works fine.
*/
#endregion
Default.Debug("Initializing logger");
SelfConfig.ReadCommandLine(arguments);
SelfConfig.Load();
DisabledConfig.Load();
if (AntiPiracy.IsInvalid(Environment.CurrentDirectory))
{
Default.Error("Invalid installation; please buy the game to run BSIPA.");
return;
}
CriticalSection.Configure();
Logging.Logger.Injector.Debug("Prepping bootstrapper");
// make sure to load the game version and check boundaries before installing the bootstrap, because that uses the game assemblies property
GameVersionEarly.Load();
SelfConfig.Instance.CheckVersionBoundary();
// updates backup
InstallBootstrapPatch();
AntiMalwareEngine.Initialize();
Updates.InstallPendingUpdates();
Loader.LibLoader.SetupAssemblyFilenames(true);
pluginAsyncLoadTask = PluginLoader.LoadTask();
permissionFixTask = PermissionFix.FixPermissions(new DirectoryInfo(Environment.CurrentDirectory));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static void MaybeInitializeConsole(string[] arguments)
{
var i = 0;
while (i < arguments.Length)
{
if (arguments[i++] == "--verbose")
{
if (i == arguments.Length)
{
WinConsole.Initialize(WinConsole.AttachParent);
return;
}
WinConsole.Initialize(int.TryParse(arguments[i], out int processId) ? processId : WinConsole.AttachParent);
return;
}
}
}
private static void EnsureDirectories()
{
string path;
if (!Directory.Exists(path = Path.Combine(Environment.CurrentDirectory, "UserData")))
_ = Directory.CreateDirectory(path);
if (!Directory.Exists(path = Path.Combine(Environment.CurrentDirectory, "Plugins")))
_ = Directory.CreateDirectory(path);
}
private static void SetupLibraryLoading()
{
if (loadingDone) return;
loadingDone = true;
Loader.LibLoader.Configure();
}
private static void InstallBootstrapPatch()
{
var sw = Stopwatch.StartNew();
var cAsmName = Assembly.GetExecutingAssembly().GetName();
var managedPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var dataDir = new DirectoryInfo(managedPath).Parent!.Name;
var gameName = dataDir.Substring(0, dataDir.Length - 5);
Logging.Logger.Injector.Debug("Finding backup");
var backupPath = Path.Combine(Environment.CurrentDirectory, "IPA", "Backups", gameName);
var bkp = BackupManager.FindLatestBackup(backupPath);
if (bkp == null)
Logging.Logger.Injector.Warn("No backup found! Was BSIPA installed using the installer?");
// TODO: Investigate if this ever worked properly.
// this is a critical section because if you exit in here, assembly can die
using var critSec = CriticalSection.ExecuteSection();
var readerParameters = new ReaderParameters
{
ReadWrite = false,
InMemory = true,
ReadingMode = ReadingMode.Immediate
};
Logging.Logger.Injector.Debug("Ensuring patch on UnityEngine.CoreModule exists");
#region Insert patch into UnityEngine.CoreModule.dll
var unityPath = Path.Combine(managedPath, "UnityEngine.CoreModule.dll");
using var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath, readerParameters);
var unityModDef = unityAsmDef.MainModule;
bool modified = false;
foreach (var asmref in unityModDef.AssemblyReferences)
{
if (asmref.Name == cAsmName.Name)
{
if (asmref.Version != cAsmName.Version)
{
asmref.Version = cAsmName.Version;
modified = true;
}
}
}
var application = unityModDef.GetType("UnityEngine", "Camera");
if (application == null)
{
Logging.Logger.Injector.Critical("UnityEngine.CoreModule doesn't have a definition for UnityEngine.Camera!"
+ "Nothing to patch to get ourselves into the Unity run cycle!");
goto endPatchCoreModule;
}
MethodDefinition? cctor = null;
foreach (var m in application.Methods)
if (m.IsRuntimeSpecialName && m.Name == ".cctor")
cctor = m;
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++)
{
var ins = cctor.Body.Instructions[i];
switch (i)
{
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;
}
case 1 when ins.OpCode != OpCodes.Ret:
ilp.Replace(ins, ilp.Create(OpCodes.Ret));
modified = true;
break;
}
}
}
if (modified)
{
string tempFilePath = Path.GetTempFileName();
bkp?.Add(unityPath);
unityAsmDef.Write(tempFilePath);
File.Delete(unityPath);
File.Move(tempFilePath, unityPath);
}
endPatchCoreModule:
#endregion Insert patch into UnityEngine.CoreModule.dll
#if BeatSaber
Logging.Logger.Injector.Debug("Ensuring anti-yeet patch exists");
var name = SelfConfig.GameAssemblies_.FirstOrDefault() ?? SelfConfig.GetDefaultGameAssemblies().First();
var ascPath = Path.Combine(managedPath, name);
try
{
using var ascAsmDef = AssemblyDefinition.ReadAssembly(ascPath, readerParameters);
var ascModDef = ascAsmDef.MainModule;
var deleter = ascModDef.GetType("IPAPluginsDirDeleter");
if (deleter.Methods.Count > 0)
{
deleter.Methods.Clear(); // delete all methods
string tempFilePath = Path.GetTempFileName();
bkp?.Add(ascPath);
ascAsmDef.Write(tempFilePath);
File.Delete(ascPath);
File.Move(tempFilePath, ascPath);
}
}
catch (Exception e)
{
Logging.Logger.Injector.Warn($"Could not apply anti-yeet patch to {ascPath}");
if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
Logging.Logger.Injector.Warn(e);
}
#endif
sw.Stop();
Logging.Logger.Injector.Info($"Installing bootstrapper took {sw.Elapsed}");
}
private static bool bootstrapped;
private static void CreateBootstrapper()
{
if (bootstrapped) return;
bootstrapped = true;
Application.logMessageReceivedThreaded += delegate (string condition, string stackTrace, LogType type)
{
var level = UnityLogRedirector.LogTypeToLevel(type);
UnityLogProvider.UnityLogger.Log(level, $"{condition}");
UnityLogProvider.UnityLogger.Log(level, $"{stackTrace}");
};
StdoutInterceptor.EnsureHarmonyLogging();
// need to reinit streams singe Unity seems to redirect stdout
StdoutInterceptor.RedirectConsole();
var bootstrapper = new GameObject("NonDestructiveBootstrapper").AddComponent<Bootstrapper>();
bootstrapper.Destroyed += Bootstrapper_Destroyed;
}
private static bool loadingDone;
private static void Bootstrapper_Destroyed()
{
// wait for plugins to finish loading
pluginAsyncLoadTask?.Wait();
permissionFixTask?.Wait();
Default.Debug("Plugins loaded");
Default.Debug(string.Join(", ", PluginLoader.PluginsMetadata.StrJP()));
_ = PluginComponent.Create();
}
}
}
#nullable enable
using IPA.AntiMalware;
using IPA.Config;
using IPA.Injector.Backups;
using IPA.Loader;
using IPA.Logging;
using IPA.Utilities;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using static IPA.Logging.Logger;
using MethodAttributes = Mono.Cecil.MethodAttributes;
#if NET3
using Net3_Proxy;
using Path = Net3_Proxy.Path;
using File = Net3_Proxy.File;
using Directory = Net3_Proxy.Directory;
#endif
namespace IPA.Injector
{
/// <summary>
/// The entry point type for BSIPA's Doorstop injector.
/// </summary>
// ReSharper disable once UnusedMember.Global
internal static class Injector
{
private static Task? pluginAsyncLoadTask;
private static Task? permissionFixTask;
// ReSharper disable once UnusedParameter.Global
internal static void Main(string[] args)
{ // entry point for doorstop
// At this point, literally nothing but mscorlib is loaded,
// and since this class doesn't have any static fields that
// aren't defined in mscorlib, we can control exactly what
// gets loaded.
_ = args;
try
{
var arguments = Environment.GetCommandLineArgs();
MaybeInitializeConsole(arguments);
SetupLibraryLoading();
EnsureDirectories();
// this is weird, but it prevents Mono from having issues loading the type.
// IMPORTANT: NO CALLS TO ANY LOGGER CAN HAPPEN BEFORE THIS
var unused = StandardLogger.PrintFilter;
#region // Above hack explanation
/*
* Due to an unknown bug in the version of Mono that Unity uses, if the first access to StandardLogger
* is a call to a constructor, then Mono fails to load the type correctly. However, if the first access is to
* the above static property (or maybe any, but I don't really know) it behaves as expected and works fine.
*/
#endregion
Default.Debug("Initializing logger");
SelfConfig.ReadCommandLine(arguments);
SelfConfig.Load();
DisabledConfig.Load();
if (AntiPiracy.IsInvalid(Environment.CurrentDirectory))
{
Default.Error("Invalid installation; please buy the game to run BSIPA.");
return;
}
CriticalSection.Configure();
Logging.Logger.Injector.Debug("Prepping bootstrapper");
// make sure to load the game version and check boundaries before installing the bootstrap, because that uses the game assemblies property
GameVersionEarly.Load();
SelfConfig.Instance.CheckVersionBoundary();
// updates backup
InstallBootstrapPatch();
AntiMalwareEngine.Initialize();
Updates.InstallPendingUpdates();
Loader.LibLoader.SetupAssemblyFilenames(true);
pluginAsyncLoadTask = PluginLoader.LoadTask();
permissionFixTask = PermissionFix.FixPermissions(new DirectoryInfo(Environment.CurrentDirectory));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static void MaybeInitializeConsole(string[] arguments)
{
var i = 0;
while (i < arguments.Length)
{
if (arguments[i++] == "--verbose")
{
if (i == arguments.Length)
{
WinConsole.Initialize(WinConsole.AttachParent);
return;
}
WinConsole.Initialize(int.TryParse(arguments[i], out int processId) ? processId : WinConsole.AttachParent);
return;
}
}
}
private static void EnsureDirectories()
{
string path;
if (!Directory.Exists(path = Path.Combine(Environment.CurrentDirectory, "UserData")))
_ = Directory.CreateDirectory(path);
if (!Directory.Exists(path = Path.Combine(Environment.CurrentDirectory, "Plugins")))
_ = Directory.CreateDirectory(path);
}
private static void SetupLibraryLoading()
{
if (loadingDone) return;
loadingDone = true;
Loader.LibLoader.Configure();
}
private static void InstallBootstrapPatch()
{
var sw = Stopwatch.StartNew();
var cAsmName = Assembly.GetExecutingAssembly().GetName();
var managedPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var dataDir = new DirectoryInfo(managedPath).Parent!.Name;
var gameName = dataDir.Substring(0, dataDir.Length - 5);
Logging.Logger.Injector.Debug("Finding backup");
var backupPath = Path.Combine(Environment.CurrentDirectory, "IPA", "Backups", gameName);
var bkp = BackupManager.FindLatestBackup(backupPath);
if (bkp == null)
Logging.Logger.Injector.Warn("No backup found! Was BSIPA installed using the installer?");
// TODO: Investigate if this ever worked properly.
// this is a critical section because if you exit in here, assembly can die
using var critSec = CriticalSection.ExecuteSection();
var readerParameters = new ReaderParameters
{
ReadWrite = false,
InMemory = true,
ReadingMode = ReadingMode.Immediate
};
Logging.Logger.Injector.Debug("Ensuring patch on UnityEngine.CoreModule exists");
#region Insert patch into UnityEngine.CoreModule.dll
var unityPath = Path.Combine(managedPath, "UnityEngine.CoreModule.dll");
using var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath, readerParameters);
var unityModDef = unityAsmDef.MainModule;
bool modified = false;
foreach (var asmref in unityModDef.AssemblyReferences)
{
if (asmref.Name == cAsmName.Name)
{
if (asmref.Version != cAsmName.Version)
{
asmref.Version = cAsmName.Version;
modified = true;
}
}
}
var application = unityModDef.GetType("UnityEngine", "Camera");
if (application == null)
{
Logging.Logger.Injector.Critical("UnityEngine.CoreModule doesn't have a definition for UnityEngine.Camera!"
+ "Nothing to patch to get ourselves into the Unity run cycle!");
goto endPatchCoreModule;
}
MethodDefinition? cctor = null;
foreach (var m in application.Methods)
if (m.IsRuntimeSpecialName && m.Name == ".cctor")
cctor = m;
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++)
{
var ins = cctor.Body.Instructions[i];
switch (i)
{
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;
}
case 1 when ins.OpCode != OpCodes.Ret:
ilp.Replace(ins, ilp.Create(OpCodes.Ret));
modified = true;
break;
}
}
}
if (modified)
{
string tempFilePath = Path.GetTempFileName();
bkp?.Add(unityPath);
unityAsmDef.Write(tempFilePath);
File.Delete(unityPath);
File.Move(tempFilePath, unityPath);
}
endPatchCoreModule:
#endregion Insert patch into UnityEngine.CoreModule.dll
#if BeatSaber
Logging.Logger.Injector.Debug("Ensuring anti-yeet patch exists");
var name = SelfConfig.GameAssemblies_.FirstOrDefault() ?? SelfConfig.GetDefaultGameAssemblies().First();
var ascPath = Path.Combine(managedPath, name);
try
{
using var ascAsmDef = AssemblyDefinition.ReadAssembly(ascPath, readerParameters);
var ascModDef = ascAsmDef.MainModule;
var deleter = ascModDef.GetType("IPAPluginsDirDeleter");
if (deleter.Methods.Count > 0)
{
deleter.Methods.Clear(); // delete all methods
string tempFilePath = Path.GetTempFileName();
bkp?.Add(ascPath);
ascAsmDef.Write(tempFilePath);
File.Delete(ascPath);
File.Move(tempFilePath, ascPath);
}
}
catch (Exception e)
{
Logging.Logger.Injector.Warn($"Could not apply anti-yeet patch to {ascPath}");
if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
Logging.Logger.Injector.Warn(e);
}
#endif
sw.Stop();
Logging.Logger.Injector.Info($"Installing bootstrapper took {sw.Elapsed}");
}
private static bool bootstrapped;
private static void CreateBootstrapper()
{
if (bootstrapped) return;
bootstrapped = true;
Application.logMessageReceivedThreaded += delegate (string condition, string stackTrace, LogType type)
{
var level = UnityLogRedirector.LogTypeToLevel(type);
UnityLogProvider.UnityLogger.Log(level, $"{condition}");
UnityLogProvider.UnityLogger.Log(level, $"{stackTrace}");
};
StdoutInterceptor.EnsureHarmonyLogging();
// need to reinit streams singe Unity seems to redirect stdout
StdoutInterceptor.RedirectConsole();
var bootstrapper = new GameObject("NonDestructiveBootstrapper").AddComponent<Bootstrapper>();
bootstrapper.Destroyed += Bootstrapper_Destroyed;
}
private static bool loadingDone;
private static void Bootstrapper_Destroyed()
{
// wait for plugins to finish loading
pluginAsyncLoadTask?.Wait();
permissionFixTask?.Wait();
Default.Debug("Plugins loaded");
Default.Debug(string.Join(", ", PluginLoader.PluginsMetadata.StrJP()));
_ = PluginComponent.Create();
}
}
}

+ 63
- 94
IPA.Loader/Config/Providers/JsonConfigProvider.cs View File

@ -1,15 +1,13 @@
using IPA.Config.Data;
#nullable enable
using IPA.Config.Data;
using IPA.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using Boolean = IPA.Config.Data.Boolean;
using System.Threading.Tasks;
namespace IPA.Config.Providers
{
@ -22,20 +20,14 @@ namespace IPA.Config.Providers
public string Extension => "json";
public Value Load(FileInfo file)
public Value? Load(FileInfo file)
{
if (!file.Exists) return Value.Null();
try
{
JToken jtok;
using (var sreader = new StreamReader(file.OpenRead()))
{
using var jreader = new JsonTextReader(sreader);
jtok = JToken.ReadFrom(jreader);
}
return VisitToValue(jtok);
using var fileStream = file.OpenRead();
return VisitToValue(JsonNode.Parse(fileStream));
}
catch (Exception e)
{
@ -45,73 +37,46 @@ namespace IPA.Config.Providers
}
}
private Value VisitToValue(JToken tok)
private Value? VisitToValue(JsonNode? node)
{
if (tok == null) return Value.Null();
if (node == null) return Value.Null();
switch (tok.Type)
switch (node.GetValueKind())
{
case JTokenType.Raw: // idk if the parser will normally emit a Raw type, but just to be safe
return VisitToValue(JToken.Parse((tok as JRaw).Value as string));
case JTokenType.Undefined:
Logger.Config.Warn("Found JTokenType.Undefined");
goto case JTokenType.Null;
case JTokenType.Bytes: // never used by Newtonsoft
Logger.Config.Warn("Found JTokenType.Bytes");
goto case JTokenType.Null;
case JTokenType.Comment: // never used by Newtonsoft
Logger.Config.Warn("Found JTokenType.Comment");
goto case JTokenType.Null;
case JTokenType.Constructor: // never used by Newtonsoft
Logger.Config.Warn("Found JTokenType.Constructor");
goto case JTokenType.Null;
case JTokenType.Property: // never used by Newtonsoft
Logger.Config.Warn("Found JTokenType.Property");
goto case JTokenType.Null;
case JTokenType.Null:
case JsonValueKind.Undefined:
Logger.Config.Warn($"Found {nameof(JsonValueKind)}.{nameof(JsonValueKind.Undefined)}");
goto case JsonValueKind.Null;
case JsonValueKind.Null:
return Value.Null();
case JTokenType.Boolean:
return Value.Bool(((tok as JValue).Value as bool?) ?? false);
case JTokenType.String:
var val = (tok as JValue).Value;
if (val is string s) return Value.Text(s);
else if (val is char c) return Value.Text("" + c);
else return Value.Text(string.Empty);
case JTokenType.Integer:
val = (tok as JValue).Value;
if (val is long l) return Value.Integer(l);
else if (val is ulong u) return Value.Integer((long)u);
else return Value.Integer(0);
case JTokenType.Float:
val = (tok as JValue).Value;
if (val is decimal dec) return Value.Float(dec);
else if (val is double dou) return Value.Float((decimal)dou);
else if (val is float flo) return Value.Float((decimal)flo);
else return Value.Float(0); // default to 0 if something breaks
case JTokenType.Date:
val = (tok as JValue).Value;
if (val is DateTime dt) return Value.Text(dt.ToString());
else if (val is DateTimeOffset dto) return Value.Text(dto.ToString());
else return Value.Text("Unknown Date-type token");
case JTokenType.TimeSpan:
val = (tok as JValue).Value;
if (val is TimeSpan ts) return Value.Text(ts.ToString());
else return Value.Text("Unknown TimeSpan-type token");
case JTokenType.Guid:
val = (tok as JValue).Value;
if (val is Guid g) return Value.Text(g.ToString());
else return Value.Text("Unknown Guid-type token");
case JTokenType.Uri:
val = (tok as JValue).Value;
if (val is Uri ur) return Value.Text(ur.ToString());
else return Value.Text("Unknown Uri-type token");
case JTokenType.Array:
return Value.From((tok as JArray).Select(VisitToValue));
case JTokenType.Object:
return Value.From((tok as IEnumerable<KeyValuePair<string, JToken>>)
.Select(kvp => new KeyValuePair<string, Value>(kvp.Key, VisitToValue(kvp.Value))));
case JsonValueKind.True:
case JsonValueKind.False:
if (node.AsValue().TryGetValue<bool>(out var b))
return Value.Bool(b);
return Value.Bool(false);
case JsonValueKind.String:
if (node.AsValue().TryGetValue<string>(out var s))
return Value.Text(s);
return Value.Text(string.Empty);
case JsonValueKind.Number:
var value = node.AsValue();
if (value.TryGetValue<long>(out var l))
return Value.Integer(l);
if (value.TryGetValue<ulong>(out var u))
return Value.Integer((long)u);
if (value.TryGetValue<decimal>(out var dec))
return Value.Float(dec);
if (value.TryGetValue<double>(out var dou))
return Value.Float((decimal)dou);
if (value.TryGetValue<float>(out var flo))
return Value.Float((decimal)flo);
return Value.Float(0); // default to 0 if something breaks
case JsonValueKind.Array:
return Value.From(node.AsArray().Select(VisitToValue));
case JsonValueKind.Object:
return Value.From(node.AsObject()
.Select(kvp => new KeyValuePair<string, Value?>(kvp.Key, VisitToValue(kvp.Value))));
default:
throw new ArgumentException($"Unknown {nameof(JTokenType)} in parameter");
throw new ArgumentException($"Unknown {nameof(JsonValueKind)} in parameter");
}
}
@ -122,14 +87,18 @@ namespace IPA.Config.Providers
try
{
var tok = VisitToToken(value);
var jsonNode = VisitToNode(value);
using var fileStream = file.Open(FileMode.Create, FileAccess.Write);
using var jsonWriter = new Utf8JsonWriter(fileStream, new JsonWriterOptions { Indented = true });
using var swriter = new StreamWriter(file.Open(FileMode.Create, FileAccess.Write));
using var jwriter = new JsonTextWriter(swriter)
if (jsonNode == null)
{
Formatting = Formatting.Indented
};
tok.WriteTo(jwriter);
jsonWriter.WriteNullValue();
}
else
{
jsonNode.WriteTo(jsonWriter);
}
}
catch (Exception e)
{
@ -138,28 +107,28 @@ namespace IPA.Config.Providers
}
}
private JToken VisitToToken(Value val)
{
private JsonNode? VisitToNode(Value? val)
{
switch (val)
{
case Text t:
return new JValue(t.Value);
return JsonValue.Create(t.Value);
case Boolean b:
return new JValue(b.Value);
return JsonValue.Create(b.Value);
case Integer i:
return new JValue(i.Value);
return JsonValue.Create(i.Value);
case FloatingPoint f:
return new JValue(f.Value);
return JsonValue.Create(f.Value);
case List l:
var jarr = new JArray();
foreach (var tok in l.Select(VisitToToken)) jarr.Add(tok);
var jarr = new JsonArray();
foreach (var tok in l.Select(VisitToNode)) jarr.Add(tok);
return jarr;
case Map m:
var jobj = new JObject();
foreach (var kvp in m) jobj.Add(kvp.Key, VisitToToken(kvp.Value));
var jobj = new JsonObject();
foreach (var kvp in m) jobj.Add(kvp.Key, VisitToNode(kvp.Value));
return jobj;
case null:
return JValue.CreateNull();
return null;
default:
throw new ArgumentException($"Unsupported subtype of {nameof(Value)}");
}


+ 1
- 1
IPA.Loader/Config/SelfConfig.cs View File

@ -5,8 +5,8 @@ using IPA.Config.Stores;
using IPA.Config.Stores.Attributes;
using IPA.Config.Stores.Converters;
// END: section ignore
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace IPA.Config
{


+ 3
- 9
IPA.Loader/IPA.Loader.csproj View File

@ -48,17 +48,11 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Ionic.Zip" Version="1.9.1.8" />
<!--<PackageReference Include="Lib.Harmony" Version="2.0.2" />-->
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<!--<PackageReference Include="AsyncBridge" Version="0.3.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0">
<ExcludeAssets>buildtransitive</ExcludeAssets>
</PackageReference>-->
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
<PackageReference Include="HarmonyX" Version="2.10.2" />
<PackageReference Include="Hive.Versioning.Standalone" Version="0.1.0-gh846.1" />
<PackageReference Include="Ionic.Zip" Version="1.9.1.8" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
<ProjectReference Include="..\SemVer\SemVer.csproj" />
</ItemGroup>


+ 7
- 10
IPA.Loader/JsonConverters/AlmostVersionConverter.cs View File

@ -1,22 +1,19 @@
using IPA.Utilities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace IPA.JsonConverters
{
internal class AlmostVersionConverter : JsonConverter<AlmostVersion>
{
public override AlmostVersion ReadJson(JsonReader reader, Type objectType, AlmostVersion existingValue, bool hasExistingValue, JsonSerializer serializer) =>
reader.Value == null ? null : new AlmostVersion(reader.Value as string);
public override AlmostVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType == JsonTokenType.Null ? null : new AlmostVersion(reader.GetString());
public override void WriteJson(JsonWriter writer, AlmostVersion value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, AlmostVersion value, JsonSerializerOptions options)
{
if (value == null) writer.WriteNull();
else writer.WriteValue(value.ToString());
if (value == null) writer.WriteNullValue();
else writer.WriteStringValue(value.ToString());
}
}
}

+ 19
- 16
IPA.Loader/JsonConverters/FeaturesFieldConverter.cs View File

@ -1,14 +1,15 @@
using IPA.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace IPA.JsonConverters
{
internal class FeaturesFieldConverter : JsonConverter<Dictionary<string, List<JObject>>>
internal class FeaturesFieldConverter : JsonConverter<Dictionary<string, List<JsonObject>>>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Assert([DoesNotReturnIf(false)] bool condition)
@ -17,26 +18,28 @@ namespace IPA.JsonConverters
throw new InvalidOperationException();
}
public override Dictionary<string, List<JObject>> ReadJson(JsonReader reader, Type objectType, Dictionary<string, List<JObject>> existingValue, bool hasExistingValue, JsonSerializer serializer)
public override Dictionary<string, List<JsonObject>> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonToken.StartArray)
if (reader.TokenType == JsonTokenType.StartArray)
{
_ = serializer.Deserialize<string[]>(reader);
// TODO: Why?
_ = JsonSerializer.Deserialize<string[]>(ref reader, options);
Logger.Features.Warn("Encountered old features used. They no longer do anything, please move to the new format.");
return existingValue;
// TODO: Is there an alternative to existingValue?
return null;
}
var dict = new Dictionary<string, List<JObject>>();
Assert(reader.TokenType == JsonToken.StartObject && reader.Read());
var dict = new Dictionary<string, List<JsonObject>>();
Assert(reader.TokenType == JsonTokenType.StartObject && reader.Read());
while (reader.TokenType == JsonToken.PropertyName)
while (reader.TokenType == JsonTokenType.PropertyName)
{
var name = (string)reader.Value;
var name = reader.GetString();
Assert(reader.Read());
var list = reader.TokenType == JsonToken.StartObject
? (new() { serializer.Deserialize<JObject>(reader) })
: serializer.Deserialize<List<JObject>>(reader);
var list = reader.TokenType == JsonTokenType.StartObject
? (new() { JsonSerializer.Deserialize<JsonObject>(ref reader, options) })
: JsonSerializer.Deserialize<List<JsonObject>>(ref reader, options);
dict.Add(name, list);
Assert(reader.Read());
@ -45,9 +48,9 @@ namespace IPA.JsonConverters
return dict;
}
public override void WriteJson(JsonWriter writer, Dictionary<string, List<JObject>> value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Dictionary<string, List<JsonObject>> value, JsonSerializerOptions options)
{
serializer.Serialize(writer, value);
JsonSerializer.Serialize(writer, value, options);
}
}
}

+ 11
- 14
IPA.Loader/JsonConverters/MultilineStringConverter.cs View File

@ -1,32 +1,29 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace IPA.JsonConverters
{
internal class MultilineStringConverter : JsonConverter<string>
{
public override string ReadJson(JsonReader reader, Type objectType, string existingValue, bool hasExistingValue, JsonSerializer serializer)
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonToken.StartArray)
if (reader.TokenType == JsonTokenType.StartArray)
{
var list = serializer.Deserialize<string[]>(reader);
var list = JsonSerializer.Deserialize<string[]>(ref reader, options);
return string.Join("\n", list);
}
else
return reader.Value as string;
return reader.GetString();
}
public override void WriteJson(JsonWriter writer, string value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
var list = value.Split('\n');
if (list.Length == 1)
serializer.Serialize(writer, value);
writer.WriteStringValue(value);
else
serializer.Serialize(writer, list);
JsonSerializer.Serialize(writer, list, options);
}
}
}

+ 7
- 7
IPA.Loader/JsonConverters/SemverRangeConverter.cs View File

@ -1,20 +1,20 @@
#nullable enable
using System;
using System.Runtime.Remoting.Messaging;
using Hive.Versioning;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace IPA.JsonConverters
{
internal class SemverRangeConverter : JsonConverter<VersionRange?>
{
public override VersionRange? ReadJson(JsonReader reader, Type objectType, VersionRange? existingValue, bool hasExistingValue, JsonSerializer serializer)
=> reader.Value is not string s ? existingValue : new VersionRange(s);
public override VersionRange? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType is not JsonTokenType.String ? null : new VersionRange(reader.GetString()!);
public override void WriteJson(JsonWriter writer, VersionRange? value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, VersionRange? value, JsonSerializerOptions options)
{
if (value is null) writer.WriteNull();
else writer.WriteValue(value.ToString());
if (value is null) writer.WriteNullValue();
else writer.WriteStringValue(value.ToString());
}
}
}

+ 7
- 6
IPA.Loader/JsonConverters/SemverVersionConverter.cs View File

@ -1,19 +1,20 @@
#nullable enable
using System;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using Version = Hive.Versioning.Version;
namespace IPA.JsonConverters
{
internal class SemverVersionConverter : JsonConverter<Version?>
{
public override Version? ReadJson(JsonReader reader, Type objectType, Version? existingValue, bool hasExistingValue, JsonSerializer serializer)
=> reader.Value is not string s ? existingValue : new Version(s);
public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType is not JsonTokenType.String ? null : new Version(reader.GetString()!);
public override void WriteJson(JsonWriter writer, Version? value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Version? value, JsonSerializerOptions options)
{
if (value == null) writer.WriteNull();
else writer.WriteValue(value.ToString());
if (value is null) writer.WriteNullValue();
else writer.WriteStringValue(value.ToString());
}
}
}

+ 8
- 6
IPA.Loader/Loader/Features/ConfigProviderFeature.cs View File

@ -1,8 +1,9 @@
#nullable enable
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace IPA.Loader.Features
{
@ -10,16 +11,17 @@ namespace IPA.Loader.Features
{
private class DataModel
{
[JsonProperty("type", Required = Required.Always)]
public string TypeName = "";
[JsonPropertyName("type")]
[JsonRequired]
public string TypeName { get; init; } = "";
}
protected override bool Initialize(PluginMetadata meta, JObject featureData)
protected override bool Initialize(PluginMetadata meta, JsonObject featureData)
{
DataModel data;
try
{
data = featureData.ToObject<DataModel>() ?? throw new InvalidOperationException("Feature data is null");
data = featureData.Deserialize<DataModel>() ?? throw new InvalidOperationException("Feature data is null");
}
catch (Exception e)
{


+ 12
- 8
IPA.Loader/Loader/Features/DefineFeature.cs View File

@ -1,9 +1,10 @@
#nullable enable
using IPA.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace IPA.Loader.Features
{
@ -13,23 +14,26 @@ namespace IPA.Loader.Features
private class DataModel
{
[JsonProperty("type", Required = Required.Always)]
public string TypeName = "";
[JsonProperty("name", Required = Required.DisallowNull)]
public string? ActualName = null;
[JsonPropertyName("type")]
[JsonRequired]
public string TypeName { get; init; } = "";
[JsonPropertyName("name")]
// TODO: Originally DisallowNull
public string? ActualName { get; init; }
public string Name => ActualName ?? TypeName;
}
private DataModel data = null!;
protected override bool Initialize(PluginMetadata meta, JObject featureData)
protected override bool Initialize(PluginMetadata meta, JsonObject featureData)
{
Logger.Features.Debug("Executing DefineFeature Init");
try
{
data = featureData.ToObject<DataModel>() ?? throw new InvalidOperationException("Feature data is null");
data = featureData.Deserialize<DataModel>() ?? throw new InvalidOperationException("Feature data is null");
}
catch (Exception e)
{


+ 5
- 6
IPA.Loader/Loader/Features/Feature.cs View File

@ -1,8 +1,7 @@
#nullable enable
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
#if NET3
using Net3_Proxy;
#endif
@ -27,7 +26,7 @@ namespace IPA.Loader.Features
/// <param name="meta">the metadata of the plugin that is being prepared</param>
/// <param name="featureData">the data provided with the feature</param>
/// <returns><see langword="true"/> if the feature is valid for the plugin, <see langword="false"/> otherwise</returns>
protected abstract bool Initialize(PluginMetadata meta, JObject featureData);
protected abstract bool Initialize(PluginMetadata meta, JsonObject featureData);
/// <summary>
/// The message to be logged when the feature is not valid for a plugin.
@ -116,7 +115,7 @@ namespace IPA.Loader.Features
private class EmptyFeature : Feature
{
protected override bool Initialize(PluginMetadata meta, JObject featureData)
protected override bool Initialize(PluginMetadata meta, JsonObject featureData)
{
throw new NotImplementedException();
}
@ -128,9 +127,9 @@ namespace IPA.Loader.Features
{
public readonly PluginMetadata AppliedTo;
public readonly string Name;
public readonly JObject Data;
public readonly JsonObject Data;
public Instance(PluginMetadata appliedTo, string name, JObject data)
public Instance(PluginMetadata appliedTo, string name, JsonObject data)
{
AppliedTo = appliedTo;
Name = name;


+ 234
- 237
IPA.Loader/Loader/LibLoader.cs View File

@ -1,237 +1,234 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Linq;
using IPA.Logging;
using IPA.Utilities;
using Mono.Cecil;
using IPA.AntiMalware;
using IPA.Config;
#if NET3
using Net3_Proxy;
using Directory = Net3_Proxy.Directory;
using Path = Net3_Proxy.Path;
using File = Net3_Proxy.File;
#endif
namespace IPA.Loader
{
internal class CecilLibLoader : BaseAssemblyResolver
{
private static readonly string CurrentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
private static readonly string CurrentAssemblyPath = Assembly.GetExecutingAssembly().Location;
public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
{
LibLoader.SetupAssemblyFilenames();
if (name.Name == CurrentAssemblyName)
return AssemblyDefinition.ReadAssembly(CurrentAssemblyPath, parameters);
if (LibLoader.FilenameLocations.TryGetValue($"{name.Name}.dll", out var path))
{
if (File.Exists(path))
return AssemblyDefinition.ReadAssembly(path, parameters);
}
else if (LibLoader.FilenameLocations.TryGetValue($"{name.Name}.{name.Version}.dll", out path))
{
if (File.Exists(path))
return AssemblyDefinition.ReadAssembly(path, parameters);
}
return base.Resolve(name, parameters);
}
}
internal static class LibLoader
{
internal static string LibraryPath => Path.Combine(Environment.CurrentDirectory, "Libs");
internal static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
internal static Dictionary<string, string> FilenameLocations = null!;
internal static void Configure()
{
SetupAssemblyFilenames(true);
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyLibLoader;
AppDomain.CurrentDomain.AssemblyResolve += AssemblyLibLoader;
}
internal static void SetupAssemblyFilenames(bool force = false)
{
if (FilenameLocations == null || force)
{
FilenameLocations = new Dictionary<string, string>();
foreach (var fn in TraverseTree(LibraryPath, s => s != NativeLibraryPath))
{
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);
}
static void AddDir(string path)
{
var pathEnvironmentVariable = Environment.GetEnvironmentVariable("Path");
Environment.SetEnvironmentVariable("Path", path + Path.PathSeparator + pathEnvironmentVariable);
}
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"));
// TODO: find a way to either safely remove Newtonsoft, or switch to a different JSON lib
_ = LoadLibrary(new AssemblyName("Newtonsoft.Json, Version=12.0.0.0, Culture=neutral"));
}
}
public static Assembly? AssemblyLibLoader(object source, ResolveEventArgs e)
{
var asmName = new AssemblyName(e.Name);
return LoadLibrary(asmName);
}
internal static Assembly? LoadLibrary(AssemblyName asmName)
{
Log(Logger.Level.Debug, $"Resolving library {asmName}");
SetupAssemblyFilenames();
var testFile = $"{asmName.Name}.dll";
Log(Logger.Level.Debug, $"Looking for file {asmName.Name}.dll");
if (FilenameLocations.TryGetValue(testFile, out var path))
{
Log(Logger.Level.Debug, $"Found file {testFile} as {path}");
return LoadSafe(path);
}
else if (FilenameLocations.TryGetValue(testFile = $"{asmName.Name}.{asmName.Version}.dll", out path))
{
Log(Logger.Level.Debug, $"Found file {testFile} as {path}");
Log(Logger.Level.Warning, $"File {testFile} should be renamed to just {asmName.Name}.dll");
return LoadSafe(path);
}
Log(Logger.Level.Critical, $"No library {asmName} found");
return null;
}
private static Assembly? LoadSafe(string path)
{
if (!File.Exists(path))
{
Log(Logger.Level.Critical, $"{path} no longer exists!");
return null;
}
if (AntiMalwareEngine.IsInitialized)
{
var result = AntiMalwareEngine.Engine.ScanFile(new FileInfo(path));
if (result is ScanResult.Detected)
{
Log(Logger.Level.Error, $"Scan of '{path}' found malware; not loading");
return null;
}
if (!SelfConfig.AntiMalware_.RunPartialThreatCode_ && result is not ScanResult.KnownSafe and not ScanResult.NotDetected)
{
Log(Logger.Level.Error, $"Scan of '{path}' found partial threat; not loading. To load this, enable AntiMalware.RunPartialThreatCode in the config.");
return null;
}
}
return Assembly.LoadFrom(path);
}
internal static void Log(Logger.Level lvl, string message)
{ // multiple proxy methods to delay loading of assemblies until it's done
if (Logger.LogCreated)
{
AssemblyLibLoaderCallLogger(lvl, message);
}
else
{
if (((byte)lvl & (byte)StandardLogger.PrintFilter) != 0)
Console.WriteLine($"[{lvl}] {message}");
}
}
internal static void Log(Logger.Level lvl, Exception message)
{ // multiple proxy methods to delay loading of assemblies until it's done
if (Logger.LogCreated)
{
AssemblyLibLoaderCallLogger(lvl, message);
}
else
{
if (((byte)lvl & (byte)StandardLogger.PrintFilter) != 0)
Console.WriteLine($"[{lvl}] {message}");
}
}
private static void AssemblyLibLoaderCallLogger(Logger.Level lvl, string message) => Logger.LibLoader.Log(lvl, message);
private static void AssemblyLibLoaderCallLogger(Logger.Level lvl, Exception message) => Logger.LibLoader.Log(lvl, message);
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree
private static IEnumerable<FileInfo> TraverseTree(string root, Func<string, bool>? dirValidator = null)
{
if (dirValidator == null) dirValidator = s => true;
var dirs = new Stack<string>(32);
if (!Directory.Exists(root))
throw new ArgumentException("Directory does not exist", nameof(root));
dirs.Push(root);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs;
try
{
subDirs = Directory.GetDirectories(currentDir);
}
catch (UnauthorizedAccessException)
{ continue; }
catch (DirectoryNotFoundException)
{ continue; }
string[] files;
try
{
files = Directory.GetFiles(currentDir);
}
catch (UnauthorizedAccessException)
{ continue; }
catch (DirectoryNotFoundException)
{ continue; }
foreach (string str in subDirs)
if (dirValidator(str)) dirs.Push(str);
foreach (string file in files)
{
FileInfo nextValue;
try
{
nextValue = new FileInfo(file);
}
catch (FileNotFoundException)
{ continue; }
yield return nextValue;
}
}
}
}
}
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Linq;
using IPA.Logging;
using IPA.Utilities;
using Mono.Cecil;
using IPA.AntiMalware;
using IPA.Config;
#if NET3
using Net3_Proxy;
using Directory = Net3_Proxy.Directory;
using Path = Net3_Proxy.Path;
using File = Net3_Proxy.File;
#endif
namespace IPA.Loader
{
internal class CecilLibLoader : BaseAssemblyResolver
{
private static readonly string CurrentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
private static readonly string CurrentAssemblyPath = Assembly.GetExecutingAssembly().Location;
public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
{
LibLoader.SetupAssemblyFilenames();
if (name.Name == CurrentAssemblyName)
return AssemblyDefinition.ReadAssembly(CurrentAssemblyPath, parameters);
if (LibLoader.FilenameLocations.TryGetValue($"{name.Name}.dll", out var path))
{
if (File.Exists(path))
return AssemblyDefinition.ReadAssembly(path, parameters);
}
else if (LibLoader.FilenameLocations.TryGetValue($"{name.Name}.{name.Version}.dll", out path))
{
if (File.Exists(path))
return AssemblyDefinition.ReadAssembly(path, parameters);
}
return base.Resolve(name, parameters);
}
}
internal static class LibLoader
{
internal static string LibraryPath => Path.Combine(Environment.CurrentDirectory, "Libs");
internal static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
internal static Dictionary<string, string> FilenameLocations = null!;
internal static void Configure()
{
SetupAssemblyFilenames(true);
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyLibLoader;
AppDomain.CurrentDomain.AssemblyResolve += AssemblyLibLoader;
}
internal static void SetupAssemblyFilenames(bool force = false)
{
if (FilenameLocations == null || force)
{
FilenameLocations = new Dictionary<string, string>();
foreach (var fn in TraverseTree(LibraryPath, s => s != NativeLibraryPath))
{
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);
}
static void AddDir(string path)
{
var pathEnvironmentVariable = Environment.GetEnvironmentVariable("Path");
Environment.SetEnvironmentVariable("Path", path + Path.PathSeparator + pathEnvironmentVariable);
}
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"));
}
}
public static Assembly? AssemblyLibLoader(object source, ResolveEventArgs e)
{
var asmName = new AssemblyName(e.Name);
return LoadLibrary(asmName);
}
internal static Assembly? LoadLibrary(AssemblyName asmName)
{
Log(Logger.Level.Debug, $"Resolving library {asmName}");
SetupAssemblyFilenames();
var testFile = $"{asmName.Name}.dll";
Log(Logger.Level.Debug, $"Looking for file {asmName.Name}.dll");
if (FilenameLocations.TryGetValue(testFile, out var path))
{
Log(Logger.Level.Debug, $"Found file {testFile} as {path}");
return LoadSafe(path);
}
else if (FilenameLocations.TryGetValue(testFile = $"{asmName.Name}.{asmName.Version}.dll", out path))
{
Log(Logger.Level.Debug, $"Found file {testFile} as {path}");
Log(Logger.Level.Warning, $"File {testFile} should be renamed to just {asmName.Name}.dll");
return LoadSafe(path);
}
Log(Logger.Level.Critical, $"No library {asmName} found");
return null;
}
private static Assembly? LoadSafe(string path)
{
if (!File.Exists(path))
{
Log(Logger.Level.Critical, $"{path} no longer exists!");
return null;
}
if (AntiMalwareEngine.IsInitialized)
{
var result = AntiMalwareEngine.Engine.ScanFile(new FileInfo(path));
if (result is ScanResult.Detected)
{
Log(Logger.Level.Error, $"Scan of '{path}' found malware; not loading");
return null;
}
if (!SelfConfig.AntiMalware_.RunPartialThreatCode_ && result is not ScanResult.KnownSafe and not ScanResult.NotDetected)
{
Log(Logger.Level.Error, $"Scan of '{path}' found partial threat; not loading. To load this, enable AntiMalware.RunPartialThreatCode in the config.");
return null;
}
}
return Assembly.LoadFrom(path);
}
internal static void Log(Logger.Level lvl, string message)
{ // multiple proxy methods to delay loading of assemblies until it's done
if (Logger.LogCreated)
{
AssemblyLibLoaderCallLogger(lvl, message);
}
else
{
if (((byte)lvl & (byte)StandardLogger.PrintFilter) != 0)
Console.WriteLine($"[{lvl}] {message}");
}
}
internal static void Log(Logger.Level lvl, Exception message)
{ // multiple proxy methods to delay loading of assemblies until it's done
if (Logger.LogCreated)
{
AssemblyLibLoaderCallLogger(lvl, message);
}
else
{
if (((byte)lvl & (byte)StandardLogger.PrintFilter) != 0)
Console.WriteLine($"[{lvl}] {message}");
}
}
private static void AssemblyLibLoaderCallLogger(Logger.Level lvl, string message) => Logger.LibLoader.Log(lvl, message);
private static void AssemblyLibLoaderCallLogger(Logger.Level lvl, Exception message) => Logger.LibLoader.Log(lvl, message);
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree
private static IEnumerable<FileInfo> TraverseTree(string root, Func<string, bool>? dirValidator = null)
{
if (dirValidator == null) dirValidator = s => true;
var dirs = new Stack<string>(32);
if (!Directory.Exists(root))
throw new ArgumentException("Directory does not exist", nameof(root));
dirs.Push(root);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs;
try
{
subDirs = Directory.GetDirectories(currentDir);
}
catch (UnauthorizedAccessException)
{ continue; }
catch (DirectoryNotFoundException)
{ continue; }
string[] files;
try
{
files = Directory.GetFiles(currentDir);
}
catch (UnauthorizedAccessException)
{ continue; }
catch (DirectoryNotFoundException)
{ continue; }
foreach (string str in subDirs)
if (dirValidator(str)) dirs.Push(str);
foreach (string file in files)
{
FileInfo nextValue;
try
{
nextValue = new FileInfo(file);
}
catch (FileNotFoundException)
{ continue; }
yield return nextValue;
}
}
}
}
}

+ 21
- 19
IPA.Loader/Loader/PluginLoader.cs View File

@ -4,7 +4,6 @@ using IPA.Loader.Features;
using IPA.Logging;
using IPA.Utilities;
using Mono.Cecil;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
@ -15,6 +14,8 @@ using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using IPA.AntiMalware;
using Hive.Versioning;
using IPA.JsonConverters;
using System.Text.Json;
#if NET4
using Task = System.Threading.Tasks.Task;
using TaskEx = System.Threading.Tasks.Task;
@ -37,13 +38,13 @@ namespace IPA.Loader
internal static PluginMetadata SelfMeta = null!;
internal static Task LoadTask() =>
TaskEx.Run(() =>
TaskEx.Run(async () =>
{
YeetIfNeeded();
var sw = Stopwatch.StartNew();
LoadMetadata();
await LoadMetadata();
sw.Stop();
Logger.Loader.Info($"Loading metadata took {sw.Elapsed}");
@ -83,10 +84,15 @@ namespace IPA.Loader
private static readonly Regex embeddedTextDescriptionPattern = new(@"#!\[(.+)\]", RegexOptions.Compiled | RegexOptions.Singleline);
internal static void LoadMetadata()
private static async Task LoadMetadata()
{
string[] plugins = Directory.GetFiles(UnityGame.PluginsPath, "*.dll");
var options = new JsonSerializerOptions
{
Converters = { new SemverRangeConverter(), new FeaturesFieldConverter() }
};
try
{
var selfMeta = new PluginMetadata
@ -97,14 +103,10 @@ namespace IPA.Loader
IsSelf = true
};
string manifest;
using (var manifestReader =
new StreamReader(
selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
throw new InvalidOperationException()))
manifest = manifestReader.ReadToEnd();
using var manifestStream = selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
throw new InvalidOperationException();
var manifestObj = JsonConvert.DeserializeObject<PluginManifest>(manifest);
var manifestObj = await JsonSerializer.DeserializeAsync<PluginManifest>(manifestStream, options).ConfigureAwait(false);
selfMeta.Manifest = manifestObj ?? throw new InvalidOperationException("Deserialized manifest was null");
PluginsMetadata.Add(selfMeta);
@ -159,11 +161,9 @@ namespace IPA.Loader
pluginNs = embedded.Name.Substring(0, embedded.Name.Length - manifestSuffix.Length);
string manifest;
using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
manifest = manifestReader.ReadToEnd();
using var manifestStream = embedded.GetResourceStream();
pluginManifest = JsonConvert.DeserializeObject<PluginManifest?>(manifest);
pluginManifest = await JsonSerializer.DeserializeAsync<PluginManifest?>(manifestStream, options).ConfigureAwait(false);
break;
}
@ -277,7 +277,8 @@ namespace IPA.Loader
IsBare = true,
};
var manifestObj = JsonConvert.DeserializeObject<PluginManifest>(File.ReadAllText(manifest));
using var manifestStream = File.OpenRead(manifest);
var manifestObj = await JsonSerializer.DeserializeAsync<PluginManifest>(manifestStream, options).ConfigureAwait(false);
if (manifestObj is null)
{
Logger.Loader.Error($"Bare manifest {Path.GetFileName(manifest)} deserialized to null");
@ -329,12 +330,12 @@ namespace IPA.Loader
}
using var reader = new StreamReader(resc.GetResourceStream());
description = reader.ReadToEnd();
description = await reader.ReadToEndAsync().ConfigureAwait(false);
}
else
{
using var descriptionReader = new StreamReader(meta.Assembly.GetManifestResourceStream(name));
description = descriptionReader.ReadToEnd();
description = await descriptionReader.ReadToEndAsync().ConfigureAwait(false);
}
meta.Manifest.Description = description;
@ -809,7 +810,8 @@ namespace IPA.Loader
internal static void InitFeatures()
{
foreach (var meta in PluginsMetadata)
// TODO: Find out why this is null.
foreach (var meta in PluginsMetadata.Where(m => m.Manifest.Features is not null))
{
foreach (var feature in meta.Manifest.Features
.SelectMany(f => f.Value.Select(o => (f.Key, o)))


+ 62
- 41
IPA.Loader/Loader/PluginManifest.cs View File

@ -2,11 +2,10 @@
using Hive.Versioning;
using IPA.JsonConverters;
using IPA.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SemVer;
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using AlmostVersionConverter = IPA.JsonConverters.AlmostVersionConverter;
using Version = Hive.Versioning.Version;
#if NET3
@ -18,69 +17,91 @@ namespace IPA.Loader
{
internal class PluginManifest
{
[JsonProperty("name", Required = Required.Always)]
public string Name = null!;
[JsonPropertyName("name")]
[JsonRequired]
public string Name { get; init; } = null!;
[JsonProperty("id", Required = Required.AllowNull)] // TODO: on major version bump, make this always
public string? Id;
[JsonPropertyName("id")]
[JsonRequired] // TODO: Originally AllowNull
public string? Id { get; set; }
[JsonProperty("description", Required = Required.Always), JsonConverter(typeof(MultilineStringConverter))]
public string Description = null!;
[JsonPropertyName("description")]
[JsonRequired]
[JsonConverter(typeof(MultilineStringConverter))]
public string Description { get; set; } = null!;
[JsonProperty("version", Required = Required.Always), JsonConverter(typeof(SemverVersionConverter))]
public Version Version = null!;
[JsonPropertyName("version")]
[JsonRequired]
[JsonConverter(typeof(SemverVersionConverter))]
public Version Version { get; init; } = null!;
[JsonProperty("gameVersion", Required = Required.DisallowNull), JsonConverter(typeof(AlmostVersionConverter))]
public AlmostVersion? GameVersion;
[JsonPropertyName("gameVersion")]
[JsonRequired] // TODO: Originally DisallowNull
[JsonConverter(typeof(AlmostVersionConverter))]
public AlmostVersion? GameVersion { get; init; }
[JsonProperty("author", Required = Required.Always)]
public string Author = null!;
[JsonPropertyName("author")]
[JsonRequired]
public string Author { get; init; } = null!;
[JsonProperty("dependsOn", Required = Required.DisallowNull, ItemConverterType = typeof(SemverRangeConverter))]
public Dictionary<string, VersionRange> Dependencies = new();
[JsonPropertyName("dependsOn")]
[JsonRequired] // TODO: Originally DisallowNull
public Dictionary<string, VersionRange> Dependencies { get; init; } = new();
[JsonProperty("conflictsWith", Required = Required.DisallowNull, ItemConverterType = typeof(SemverRangeConverter))]
public Dictionary<string, VersionRange> Conflicts = new();
[JsonPropertyName("conflictsWith")]
// TODO: Originally DisallowNull
public Dictionary<string, VersionRange> Conflicts { get; init; } = new();
[JsonProperty("features", Required = Required.DisallowNull), JsonConverter(typeof(FeaturesFieldConverter))]
public Dictionary<string, List<JObject>> Features = new();
[JsonPropertyName("features")]
// TODO: Originally DisallowNull
public Dictionary<string, List<JsonObject>> Features { get; init; } = new();
[JsonProperty("loadBefore", Required = Required.DisallowNull)]
public string[] LoadBefore = Array.Empty<string>();
[JsonPropertyName("loadBefore")]
// TODO: Originally DisallowNull
public string[] LoadBefore { get; init; } = Array.Empty<string>();
[JsonProperty("loadAfter", Required = Required.DisallowNull)]
public string[] LoadAfter = Array.Empty<string>();
[JsonPropertyName("loadAfter")]
// TODO: Originally DisallowNull
public string[] LoadAfter { get; init; } = Array.Empty<string>();
[JsonProperty("icon", Required = Required.DisallowNull)]
public string? IconPath = null;
[JsonPropertyName("icon")]
// TODO: Originally DisallowNull
public string? IconPath { get; init; }
[JsonProperty("files", Required = Required.DisallowNull)]
public string[] Files = Array.Empty<string>();
[JsonPropertyName("files")]
// TODO: Originally DisallowNull
public string[] Files { get; init; } = Array.Empty<string>();
[Serializable]
public class LinksObject
{
[JsonProperty("project-home", Required = Required.DisallowNull)]
public Uri? ProjectHome = null;
[JsonPropertyName("project-home")]
// TODO: Originally DisallowNull
public Uri? ProjectHome { get; init; }
[JsonProperty("project-source", Required = Required.DisallowNull)]
public Uri? ProjectSource = null;
[JsonPropertyName("project-source")]
// TODO: Originally DisallowNull
public Uri? ProjectSource { get; init; }
[JsonProperty("donate", Required = Required.DisallowNull)]
public Uri? Donate = null;
[JsonPropertyName("donate")]
// TODO: Originally DisallowNull
public Uri? Donate { get; init; }
}
[JsonProperty("links", Required = Required.DisallowNull)]
public LinksObject? Links = null;
[JsonPropertyName("links")]
// TODO: Originally DisallowNull
public LinksObject? Links { get; init; }
[Serializable]
public class MiscObject
{
[JsonProperty("plugin-hint", Required = Required.DisallowNull)]
public string? PluginMainHint = null;
[JsonPropertyName("plugin-hint")]
// TODO: Originally DisallowNull
public string? PluginMainHint { get; init; }
}
[JsonProperty("misc", Required = Required.DisallowNull)]
public MiscObject? Misc = null;
[JsonPropertyName("misc")]
// TODO: Originally DisallowNull
public MiscObject? Misc { get; init; }
}
}

+ 1
- 3
IPA.Loader/Updating/BeatMods/ApiEndpoint.cs View File

@ -3,8 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using IPA.JsonConverters;
using IPA.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SemVer;
using Version = SemVer.Version;
@ -133,7 +131,7 @@ namespace IPA.Updating.BeatMods
[JsonProperty("link")]
public Uri Link;
#pragma warning restore CS0649
[Serializable]


+ 10
- 11
IPA.Loader/Updating/BeatMods/Updater.cs View File

@ -15,7 +15,6 @@ using IPA.Loader;
using IPA.Loader.Features;
using IPA.Utilities;
using IPA.Utilities.Async;
using Newtonsoft.Json;
using SemVer;
using UnityEngine;
using UnityEngine.Networking;
@ -268,7 +267,7 @@ namespace IPA.Updating.BeatMods
Logger.updater.Debug($"Phantom Dependency: {dep}");
yield return ResolveDependencyRanges(depList);
foreach (var dep in depList.Value)
Logger.updater.Debug($"Dependency: {dep}");
@ -292,7 +291,7 @@ namespace IPA.Updating.BeatMods
var dep = list.Value[i];
var mod = new Ref<ApiEndpoint.Mod>(null);
yield return GetModInfo(dep.Name, "", mod);
try { mod.Verify(); }
@ -381,12 +380,12 @@ namespace IPA.Updating.BeatMods
.Select(mod => mod.Version).Max(); // (2.1) get the max version
dep.Resolved = ver != null;
if (dep.Resolved) dep.ResolvedVersion = ver; // (2.2)
dep.Has = dep.Resolved && dep.Version == dep.ResolvedVersion;
dep.Has = dep.Resolved && dep.Version == dep.ResolvedVersion;
}
}
internal void CheckDependencies(Ref<List<DependencyObject>> list)
{
{
var toDl = new List<DependencyObject>();
foreach (var dep in list.Value)
@ -432,8 +431,8 @@ namespace IPA.Updating.BeatMods
/// <param name="error"></param>
internal delegate void InstallFailed(DependencyObject obj, Exception error);
internal void StartDownload(IEnumerable<DependencyObject> download, DownloadStart downloadStart = null,
DownloadProgress downloadProgress = null, DownloadFailed downloadFail = null, DownloadFinish downloadFinish = null,
internal void StartDownload(IEnumerable<DependencyObject> download, DownloadStart downloadStart = null,
DownloadProgress downloadProgress = null, DownloadFailed downloadFail = null, DownloadFinish downloadFinish = null,
InstallFailed installFail = null, InstallFinish installFinish = null)
{
foreach (var item in download)
@ -457,7 +456,7 @@ namespace IPA.Updating.BeatMods
yield break;
}
var releaseName = UnityGame.ReleaseType == UnityGame.Release.Steam
var releaseName = UnityGame.ReleaseType == UnityGame.Release.Steam
? ApiEndpoint.Mod.DownloadsObject.TypeSteam : ApiEndpoint.Mod.DownloadsObject.TypeOculus;
var platformFile = mod.Value.Downloads.First(f => f.Type == ApiEndpoint.Mod.DownloadsObject.TypeUniversal || f.Type == releaseName);
@ -658,7 +657,7 @@ namespace IPA.Updating.BeatMods
FileInfo targetFile = new FileInfo(Path.Combine(targetDir, entry.FileName));
Directory.CreateDirectory(targetFile.DirectoryName ?? throw new InvalidOperationException());
if (item.LocalPluginMeta != null &&
if (item.LocalPluginMeta != null &&
Utils.GetRelativePath(targetFile.FullName, targetDir) == Utils.GetRelativePath(item.LocalPluginMeta?.File.FullName, UnityGame.InstallPath))
shouldDeleteOldFile = false; // overwriting old file, no need to delete
@ -676,7 +675,7 @@ namespace IPA.Updating.BeatMods
}
}
}
if (shouldDeleteOldFile && item.LocalPluginMeta != null)
File.AppendAllLines(Path.Combine(targetDir, SpecialDeletionsFile), new[] { Utils.GetRelativePath(item.LocalPluginMeta?.File.FullName, UnityGame.InstallPath) });
}
@ -730,7 +729,7 @@ namespace IPA.Updating.BeatMods
{
}
}
[Serializable]
internal class BeatmodsInterceptException : Exception
{


Loading…
Cancel
Save