Decompiled source of SkipIntroVideo v0.0.1

SkipIntroVideo/SkipIntroVideo.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Valheim.SettingsGui;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SkipIntroVideo")]
[assembly: AssemblyDescription("Adds a startup-video toggle to Valheim gameplay settings.")]
[assembly: AssemblyCompany("Marlthon")]
[assembly: AssemblyProduct("SkipIntroVideo")]
[assembly: ComVisible(false)]
[assembly: Guid("2E46B8D4-39AF-4AEF-90A6-4D4F29D6F640")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.1.0")]
namespace SkipIntroVideo;

[BepInPlugin("com.marlthon.skipintrovideo", "Skip Intro Video", "0.0.1")]
public sealed class SkipIntroVideoPlugin : BaseUnityPlugin
{
	internal const string PluginGuid = "com.marlthon.skipintrovideo";

	internal const string PluginName = "Skip Intro Video";

	internal const string PluginVersion = "0.0.1";

	internal const string PreferenceKey = "Marlthon.SkipIntroVideo.PlayStartupVideo";

	private void Awake()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		new Harmony("com.marlthon.skipintrovideo").PatchAll();
		Localization.OnLanguageChange = (Action)Delegate.Combine(Localization.OnLanguageChange, new Action(RefreshAllLabels));
	}

	private void OnDestroy()
	{
		Localization.OnLanguageChange = (Action)Delegate.Remove(Localization.OnLanguageChange, new Action(RefreshAllLabels));
	}

	internal static bool ShouldPlayStartupVideo()
	{
		return PlatformPrefs.GetInt("Marlthon.SkipIntroVideo.PlayStartupVideo", 1) == 1;
	}

	internal static void SetPlayStartupVideo(bool value)
	{
		PlatformPrefs.SetInt("Marlthon.SkipIntroVideo.PlayStartupVideo", value ? 1 : 0);
		PlayerPrefs.Save();
	}

	internal static string GetToggleLabel()
	{
		return GetLocalizedText(0);
	}

	internal static string GetTooltipTitle()
	{
		return GetLocalizedText(0);
	}

	internal static string GetTooltipDescription()
	{
		return GetLocalizedText(1);
	}

	internal static void ConfigureTooltip(Toggle toggle)
	{
		SettingsTooltip[] componentsInChildren = ((Component)toggle).GetComponentsInChildren<SettingsTooltip>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].SetTexts(GetTooltipTitle(), GetTooltipDescription());
		}
	}

	private static string GetLocalizedText(int textIndex)
	{
		switch (Localization.instance.GetSelectedLanguage())
		{
		case "Chinese":
			if (textIndex != 0)
			{
				return "选择是否在打开游戏时播放《英灵神殿》的开场视频。";
			}
			return "显示开场视频";
		case "Chinese_Trad":
			if (textIndex != 0)
			{
				return "選擇是否在開啟遊戲時播放 Valheim 的開場影片。";
			}
			return "顯示開場影片";
		case "Czech":
			if (textIndex != 0)
			{
				return "Zvolte, zda se při spuštění hry přehraje úvodní video Valheimu.";
			}
			return "Zobrazit úvodní video";
		case "Danish":
			if (textIndex != 0)
			{
				return "Vælg, om Valheims startvideo skal afspilles, når spillet åbnes.";
			}
			return "Vis startvideo";
		case "Dutch":
			if (textIndex != 0)
			{
				return "Kies of de startvideo van Valheim wordt afgespeeld wanneer het spel wordt geopend.";
			}
			return "Startvideo weergeven";
		case "Finnish":
			if (textIndex != 0)
			{
				return "Valitse, toistetaanko Valheimin aloitusvideo, kun peli avataan.";
			}
			return "Näytä aloitusvideo";
		case "French":
			if (textIndex != 0)
			{
				return "Choisissez si la vidéo d'introduction de Valheim est lue à l'ouverture du jeu.";
			}
			return "Afficher la vidéo d'introduction";
		case "German":
			if (textIndex != 0)
			{
				return "Wähle, ob das Startvideo von Valheim beim Öffnen des Spiels abgespielt wird.";
			}
			return "Startvideo anzeigen";
		case "Greek":
			if (textIndex != 0)
			{
				return "Επιλέξτε αν θα αναπαράγεται το βίντεο έναρξης του Valheim όταν ανοίγει το παιχνίδι.";
			}
			return "Εμφάνιση βίντεο έναρξης";
		case "Hungarian":
			if (textIndex != 0)
			{
				return "Válaszd ki, hogy a Valheim nyitóvideója lejátszódjon-e a játék indításakor.";
			}
			return "Nyitóvideó megjelenítése";
		case "Italian":
			if (textIndex != 0)
			{
				return "Scegli se riprodurre il video di avvio di Valheim all'apertura del gioco.";
			}
			return "Mostra video di avvio";
		case "Japanese":
			if (textIndex != 0)
			{
				return "ゲーム起動時に Valheim のオープニング動画を再生するか選択します。";
			}
			return "オープニング動画を表示";
		case "Korean":
			if (textIndex != 0)
			{
				return "게임을 열 때 Valheim 시작 영상을 재생할지 선택합니다.";
			}
			return "시작 영상 표시";
		case "Norwegian":
			if (textIndex != 0)
			{
				return "Velg om Valheims oppstartsvideo skal spilles av når spillet åpnes.";
			}
			return "Vis oppstartsvideo";
		case "Polish":
			if (textIndex != 0)
			{
				return "Wybierz, czy film otwierający Valheim ma być odtwarzany przy uruchamianiu gry.";
			}
			return "Pokaż film otwierający";
		case "Portuguese_Brazilian":
			if (textIndex != 0)
			{
				return "Escolha se o vídeo de abertura de Valheim será reproduzido ao iniciar o jogo.";
			}
			return "Exibir vídeo de abertura";
		case "Portuguese_European":
			if (textIndex != 0)
			{
				return "Escolha se o vídeo de abertura de Valheim será reproduzido ao iniciar o jogo.";
			}
			return "Mostrar vídeo de abertura";
		case "Russian":
			if (textIndex != 0)
			{
				return "Выберите, воспроизводить ли вступительное видео Valheim при запуске игры.";
			}
			return "Показывать вступительное видео";
		case "Slovak":
			if (textIndex != 0)
			{
				return "Vyberte, či sa má pri spustení hry prehrať úvodné video Valheimu.";
			}
			return "Zobraziť úvodné video";
		case "Spanish":
			if (textIndex != 0)
			{
				return "Elige si el vídeo de inicio de Valheim se reproduce al abrir el juego.";
			}
			return "Mostrar vídeo de inicio";
		case "Swedish":
			if (textIndex != 0)
			{
				return "Välj om Valheims startvideo ska spelas upp när spelet öppnas.";
			}
			return "Visa startvideo";
		case "Turkish":
			if (textIndex != 0)
			{
				return "Oyun açıldığında Valheim başlangıç videosunun oynatılıp oynatılmayacağını seçin.";
			}
			return "Başlangıç videosunu göster";
		case "Ukrainian":
			if (textIndex != 0)
			{
				return "Виберіть, чи відтворювати вступне відео Valheim під час запуску гри.";
			}
			return "Показувати вступне відео";
		default:
			if (textIndex != 0)
			{
				return "Choose whether Valheim's startup video plays when the game opens.";
			}
			return "Show startup video";
		}
	}

	internal static void RefreshLabels(GameplaySettings settings)
	{
		if ((Object)(object)settings == (Object)null)
		{
			return;
		}
		Toggle[] componentsInChildren = ((Component)settings).GetComponentsInChildren<Toggle>(true);
		foreach (Toggle val in componentsInChildren)
		{
			if (!((Object)(object)val == (Object)null) && !(((Object)val).name != "SkipIntroVideoToggle"))
			{
				TMP_Text[] componentsInChildren2 = ((Component)val).GetComponentsInChildren<TMP_Text>(true);
				for (int j = 0; j < componentsInChildren2.Length; j++)
				{
					componentsInChildren2[j].text = GetToggleLabel();
				}
				ConfigureTooltip(val);
			}
		}
	}

	private static void RefreshAllLabels()
	{
		GameplaySettings[] array = Resources.FindObjectsOfTypeAll<GameplaySettings>();
		for (int i = 0; i < array.Length; i++)
		{
			RefreshLabels(array[i]);
		}
	}
}
[HarmonyPatch(typeof(CinematicsManager), "Play", new Type[]
{
	typeof(Settings),
	typeof(VideoCompleteAction)
})]
internal static class CinematicsManagerPlayPatch
{
	private static bool Prefix(Settings firstWithSetting, ref bool __result)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Invalid comparison between Unknown and I4
		if ((int)firstWithSetting != 2 || Game.m_hasStartedOnce || (Object)(object)FejdStartup.instance == (Object)null || SkipIntroVideoPlugin.ShouldPlayStartupVideo())
		{
			return true;
		}
		FejdStartup.instance.m_mainMenu.SetActive(true);
		__result = true;
		return false;
	}
}
[HarmonyPatch(typeof(GameplaySettings), "Initialize")]
internal static class GameplaySettingsPatch
{
	internal const string ToggleObjectName = "SkipIntroVideoToggle";

