Decompiled source of VideoReloader v0.1.0

plugins/VideoReloader.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.NET.Common;
using BepInExResoniteShim;
using BepisModSettings.ConfigAttributes;
using BepisResoniteWrapper;
using Elements.Core;
using FrooxEngine;
using NYoutubeDL.Options;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: TargetFramework(".NETCoreApp,Version=v10.0", FrameworkDisplayName = ".NET 10.0")]
[assembly: AssemblyCompany("VideoReloader")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("VideoReloader")]
[assembly: AssemblyTitle("VideoReloader")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace VideoReloader;

[ResonitePlugin("markn2000.VideoReloader", "VideoReloader", "0.1.0", "markn2000", "https://github.com/MarkN2000/VideoReloader")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency("ResoniteModding.BepisModSettings", "1.6.6")]
public sealed class Plugin : BasePlugin
{
	private ConfigEntry<bool> _automaticReload;

	private Action<VideoTextureProvider> _freeAsset;

	private WorldManager? _worldManager;

	private ValueTuple<CookiesBrowser, PreferredVideoResolution>? _lastSettings;

	private volatile bool _ready;

	private long _requestVersion;

	private long _automaticVersion;

	public override void Load()
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Expected O, but got Unknown
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Expected O, but got Unknown
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Expected O, but got Unknown
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Expected O, but got Unknown
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Expected O, but got Unknown
		_freeAsset = CreateFreeAssetDelegate();
		((BasePlugin)this).Config.SaveOnConfigSet = false;
		_automaticReload = ((BasePlugin)this).Config.Bind<bool>("Reloading", "Auto-Reload on Settings Change", true, "Automatically reload videos locally 1 second after changing the cookie browser or Preferred Resolution.");
		((BasePlugin)this).Config.Bind<dummy>("Reloading", "Reload Session Videos (Local)", default(dummy), new ConfigDescription("Reload all videos in the current session locally. Video and audio may briefly stop.", (AcceptableValueBase)null, new object[1] { (object)new ActionConfig((global::System.Delegate)(Action)([CompilerGenerated] () =>
		{
			RequestReload(automatic: false);
		})) }));
		_automaticReload.SettingChanged += new EventHandler(AutomaticReloadChanged);
		if (!File.Exists(((BasePlugin)this).Config.ConfigFilePath))
		{
			((BasePlugin)this).Config.Save();
		}
		Engine current = Engine.Current;
		if (current != null && current.IsReady)
		{
			OnEngineReady();
		}
		else
		{
			ResoniteHooks.OnEngineReady += new Action(OnEngineReady);
		}
	}

	private static Action<VideoTextureProvider> CreateFreeAssetDelegate()
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		return (typeof(VideoTextureProvider).GetMethod("FreeAsset", (BindingFlags)38) ?? throw new MissingMethodException(typeof(VideoTextureProvider).FullName, "FreeAsset")).CreateDelegate<Action<VideoTextureProvider>>();
	}

	private void OnEngineReady()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Expected O, but got Unknown
		ResoniteHooks.OnEngineReady -= new Action(OnEngineReady);
		if (_ready)
		{
			return;
		}
		try
		{
			_worldManager = Engine.Current.WorldManager;
			_worldManager.WorldFocused += InvalidateRequests;
			_lastSettings = null;
			_ready = true;
			Settings.RegisterValueChanges<VideoStreamingServicesSettings>((SettingUpdateHandler<VideoStreamingServicesSettings>)SettingsChanged);
			((BasePlugin)this).Log.LogInfo((object)"Local video reloading enabled.");
		}
		catch (global::System.Exception ex)
		{
			((BasePlugin)this).Unload();
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(49, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Initialization failed; video reloading disabled: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<global::System.Exception>(ex);
			}
			log.LogError(val);
		}
	}

	private void SettingsChanged(VideoStreamingServicesSettings settings)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		if (settings == null)
		{
			_lastSettings = null;
			Interlocked.Increment(ref _automaticVersion);
		}
		else if (RememberSettings(((SyncField<CookiesBrowser>)(object)settings.UseCookiesFromBrowser).Value, ((SyncField<PreferredVideoResolution>)(object)settings.PreferredResolution).Value) && _automaticReload.Value)
		{
			RequestReload(automatic: true);
		}
	}

	private bool RememberSettings(CookiesBrowser cookies, PreferredVideoResolution resolution)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		ValueTuple<CookiesBrowser, PreferredVideoResolution> val = default(ValueTuple<CookiesBrowser, PreferredVideoResolution>);
		val..ctor(cookies, resolution);
		int result;
		if (_lastSettings.HasValue)
		{
			ValueTuple<CookiesBrowser, PreferredVideoResolution> value = _lastSettings.Value;
			ValueTuple<CookiesBrowser, PreferredVideoResolution> val2 = val;
			result = ((value.Item1 != val2.Item1 || value.Item2 != val2.Item2) ? 1 : 0);
		}
		else
		{
			result = 0;
		}
		_lastSettings = val;
		return (byte)result != 0;
	}

	private void AutomaticReloadChanged(object? sender, EventArgs args)
	{
		Interlocked.Increment(ref _automaticVersion);
	}

	private void InvalidateRequests(World _)
	{
		Interlocked.Increment(ref _requestVersion);
	}

	private bool IsCurrentRequest(long request, long automaticVersion, bool automatic)
	{
		if (_ready && request == Volatile.Read(ref _requestVersion))
		{
			if (automatic)
			{
				return automaticVersion == Volatile.Read(ref _automaticVersion);
			}
			return true;
		}
		return false;
	}

	private bool CanRun(World world, long request, long automaticVersion, bool automatic)
	{
		if (IsCurrentRequest(request, automaticVersion, automatic) && (!automatic || _automaticReload.Value) && !world.IsDisposed && !world.IsDestroyed)
		{
			WorldManager? worldManager = _worldManager;
			return ((worldManager != null) ? worldManager.FocusedWorld : null) == world;
		}
		return false;
	}

	private void RequestReload(bool automatic)
	{
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Expected O, but got Unknown
		if (!_ready)
		{
			return;
		}
		WorldManager? worldManager = _worldManager;
		World world = ((worldManager != null) ? worldManager.FocusedWorld : null);
		if (world == null || world.IsDisposed || world.IsDestroyed || WorldExtensions.IsUserspace(world))
		{
			return;
		}
		long request = Interlocked.Increment(ref _requestVersion);
		long automaticVersion = Volatile.Read(ref _automaticVersion);
		Action val = default(Action);
		world.RunSynchronously((Action)delegate
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_0057: Expected O, but got Unknown
			if (CanRun(world, request, automaticVersion, automatic))
			{
				if (automatic)
				{
					World obj = world;
					Action obj2 = val;
					if (obj2 == null)
					{
						Action val2 = delegate
						{
							Reload(world, request, automaticVersion, automatic);
						};
						Action val3 = val2;
						val = val2;
						obj2 = val3;
					}
					obj.RunInSeconds(1f, obj2);
				}
				else
				{
					Reload(world, request, automaticVersion, automatic);
				}
			}
		}, false, (IUpdatable)null, false);
	}

	private void Reload(World world, long request, long automaticVersion, bool automatic)
	{
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Expected O, but got Unknown
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Expected O, but got Unknown
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Expected O, but got Unknown
		if (!CanRun(world, request, automaticVersion, automatic))
		{
			return;
		}
		bool flag = default(bool);
		try
		{
			List<VideoTextureProvider> componentsInChildren = world.RootSlot.GetComponentsInChildren<VideoTextureProvider>((Predicate<VideoTextureProvider>)null, false, true, (Predicate<Slot>)null);
			ManualLogSource log = ((BasePlugin)this).Log;
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(33, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Found ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(componentsInChildren.Count);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" video providers to reload.");
			}
			log.LogInfo(val);
			Enumerator<VideoTextureProvider> enumerator = componentsInChildren.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					VideoTextureProvider video = enumerator.Current;
					((ComponentBase<Component>)(object)video).RunSynchronously((Action)delegate
					{
						//IL_0098: Unknown result type (might be due to invalid IL or missing references)
						//IL_009e: Expected O, but got Unknown
						if (!CanRun(world, request, automaticVersion, automatic) || ((Worker)video).IsRemoved || !((ComponentBase<Component>)(object)video).IsStarted)
						{
							return;
						}
						try
						{
							_freeAsset.Invoke(video);
							((ComponentBase<Component>)(object)video).MarkChangeDirty();
						}
						catch (global::System.Exception ex2)
						{
							ManualLogSource log3 = ((BasePlugin)this).Log;
							bool flag2 = default(bool);
							BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(49, 1, ref flag2);
							if (flag2)
							{
								((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Failed to request a reload for a video provider: ");
								((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<global::System.Exception>(ex2);
							}
							log3.LogError(val3);
						}
					}, false);
				}
			}
			finally
			{
				((global::System.IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
		}
		catch (global::System.Exception ex)
		{
			ManualLogSource log2 = ((BasePlugin)this).Log;
			BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(48, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Failed to enumerate videos or schedule reloads: ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<global::System.Exception>(ex);
			}
			log2.LogError(val2);
		}
	}

	public override bool Unload()
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Expected O, but got Unknown
		bool ready = _ready;
		_ready = false;
		Interlocked.Increment(ref _requestVersion);
		ResoniteHooks.OnEngineReady -= new Action(OnEngineReady);
		if (_automaticReload != null)
		{
			_automaticReload.SettingChanged -= new EventHandler(AutomaticReloadChanged);
		}
		if (_worldManager != null)
		{
			_worldManager.WorldFocused -= InvalidateRequests;
		}
		if (ready)
		{
			Settings.UnregisterValueChanges<VideoStreamingServicesSettings>((SettingUpdateHandler<VideoStreamingServicesSettings>)SettingsChanged);
		}
		return true;
	}
}