Decompiled source of RandomPaintingSwap v1.1.0

RandomPaintingSwap.dll

Decompiled 2 months 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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[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: AssemblyCompany("RandomPaintingSwap")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+17d5219563f01300bed99d5c13eaec59d4a12eea")]
[assembly: AssemblyProduct("Random Painting Swap")]
[assembly: AssemblyTitle("RandomPaintingSwap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 RandomPaintingSwap
{
	[BepInPlugin("ch.gabzdev.randompaintingswap", "Random Painting Swap", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(LoadingUI), "LevelAnimationComplete")]
		public class PatchLoadingUI
		{
			[HarmonyPostfix]
			private static void Postfix()
			{
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				//IL_0016: Unknown result type (might be due to invalid IL or missing references)
				Logger.LogInfo((object)"Remplacement des images de base par les images du plugin");
				Scene activeScene = SceneManager.GetActiveScene();
				List<GameObject> list = ((Scene)(ref activeScene)).GetRootGameObjects().ToList();
				foreach (GameObject item in list)
				{
					MeshRenderer[] componentsInChildren = item.GetComponentsInChildren<MeshRenderer>();
					foreach (MeshRenderer val in componentsInChildren)
					{
						Material[] sharedMaterials = ((Renderer)val).sharedMaterials;
						if (sharedMaterials == null)
						{
							continue;
						}
						for (int j = 0; j < sharedMaterials.Length; j++)
						{
							Material val2 = sharedMaterials[j];
							if ((Object)(object)val2 != (Object)null && targetMaterials.Contains(((Object)val2).name) && loadedMaterials.Count > 0)
							{
								sharedMaterials[j] = loadedMaterials[Random.Range(0, loadedMaterials.Count)];
							}
						}
						((Renderer)val).sharedMaterials = sharedMaterials;
					}
				}
			}
		}

		private const string IMAGE_FOLDER_NAME = "RandomPaintingSwap_Images";

		internal static ManualLogSource Logger;

		public static List<Material> loadedMaterials = new List<Material>();

		public static readonly HashSet<string> targetMaterials = new HashSet<string> { "Painting_H_Landscape", "Painting_V_Furman", "painting teacher02", "Painting_S_Tree" };

		public static readonly HashSet<string> imagePatterns = new HashSet<string> { "*.png", "*.jpg", "*.jpeg" };

		private readonly Harmony harmony = new Harmony("ch.gabzdev.randompaintingswap");

		private string imagesDirectoryPath;

		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin Random Painting Swap is loaded!");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			CreateImagesDirectory();
			LoadImagesFromDirectory();
		}

		private void CreateImagesDirectory()
		{
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			imagesDirectoryPath = Path.Combine(directoryName, "RandomPaintingSwap_Images");
			if (!Directory.Exists(imagesDirectoryPath))
			{
				Directory.CreateDirectory(imagesDirectoryPath);
				Logger.LogInfo((object)("Dossier " + imagesDirectoryPath + " creer avec succes !"));
			}
			else
			{
				Logger.LogInfo((object)("Dossier " + imagesDirectoryPath + " detecte !"));
			}
		}

		private void LoadImagesFromDirectory()
		{
			//IL_00d6: 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_00e6: Expected O, but got Unknown
			if (!Directory.Exists(imagesDirectoryPath))
			{
				Logger.LogWarning((object)("Le dossier " + imagesDirectoryPath + " n'existe pas !"));
				return;
			}
			List<string> list = imagePatterns.SelectMany((string pattern) => Directory.GetFiles(imagesDirectoryPath, pattern)).ToList();
			if (!list.Any())
			{
				Logger.LogWarning((object)("Aucune image trouvee dans le dossier " + imagesDirectoryPath));
				return;
			}
			foreach (string item2 in list)
			{
				Texture2D val = LoadTextureFromFile(item2);
				if ((Object)(object)val == (Object)null)
				{
					Logger.LogWarning((object)("Erreur chargement image : " + item2));
					continue;
				}
				Material item = new Material(Shader.Find("Standard"))
				{
					mainTexture = (Texture)(object)val
				};
				loadedMaterials.Add(item);
				Logger.LogInfo((object)("Image chargée et Material créé : " + Path.GetFileNameWithoutExtension(item2)));
			}
			Logger.LogInfo((object)$"Total Images : {list.Count}");
		}

		private Texture2D LoadTextureFromFile(string filePath)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			byte[] array = File.ReadAllBytes(filePath);
			Texture2D val = new Texture2D(2, 2);
			if (ImageConversion.LoadImage(val, array))
			{
				val.Apply();
				return val;
			}
			val = null;
			return null;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "RandomPaintingSwap";

		public const string PLUGIN_NAME = "Random Painting Swap";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}