	private static void Postfix(GameplaySettings __instance)
	{
		if ((Object)(object)__instance == (Object)null)
		{
			return;
		}
		if ((Object)(object)FindExistingToggle(__instance) != (Object)null)
		{
			SkipIntroVideoPlugin.RefreshLabels(__instance);
			return;
		}
		Toggle field = GetField<Toggle>(__instance, "m_showBuildPieceAuthor");
		if (!((Object)(object)field == (Object)null) && !((Object)(object)((Component)field).transform.parent == (Object)null))
		{
			Toggle val = Object.Instantiate<Toggle>(field, ((Component)field).transform.parent, false);
			((Object)val).name = "SkipIntroVideoToggle";
			((Component)val).transform.SetSiblingIndex(((Component)field).transform.GetSiblingIndex() + 1);
			((UnityEventBase)val.onValueChanged).RemoveAllListeners();
			val.SetIsOnWithoutNotify(SkipIntroVideoPlugin.ShouldPlayStartupVideo());
			((UnityEvent<bool>)(object)val.onValueChanged).AddListener((UnityAction<bool>)SkipIntroVideoPlugin.SetPlayStartupVideo);
			TMP_Text[] componentsInChildren = ((Component)val).GetComponentsInChildren<TMP_Text>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].text = SkipIntroVideoPlugin.GetToggleLabel();
			}
			SkipIntroVideoPlugin.ConfigureTooltip(val);
			Transform parent = ((Component)field).transform.parent;
			RectTransform val2 = (RectTransform)(object)((parent is RectTransform) ? parent : null);
			if ((Object)(object)val2 != (Object)null)
			{
				LayoutRebuilder.ForceRebuildLayoutImmediate(val2);
			}
		}
	}

	private static T GetField<T>(GameplaySettings settings, string fieldName) where T : class
	{
		return AccessTools.Field(typeof(GameplaySettings), fieldName).GetValue(settings) as T;
	}

	private static Toggle FindExistingToggle(GameplaySettings settings)
	{
		Toggle[] componentsInChildren = ((Component)settings).GetComponentsInChildren<Toggle>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			if ((Object)(object)componentsInChildren[i] != (Object)null && ((Object)componentsInChildren[i]).name == "SkipIntroVideoToggle")
			{
				return componentsInChildren[i];
			}
		}
		return null;
	}
}