Decompiled source of ObrazkyJakCyp v1.0.2

ObrazkyJakCyp.dll

Decompiled 12 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.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ObrazkyJakCyp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("997-13854")]
[assembly: AssemblyProduct("ObrazkyJakCyp")]
[assembly: AssemblyCopyright("Copyright © 997-13854 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1e5440df-10c4-4220-8554-1e5edfe67572")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ObrazkyJakCyp
{
	internal static class Extensions
	{
		public static bool IsPainting(this GrabbableObject obj)
		{
			return obj.itemProperties.itemName == "Painting";
		}
	}
	internal static class Globals
	{
		public static Random GRandom = new Random();

		public static Texture2D PaintingTemplate = null;

		public static Dictionary<string, Texture2D> LoadedImages = new Dictionary<string, Texture2D>();

		public static Dictionary<int, Material> PaintingCache = new Dictionary<int, Material>();
	}
	[BepInPlugin("CecekMan.ObrazkyJakCyp", "ObrazkyJakCyp", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource logger { get; private set; }

		public static PluginConfig Config { get; private set; }

		internal static bool IsInitialized { get; private set; }

		private void Awake()
		{
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			//IL_00f5: Expected O, but got Unknown
			if (IsInitialized)
			{
				return;
			}
			logger = ((BaseUnityPlugin)this).Logger;
			Config = new PluginConfig(((BaseUnityPlugin)this).Config);
			if ((Object)(object)Globals.PaintingTemplate == (Object)null)
			{
				using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ObrazkyJakCyp.BasePainting.png");
				if (stream == null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Failed to find BasePainting.png!");
					return;
				}
				using MemoryStream memoryStream = new MemoryStream();
				stream.CopyTo(memoryStream);
				Globals.PaintingTemplate = new Texture2D(1, 1);
				ImageConversion.LoadImage(Globals.PaintingTemplate, memoryStream.ToArray());
			}
			string fullPath = Path.GetFullPath(Path.Combine(Paths.BepInExRootPath, "Paintings"));
			if (!Directory.Exists(fullPath))
			{
				Directory.CreateDirectory(fullPath);
			}
			else
			{
				string[] files = Directory.GetFiles(fullPath);
				foreach (string text in files)
				{
					try
					{
						byte[] array = File.ReadAllBytes(text);
						Texture2D val = new Texture2D(1, 1);
						if (!ImageConversion.LoadImage(val, array))
						{
							throw new Exception();
						}
						Object.DestroyImmediate((Object)val);
						Globals.LoadedImages[text] = null;
					}
					catch (Exception)
					{
						((BaseUnityPlugin)this).Logger.LogError((object)("Painting: '" + Path.GetFileName(text) + "' is not a valid image file!"));
					}
				}
			}
			new Harmony("CecekMan.ObrazkyJakCyp").PatchAll();
			IsInitialized = true;
		}
	}
	public class PluginConfig
	{
		public ConfigEntry<string> Directory;

		public PluginConfig(ConfigFile cfg)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			Directory = cfg.Bind<string>(new ConfigDefinition("General", "Directory"), "Paintings", (ConfigDescription)null);
		}
	}
	internal static class Utils
	{
		private static readonly Vector2Int _startPoint;

		private static readonly Vector2Int _endPoint;

		private static readonly Vector2Int _targetSize;

		static Utils()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//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_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			_startPoint = new Vector2Int(527, 37);
			_endPoint = new Vector2Int(1015, 687);
			_targetSize = _endPoint - _startPoint;
		}

		public static string GetRandomImage()
		{
			if (Globals.LoadedImages.Count == 0)
			{
				return null;
			}
			string[] array = Globals.LoadedImages.Keys.ToArray();
			return array[Globals.GRandom.Next(array.Length - 1)];
		}

		public static Texture2D ResizeTexture(Texture2D tex, int w, int h)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			RenderTexture temporary = RenderTexture.GetTemporary(w, h);
			Graphics.Blit((Texture)(object)tex, temporary);
			RenderTexture active = RenderTexture.active;
			RenderTexture.active = temporary;
			Texture2D val = new Texture2D(w, h);
			val.ReadPixels(new Rect(0f, 0f, (float)w, (float)h), 0, 0);
			val.Apply();
			RenderTexture.active = active;
			RenderTexture.ReleaseTemporary(temporary);
			return val;
		}

		public static void ApplyTextureToPainting(GrabbableObject p)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024d: Expected O, but got Unknown
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Expected O, but got Unknown
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: 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_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			if (!Globals.PaintingCache.TryGetValue(((Object)p).GetInstanceID(), out var value))
			{
				string randomImage = GetRandomImage();
				if (randomImage == null)
				{
					return;
				}
				if (!Globals.LoadedImages.TryGetValue(randomImage, out var value2) || !((Object)(object)value2 != (Object)null))
				{
					byte[] array = File.ReadAllBytes(randomImage);
					Texture2D val = new Texture2D(1, 1);
					if (!ImageConversion.LoadImage(val, array))
					{
						Plugin.logger.LogError((object)$"Failed to load image: {Path.GetFileName(randomImage)} with size: 0x{array.Length:X8}");
						return;
					}
					if (!Path.GetFileNameWithoutExtension(randomImage).EndsWith("_tex"))
					{
						if (((Texture)val).width > ((Texture)val).height)
						{
							Color[] pixels = val.GetPixels();
							Color[] array2 = (Color[])(object)new Color[pixels.Length];
							for (int i = 0; i < ((Texture)val).width; i++)
							{
								for (int j = 0; j < ((Texture)val).height; j++)
								{
									array2[i * ((Texture)val).height + j] = pixels[j * ((Texture)val).width + i];
								}
							}
							val.Reinitialize(((Texture)val).height, ((Texture)val).width);
							val.SetPixels(array2);
							val.Apply();
						}
						Texture2D tex = val;
						Vector2Int val2 = _targetSize;
						int x = ((Vector2Int)(ref val2)).x;
						val2 = _targetSize;
						Texture2D obj = ResizeTexture(tex, x, ((Vector2Int)(ref val2)).y);
						Object.DestroyImmediate((Object)(object)val);
						val = obj;
						Color[] pixels2 = val.GetPixels();
						Color[] pixels3 = Globals.PaintingTemplate.GetPixels();
						value2 = new Texture2D(((Texture)Globals.PaintingTemplate).width, ((Texture)Globals.PaintingTemplate).height, Globals.PaintingTemplate.format, false);
						for (int k = 0; k < ((Texture)val).width; k++)
						{
							for (int l = 0; l < ((Texture)val).height; l++)
							{
								val2 = _startPoint;
								int num = ((Vector2Int)(ref val2)).x + k;
								int height = ((Texture)value2).height;
								val2 = _endPoint;
								pixels3[num + (height - ((Vector2Int)(ref val2)).y + l) * ((Texture)value2).width] = pixels2[k + l * ((Texture)val).width];
							}
						}
						value2.SetPixels(pixels3);
						value2.Apply();
						Object.DestroyImmediate((Object)(object)val);
					}
					else
					{
						value2 = val;
					}
					Globals.LoadedImages[randomImage] = value2;
				}
				value = new Material(p.itemProperties.materialVariants[0])
				{
					mainTexture = (Texture)(object)value2
				};
			}
			((Renderer)((Component)p).gameObject.GetComponent<MeshRenderer>()).sharedMaterial = value;
			Globals.PaintingCache[((Object)p).GetInstanceID()] = value;
		}
	}
}
namespace ObrazkyJakCyp.Patches
{
	[HarmonyPatch]
	internal static class GameNetworkManagerPatches
	{
		[HarmonyPatch(typeof(GameNetworkManager), "ResetGameValuesToDefault")]
		[HarmonyPostfix]
		private static void OnResetGameValues()
		{
			if (Plugin.IsInitialized)
			{
				Globals.PaintingCache.Clear();
			}
		}
	}
	[HarmonyPatch]
	internal static class GrabbableObjectPatches
	{
		[HarmonyPatch(typeof(GrabbableObject), "Start")]
		[HarmonyPrefix]
		private static void OnStart(GrabbableObject __instance)
		{
			if (Plugin.IsInitialized && __instance.IsPainting())
			{
				Utils.ApplyTextureToPainting(__instance);
			}
		}
	}
	[HarmonyPatch]
	internal static class RoundManagerPatches
	{
		[HarmonyPatch(typeof(RoundManager), "SyncScrapValuesClientRpc")]
		[HarmonyPostfix]
		private static void OnSyncScrapValues(NetworkObjectReference[] spawnedScrap)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.IsInitialized)
			{
				return;
			}
			NetworkObject val2 = default(NetworkObject);
			for (int i = 0; i < spawnedScrap.Length; i++)
			{
				NetworkObjectReference val = spawnedScrap[i];
				if (((NetworkObjectReference)(ref val)).TryGet(ref val2, (NetworkManager)null))
				{
					GrabbableObject component = ((Component)val2).GetComponent<GrabbableObject>();
					if (component.IsPainting())
					{
						Utils.ApplyTextureToPainting(component);
					}
				}
			}
		}

		[HarmonyPatch(typeof(RoundManager), "GenerateNewLevelClientRpc")]
		[HarmonyPrefix]
		private static void OnGenerateNewLevel(int randomSeed)
		{
			Globals.GRandom = new Random(randomSeed + 300);
		}
	}
}