2019-09-04 19:22:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using HarmonyLib;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.RuntimePatches
|
|
|
|
|
{
|
|
|
|
|
public static class RuntimePatcher
|
|
|
|
|
{
|
|
|
|
|
public static void Initialize()
|
|
|
|
|
{
|
2020-06-08 21:21:22 +00:00
|
|
|
|
var envDisableRuntimePatches = Environment.GetEnvironmentVariable("DISABLE_RUNTIMEPATCHES");
|
|
|
|
|
var envDebugRuntimePatches = Environment.GetEnvironmentVariable("DEBUG_RUNTIMEPATCHES");
|
|
|
|
|
|
|
|
|
|
if (envDisableRuntimePatches != "1")
|
2019-09-04 19:22:06 +00:00
|
|
|
|
{
|
2020-06-08 21:21:22 +00:00
|
|
|
|
if (envDebugRuntimePatches == "1")
|
|
|
|
|
{
|
|
|
|
|
RuntimePatchBase.IsDebug = true;
|
|
|
|
|
}
|
|
|
|
|
else if (envDebugRuntimePatches == "0")
|
|
|
|
|
{
|
|
|
|
|
RuntimePatchBase.IsDebug = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
RuntimePatchBase.IsDebug = true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 19:22:06 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ApplyPatches();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Failed to apply runtime patches, attempting to continue normally.\r\n" + ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ApplyPatches()
|
|
|
|
|
{
|
|
|
|
|
var patches = Assembly.GetExecutingAssembly()
|
|
|
|
|
.GetExportedTypes()
|
|
|
|
|
.Where(type => !type.IsAbstract && typeof(RuntimePatchBase).IsAssignableFrom(type))
|
|
|
|
|
.Select(Activator.CreateInstance)
|
|
|
|
|
.Cast<RuntimePatchBase>()
|
|
|
|
|
.Where(patch => patch.ShouldPatch())
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (patches.Any())
|
|
|
|
|
{
|
|
|
|
|
var harmony = new Harmony("tv.sonarr");
|
|
|
|
|
|
|
|
|
|
foreach (var patch in patches)
|
|
|
|
|
{
|
|
|
|
|
patch.Patch(harmony);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|