Decompiled source of CustomPaintings v1.1.0

CustomPaintings.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CustomPaintings.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CustomPaintings")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CustomPaintings")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1613e4f1-ef37-476f-b4ce-93cebe0156ab")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CustomPaintings
{
	[BepInPlugin("Boniato.CustomPaintings", "CustomPaintings", "1.1.0")]
	public class CustomPaintings : BaseUnityPlugin
	{
		private const string modGUID = "Boniato.CustomPaintings";

		private const string modName = "CustomPaintings";

		private const string modVersion = "1.1.0";

		private readonly Harmony harmony = new Harmony("Boniato.CustomPaintings");

		internal static CustomPaintings instance;

		internal static ManualLogSource mls;

		public static readonly List<string> paintingFiles = new List<string>();

		public static Random random;

		private string separator = "/";

		public string[] urls = new string[0];

		public static Config cfg { get; internal set; }

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			mls = Logger.CreateLogSource("Boniato.CustomPaintings");
			cfg = new Config(((BaseUnityPlugin)this).Config);
			if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
			{
				separator = "\\";
			}
			if (cfg.defaultPaintings)
			{
				urls = new string[8] { "https://i.imgur.com/ePiClDl.png", "https://i.imgur.com/yZCdjxh.png", "https://i.imgur.com/bIb8wXG.png", "https://i.imgur.com/kl1QiwG.png", "https://i.imgur.com/2kHSzn6.png", "https://i.imgur.com/rvdZYBH.png", "https://i.imgur.com/tgJinSI.png", "https://i.imgur.com/ZullTxg.png" };
			}
			if (!cfg.customUrls.Equals(""))
			{
				urls = CollectionExtensions.AddRangeToArray<string>(urls, cfg.customUrls.Split(new char[1] { ',' }));
			}
			string[] array = new string[1] { Paths.BepInExRootPath };
			string[] paths = CollectionExtensions.AddRangeToArray<string>(array, cfg.directory.Split(new char[1] { '/' }));
			string text = Path.Combine(paths);
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
				mls.LogInfo((object)("Created directory at " + text));
				if (!cfg.forceDownload)
				{
					DownloadTextures(text);
				}
			}
			else if (!cfg.defaultPaintings)
			{
				DeleteDefaultTextures(text);
			}
			if (cfg.forceDownload)
			{
				DownloadTextures(text);
			}
			string[] files = Directory.GetFiles(text);
			if (files.Length == 0)
			{
				mls.LogWarning((object)"No paintings found");
			}
			else
			{
				paintingFiles.AddRange(files);
				harmony.PatchAll(typeof(CustomPaintings));
			}
			mls.LogInfo((object)"------- CustomPaintings loaded -------");
		}

		[HarmonyPatch(typeof(GrabbableObject), "Start")]
		[HarmonyPostfix]
		private static void PaintingPatch(GrabbableObject __instance)
		{
			if (__instance.itemProperties.itemName == "Painting")
			{
				random = new Random((int)((NetworkBehaviour)__instance).NetworkObjectId);
				UpdateTextures(paintingFiles, __instance);
			}
		}

		private static void UpdateTextures(IReadOnlyList<string> files, GrabbableObject __instance)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Expected O, but got Unknown
			if (files.Count == 0)
			{
				return;
			}
			if (cfg.maxTextures > files.Count)
			{
				cfg.maxTextures = files.Count;
			}
			Material[] array = (Material[])(object)new Material[cfg.maxTextures];
			List<int> list = new List<int>();
			list.Clear();
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = new Material(__instance.itemProperties.materialVariants[0]);
				int num;
				do
				{
					num = random.Next(files.Count);
				}
				while (list.Contains(num));
				list.Add(num);
				Texture2D val = new Texture2D(2, 2);
				ImageConversion.LoadImage(val, File.ReadAllBytes(files[num]));
				array[i].mainTexture = (Texture)(object)val;
			}
			__instance.itemProperties.materialVariants = array;
		}

		private void DownloadTextures(string directory)
		{
			int num = 1;
			string[] array = urls;
			foreach (string requestUriString in array)
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUriString);
				HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				if (httpWebResponse.StatusCode == HttpStatusCode.OK)
				{
					Image val = Image.FromStream(httpWebResponse.GetResponseStream());
					if (cfg.defaultPaintings)
					{
						if (num < 9)
						{
							val.Save(directory + separator + "default" + num + ".png", ImageFormat.Png);
						}
						else
						{
							val.Save(directory + separator + "custom" + (num - 8) + ".png", ImageFormat.Png);
						}
					}
					else
					{
						val.Save(directory + separator + "custom" + num + ".png", ImageFormat.Png);
					}
				}
				else if (cfg.defaultPaintings)
				{
					if (num < 9)
					{
						mls.LogWarning((object)("Error downloading default image " + num));
					}
					else
					{
						mls.LogWarning((object)("Error downloading custom image " + (num - 8)));
					}
				}
				else
				{
					mls.LogWarning((object)("Error downloading custom image " + num));
				}
				num++;
			}
		}

		private void DeleteDefaultTextures(string directory)
		{
			if (File.Exists(directory + separator + "default1.png"))
			{
				for (int i = 1; i < 9; i++)
				{
					File.Delete(directory + separator + "default" + i + ".png");
				}
			}
		}
	}
	[Serializable]
	public class SyncedInstance<T>
	{
		[NonSerialized]
		protected static int IntSize = 4;

		internal static CustomMessagingManager MessageManager => NetworkManager.Singleton.CustomMessagingManager;

		internal static bool IsClient => NetworkManager.Singleton.IsClient;

		internal static bool IsHost => NetworkManager.Singleton.IsHost;

		public static T Default { get; private set; }

		public static T Instance { get; private set; }

		public static bool Synced { get; internal set; }

		protected void InitInstance(T instance)
		{
			Default = instance;
			Instance = instance;
			IntSize = 4;
		}

		internal static void SyncInstance(byte[] data)
		{
			Instance = DeserializeFromBytes(data);
			Synced = true;
		}

		internal static void RevertSync()
		{
			Instance = Default;
			Synced = false;
		}

		public static byte[] SerializeToBytes(T val)
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			using MemoryStream memoryStream = new MemoryStream();
			try
			{
				binaryFormatter.Serialize(memoryStream, val);
				return memoryStream.ToArray();
			}
			catch (Exception arg)
			{
				Plugin.logger.LogError((object)$"Error serializing instance: {arg}");
				return null;
			}
		}

		public static T DeserializeFromBytes(byte[] data)
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			using MemoryStream serializationStream = new MemoryStream(data);
			try
			{
				return (T)binaryFormatter.Deserialize(serializationStream);
			}
			catch (Exception arg)
			{
				Plugin.logger.LogError((object)$"Error deserializing instance: {arg}");
				return default(T);
			}
		}
	}
}
namespace CustomPaintings.Configuration
{
	[Serializable]
	public class Config : SyncedInstance<Config>
	{
		public string directory;

