using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Decal.Adapter;

internal static class RuntimePolicyHelper
{
	[ComImport]
	[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
	[Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
	private interface ICLRRuntimeInfo
	{
		void xGetVersionString();

		void xGetRuntimeDirectory();

		void xIsLoaded();

		void xIsLoadable();

		void xLoadErrorString();

		void xLoadLibrary();

		void xGetProcAddress();

		void xGetInterface();

		void xSetDefaultStartupFlags();

		void xGetDefaultStartupFlags();

		[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
		void BindAsLegacyV2Runtime();
	}

	public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }

	static RuntimePolicyHelper()
	{
		ICLRRuntimeInfo iCLRRuntimeInfo = (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(Guid.Empty, typeof(ICLRRuntimeInfo).GUID);
		try
		{
			iCLRRuntimeInfo.BindAsLegacyV2Runtime();
			LegacyV2RuntimeEnabledSuccessfully = true;
		}
		catch (COMException)
		{
			LegacyV2RuntimeEnabledSuccessfully = false;
		}
	}
}
