Decompiled source of CybergrindSlideshow v1.0.1

plugins\CybergrindSlideshow.dll

Decompiled 8 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Notiffy.API;
using NukeLib.Assets;
using NukeLib.Reflection;
using PluginConfig.API;
using PluginConfig.API.Decorators;
using PluginConfig.API.Fields;
using UnityEngine;
using UnityEngine.SceneManagement;
using VolumetricSkyboxes;
using VolumetricSkyboxes.Components;
using VolumetricSkyboxes.Utils;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("CybergrindSlideshow")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0ae4baedf3ab4c8c15e1ab9401cc5ce48a555049")]
[assembly: AssemblyProduct("CybergrindSlideshow")]
[assembly: AssemblyTitle("CybergrindSlideshow")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 CybergrindSlideshow
{
	public class ConfigManager
	{
		public enum SelectionOrder
		{
			Random,
			Sequential
		}

		internal static readonly string ApplicationPath;

		internal static readonly string SkyboxesPath;

		internal static readonly string VolumetricSkyboxesPath;

		private static PluginConfigurator config;

		public static BoolField SkyboxEnabled;

		public static StringField SkyboxDir;

		public static EnumField<SelectionOrder> SkyboxChangeOrder;

		public static BoolField ForceDisableVolumetricSkyboxGridTexture;

		public static BoolField FirstRun;

		public static void Initialize()
		{
		}

		static ConfigManager()
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Expected O, but got Unknown
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Expected O, but got Unknown
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Expected O, but got Unknown
			ApplicationPath = Path.Combine(Directory.GetParent(Application.dataPath)?.FullName);
			SkyboxesPath = Path.Combine(ApplicationPath, "Cybergrind", "Textures", "Skyboxes");
			VolumetricSkyboxesPath = Path.Combine(ApplicationPath, "Cybergrind", "VolumetricSkyboxes");
			config = PluginConfigurator.Create("Cybergrind Slideshow", "com.github.end-4.cybergrindSlideshow");
			string text = Path.Combine(Plugin.workingDir, "icon.png");
			if (File.Exists(text))
			{
				config.SetIconWithURL(text);
			}
			new ConfigHeader(config.rootPanel, "", 10);
			new ConfigHeader(config.rootPanel, "-- CYBERGRIND SLIDESHOW --", 24);
			SkyboxEnabled = new BoolField(config.rootPanel, "Change skybox", "skyboxEnabled", true);
			SkyboxDir = new StringField(config.rootPanel, "Skybox folder", "skyboxDir", SkyboxesPath);
			SkyboxChangeOrder = new EnumField<SelectionOrder>(config.rootPanel, "Skybox change order", "skyboxChangeOrder", SelectionOrder.Sequential);
			ForceDisableVolumetricSkyboxGridTexture = new BoolField(config.rootPanel, "Disallow grid change from volumetric skyboxes", "forceDisableVolumetricSkyboxGridTexture", true);
			FirstRun = new BoolField(config.rootPanel, "First run", "firstRun", true);
			((ConfigField)FirstRun).hidden = true;
		}
	}
	[BepInPlugin("com.github.end-4.cybergrindSlideshow", "CybergrindSlideshow", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(EndlessGrid))]
		[HarmonyPatch("NextWave")]
		public class EndlessGridPatch
		{
			public static void Postfix()
			{
				Log.LogInfo((object)"Cycling stuff");
				ChangeTheme();
			}
		}

		internal static ManualLogSource Log;

		public static string workingPath = Assembly.GetExecutingAssembly().Location;

		public static string workingDir = Path.GetDirectoryName(workingPath);

		public const string PluginGUID = "com.github.end-4.cybergrindSlideshow";

		public const string PluginName = "CybergrindSlideshow";

		public const string PluginVersion = "1.0.1";

		private void Awake()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			ConfigManager.Initialize();
			UserHints.Initialize();
			new Harmony("CybergrindSlideshow").PatchAll();
			Log.LogInfo((object)"CybergrindSlideshow loaded");
		}

		private static string SelectSkyboxFile(string folderPath, ConfigManager.SelectionOrder selectionOrder)
		{
			string[] allowedExtensions = new string[4] { ".jpg", ".jpeg", ".png", ".cgvsb" };
			if (!Directory.Exists(folderPath))
			{
				return "";
			}
			string[] array = (from file in Directory.EnumerateFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly)
				where allowedExtensions.Contains<string>(Path.GetExtension(file), StringComparer.OrdinalIgnoreCase)
				select file).ToArray();
			int num = 0;
			switch (selectionOrder)
			{
			case ConfigManager.SelectionOrder.Random:
				num = Random.Range(0, array.Count());
				Log.LogInfo((object)$"Random index {num}");
				break;
			default:
				num = MonoSingleton<EndlessGrid>.Instance.currentWave % array.Count();
				Log.LogInfo((object)$"Sequential index {num}");
				break;
			}
			return array[num];
		}

		public static void UnloadAllVolumetricSkyboxes()
		{
			VolumetricSkyboxesManager instance = MonoSingleton<VolumetricSkyboxesManager>.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			Dictionary<string, VolumetricSkyboxContainer> dictionary = ReflectionUtils.GetPrivate<Dictionary<string, VolumetricSkyboxContainer>>((object)instance, "_skyboxes");
			if (dictionary == null)
			{
				return;
			}
			Dictionary<string, VolumetricSkyboxContainer>.KeyCollection keyCollection = dictionary?.Keys;
			if (keyCollection == null)
			{
				return;
			}
			foreach (string item in new List<string>(keyCollection))
			{
				if ((Object)(object)instance == (Object)null)
				{
					break;
				}
				instance.RemoveSkybox(item);
			}
		}

		private static void LoadVolumetricSkybox(string guid)
		{
			int num;
			if (!ConfigManager.ForceDisableVolumetricSkyboxGridTexture.value)
			{
				num = 0;
			}
			else
			{
				num = (MonoSingleton<PrefsManager>.Instance.GetBoolLocal("cyberGrind.volumetricSkyboxes.useSkyboxesGridTextures", true) ? 1 : 0);
				if (num != 0)
				{
					MonoSingleton<PrefsManager>.Instance.SetBoolLocal("cyberGrind.volumetricSkyboxes.useSkyboxesGridTextures", false);
				}
			}
			VolumetricSkyboxesManager instance = MonoSingleton<VolumetricSkyboxesManager>.Instance;
			if (instance != null)
			{
				instance.AddSkybox(guid);
			}
			if (num != 0)
			{
				MonoSingleton<PrefsManager>.Instance.SetBoolLocal("cyberGrind.volumetricSkyboxes.useSkyboxesGridTextures", true);
			}
		}

		private static async void ChangeSkybox()
		{
			try
			{
				string text = SelectSkyboxFile(ConfigManager.SkyboxDir.value, ConfigManager.SkyboxChangeOrder.value);
				if (!(text == ""))
				{
					Log.LogInfo((object)("Switching skybox to " + text));
					try
					{
						UnloadAllVolumetricSkyboxes();
					}
					catch (Exception ex)
					{
						Log.LogWarning((object)ex);
					}
					if (text.EndsWith(".cgvsb"))
					{
						LoadVolumetricSkybox(SkyboxFileUtility.UnpackSkybox(text, false).guid);
						return;
					}
					Material mat = ReflectionUtils.GetPrivate<Material>((object)(OutdoorLightMaster)Object.FindObjectsOfType(typeof(OutdoorLightMaster))[0], "skyboxMaterial");
					Material mat2 = ReflectionUtils.GetPrivate<Material>((object)(OutdoorLightMaster)Object.FindObjectsOfType(typeof(OutdoorLightMaster))[0], "tempSkybox");
					Texture2D mainTexture = (Texture2D)(object)(mat.mainTexture = (Texture)(object)(await FileAssetHelper.LoadTextureAsync(text)));
					mat2.mainTexture = (Texture)(object)mainTexture;
				}
			}
			catch (Exception ex2)
			{
				Log.LogWarning((object)ex2);
			}
		}

		private static void ChangeTheme()
		{
			if (ConfigManager.SkyboxEnabled.value)
			{
				ChangeSkybox();
			}
		}
	}
	internal static class UserHints
	{
		private static Dictionary<string, string> volumetricCopyActions;

		private static uint notificationId;

		private static void Hook()
		{
			NotificationSystem.ActionInvoked += OnActionInvoked;
			NotificationSystem.NotificationDeleted += OnNotificationDeleted;
		}

		private static void Unhook()
		{
			NotificationSystem.ActionInvoked -= OnActionInvoked;
			NotificationSystem.NotificationDeleted -= OnNotificationDeleted;
		}

		private static void OnActionInvoked(uint id, string actionIdentifier)
		{
			if (id == notificationId)
			{
				switch (actionIdentifier)
				{
				case "yes":
					CopyVolumetricSkyboxes();
					break;
				case "remind":
					ConfigManager.FirstRun.value = true;
					break;
				}
				Unhook();
			}
		}

		private static void OnNotificationDeleted(uint id)
		{
			if (id == notificationId)
			{
				Unhook();
			}
		}

		private static void IssueFirstRunNoticeIfNecessary()
		{
			if (ConfigManager.FirstRun.value)
			{
				ConfigManager.FirstRun.value = false;
				notificationId = NotificationSystem.NotifySend("CybergrindSlideshow", "1. The folder for slideshow files can be adjusted in <color=#55c7f6>Options > Plugin Config > Cybergrind Slideshow</color>\n2. Would you like to copy volumetric skyboxes to the slideshow folder?", (Sprite)null, Path.Combine(Plugin.workingDir, "icon.png"), (Urgency)1, "CybergrindSlideshow", 0u, volumetricCopyActions, 20000);
				Hook();
			}
		}

		private static void OnSceneLoaded(Scene s, LoadSceneMode m)
		{
			if (SceneHelper.CurrentScene == "Endless")
			{
				IssueFirstRunNoticeIfNecessary();
			}
		}

		private static void CopyVolumetricSkyboxes()
		{
			string volumetricSkyboxesPath = ConfigManager.VolumetricSkyboxesPath;
			string value = ConfigManager.SkyboxDir.value;
			string[] files = Directory.GetFiles(volumetricSkyboxesPath);
			foreach (string obj in files)
			{
				string fileName = Path.GetFileName(obj);
				string destFileName = Path.Combine(value, fileName);
				File.Copy(obj, destFileName);
				Plugin.Log.LogInfo((object)("Copied: " + fileName));
			}
		}

		public static void Initialize()
		{
		}

		static UserHints()
		{
			volumetricCopyActions = new Dictionary<string, string>
			{
				{ "yes", "Yes" },
				{ "no", "No" },
				{ "remind", "Remind me later" }
			};
			SceneManager.sceneLoaded += OnSceneLoaded;
		}
	}
}