		public string customUrls;

		public bool defaultPaintings;

		public bool forceDownload;

		public int maxTextures;

		public Config(ConfigFile cfg)
		{
			InitInstance(this);
			directory = cfg.Bind<string>("General", "Directory", "plugins/CustomPaintings", "Directory in which the mod will search for the paintings (using BepInEx as root, use / as separator)").Value;
			customUrls = cfg.Bind<string>("General", "Custom Urls", "", "Custom urls of the images to download them as default (separate them with commas, for example: https://i.imgur.com/ePiClDl.png,https://i.imgur.com/yZCdjxh.png)").Value;
			defaultPaintings = cfg.Bind<bool>("General", "Default Paintings", true, "Enable it to use the default paintings of famous works").Value;
			forceDownload = cfg.Bind<bool>("General", "Force Download", false, "Enable it to download the url images in every launch (if it is false, the mod will download the textures only when creates the directory)").Value;
			maxTextures = cfg.Bind<int>("General", "Max Textures", 5, "Number of textures per game to prevent duplicated ones (try to avoid high numbers)").Value;
		}

		public static void RequestSync()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (!SyncedInstance<Config>.IsClient)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(SyncedInstance<Config>.IntSize, (Allocator)2, -1);
			try
			{
				SyncedInstance<Config>.MessageManager.SendNamedMessage("CustomPaintings_OnRequestConfigSync", 0uL, val, (NetworkDelivery)3);
			}
			finally
			{
				((IDisposable)(FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnRequestSync(ulong clientId, FastBufferReader _)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			if (!SyncedInstance<Config>.IsHost)
			{
				return;
			}
			Plugin.logger.LogInfo((object)$"Config sync request received from client: {clientId}");
			byte[] array = SyncedInstance<Config>.SerializeToBytes(SyncedInstance<Config>.Instance);
			int num = array.Length;
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(num + SyncedInstance<Config>.IntSize, (Allocator)2, -1);
			try
			{
				((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref num, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteBytesSafe(array, -1, 0);
				SyncedInstance<Config>.MessageManager.SendNamedMessage("CustomPaintings_OnReceiveConfigSync", clientId, val, (NetworkDelivery)3);
			}
			catch (Exception arg)
			{
				Plugin.logger.LogInfo((object)$"Error occurred syncing config with client: {clientId}\n{arg}");
			}
			finally
			{
				((IDisposable)(FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnReceiveSync(ulong _, FastBufferReader reader)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			if (!((FastBufferReader)(ref reader)).TryBeginRead(SyncedInstance<Config>.IntSize))
			{
				Plugin.logger.LogError((object)"Config sync error: Could not begin reading buffer.");
				return;
			}
			int num = default(int);
			((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref num, default(ForPrimitives));
			if (!((FastBufferReader)(ref reader)).TryBeginRead(num))
			{
				Plugin.logger.LogError((object)"Config sync error: Host could not sync.");
				return;
			}
			byte[] data = new byte[num];
			((FastBufferReader)(ref reader)).ReadBytesSafe(ref data, num, 0);
			SyncedInstance<Config>.SyncInstance(data);
			Plugin.logger.LogInfo((object)"Successfully synced config with host.");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		public static void InitializeLocalPlayer()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			if (SyncedInstance<Config>.IsHost)
			{
				SyncedInstance<Config>.MessageManager.RegisterNamedMessageHandler("CustomPaintings_OnRequestConfigSync", new HandleNamedMessageDelegate(OnRequestSync));
				SyncedInstance<Config>.Synced = true;
			}
			else
			{
				SyncedInstance<Config>.Synced = false;
				SyncedInstance<Config>.MessageManager.RegisterNamedMessageHandler("CustomPaintings_OnReceiveConfigSync", new HandleNamedMessageDelegate(OnReceiveSync));
				RequestSync();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
		public static void PlayerLeave()
		{
			SyncedInstance<Config>.RevertSync();
		}
	}
}