Decompiled source of Globglogabgalab v1.0.0

ValheimVideoMod.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
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 UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ValheimVideoMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Burial Chamber Video Player - Client-side mod")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ValheimVideoMod")]
[assembly: AssemblyTitle("ValheimVideoMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace ValheimVideoMod;

[BepInPlugin("com.loar.valheim.Globglogabgalab", "Burial Chamber Video Player", "1.0.0")]
[BepInProcess("valheim.exe")]
public class BurialVideoPlugin : BaseUnityPlugin
{
	internal static Harmony HarmonyInstance = new Harmony("com.loar.valheim.Globglogabgalab");

	internal static BurialVideoPlugin Instance;

	public static string VideoPath;

	public static bool AllowSkip = true;

	internal static ManualLogSource PluginLogger
	{
		get
		{
			BurialVideoPlugin instance = Instance;
			if (instance == null)
			{
				return null;
			}
			return ((BaseUnityPlugin)instance).Logger;
		}
	}

	private void Awake()
	{
		Instance = this;
		VideoPath = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "burial_chamber_intro.mp4");
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Burial Chamber Video Mod loaded. Looking for: " + VideoPath));
		if (!File.Exists(VideoPath))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"burial_chamber_intro.mp4 not found – mod will be inactive until a video is placed.");
		}
		HarmonyInstance.PatchAll();
	}

	internal void RunCoroutine(IEnumerator routine)
	{
		((MonoBehaviour)this).StartCoroutine(routine);
	}
}
[HarmonyPatch]
public static class DungeonVideoPatch
{
	private static bool _videoPlayedThisVisit;

	private static bool _wasInCrypt;

	private const string CryptEnv = "Crypt";

	private const string DungeonTag = "DG_ForestCrypt";

	private static MethodBase TargetMethod()
	{
		MethodInfo methodInfo = AccessTools.Method(typeof(EnvMan), "UpdateEnvironment", (Type[])null, (Type[])null);
		if (methodInfo != null)
		{
			return methodInfo;
		}
		methodInfo = AccessTools.Method(typeof(EnvMan), "UpdateEnv", (Type[])null, (Type[])null);
		if (methodInfo != null)
		{
			return methodInfo;
		}
		methodInfo = AccessTools.Method(typeof(Player), "UpdateEnvironment", (Type[])null, (Type[])null);
		if (methodInfo != null)
		{
			return methodInfo;
		}
		return AccessTools.Method(typeof(Player), "UpdateEnv", (Type[])null, (Type[])null);
	}

	public static void Postfix()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return;
		}
		bool flag = IsInBurialChamber(localPlayer);
		if (flag && !_videoPlayedThisVisit)
		{
			BurialVideoPlugin.Instance.RunCoroutine(PlayVideoRoutine());
			_videoPlayedThisVisit = true;
			_wasInCrypt = true;
		}
		else if (!flag && _wasInCrypt)
		{
			_wasInCrypt = false;
			_videoPlayedThisVisit = false;
			ManualLogSource pluginLogger = BurialVideoPlugin.PluginLogger;
			if (pluginLogger != null)
			{
				pluginLogger.LogInfo((object)"Exited Burial Chamber – video trigger reset.");
			}
		}
	}

	private static bool IsInBurialChamber(Player player)
	{
		if ((Object)(object)EnvMan.instance != (Object)null)
		{
			string text = EnvMan.instance.GetCurrentEnvironment()?.m_name ?? "";
			if (text.Contains("Crypt") || text.Contains("DG_ForestCrypt"))
			{
				return true;
			}
		}
		if (((Character)player).InInterior())
		{
			DungeonGenerator val = FindActiveDungeonGenerator();
			if ((Object)(object)val != (Object)null && ((Object)((Component)val).gameObject).name.Contains("DG_ForestCrypt"))
			{
				return true;
			}
			ManualLogSource pluginLogger = BurialVideoPlugin.PluginLogger;
			if (pluginLogger != null)
			{
				pluginLogger.LogInfo((object)"InInterior true, playing video (dungeon detected)");
			}
			return true;
		}
		return false;
	}

	private static DungeonGenerator FindActiveDungeonGenerator()
	{
		DungeonGenerator[] array = Resources.FindObjectsOfTypeAll<DungeonGenerator>();
		foreach (DungeonGenerator val in array)
		{
			if (((Object)val).name.Contains("DG_ForestCrypt"))
			{
				return val;
			}
		}
		return null;
	}

	private static IEnumerator PlayVideoRoutine()
	{
		string videoPath = BurialVideoPlugin.VideoPath;
		if (!File.Exists(videoPath))
		{
			ManualLogSource pluginLogger = BurialVideoPlugin.PluginLogger;
			if (pluginLogger != null)
			{
				pluginLogger.LogWarning((object)"[VideoMod] Video file missing – skipping playback.");
			}
			yield break;
		}
		ManualLogSource pluginLogger2 = BurialVideoPlugin.PluginLogger;
		if (pluginLogger2 != null)
		{
			pluginLogger2.LogInfo((object)"[VideoMod] Playing Burial Chamber intro video...");
		}
		GameObject canvasGo = new GameObject("BurialVideoCanvas");
		Object.DontDestroyOnLoad((Object)(object)canvasGo);
		Canvas obj = canvasGo.AddComponent<Canvas>();
		obj.renderMode = (RenderMode)0;
		obj.sortingOrder = 9999;
		GameObject val = new GameObject("VideoBackground");
		val.transform.SetParent(canvasGo.transform, false);
		((Graphic)val.AddComponent<RawImage>()).color = Color.black;
		RectTransform component = val.GetComponent<RectTransform>();
		component.anchorMin = Vector2.zero;
		component.anchorMax = Vector2.one;
		component.sizeDelta = Vector2.zero;
		GameObject val2 = new GameObject("VideoRender");
		val2.transform.SetParent(canvasGo.transform, false);
		RawImage rawImage = val2.AddComponent<RawImage>();
		RectTransform component2 = val2.GetComponent<RectTransform>();
		component2.anchorMin = Vector2.zero;
		component2.anchorMax = Vector2.one;
		component2.sizeDelta = Vector2.zero;
		VideoPlayer videoPlayer = canvasGo.AddComponent<VideoPlayer>();
		videoPlayer.playOnAwake = false;
		videoPlayer.source = (VideoSource)1;
		videoPlayer.url = videoPath;
		videoPlayer.isLooping = false;
		videoPlayer.skipOnDrop = true;
		videoPlayer.renderMode = (VideoRenderMode)4;
		videoPlayer.Prepare();
		while (!videoPlayer.isPrepared)
		{
			yield return null;
		}
		rawImage.texture = videoPlayer.texture;
		bool skipped = false;
		videoPlayer.Play();
		while (videoPlayer.isPlaying && !skipped)
		{
			if (BurialVideoPlugin.AllowSkip && Input.GetKeyDown((KeyCode)32))
			{
				skipped = true;
				ManualLogSource pluginLogger3 = BurialVideoPlugin.PluginLogger;
				if (pluginLogger3 != null)
				{
					pluginLogger3.LogInfo((object)"[VideoMod] Video skipped by player.");
				}
			}
			yield return null;
		}
		videoPlayer.Stop();
		Object.Destroy((Object)(object)canvasGo);
		ManualLogSource pluginLogger4 = BurialVideoPlugin.PluginLogger;
		if (pluginLogger4 != null)
		{
			pluginLogger4.LogInfo((object)"[VideoMod] Video finished.");
		}
	}
}