Decompiled source of CGPacksPlus v0.0.1

CGPacksPlus.dll

Decompiled 2 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using CGPacksPlus.HelperExtensions;
using CGPacksPlus.Patches;
using CGPacksPlus.Patterns;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using plog;
using plog.Models;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("0Harmony")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("BepInEx")]
[assembly: IgnoresAccessChecksTo("Microsoft.Win32.Primitives")]
[assembly: IgnoresAccessChecksTo("mscorlib")]
[assembly: IgnoresAccessChecksTo("netstandard")]
[assembly: IgnoresAccessChecksTo("plog")]
[assembly: IgnoresAccessChecksTo("plog.unity")]
[assembly: IgnoresAccessChecksTo("System.AppContext")]
[assembly: IgnoresAccessChecksTo("System.Buffers")]
[assembly: IgnoresAccessChecksTo("System.Collections.Concurrent")]
[assembly: IgnoresAccessChecksTo("System.Collections")]
[assembly: IgnoresAccessChecksTo("System.Collections.NonGeneric")]
[assembly: IgnoresAccessChecksTo("System.Collections.Specialized")]
[assembly: IgnoresAccessChecksTo("System.ComponentModel.Composition")]
[assembly: IgnoresAccessChecksTo("System.ComponentModel")]
[assembly: IgnoresAccessChecksTo("System.ComponentModel.EventBasedAsync")]
[assembly: IgnoresAccessChecksTo("System.ComponentModel.Primitives")]
[assembly: IgnoresAccessChecksTo("System.ComponentModel.TypeConverter")]
[assembly: IgnoresAccessChecksTo("System.Console")]
[assembly: IgnoresAccessChecksTo("System.Core")]
[assembly: IgnoresAccessChecksTo("System.Data.Common")]
[assembly: IgnoresAccessChecksTo("System.Data")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.Contracts")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.Debug")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.FileVersionInfo")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.Process")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.StackTrace")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.TextWriterTraceListener")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.Tools")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.TraceSource")]
[assembly: IgnoresAccessChecksTo("System.Diagnostics.Tracing")]
[assembly: IgnoresAccessChecksTo("System")]
[assembly: IgnoresAccessChecksTo("System.Drawing")]
[assembly: IgnoresAccessChecksTo("System.Drawing.Primitives")]
[assembly: IgnoresAccessChecksTo("System.Dynamic.Runtime")]
[assembly: IgnoresAccessChecksTo("System.Globalization.Calendars")]
[assembly: IgnoresAccessChecksTo("System.Globalization")]
[assembly: IgnoresAccessChecksTo("System.Globalization.Extensions")]
[assembly: IgnoresAccessChecksTo("System.IO.Compression")]
[assembly: IgnoresAccessChecksTo("System.IO.Compression.FileSystem")]
[assembly: IgnoresAccessChecksTo("System.IO.Compression.ZipFile")]
[assembly: IgnoresAccessChecksTo("System.IO")]
[assembly: IgnoresAccessChecksTo("System.IO.FileSystem")]
[assembly: IgnoresAccessChecksTo("System.IO.FileSystem.DriveInfo")]
[assembly: IgnoresAccessChecksTo("System.IO.FileSystem.Primitives")]
[assembly: IgnoresAccessChecksTo("System.IO.FileSystem.Watcher")]
[assembly: IgnoresAccessChecksTo("System.IO.IsolatedStorage")]
[assembly: IgnoresAccessChecksTo("System.IO.MemoryMappedFiles")]
[assembly: IgnoresAccessChecksTo("System.IO.Pipes")]
[assembly: IgnoresAccessChecksTo("System.IO.UnmanagedMemoryStream")]
[assembly: IgnoresAccessChecksTo("System.Linq")]
[assembly: IgnoresAccessChecksTo("System.Linq.Expressions")]
[assembly: IgnoresAccessChecksTo("System.Linq.Parallel")]
[assembly: IgnoresAccessChecksTo("System.Linq.Queryable")]
[assembly: IgnoresAccessChecksTo("System.Memory")]
[assembly: IgnoresAccessChecksTo("System.Net")]
[assembly: IgnoresAccessChecksTo("System.Net.Http")]
[assembly: IgnoresAccessChecksTo("System.Net.NameResolution")]
[assembly: IgnoresAccessChecksTo("System.Net.NetworkInformation")]
[assembly: IgnoresAccessChecksTo("System.Net.Ping")]
[assembly: IgnoresAccessChecksTo("System.Net.Primitives")]
[assembly: IgnoresAccessChecksTo("System.Net.Requests")]
[assembly: IgnoresAccessChecksTo("System.Net.Security")]
[assembly: IgnoresAccessChecksTo("System.Net.Sockets")]
[assembly: IgnoresAccessChecksTo("System.Net.WebHeaderCollection")]
[assembly: IgnoresAccessChecksTo("System.Net.WebSockets.Client")]
[assembly: IgnoresAccessChecksTo("System.Net.WebSockets")]
[assembly: IgnoresAccessChecksTo("System.Numerics")]
[assembly: IgnoresAccessChecksTo("System.Numerics.Vectors")]
[assembly: IgnoresAccessChecksTo("System.ObjectModel")]
[assembly: IgnoresAccessChecksTo("System.Reflection.DispatchProxy")]
[assembly: IgnoresAccessChecksTo("System.Reflection")]
[assembly: IgnoresAccessChecksTo("System.Reflection.Emit")]
[assembly: IgnoresAccessChecksTo("System.Reflection.Emit.ILGeneration")]
[assembly: IgnoresAccessChecksTo("System.Reflection.Emit.Lightweight")]
[assembly: IgnoresAccessChecksTo("System.Reflection.Extensions")]
[assembly: IgnoresAccessChecksTo("System.Reflection.Primitives")]
[assembly: IgnoresAccessChecksTo("System.Resources.Reader")]
[assembly: IgnoresAccessChecksTo("System.Resources.ResourceManager")]
[assembly: IgnoresAccessChecksTo("System.Resources.Writer")]
[assembly: IgnoresAccessChecksTo("System.Runtime.CompilerServices.VisualC")]
[assembly: IgnoresAccessChecksTo("System.Runtime")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Extensions")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Handles")]
[assembly: IgnoresAccessChecksTo("System.Runtime.InteropServices")]
[assembly: IgnoresAccessChecksTo("System.Runtime.InteropServices.RuntimeInformation")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Numerics")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Serialization")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Serialization.Formatters")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Serialization.Json")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Serialization.Primitives")]
[assembly: IgnoresAccessChecksTo("System.Runtime.Serialization.Xml")]
[assembly: IgnoresAccessChecksTo("System.Security.Claims")]
[assembly: IgnoresAccessChecksTo("System.Security.Cryptography.Algorithms")]
[assembly: IgnoresAccessChecksTo("System.Security.Cryptography.Csp")]
[assembly: IgnoresAccessChecksTo("System.Security.Cryptography.Encoding")]
[assembly: IgnoresAccessChecksTo("System.Security.Cryptography.Primitives")]
[assembly: IgnoresAccessChecksTo("System.Security.Cryptography.X509Certificates")]
[assembly: IgnoresAccessChecksTo("System.Security.Principal")]
[assembly: IgnoresAccessChecksTo("System.Security.SecureString")]
[assembly: IgnoresAccessChecksTo("System.ServiceModel.Web")]
[assembly: IgnoresAccessChecksTo("System.Text.Encoding")]
[assembly: IgnoresAccessChecksTo("System.Text.Encoding.Extensions")]
[assembly: IgnoresAccessChecksTo("System.Text.RegularExpressions")]
[assembly: IgnoresAccessChecksTo("System.Threading")]
[assembly: IgnoresAccessChecksTo("System.Threading.Overlapped")]
[assembly: IgnoresAccessChecksTo("System.Threading.Tasks")]
[assembly: IgnoresAccessChecksTo("System.Threading.Tasks.Extensions")]
[assembly: IgnoresAccessChecksTo("System.Threading.Tasks.Parallel")]
[assembly: IgnoresAccessChecksTo("System.Threading.Thread")]
[assembly: IgnoresAccessChecksTo("System.Threading.ThreadPool")]
[assembly: IgnoresAccessChecksTo("System.Threading.Timer")]
[assembly: IgnoresAccessChecksTo("System.Transactions")]
[assembly: IgnoresAccessChecksTo("System.ValueTuple")]
[assembly: IgnoresAccessChecksTo("System.Web")]
[assembly: IgnoresAccessChecksTo("System.Windows")]
[assembly: IgnoresAccessChecksTo("System.Xml")]
[assembly: IgnoresAccessChecksTo("System.Xml.Linq")]
[assembly: IgnoresAccessChecksTo("System.Xml.ReaderWriter")]
[assembly: IgnoresAccessChecksTo("System.Xml.Serialization")]
[assembly: IgnoresAccessChecksTo("System.Xml.XDocument")]
[assembly: IgnoresAccessChecksTo("System.Xml.XmlDocument")]
[assembly: IgnoresAccessChecksTo("System.Xml.XmlSerializer")]
[assembly: IgnoresAccessChecksTo("System.Xml.XPath")]
[assembly: IgnoresAccessChecksTo("System.Xml.XPath.XDocument")]
[assembly: IgnoresAccessChecksTo("Unity.Addressables")]
[assembly: IgnoresAccessChecksTo("Unity.ResourceManager")]
[assembly: IgnoresAccessChecksTo("Unity.TextMeshPro")]
[assembly: IgnoresAccessChecksTo("UnityEngine.AccessibilityModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.AIModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.AndroidJNIModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.AnimationModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.AssetBundleModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.AudioModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ClothModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ClusterInputModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ClusterRendererModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ContentLoadModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.CoreModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.CrashReportingModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.DirectorModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine")]
[assembly: IgnoresAccessChecksTo("UnityEngine.DSPGraphModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.GameCenterModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.GIModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.GridModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.HotReloadModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ImageConversionModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.IMGUIModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.InputLegacyModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.InputModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.JSONSerializeModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.LocalizationModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ParticleSystemModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.PerformanceReportingModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.Physics2DModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.PhysicsModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ProfilerModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.PropertiesModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.RuntimeInitializeOnLoadManagerInitializerModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ScreenCaptureModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.SharedInternalsModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.SpriteMaskModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.SpriteShapeModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.StreamingModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.SubstanceModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.SubsystemsModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.TerrainModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.TerrainPhysicsModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.TextCoreFontEngineModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.TextCoreTextEngineModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.TextRenderingModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.TilemapModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.TLSModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UI")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UIElementsModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UIModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UmbraModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityAnalyticsCommonModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityAnalyticsModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityConnectModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityCurlModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityTestProtocolModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityWebRequestAssetBundleModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityWebRequestAudioModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityWebRequestModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityWebRequestTextureModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UnityWebRequestWWWModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.VehiclesModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.VFXModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.VideoModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.VirtualTexturingModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.VRModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.WindModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.XRModule")]
[assembly: AssemblyCompany("CGPacksPlus")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Adds some useful features to cybergrind packs.")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1+3e40552180611fe13911078af12f48568ca30f1a")]
[assembly: AssemblyProduct("CGPacksPlus")]
[assembly: AssemblyTitle("CGPacksPlus")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 CGPacksPlus
{
	[BepInPlugin("whyis2plus2.ULTRAKILL.CGPacksPlus", "CGPacksPlus", "0.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PLUGIN_FULLNAME = "whyis2plus2.ULTRAKILL.CGPacksPlus";

		public const string PLUGIN_SHORTNAME = "CGPacksPlus";

		public const string PLUGIN_VERSION = "0.0.1";

		public static Plugin Instance;

		public readonly Harmony Harmony = new Harmony("CGPacksPlus");

		private void Awake()
		{
			Instance = this;
			Harmony.PatchAll(typeof(CustomPatternsPatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded CGPacksPlus");
		}
	}
}
namespace CGPacksPlus.Patterns
{
	public class PatternManager : MonoSingleton<PatternManager>
	{
		private static Logger log = new Logger("CGPacksPlus.PatternManager");

		private Dictionary<string, ArenaPattern> patternCache = new Dictionary<string, ArenaPattern>();

		private Dictionary<string, PatternPack> patternPackCache = new Dictionary<string, PatternPack>();

		private Dictionary<string, ArenaPattern> _enabledPatterns = new Dictionary<string, ArenaPattern>();

		public ArenaPattern[] EnabledPatterns => _enabledPatterns.Values?.ToArray() ?? Array.Empty<ArenaPattern>();

		public HashSet<string> EnabledPatternsPaths => _enabledPatterns.Keys?.ToHashSet() ?? new HashSet<string>();

		public static string PatternsPath => Path.Combine(Directory.GetParent(Application.dataPath).FullName, "CyberGrind", "Patterns");

		public void EnablePattern(PatternPack parent, string patternName)
		{
			if (parent == null)
			{
				_enabledPatterns.TryAdd(patternName, LoadPattern(patternName));
				return;
			}
			_enabledPatterns.TryAdd(Path.Join(parent.Path.AsSpan(), patternName.AsSpan()), LoadPattern(Path.Join(parent.Path.AsSpan(), patternName.AsSpan())));
			parent.EnabledPatterns.AddDistinct(patternName);
			File.WriteAllText(Path.Join(PatternsPath.AsSpan(), parent.Path.AsSpan(), "cgpack.json".AsSpan()), parent.ToJson());
			MonoSingleton<EndlessGrid>.Instance.customPatterns = EnabledPatterns;
		}

		public void DisablePattern(PatternPack parent, string patternName)
		{
			if (parent == null)
			{
				_enabledPatterns.Remove(patternName);
				return;
			}
			_enabledPatterns.Remove(Path.Join(parent.Path.AsSpan(), patternName.AsSpan()));
			parent.EnabledPatterns.Remove(patternName);
			File.WriteAllText(Path.Join(PatternsPath.AsSpan(), parent.Path.AsSpan(), "cgpack.json".AsSpan()), parent.ToJson());
			MonoSingleton<EndlessGrid>.Instance.customPatterns = EnabledPatterns;
		}

		public void TogglePattern(PatternPack parent, string patternName)
		{
			string text = ((parent == null) ? patternName : Path.Join(parent.Path.AsSpan(), patternName.AsSpan()));
			if (Enumerable.Contains(_enabledPatterns.Keys, text))
			{
				DisablePattern(parent, text);
			}
			else
			{
				EnablePattern(parent, text);
			}
		}

		public bool GeneratePatternPreview(ArenaPattern pattern, Vector2Int offset, ref Texture2D target)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			return CustomPatternsPatch.CustomPatternsInstance.GeneratePatternPreview(pattern, offset, ref target);
		}

		public bool GeneratePackPreview(PatternPack pack, ref Texture2D target)
		{
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			if (pack == null)
			{
				return false;
			}
			string[] array = pack.ListAllPatterns();
			if (pack.HasValidThumbnail)
			{
				Texture2D val = new Texture2D(1, 1);
				ImageConversion.LoadImage(val, File.ReadAllBytes(Path.Join(PatternsPath.AsSpan(), pack.Path.AsSpan(), pack.ThumbnailPath.AsSpan())));
				if (((Texture)val).width == ((Texture)val).height)
				{
					target = val;
					return true;
				}
				int num = Math.Max(((Texture)val).width, ((Texture)val).height);
				target.Reinitialize(num, num);
				List<Color> list = new List<Color>();
				list.AddRange(Enumerable.Repeat<Color>(Color.black, num * num - ((Texture)val).width * ((Texture)val).height));
				list.AddRange(val.GetPixels());
				target.SetPixels(list.ToArray());
				return true;
			}
			target.Reinitialize(48, 48);
			target.SetPixels(Enumerable.Repeat<Color>(Color.black, 2304).ToArray());
			Vector2Int offset = default(Vector2Int);
			for (int i = 0; i < array.Length && i < 6; i++)
			{
				ArenaPattern pattern = LoadPattern(Path.Join(pack.Path.AsSpan(), array[i].AsSpan()));
				((Vector2Int)(ref offset))..ctor(i % 3, (i > 2) ? 1 : 0);
				if (!GeneratePatternPreview(pattern, offset, ref target))
				{
					return false;
				}
			}
			return true;
		}

		public void SaveEnabledPatterns()
		{
			//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_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			string stringLocal = MonoSingleton<PrefsManager>.Instance.GetStringLocal("cyberGrind.enabledPatterns", (string)null);
			string[] enabledPatternPacks = JsonUtility.FromJson<ActivePatterns>(stringLocal)?.enabledPatternPacks;
			ActivePatterns val = new ActivePatterns
			{
				enabledPatterns = _enabledPatterns.Keys.SkipWhile((string key) => key.Contains(Path.DirectorySeparatorChar)).ToArray(),
				enabledPatternPacks = enabledPatternPacks
			};
			MonoSingleton<PrefsManager>.Instance.SetStringLocal("cyberGrind.enabledPatterns", JsonUtility.ToJson((object)val));
		}

		public void LoadEnabledPatterns()
		{
			string stringLocal = MonoSingleton<PrefsManager>.Instance.GetStringLocal("cyberGrind.enabledPatterns", (string)null);
			if (string.IsNullOrEmpty(stringLocal))
			{
				return;
			}
			ActivePatterns val = JsonUtility.FromJson<ActivePatterns>(stringLocal);
			(val.enabledPatterns ?? Array.Empty<string>()).ForEach(delegate(string path)
			{
				if (!path.Contains(Path.DirectorySeparatorChar))
				{
					ArenaPattern val2 = LoadPattern(path);
					if (Object.op_Implicit((Object)(object)val2))
					{
						EnablePattern(null, path);
					}
					else
					{
						log.Warning("LoadEnabledPatterns: Failed to load pattern \"" + path + "\"", (IEnumerable<Tag>)null, (string)null, (object)null);
					}
				}
			});
			MonoSingleton<EndlessGrid>.Instance.customPatterns = EnabledPatterns;
		}

		public ArenaPattern LoadPattern(string relativePath)
		{
			return LoadPatternAbsolute(Path.Combine(PatternsPath, relativePath));
		}

		public PatternPack LoadPack(string relativePath)
		{
			return LoadPackAbsolute(Path.Combine(PatternsPath, relativePath));
		}

		public ArenaPattern LoadPatternAbsolute(string absolutePath)
		{
			if (!File.Exists(absolutePath))
			{
				return null;
			}
			string relativePath = Path.GetRelativePath(PatternsPath, absolutePath);
			if (patternCache.ContainsKey(relativePath))
			{
				return patternCache[relativePath];
			}
			ArenaPattern val = CustomPatternsPatch.CustomPatternsInstance.LoadPattern(relativePath);
			if ((Object)(object)val == (Object)null)
			{
				log.Warning("Failed to load pattern from path '" + absolutePath + "'", (IEnumerable<Tag>)null, (string)null, (object)null);
				return null;
			}
			patternCache[relativePath] = val;
			return val;
		}

		public PatternPack LoadPackAbsolute(string absolutePath)
		{
			if (!Directory.Exists(absolutePath))
			{
				return null;
			}
			string relativePath = Path.GetRelativePath(PatternsPath, absolutePath);
			if (patternPackCache.ContainsKey(relativePath))
			{
				return patternPackCache[relativePath];
			}
			PatternPack result = null;
			if (!File.Exists(Path.Join(absolutePath.AsSpan(), "cgpack.json".AsSpan())))
			{
				result = new PatternPack();
				result.Path = (result.DisplayName = relativePath);
				patternPackCache[relativePath] = result;
				return result;
			}
			result = PatternPack.FromJson(File.ReadAllText(Path.Join(absolutePath.AsSpan(), "cgpack.json".AsSpan())));
			result.DisplayName = result.DisplayName.Trim();
			result.Path = relativePath;
			patternPackCache[relativePath] = result;
			result.ListAllPatterns().ForEach(delegate(string p)
			{
				if (result.EnabledPatterns.Contains(p))
				{
					EnablePattern(result, p);
				}
			});
			return result;
		}
	}
	[Serializable]
	public class PatternPack
	{
		public string DisplayName = "";

		public string ThumbnailPath = "";

		public List<string> EnabledPatterns = new List<string>();

		[NonSerialized]
		public string Path = "";

		private static string patternsPath => System.IO.Path.Combine(Directory.GetParent(Application.dataPath).FullName, "CyberGrind", "Patterns");

		public bool HasValidThumbnail => File.Exists(System.IO.Path.Join(PatternManager.PatternsPath.AsSpan(), Path.AsSpan(), ThumbnailPath.AsSpan()));

		public static PatternPack FromJson(string jsonData)
		{
			PatternPack patternPack = JsonUtility.FromJson<PatternPack>(jsonData);
			patternPack.EnabledPatterns = patternPack.EnabledPatterns.Distinct().ToList();
			return patternPack;
		}

		public string ToJson()
		{
			return JsonUtility.ToJson((object)this);
		}

		public string[] ListAllPatterns()
		{
			string[] files = Directory.GetFiles(System.IO.Path.Join(patternsPath.AsSpan(), Path.AsSpan()), "*.cgp", SearchOption.TopDirectoryOnly);
			return files.Select((string path) => System.IO.Path.GetFileName(path)).ToArray();
		}
	}
	public class PatternPackUI
	{
		private const int MAX_ITEMS_PER_PAGE = 12;

		private PatternPack focusedPatternPack = null;

		private Transform packUI = null;

		private Transform grid = null;

		private Transform toggleAllButton = null;

		private TMP_Text pageText = null;

		private Dictionary<string, GameObject> patternActiveIndicators = new Dictionary<string, GameObject>();

		private int currentPage = 1;

		private int maxPages = 1;

		private static Logger log = new Logger("CGPacksPlus.PatternPackUI");

		public Transform CGPatternsPanelUI
		{
			get
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_0005: Unknown result type (might be due to invalid IL or missing references)
				Scene activeScene = SceneManager.GetActiveScene();
				return ((Scene)(ref activeScene)).GetRootGameObjects().First((GameObject obj) => ((Object)obj).name == "FirstRoom").transform.Find("Room/Cybergrind Shop/Canvas/Background/Main Panel/Patterns");
			}
		}

		public PatternPackUI()
		{
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Expected O, but got Unknown
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Expected O, but got Unknown
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Expected O, but got Unknown
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Expected O, but got Unknown
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Expected O, but got Unknown
			//IL_0244: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Expected O, but got Unknown
			packUI = Object.Instantiate<Transform>(CGPatternsPanelUI, CGPatternsPanelUI.parent);
			((Component)packUI).gameObject.SetActive(false);
			grid = packUI.Find("Patterns Window/Panel/Patterns/Panel/Grid");
			toggleAllButton = packUI.Find("Patterns Window/Panel").GetChild(1);
			((UnityEventBase)((Component)toggleAllButton).GetComponent<ControllerPointer>().OnPressed).RemoveAllListeners();
			((Component)toggleAllButton).GetComponent<ControllerPointer>().OnPressed.AddListener(new UnityAction(ToggleAllPatterns));
			Transform child = packUI.GetChild(1);
			((UnityEventBase)((Component)child).GetComponent<ControllerPointer>().OnPressed).RemoveAllListeners();
			((Component)child).GetComponent<ControllerPointer>().OnPressed.AddListener((UnityAction)delegate
			{
				SetFocusedPack(null);
			});
			Transform val = packUI.Find("Patterns Window/Panel/Patterns/Refresh Button");
			((UnityEventBase)((Component)val).GetComponent<ControllerPointer>().OnPressed).RemoveAllListeners();
			((Component)val).GetComponent<ControllerPointer>().OnPressed.AddListener(new UnityAction(BuildButtons));
			Transform val2 = packUI.Find("Patterns Window/Panel/Patterns/Pages");
			((UnityEventBase)((Component)val2.GetChild(0)).GetComponent<ControllerPointer>().OnPressed).RemoveAllListeners();
			((Component)val2.GetChild(0)).GetComponent<ControllerPointer>().OnPressed.AddListener(new UnityAction(PrevPage));
			((UnityEventBase)((Component)val2.GetChild(2)).GetComponent<ControllerPointer>().OnPressed).RemoveAllListeners();
			((Component)val2.GetChild(2)).GetComponent<ControllerPointer>().OnPressed.AddListener(new UnityAction(NextPage));
			((Component)packUI.Find("Patterns Window/Panel/Warning Text")).gameObject.SetActive(false);
			((Component)packUI.Find("Patterns Window/Panel/Patterns")).gameObject.SetActive(true);
			for (int num = 0; num < 4; num++)
			{
				((Component)CGPatternsPanelUI.parent.Find("Main Menu/Buttons").GetChild(num)).GetComponent<ControllerPointer>().OnPressed.AddListener((UnityAction)delegate
				{
					((Component)packUI).gameObject.SetActive(false);
				});
			}
			pageText = ((Component)val2.GetChild(1)).GetComponent<TMP_Text>();
		}

		public void SetFocusedPack(PatternPack pack)
		{
			((Component)CGPatternsPanelUI).gameObject.SetActive(pack == null);
			if (pack == null)
			{
				((Component)packUI).gameObject.SetActive(false);
				CustomPatternsPatch.CustomPatternsInstance.BuildButtons();
				return;
			}
			focusedPatternPack = pack;
			patternActiveIndicators = new Dictionary<string, GameObject>();
			currentPage = 1;
			maxPages = Mathf.CeilToInt((float)pack.ListAllPatterns().Length / 12f);
			if (pack.EnabledPatterns.Count == 0)
			{
				((TMP_Text)((Component)toggleAllButton).GetComponentInChildren<TextMeshProUGUI>()).text = "Enable All";
			}
			else
			{
				((TMP_Text)((Component)toggleAllButton).GetComponentInChildren<TextMeshProUGUI>()).text = "Disable All";
			}
			Transform obj = packUI.Find("Patterns Window/Title");
			if (obj != null)
			{
				((TMP_Text)((Component)obj).GetComponent<TextMeshProUGUI>()).text = ((focusedPatternPack.DisplayName.Length > 18) ? (focusedPatternPack.DisplayName.Substring(0, 15) + "...") : focusedPatternPack.DisplayName);
			}
			BuildButtons();
			((Component)packUI).gameObject.SetActive(true);
		}

		private void BuildButtons()
		{
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Expected O, but got Unknown
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Expected O, but got Unknown
			if (focusedPatternPack == null)
			{
				return;
			}
			string[] array = focusedPatternPack.ListAllPatterns();
			for (int i = 2; i < grid.childCount; i++)
			{
				Object.Destroy((Object)(object)((Component)grid.GetChild(i)).gameObject);
			}
			for (int j = (currentPage - 1) * 12; j < array.Length && j < currentPage * 12; j++)
			{
				ArenaPattern val = MonoSingleton<PatternManager>.Instance.LoadPattern(Path.Join(focusedPatternPack.Path.AsSpan(), array[j].AsSpan()));
				if ((Object)(object)val == (Object)null)
				{
					log.Error("Failed to load pattern \"" + Path.Join(focusedPatternPack.Path.AsSpan(), array[j].AsSpan()) + "\"", (IEnumerable<Tag>)null, (string)null, (object)null);
					return;
				}
				GameObject gameObject = ((Component)Object.Instantiate<Transform>(grid.GetChild(0), grid, false)).gameObject;
				Texture2D target = new Texture2D(16, 16);
				bool flag = MonoSingleton<PatternManager>.Instance.GeneratePatternPreview(val, Vector2Int.zero, ref target);
				target.Apply();
				if (!flag)
				{
					log.Warning("Failed to generate preview for " + Path.Join(focusedPatternPack.Path.AsSpan(), array[j].AsSpan()), (IEnumerable<Tag>)null, (string)null, (object)null);
				}
				Sprite val2 = Sprite.Create(target, new Rect(0f, 0f, 16f, 16f), Vector2.zero, 100f);
				((Texture)val2.texture).filterMode = (FilterMode)0;
				gameObject.GetComponent<Image>().sprite = val2;
				gameObject.SetActive(true);
				string currentPattern = array[j];
				gameObject.GetComponent<ControllerPointer>().OnPressed.AddListener((UnityAction)delegate
				{
					if (focusedPatternPack.EnabledPatterns.Contains(currentPattern))
					{
						MonoSingleton<PatternManager>.Instance.DisablePattern(focusedPatternPack, currentPattern);
						patternActiveIndicators[currentPattern].SetActive(false);
					}
					else
					{
						MonoSingleton<PatternManager>.Instance.EnablePattern(focusedPatternPack, currentPattern);
						patternActiveIndicators[currentPattern].SetActive(true);
					}
					if (focusedPatternPack.EnabledPatterns.Count == 0)
					{
						((TMP_Text)((Component)toggleAllButton).GetComponentInChildren<TextMeshProUGUI>()).text = "Enable All";
					}
					else
					{
						((TMP_Text)((Component)toggleAllButton).GetComponentInChildren<TextMeshProUGUI>()).text = "Disable All";
					}
				});
				patternActiveIndicators[currentPattern] = ((Component)gameObject.transform.GetChild(0)).gameObject;
				patternActiveIndicators[currentPattern].SetActive(focusedPatternPack.EnabledPatterns.Contains(currentPattern));
			}
			pageText.text = $"{currentPage}/{maxPages}";
			((Component)grid.GetChild(0)).gameObject.SetActive(false);
		}

		private void NextPage()
		{
			if (currentPage != maxPages)
			{
				currentPage++;
				BuildButtons();
			}
		}

		private void PrevPage()
		{
			if (currentPage != 1)
			{
				currentPage--;
				BuildButtons();
			}
		}

		private void ToggleAllPatterns()
		{
			if (focusedPatternPack == null)
			{
				return;
			}
			if (focusedPatternPack.EnabledPatterns.Count == 0)
			{
				Transform obj = toggleAllButton;
				if (obj != null)
				{
					((TMP_Text)((Component)obj).GetComponentInChildren<TextMeshProUGUI>()).text = "Disable All";
				}
				focusedPatternPack.ListAllPatterns().ForEach(delegate(string p)
				{
					MonoSingleton<PatternManager>.Instance.EnablePattern(focusedPatternPack, p);
				});
				patternActiveIndicators.Values.ForEach(delegate(GameObject indicator)
				{
					if (indicator != null)
					{
						indicator.SetActive(true);
					}
				});
			}
			else
			{
				Transform obj2 = toggleAllButton;
				if (obj2 != null)
				{
					((TMP_Text)((Component)obj2).GetComponentInChildren<TextMeshProUGUI>()).text = "Enable All";
				}
				focusedPatternPack.ListAllPatterns().ForEach(delegate(string p)
				{
					MonoSingleton<PatternManager>.Instance.DisablePattern(focusedPatternPack, p);
				});
				patternActiveIndicators.Values.ForEach(delegate(GameObject indicator)
				{
					if (indicator != null)
					{
						indicator.SetActive(false);
					}
				});
			}
			if (!MonoSingleton<EndlessGrid>.Instance.customPatternMode)
			{
				CustomPatternsPatch.CustomPatternsInstance.Toggle();
			}
		}
	}
}
namespace CGPacksPlus.Patches
{
	public static class CustomPatternsPatch
	{
		private static PatternPackUI packUI;

		private static PatternManager pm;

		public static CustomPatterns CustomPatternsInstance;

		private static Dictionary<string, GameObject> patternPackActiveIndicators => CustomPatternsInstance.patternPackActiveIndicators;

		private static Dictionary<string, GameObject> patternActiveIndicators => CustomPatternsInstance.patternActiveIndicators;

		private static int maxItemsPerPage => CustomPatternsInstance.maxItemsPerPage;

		private static string patternsPath => Path.Combine(Directory.GetParent(Application.dataPath).FullName, "CyberGrind", "Patterns");

		[HarmonyPrefix]
		[HarmonyPatch(typeof(CustomPatterns), "Awake")]
		public static void Prefix_CustomPatterns_Awake(ref CustomPatterns __instance)
		{
			CustomPatternsInstance = __instance;
			pm = new PatternManager();
			packUI = new PatternPackUI();
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(CustomPatterns), "SaveEnabledPatterns")]
		public static bool Prefix_CustomPatterns_SaveEnabledPatterns(ref CustomPatterns __instance)
		{
			pm.SaveEnabledPatterns();
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(CustomPatterns), "LoadEnabledPatterns")]
		public static bool Prefix_CustomPatterns_LoadEnabledPatterns(ref CustomPatterns __instance)
		{
			pm.LoadEnabledPatterns();
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(CustomPatterns), "BuildButtons")]
		public static bool Prefix_CustomPatterns_BuildButtons(ref CustomPatterns __instance, ref Transform ___grid)
		{
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_0278: Expected O, but got Unknown
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_02af: Unknown result type (might be due to invalid IL or missing references)
			//IL_030d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0317: Expected O, but got Unknown
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Expected O, but got Unknown
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Expected O, but got Unknown
			for (int i = 2; i < ___grid.childCount; i++)
			{
				Object.Destroy((Object)(object)((Component)___grid.GetChild(i)).gameObject);
			}
			List<GridTile> allPacksAndPatterns = new List<GridTile>();
			Directory.GetDirectories(patternsPath, "*", SearchOption.TopDirectoryOnly).ForEach(delegate(string dir)
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_0023: Expected O, but got Unknown
				allPacksAndPatterns.Add(new GridTile
				{
					folder = true,
					path = Path.GetFileName(dir)
				});
			});
			Directory.GetFiles(patternsPath, "*.cgp", SearchOption.TopDirectoryOnly).ForEach(delegate(string dir)
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_0023: Expected O, but got Unknown
				allPacksAndPatterns.Add(new GridTile
				{
					folder = false,
					path = Path.GetFileName(dir)
				});
			});
			__instance.maxPages = Mathf.CeilToInt((float)allPacksAndPatterns.Count / (float)maxItemsPerPage);
			int currentPage = __instance.currentPage;
			for (int num = (currentPage - 1) * maxItemsPerPage; num < allPacksAndPatterns.Count && num < currentPage * maxItemsPerPage; num++)
			{
				GridTile val = allPacksAndPatterns[num];
				if (val.folder)
				{
					PatternPack pack = pm.LoadPack(val.path);
					Texture2D target = new Texture2D(48, 48);
					pm.GeneratePackPreview(pack, ref target);
					target.Apply();
					bool hasValidThumbnail = pack.HasValidThumbnail;
					Sprite val2 = Sprite.Create(target, new Rect(0f, 0f, (float)((Texture)target).width, (float)(((!hasValidThumbnail) ? 1 : (-1)) * ((Texture)target).height)), Vector2.zero, 100f);
					((Texture)val2.texture).filterMode = (FilterMode)0;
					GameObject val3 = Object.Instantiate<GameObject>(__instance.packButtonTemplate, __instance.grid, false);
					val3.GetComponentInChildren<TMP_Text>(true).text = pack.DisplayName;
					val3.GetComponent<Image>().sprite = val2;
					val3.GetComponent<ControllerPointer>().OnPressed.AddListener((UnityAction)delegate
					{
						packUI.SetFocusedPack(pack);
					});
					val3.SetActive(true);
					patternActiveIndicators[val.path] = ((Component)val3.transform.GetChild(0)).gameObject;
					patternActiveIndicators[val.path].SetActive(pack.EnabledPatterns.Count == pack.ListAllPatterns().Length);
					continue;
				}
				ArenaPattern pattern = pm.LoadPattern(val.path);
				GameObject gameObject = ((Component)Object.Instantiate<Transform>(___grid.GetChild(0), ___grid, false)).gameObject;
				Texture2D target2 = new Texture2D(16, 16);
				pm.GeneratePatternPreview(pattern, Vector2Int.zero, ref target2);
				target2.Apply();
				Sprite val4 = Sprite.Create(target2, new Rect(0f, 0f, 16f, 16f), Vector2.zero, 100f);
				((Texture)val4.texture).filterMode = (FilterMode)0;
				gameObject.GetComponent<Image>().sprite = val4;
				gameObject.gameObject.SetActive(true);
				string key = val.path;
				gameObject.GetComponent<ControllerPointer>().OnPressed.AddListener((UnityAction)delegate
				{
					pm.TogglePattern(null, key);
					CustomPatternsInstance.patternActiveIndicators[key].gameObject.SetActive(!CustomPatternsInstance.patternActiveIndicators[key].gameObject.activeSelf);
					MonoSingleton<EndlessGrid>.Instance.customPatterns = pm.EnabledPatterns;
					pm.SaveEnabledPatterns();
				});
				__instance.patternActiveIndicators[key] = ((Component)gameObject.transform.GetChild(0)).gameObject;
				__instance.patternActiveIndicators[key].SetActive(pm.EnabledPatternsPaths.Contains(key));
			}
			__instance.pageText.text = $"{currentPage}/{__instance.maxPages}";
			((Component)___grid.GetChild(0)).gameObject.SetActive(false);
			return false;
		}
	}
}
namespace CGPacksPlus.HelperExtensions
{
	public static class CollectionExtensions
	{
		public static void ForEach<T>(this ICollection<T> self, Action<T> action)
		{
			if (self == null || action == null)
			{
				throw new ArgumentNullException();
			}
			foreach (T item in self)
			{
				action(item);
			}
		}

		public static void Add<T>(this ICollection<T> self, params T[] items)
		{
			items.ForEach(self.Add);
		}

		public static void Add<T>(this ICollection<T> self, params ICollection<T>[] collections)
		{
			collections.ForEach(delegate(ICollection<T> c)
			{
				self.Add(c.ToArray());
			});
		}

		public static void AddDistinct<T>(this ICollection<T> self, params T[] items)
		{
			self.Add(items.Except(self).ToArray());
		}

		public static void AddDistinct<T>(this ICollection<T> self, params ICollection<T>[] collections)
		{
			self.Add(collections.SelectMany((ICollection<T> x) => x).Distinct().Except(self)
				.ToArray());
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		internal IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}