Decompiled source of VideoProgress v1.0.0

plugins/ElfDevelop.VideoProgress.dll

Decompiled 14 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace VideoProgress
{
	public struct ClipState
	{
		public bool Valid;

		public bool Local;

		public bool Recording;

		public bool Encoded;

		public bool Received;

		public int ChunksDone;

		public int ChunksTotal;
	}
	public static class ProgressMath
	{
		public static (float Fraction, int Done, int Total) Compute(IEnumerable<ClipState> clips)
		{
			int num = 0;
			int num2 = 0;
			float num3 = 0f;
			foreach (ClipState clip in clips)
			{
				if (clip.Valid)
				{
					num++;
					float num4 = (clip.Local ? ((!clip.Recording && clip.Encoded) ? 1f : 0f) : (clip.Received ? 1f : ((clip.ChunksTotal <= 0) ? 0f : ((float)Math.Min(clip.ChunksDone, clip.ChunksTotal - 1) / (float)clip.ChunksTotal))));
					if (num4 >= 1f)
					{
						num2++;
					}
					num3 += num4;
				}
			}
			if (num != 0)
			{
				return (Fraction: num3 / (float)num, Done: num2, Total: num);
			}
			return (Fraction: 1f, Done: 0, Total: 0);
		}
	}
	[ContentWarningPlugin("ElfDevelop.VideoProgress", "1.0.0", true)]
	[BepInPlugin("ElfDevelop.VideoProgress", "VideoProgress", "1.0.0")]
	public class VideoProgressPlugin : BaseUnityPlugin
	{
		internal static readonly Dictionary<ExtractVideoMachine, VideoHandle> Extracting = new Dictionary<ExtractVideoMachine, VideoHandle>();

		private readonly Dictionary<ExtractVideoMachine, TextMeshProUGUI> labels = new Dictionary<ExtractVideoMachine, TextMeshProUGUI>();

		private float nextUpdate;

		internal static ManualLogSource Logger { get; private set; } = null;

		private static bool Russian
		{
			get
			{
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					Locale selectedLocale = LocalizationSettings.SelectedLocale;
					object obj;
					if (selectedLocale == null)
					{
						obj = null;
					}
					else
					{
						LocaleIdentifier identifier = selectedLocale.Identifier;
						obj = ((LocaleIdentifier)(ref identifier)).Code;
					}
					return ((string)obj)?.StartsWith("ru", StringComparison.OrdinalIgnoreCase) ?? false;
				}
				catch (Exception)
				{
					return false;
				}
			}
		}

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			new Harmony("ElfDevelop.VideoProgress").PatchAll();
			Logger.LogInfo((object)"VideoProgress 1.0.0 has loaded!");
		}

		private void Update()
		{
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			if (Time.unscaledTime < nextUpdate)
			{
				return;
			}
			nextUpdate = Time.unscaledTime + 0.25f;
			foreach (ExtractVideoMachine item in labels.Keys.ToList())
			{
				if ((Object)(object)item == (Object)null || !item.AlreadyExtracting)
				{
					if ((Object)(object)labels[item] != (Object)null)
					{
						Object.Destroy((Object)(object)((Component)labels[item]).gameObject);
					}
					labels.Remove(item);
					Extracting.Remove(item);
				}
			}
			foreach (KeyValuePair<ExtractVideoMachine, VideoHandle> item2 in Extracting.ToList())
			{
				ExtractVideoMachine key = item2.Key;
				if ((Object)(object)key == (Object)null || !key.AlreadyExtracting)
				{
					continue;
				}
				TextMeshProUGUI label = GetLabel(key);
				if (!((Object)(object)label == (Object)null))
				{
					bool flag = ((Object)(object)key.m_loadingStateUI != (Object)null && key.m_loadingStateUI.activeInHierarchy) || ((Object)(object)key.m_extractionStateUI != (Object)null && key.m_extractionStateUI.activeInHierarchy);
					((Component)label).gameObject.SetActive(flag);
					if (flag)
					{
						((TMP_Text)label).text = Describe(item2.Value);
					}
				}
			}
		}

		private TextMeshProUGUI? GetLabel(ExtractVideoMachine machine)
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			if (labels.TryGetValue(machine, out TextMeshProUGUI value) && (Object)(object)value != (Object)null)
			{
				return value;
			}
			TextMeshProUGUI videosLeftUI = machine.m_videosLeftUI;
			Transform val = (((Object)(object)machine.m_loadingStateUI != (Object)null) ? machine.m_loadingStateUI.transform.parent : null);
			if ((Object)(object)videosLeftUI == (Object)null || (Object)(object)val == (Object)null)
			{
				return null;
			}
			GameObject val2 = new GameObject("VideoProgressLabel", new Type[1] { typeof(RectTransform) })
			{
				layer = ((Component)videosLeftUI).gameObject.layer
			};
			RectTransform val3 = (RectTransform)val2.transform;
			((Transform)val3).SetParent(val, false);
			val3.anchorMin = new Vector2(0.05f, 0.02f);
			val3.anchorMax = new Vector2(0.95f, 0.24f);
			val3.offsetMin = Vector2.zero;
			val3.offsetMax = Vector2.zero;
			TextMeshProUGUI val4 = val2.AddComponent<TextMeshProUGUI>();
			((TMP_Text)val4).font = ((TMP_Text)videosLeftUI).font;
			((TMP_Text)val4).fontSharedMaterial = ((TMP_Text)videosLeftUI).fontSharedMaterial;
			((Graphic)val4).color = ((Graphic)videosLeftUI).color;
			((TMP_Text)val4).alignment = (TextAlignmentOptions)514;
			((TMP_Text)val4).enableAutoSizing = true;
			((TMP_Text)val4).fontSizeMin = 4f;
			((TMP_Text)val4).fontSizeMax = ((TMP_Text)videosLeftUI).fontSize;
			((Graphic)val4).raycastTarget = false;
			labels[machine] = val4;
			return val4;
		}

		private static string Describe(VideoHandle handle)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			CameraRecording val = default(CameraRecording);
			if (!RecordingsHandler.TryGetRecording(handle, ref val))
			{
				if (!Russian)
				{
					return "Looking for the video...";
				}
				return "Ищу видео...";
			}
			Dictionary<ClipID, VideoChunk> dictionary = RetrievableSingleton<PhotonSendVideoHandler>.Instance?.m_VideoChunkDic;
			List<ClipState> list = new List<ClipState>();
			for (int i = 0; i < val.ClipCount; i++)
			{
				Clip clip = val.GetClip(i);
				ClipState item = new ClipState
				{
					Valid = clip.Valid,
					Local = clip.local,
					Recording = clip.isRecording,
					Encoded = clip.encoded,
					Received = clip.hasBeenRecieved
				};
				if (dictionary != null && dictionary.TryGetValue(clip.clipID, out var value) && value.m_chunks != null)
				{
					item.ChunksTotal = value.m_chunks.Length;
					item.ChunksDone = value.m_chunks.Count((byte[] c) => c != null);
				}
				list.Add(item);
			}
			(float Fraction, int Done, int Total) tuple = ProgressMath.Compute(list);
			float item2 = tuple.Fraction;
			int item3 = tuple.Done;
			int item4 = tuple.Total;
			int num = Mathf.FloorToInt(item2 * 100f);
			if (!Russian)
			{
				return $"Loading video: {num}%\nClips ready: {item3} of {item4}";
			}
			return $"Загрузка видео: {num}%\nРоликов готово: {item3} из {item4}";
		}
	}
	[HarmonyPatch(typeof(ExtractVideoMachine), "ExtractVideo")]
	internal static class ExtractVideoPatch
	{
		[HarmonyPrefix]
		private static void Remember(ExtractVideoMachine __instance, VideoHandle handle)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			VideoProgressPlugin.Extracting[__instance] = handle;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}